Config.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use think\Db;
  5. /**
  6. * 配置模型
  7. */
  8. class Config extends Model
  9. {
  10. // 表名,不含前缀
  11. protected $name = 'config';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = false;
  14. // 定义时间戳字段名
  15. protected $createTime = false;
  16. protected $updateTime = false;
  17. // 追加属性
  18. protected $append = [
  19. 'extend_html'
  20. ];
  21. protected $type = [
  22. 'setting' => 'json',
  23. ];
  24. /**
  25. * 读取配置类型
  26. * @return array
  27. */
  28. public static function getTypeList()
  29. {
  30. $typeList = [
  31. 'string' => __('String'),
  32. 'password' => __('Password'),
  33. 'text' => __('Text'),
  34. 'editor' => __('Editor'),
  35. 'number' => __('Number'),
  36. 'date' => __('Date'),
  37. 'time' => __('Time'),
  38. 'datetime' => __('Datetime'),
  39. 'datetimerange' => __('Datetimerange'),
  40. 'select' => __('Select'),
  41. 'selects' => __('Selects'),
  42. 'image' => __('Image'),
  43. 'images' => __('Images'),
  44. 'file' => __('File'),
  45. 'files' => __('Files'),
  46. 'switch' => __('Switch'),
  47. 'checkbox' => __('Checkbox'),
  48. 'radio' => __('Radio'),
  49. 'city' => __('City'),
  50. 'selectpage' => __('Selectpage'),
  51. 'selectpages' => __('Selectpages'),
  52. 'array' => __('Array'),
  53. 'custom' => __('Custom'),
  54. ];
  55. return $typeList;
  56. }
  57. public static function getRegexList()
  58. {
  59. $regexList = [
  60. 'required' => '必选',
  61. 'digits' => '数字',
  62. 'letters' => '字母',
  63. 'date' => '日期',
  64. 'time' => '时间',
  65. 'email' => '邮箱',
  66. 'url' => '网址',
  67. 'qq' => 'QQ号',
  68. 'IDcard' => '身份证',
  69. 'tel' => '座机电话',
  70. 'mobile' => '手机号',
  71. 'zipcode' => '邮编',
  72. 'chinese' => '中文',
  73. 'username' => '用户名',
  74. 'password' => '密码'
  75. ];
  76. return $regexList;
  77. }
  78. public function getExtendHtmlAttr($value, $data)
  79. {
  80. $result = preg_replace_callback("/\{([a-zA-Z]+)\}/", function ($matches) use ($data) {
  81. if (isset($data[$matches[1]])) {
  82. return $data[$matches[1]];
  83. }
  84. }, $data['extend']);
  85. return $result;
  86. }
  87. public static function getSimTypeList()
  88. {
  89. $sim_sim_type_list = Db::name('config')->where('name', 'sim_sim_type')->value('content');
  90. return json_decode($sim_sim_type_list,true);
  91. }
  92. public static function getSimQuestionList()
  93. {
  94. $sim_question_setting_method = Db::name('config')->where('name', 'sim_question_setting_method')->value('content');
  95. return json_decode($sim_question_setting_method,true);
  96. }
  97. public static function getSimExamStatus()
  98. {
  99. $sim_exam_status = Db::name('config')->where('name', 'sim_exam_status')->value('content');
  100. return json_decode($sim_exam_status,true);
  101. }
  102. /**
  103. * 读取分类分组列表
  104. * @return array
  105. */
  106. public static function getGroupList()
  107. {
  108. $groupList = config('site.configgroup');
  109. foreach ($groupList as $k => &$v) {
  110. $v = __($v);
  111. }
  112. return $groupList;
  113. }
  114. public static function getArrayData($data)
  115. {
  116. if (!isset($data['value'])) {
  117. $result = [];
  118. foreach ($data as $index => $datum) {
  119. $result['field'][$index] = $datum['key'];
  120. $result['value'][$index] = $datum['value'];
  121. }
  122. $data = $result;
  123. }
  124. $fieldarr = $valuearr = [];
  125. $field = $data['field'] ?? ($data['key'] ?? []);
  126. $value = $data['value'] ?? [];
  127. foreach ($field as $m => $n) {
  128. if ($n != '') {
  129. $fieldarr[] = $field[$m];
  130. $valuearr[] = $value[$m];
  131. }
  132. }
  133. return $fieldarr ? array_combine($fieldarr, $valuearr) : [];
  134. }
  135. /**
  136. * 将字符串解析成键值数组
  137. * @param string $text
  138. * @return array
  139. */
  140. public static function decode($text, $split = "\r\n")
  141. {
  142. $content = explode($split, $text);
  143. $arr = [];
  144. foreach ($content as $k => $v) {
  145. if (stripos($v, "|") !== false) {
  146. $item = explode('|', $v);
  147. $arr[$item[0]] = $item[1];
  148. }
  149. }
  150. return $arr;
  151. }
  152. /**
  153. * 将键值数组转换为字符串
  154. * @param array $array
  155. * @return string
  156. */
  157. public static function encode($array, $split = "\r\n")
  158. {
  159. $content = '';
  160. if ($array && is_array($array)) {
  161. $arr = [];
  162. foreach ($array as $k => $v) {
  163. $arr[] = "{$k}|{$v}";
  164. }
  165. $content = implode($split, $arr);
  166. }
  167. return $content;
  168. }
  169. /**
  170. * 本地上传配置信息
  171. * @return array
  172. */
  173. public static function upload()
  174. {
  175. $uploadcfg = config('upload');
  176. $uploadurl = request()->module() ? $uploadcfg['uploadurl'] : ($uploadcfg['uploadurl'] === 'ajax/upload' ? 'index/' . $uploadcfg['uploadurl'] : $uploadcfg['uploadurl']);
  177. if (!preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $uploadurl) && substr($uploadurl, 0, 1) !== '/') {
  178. $uploadurl = url($uploadurl, '', false);
  179. }
  180. $uploadcfg['fullmode'] = isset($uploadcfg['fullmode']) && $uploadcfg['fullmode'];
  181. $uploadcfg['thumbstyle'] = $uploadcfg['thumbstyle'] ?? '';
  182. $upload = [
  183. 'cdnurl' => $uploadcfg['cdnurl'],
  184. 'uploadurl' => $uploadurl,
  185. 'bucket' => 'local',
  186. 'maxsize' => $uploadcfg['maxsize'],
  187. 'mimetype' => $uploadcfg['mimetype'],
  188. 'chunking' => $uploadcfg['chunking'],
  189. 'chunksize' => $uploadcfg['chunksize'],
  190. 'savekey' => $uploadcfg['savekey'],
  191. 'multipart' => [],
  192. 'multiple' => $uploadcfg['multiple'],
  193. 'fullmode' => $uploadcfg['fullmode'],
  194. 'thumbstyle' => $uploadcfg['thumbstyle'],
  195. 'storage' => 'local'
  196. ];
  197. return $upload;
  198. }
  199. /**
  200. * 刷新配置文件
  201. */
  202. public static function refreshFile()
  203. {
  204. //如果没有配置权限无法进行修改
  205. if (!\app\admin\library\Auth::instance()->check('general/config/edit')) {
  206. return false;
  207. }
  208. $config = [];
  209. $configList = self::all();
  210. foreach ($configList as $k => $v) {
  211. $value = $v->toArray();
  212. if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
  213. $value['value'] = explode(',', $value['value']);
  214. }
  215. if ($value['type'] == 'array') {
  216. $value['value'] = (array)json_decode($value['value'], true);
  217. }
  218. $config[$value['name']] = $value['value'];
  219. }
  220. file_put_contents(
  221. CONF_PATH . 'extra' . DS . 'site.php',
  222. '<?php' . "\n\nreturn " . var_export_short($config) . ";\n"
  223. );
  224. return true;
  225. }
  226. }