common.php 8.0 KB

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