common.php 8.6 KB

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