Html.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace common\helpers;
  3. use Yii;
  4. use yii\helpers\BaseHtml;
  5. use common\enums\StatusEnum;
  6. /**
  7. * Class Html
  8. * @package common\helpers
  9. * @author jianyan74 <751393839@qq.com>
  10. */
  11. class Html extends BaseHtml
  12. {
  13. /**
  14. * 创建
  15. *
  16. * @param $url
  17. * @param array $options
  18. * @return string
  19. */
  20. public static function create(array $url, $content = '创建', $options = [])
  21. {
  22. $options = ArrayHelper::merge([
  23. 'class' => "btn btn-primary btn-sm"
  24. ], $options);
  25. return self::a($content, $url, $options);
  26. }
  27. /**
  28. * 编辑
  29. *
  30. * @param $url
  31. * @param array $options
  32. * @return string
  33. */
  34. public static function edit(array $url, $content = '编辑', $options = [])
  35. {
  36. $options = ArrayHelper::merge([
  37. 'class' => 'btn btn-primary btn-sm',
  38. ], $options);
  39. return self::a($content, $url, $options);
  40. }
  41. /**
  42. * 删除
  43. *
  44. * @param $url
  45. * @param array $options
  46. * @return string
  47. */
  48. public static function delete(array $url, $content = '删除', $options = [])
  49. {
  50. $options = ArrayHelper::merge([
  51. 'class' => 'btn btn-danger btn-sm',
  52. 'onclick' => "rfDelete(this);return false;"
  53. ], $options);
  54. return self::a($content, $url, $options);
  55. }
  56. /**
  57. * 普通按钮
  58. *
  59. * @param $url
  60. * @param array $options
  61. * @return string
  62. */
  63. public static function linkButton(array $url, $content, $options = [])
  64. {
  65. $options = ArrayHelper::merge([
  66. 'class' => "btn btn-white btn-sm"
  67. ], $options);
  68. return self::a($content, $url, $options);
  69. }
  70. /**
  71. * 导出按钮
  72. *
  73. * @param $url
  74. * @param array $options
  75. * @return string
  76. */
  77. public static function export(array $url, $content, $options = [])
  78. {
  79. $url = ArrayHelper::merge($url, Yii::$app->request->get());
  80. $options = ArrayHelper::merge([
  81. 'class' => "btn btn-white btn-sm"
  82. ], $options);
  83. return self::a($content, $url, $options);
  84. }
  85. /**
  86. * 状态标签
  87. *
  88. * @param int $status
  89. * @return mixed
  90. */
  91. public static function status($status = 1, $options = [])
  92. {
  93. if (!self::beforVerify('ajax-update')) {
  94. return '';
  95. }
  96. $listBut = [
  97. StatusEnum::DISABLED => self::tag('span', '启用', array_merge(
  98. [
  99. 'class' => "btn btn-success btn-sm",
  100. 'data-toggle' => 'tooltip',
  101. 'data-original-title' => '点击启用',
  102. 'onclick' => "rfStatus(this)"
  103. ],
  104. $options
  105. )),
  106. StatusEnum::ENABLED => self::tag('span', '禁用', array_merge(
  107. [
  108. 'class' => "btn btn-default btn-sm",
  109. 'data-toggle' => 'tooltip',
  110. 'data-original-title' => '点击禁用',
  111. 'onclick' => "rfStatus(this)"
  112. ],
  113. $options
  114. )),
  115. ];
  116. return $listBut[$status] ?? '';
  117. }
  118. /**
  119. * @param string $text
  120. * @param null $url
  121. * @param array $options
  122. * @return string
  123. */
  124. public static function a($text, $url = null, $options = [])
  125. {
  126. if ($url !== null) {
  127. // 权限校验
  128. if (!self::beforVerify($url)) {
  129. return '';
  130. }
  131. $options['href'] = Url::to($url);
  132. }
  133. return static::tag('a', $text, $options);
  134. }
  135. /**
  136. * 排序
  137. *
  138. * @param $value
  139. * @return string
  140. */
  141. public static function sort($value, $options = [])
  142. {
  143. // 权限校验
  144. if (!self::beforVerify('ajax-update')) {
  145. return $value;
  146. }
  147. $options = ArrayHelper::merge([
  148. 'class' => 'form-control',
  149. 'onblur' => 'rfSort(this)',
  150. 'style' => 'min-width:55px'
  151. ], $options);
  152. return self::input('text', 'sort', $value, $options);
  153. }
  154. /**
  155. * 缩减显示字符串
  156. *
  157. * @param $string
  158. * @param int $num
  159. * @return string
  160. */
  161. public static function textNewLine($string, $num = 36, $cycle_index = 3)
  162. {
  163. if (empty($string)) {
  164. return '';
  165. }
  166. return self::tag('span', implode('<br>', StringHelper::textNewLine($string, $num, $cycle_index)), [
  167. 'title' => $string,
  168. ]);
  169. }
  170. /**
  171. * 根据开始时间和结束时间发回当前状态
  172. *
  173. * @param int $start_time 开始时间
  174. * @param int $end_time 结束时间
  175. * @return mixed
  176. */
  177. public static function timeStatus($start_time, $end_time)
  178. {
  179. $time = time();
  180. if ($start_time > $end_time) {
  181. return "<span class='label label-outline-danger'>有效期错误</span>";
  182. } elseif ($start_time > $time) {
  183. return "<span class='label label-outline-default'>未开始</span>";
  184. } elseif ($start_time < $time && $end_time > $time) {
  185. return "<span class='label label-outline-success'>进行中</span>";
  186. } elseif ($end_time < $time) {
  187. return "<span class='label label-outline-default'>已结束</span>";
  188. }
  189. return false;
  190. }
  191. /**
  192. * 由于ajax加载model有些控件会重新载入样式导致基础样式失调做的修复
  193. *
  194. * @return string|void
  195. */
  196. public static function modelBaseCss()
  197. {
  198. echo Html::cssFile(Yii::getAlias('@baseResources') . '/css/rageframe.css?v=' . time());
  199. Yii::$app->controller->view->registerCss(<<<Css
  200. .modal {
  201. z-index: 999;
  202. }
  203. .modal-backdrop {
  204. z-index: 998;
  205. }
  206. Css
  207. );
  208. }
  209. /**
  210. * @param $route
  211. * @return bool
  212. */
  213. protected static function beforVerify($route)
  214. {
  215. // 未登录直接放行
  216. if (Yii::$app->user->isGuest) {
  217. return true;
  218. }
  219. is_array($route) && $route = $route[0];
  220. $route = Url::getAuthUrl($route);
  221. substr("$route", 0, 1) != '/' && $route = '/' . $route;
  222. // 判断是否在模块内容
  223. if (true === Yii::$app->params['inAddon']) {
  224. $route = StringHelper::replace('/addons/', '', $route);
  225. }
  226. return Auth::verify($route);
  227. }
  228. }
粤ICP备19079148号