common.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. use app\common\model\Category;
  3. use fast\Form;
  4. use fast\Tree;
  5. use think\Db;
  6. /**
  7. * 生成下拉列表
  8. * @param string $name
  9. * @param mixed $options
  10. * @param mixed $selected
  11. * @param mixed $attr
  12. * @return string
  13. */
  14. function build_select($name, $options, $selected = [], $attr = [])
  15. {
  16. $options = is_array($options) ? $options : explode(',', $options);
  17. $selected = is_array($selected) ? $selected : explode(',', $selected);
  18. return Form::select($name, $options, $selected, $attr);
  19. }
  20. /**
  21. * 生成单选按钮组
  22. * @param string $name
  23. * @param array $list
  24. * @param mixed $selected
  25. * @return string
  26. */
  27. function build_radios($name, $list = [], $selected = null)
  28. {
  29. $html = [];
  30. $selected = is_null($selected) ? key($list) : $selected;
  31. $selected = is_array($selected) ? $selected : explode(',', $selected);
  32. foreach ($list as $k => $v)
  33. {
  34. $html[] = sprintf(Form::label("{$name}-{$k}", "%s {$v}"), Form::radio($name, $k, in_array($k, $selected), ['id' => "{$name}-{$k}"]));
  35. }
  36. return '<div class="radio">' . implode(' ', $html) . '</div>';
  37. }
  38. /**
  39. * 生成复选按钮组
  40. * @param string $name
  41. * @param array $list
  42. * @param mixed $selected
  43. * @return string
  44. */
  45. function build_checkboxs($name, $list = [], $selected = null)
  46. {
  47. $html = [];
  48. $selected = is_null($selected) ? [] : $selected;
  49. $selected = is_array($selected) ? $selected : explode(',', $selected);
  50. foreach ($list as $k => $v)
  51. {
  52. $html[] = sprintf(Form::label("{$name}-{$k}", "%s {$v}"), Form::checkbox($name, $k, in_array($k, $selected), ['id' => "{$name}-{$k}"]));
  53. }
  54. return '<div class="checkbox">' . implode(' ', $html) . '</div>';
  55. }
  56. /**
  57. * 生成分类下拉列表框
  58. * @param string $name
  59. * @param string $type
  60. * @param mixed $selected
  61. * @param array $attr
  62. * @return string
  63. */
  64. function build_category_select($name, $type, $selected = null, $attr = [], $header = [])
  65. {
  66. $tree = Tree::instance();
  67. $tree->init(Category::getCategoryArray($type), 'pid');
  68. $categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  69. $categorydata = $header ? $header : [];
  70. foreach ($categorylist as $k => $v)
  71. {
  72. $categorydata[$v['id']] = $v['name'];
  73. }
  74. $attr = array_merge(['id' => "c-{$name}", 'class' => 'form-control selectpicker'], $attr);
  75. return build_select($name, $categorydata, $selected, $attr);
  76. }
  77. /**
  78. * 生成表格操作按钮栏
  79. * @param array $btns 按钮组
  80. * @param array $attr 按钮属性值
  81. * @return string
  82. */
  83. function build_toolbar($btns = NULL, $attr = [])
  84. {
  85. $auth = \app\admin\library\Auth::instance();
  86. $controller = str_replace('.', '/', strtolower(think\Request::instance()->controller()));
  87. $btns = $btns ? $btns : ['refresh', 'add', 'edit', 'del', 'import'];
  88. $btns = is_array($btns) ? $btns : explode(',', $btns);
  89. $index = array_search('delete', $btns);
  90. if ($index !== FALSE)
  91. {
  92. $btns[$index] = 'del';
  93. }
  94. $btnAttr = [
  95. 'refresh' => ['javascript:;', 'btn btn-primary btn-refresh', 'fa fa-refresh', '', __('Refresh')],
  96. 'add' => ['javascript:;', 'btn btn-success btn-add', 'fa fa-plus', __('Add'), __('Add')],
  97. 'edit' => ['javascript:;', 'btn btn-success btn-edit btn-disabled disabled', 'fa fa-pencil', __('Edit'), __('Edit')],
  98. 'del' => ['javascript:;', 'btn btn-danger btn-del btn-disabled disabled', 'fa fa-trash', __('Delete'), __('Delete')],
  99. 'import' => ['javascript:;', 'btn btn-danger btn-import', 'fa fa-upload', __('Import'), __('Import')],
  100. ];
  101. $btnAttr = array_merge($btnAttr, $attr);
  102. $html = [];
  103. foreach ($btns as $k => $v)
  104. {
  105. //如果未定义或没有权限
  106. if (!isset($btnAttr[$v]) || ($v !== 'refresh' && !$auth->check("{$controller}/{$v}")))
  107. {
  108. continue;
  109. }
  110. list($href, $class, $icon, $text, $title) = $btnAttr[$v];
  111. $extend = $v == 'import' ? 'id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"' : '';
  112. $html[] = '<a href="' . $href . '" class="' . $class . '" title="' . $title . '" ' . $extend . '><i class="' . $icon . '"></i> ' . $text . '</a>';
  113. }
  114. return implode(' ', $html);
  115. }
  116. /**
  117. * 生成页面Heading
  118. *
  119. * @param string $path 指定的path
  120. * @return string
  121. */
  122. function build_heading($path = NULL, $container = TRUE)
  123. {
  124. $title = $content = '';
  125. if (is_null($path))
  126. {
  127. $action = request()->action();
  128. $controller = str_replace('.', '/', request()->controller());
  129. $path = strtolower($controller . ($action && $action != 'index' ? '/' . $action : ''));
  130. }
  131. // 根据当前的URI自动匹配父节点的标题和备注
  132. $data = Db::name('auth_rule')->where('name', $path)->field('title,remark')->find();
  133. if ($data)
  134. {
  135. $title = __($data['title']);
  136. $content = __($data['remark']);
  137. }
  138. if (!$content)
  139. return '';
  140. $result = '<div class="panel-lead"><em>' . $title . '</em>' . $content . '</div>';
  141. if ($container)
  142. {
  143. $result = '<div class="panel-heading">' . $result . '</div>';
  144. }
  145. return $result;
  146. }
  147. //验证身份证是否有效
  148. function validateIDCard($IDCard) {
  149. if (strlen($IDCard) == 18) {
  150. return check18IDCard($IDCard);
  151. } elseif ((strlen($IDCard) == 15)) {
  152. $IDCard = convertIDCard15to18($IDCard);
  153. return check18IDCard($IDCard);
  154. } else {
  155. return false;
  156. }
  157. }
  158. //计算身份证的最后一位验证码,根据国家标准GB 11643-1999
  159. function calcIDCardCode($IDCardBody) {
  160. if (strlen($IDCardBody) != 17) {
  161. return false;
  162. }
  163. //加权因子
  164. $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  165. //校验码对应值
  166. $code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  167. $checksum = 0;
  168. for ($i = 0; $i < strlen($IDCardBody); $i++) {
  169. $checksum += substr($IDCardBody, $i, 1) * $factor[$i];
  170. }
  171. return $code[$checksum % 11];
  172. }
  173. // 将15位身份证升级到18位
  174. function convertIDCard15to18($IDCard) {
  175. if (strlen($IDCard) != 15) {
  176. return false;
  177. } else {
  178. // 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码
  179. if (array_search(substr($IDCard, 12, 3), array('996', '997', '998', '999')) !== false) {
  180. $IDCard = substr($IDCard, 0, 6) . '18' . substr($IDCard, 6, 9);
  181. } else {
  182. $IDCard = substr($IDCard, 0, 6) . '19' . substr($IDCard, 6, 9);
  183. }
  184. }
  185. $IDCard = $IDCard . calcIDCardCode($IDCard);
  186. return $IDCard;
  187. }
  188. // 18位身份证校验码有效性检查
  189. function check18IDCard($IDCard) {
  190. if (strlen($IDCard) != 18) {
  191. return false;
  192. }
  193. $IDCardBody = substr($IDCard, 0, 17); //身份证主体
  194. $IDCardCode = strtoupper(substr($IDCard, 17, 1)); //身份证最后一位的验证码
  195. if (calcIDCardCode($IDCardBody) != $IDCardCode) {
  196. return false;
  197. } else {
  198. return true;
  199. }
  200. }