common.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. use app\common\model\Category;
  3. use fast\Form;
  4. use fast\Tree;
  5. use think\Db;
  6. if (!function_exists('build_select')) {
  7. /**
  8. * 生成下拉列表
  9. * @param string $name
  10. * @param mixed $options
  11. * @param mixed $selected
  12. * @param mixed $attr
  13. * @return string
  14. */
  15. function build_select($name, $options, $selected = [], $attr = [])
  16. {
  17. $options = is_array($options) ? $options : explode(',', $options);
  18. $selected = is_array($selected) ? $selected : explode(',', $selected);
  19. return Form::select($name, $options, $selected, $attr);
  20. }
  21. }
  22. if (!function_exists('build_radios')) {
  23. /**
  24. * 生成单选按钮组
  25. * @param string $name
  26. * @param array $list
  27. * @param mixed $selected
  28. * @return string
  29. */
  30. function build_radios($name, $list = [], $selected = null)
  31. {
  32. $html = [];
  33. $selected = is_null($selected) ? key($list) : $selected;
  34. $selected = is_array($selected) ? $selected : explode(',', $selected);
  35. foreach ($list as $k => $v) {
  36. $html[] = sprintf(Form::label("{$name}-{$k}", "%s {$v}"), Form::radio($name, $k, in_array($k, $selected), ['id' => "{$name}-{$k}"]));
  37. }
  38. return '<div class="radio">' . implode(' ', $html) . '</div>';
  39. }
  40. }
  41. if (!function_exists('build_checkboxs')) {
  42. /**
  43. * 生成复选按钮组
  44. * @param string $name
  45. * @param array $list
  46. * @param mixed $selected
  47. * @return string
  48. */
  49. function build_checkboxs($name, $list = [], $selected = null)
  50. {
  51. $html = [];
  52. $selected = is_null($selected) ? [] : $selected;
  53. $selected = is_array($selected) ? $selected : explode(',', $selected);
  54. foreach ($list as $k => $v) {
  55. $html[] = sprintf(Form::label("{$name}-{$k}", "%s {$v}"), Form::checkbox($name, $k, in_array($k, $selected), ['id' => "{$name}-{$k}"]));
  56. }
  57. return '<div class="checkbox">' . implode(' ', $html) . '</div>';
  58. }
  59. }
  60. if (!function_exists('build_category_select')) {
  61. /**
  62. * 生成分类下拉列表框
  63. * @param string $name
  64. * @param string $type
  65. * @param mixed $selected
  66. * @param array $attr
  67. * @param array $header
  68. * @return string
  69. */
  70. function build_category_select($name, $type, $selected = null, $attr = [], $header = [])
  71. {
  72. $tree = Tree::instance();
  73. $tree->init(Category::getCategoryArray($type), 'pid');
  74. $categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  75. $categorydata = $header ? $header : [];
  76. foreach ($categorylist as $k => $v) {
  77. $categorydata[$v['id']] = $v['name'];
  78. }
  79. $attr = array_merge(['id' => "c-{$name}", 'class' => 'form-control selectpicker'], $attr);
  80. return build_select($name, $categorydata, $selected, $attr);
  81. }
  82. }
  83. if (!function_exists('build_toolbar')) {
  84. /**
  85. * 生成表格操作按钮栏
  86. * @param array $btns 按钮组
  87. * @param array $attr 按钮属性值
  88. * @return string
  89. */
  90. function build_toolbar($btns = null, $attr = [])
  91. {
  92. $auth = \app\admin\library\Auth::instance();
  93. $controller = str_replace('.', '/', strtolower(think\Request::instance()->controller()));
  94. $btns = $btns ? $btns : ['refresh', 'add', 'edit', 'del', 'import'];
  95. $btns = is_array($btns) ? $btns : explode(',', $btns);
  96. $index = array_search('delete', $btns);
  97. if ($index !== false) {
  98. $btns[$index] = 'del';
  99. }
  100. $btnAttr = [
  101. 'refresh' => ['javascript:;', 'btn btn-primary btn-refresh', 'fa fa-refresh', '', __('Refresh')],
  102. 'add' => ['javascript:;', 'btn btn-success btn-add', 'fa fa-plus', __('Add'), __('Add')],
  103. 'edit' => ['javascript:;', 'btn btn-success btn-edit btn-disabled disabled', 'fa fa-pencil', __('Edit'), __('Edit')],
  104. 'del' => ['javascript:;', 'btn btn-danger btn-del btn-disabled disabled', 'fa fa-trash', __('Delete'), __('Delete')],
  105. 'import' => ['javascript:;', 'btn btn-info btn-import', 'fa fa-upload', __('Import'), __('Import')],
  106. ];
  107. $btnAttr = array_merge($btnAttr, $attr);
  108. $html = [];
  109. foreach ($btns as $k => $v) {
  110. //如果未定义或没有权限
  111. if (!isset($btnAttr[$v]) || ($v !== 'refresh' && !$auth->check("{$controller}/{$v}"))) {
  112. continue;
  113. }
  114. list($href, $class, $icon, $text, $title) = $btnAttr[$v];
  115. //$extend = $v == 'import' ? 'id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"' : '';
  116. //$html[] = '<a href="' . $href . '" class="' . $class . '" title="' . $title . '" ' . $extend . '><i class="' . $icon . '"></i> ' . $text . '</a>';
  117. if ($v == 'import') {
  118. $template = str_replace('/', '_', $controller);
  119. $download = '';
  120. if (file_exists("./template/{$template}.xlsx")) {
  121. $download .= "<li><a href=\"/template/{$template}.xlsx\" target=\"_blank\">XLSX模版</a></li>";
  122. }
  123. if (file_exists("./template/{$template}.xls")) {
  124. $download .= "<li><a href=\"/template/{$template}.xls\" target=\"_blank\">XLS模版</a></li>";
  125. }
  126. if (file_exists("./template/{$template}.csv")) {
  127. $download .= empty($download) ? '' : "<li class=\"divider\"></li>";
  128. $download .= "<li><a href=\"/template/{$template}.csv\" target=\"_blank\">CSV模版</a></li>";
  129. }
  130. $download .= empty($download) ? '' : "\n ";
  131. if (!empty($download)) {
  132. $html[] = <<<EOT
  133. <div class="btn-group">
  134. <button type="button" href="{$href}" class="btn btn-info btn-import" title="{$title}" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="{$icon}"></i> {$text}</button>
  135. <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" title="下载批量导入模版">
  136. <span class="caret"></span>
  137. <span class="sr-only">Toggle Dropdown</span>
  138. </button>
  139. <ul class="dropdown-menu" role="menu">{$download}</ul>
  140. </div>
  141. EOT;
  142. } else {
  143. $html[] = '<a href="' . $href . '" class="' . $class . '" title="' . $title . '" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="' . $icon . '"></i> ' . $text . '</a>';
  144. }
  145. } else {
  146. $html[] = '<a href="' . $href . '" class="' . $class . '" title="' . $title . '"><i class="' . $icon . '"></i> ' . $text . '</a>';
  147. }
  148. }
  149. return implode(' ', $html);
  150. }
  151. }
  152. if (!function_exists('build_heading')) {
  153. /**
  154. * 生成页面Heading
  155. *
  156. * @param string $path 指定的path
  157. * @return string
  158. */
  159. function build_heading($path = null, $container = true)
  160. {
  161. $title = $content = '';
  162. if (is_null($path)) {
  163. $action = request()->action();
  164. $controller = str_replace('.', '/', request()->controller());
  165. $path = strtolower($controller . ($action && $action != 'index' ? '/' . $action : ''));
  166. }
  167. // 根据当前的URI自动匹配父节点的标题和备注
  168. $data = Db::name('auth_rule')->where('name', $path)->field('title,remark')->find();
  169. if ($data) {
  170. $title = __($data['title']);
  171. $content = __($data['remark']);
  172. }
  173. if (!$content) return '';
  174. $result = '<div class="panel-lead"><em>' . $title . '</em>' . $content . '</div>';
  175. if ($container) {
  176. $result = '<div class="panel-heading">' . $result . '</div>';
  177. }
  178. return $result;
  179. }
  180. }