common.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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) {
  174. return '';
  175. }
  176. $result = '<div class="panel-lead"><em>' . $title . '</em>' . $content . '</div>';
  177. if ($container) {
  178. $result = '<div class="panel-heading">' . $result . '</div>';
  179. }
  180. return $result;
  181. }
  182. }
  183. if (!function_exists('build_suffix_image')) {
  184. /**
  185. * 生成文件后缀图片
  186. * @param string $suffix 后缀
  187. * @param null $background
  188. * @return string
  189. */
  190. function build_suffix_image($suffix, $background = null)
  191. {
  192. $suffix = mb_substr(strtoupper($suffix), 0, 4);
  193. $total = unpack('L', hash('adler32', $suffix, true))[1];
  194. $hue = $total % 360;
  195. list($r, $g, $b) = hsv2rgb($hue / 360, 0.3, 0.9);
  196. $background = $background ? $background : "rgb({$r},{$g},{$b})";
  197. $icon = <<<EOT
  198. <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
  199. <path style="fill:#E2E5E7;" d="M128,0c-17.6,0-32,14.4-32,32v448c0,17.6,14.4,32,32,32h320c17.6,0,32-14.4,32-32V128L352,0H128z"/>
  200. <path style="fill:#B0B7BD;" d="M384,128h96L352,0v96C352,113.6,366.4,128,384,128z"/>
  201. <polygon style="fill:#CAD1D8;" points="480,224 384,128 480,128 "/>
  202. <path style="fill:{$background};" d="M416,416c0,8.8-7.2,16-16,16H48c-8.8,0-16-7.2-16-16V256c0-8.8,7.2-16,16-16h352c8.8,0,16,7.2,16,16 V416z"/>
  203. <path style="fill:#CAD1D8;" d="M400,432H96v16h304c8.8,0,16-7.2,16-16v-16C416,424.8,408.8,432,400,432z"/>
  204. <g><text><tspan x="220" y="380" font-size="124" font-family="Verdana, Helvetica, Arial, sans-serif" fill="white" text-anchor="middle">{$suffix}</tspan></text></g>
  205. </svg>
  206. EOT;
  207. return $icon;
  208. }
  209. }
  210. /**
  211. * 是否系统用户
  212. */
  213. if(!function_exists('is_sys_admin'))
  214. {
  215. function is_sys_admin($from= '')
  216. {
  217. $admin = session('admin');
  218. if($admin['cid']>0)
  219. {
  220. $rules = Db::name("auth_group_access")->alias('a')->join("auth_group b","a.group_id=b.id","left")->where('a.uid',$admin['id'])->column('rules');
  221. if(!in_array("*",$rules))
  222. {
  223. return [($from?"$from.":"").'cid'=>$admin['cid']];
  224. }
  225. }
  226. return null;
  227. }
  228. }