common.php 8.1 KB

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