Config.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. /**
  98. * 读取分类分组列表
  99. * @return array
  100. */
  101. public static function getGroupList()
  102. {
  103. $groupList = config('site.configgroup');
  104. foreach ($groupList as $k => &$v) {
  105. $v = __($v);
  106. }
  107. return $groupList;
  108. }
  109. public static function getArrayData($data)
  110. {
  111. if (!isset($data['value'])) {
  112. $result = [];
  113. foreach ($data as $index => $datum) {
  114. $result['field'][$index] = $datum['key'];
  115. $result['value'][$index] = $datum['value'];
  116. }
  117. $data = $result;
  118. }
  119. $fieldarr = $valuearr = [];
  120. $field = $data['field'] ?? ($data['key'] ?? []);
  121. $value = $data['value'] ?? [];
  122. foreach ($field as $m => $n) {
  123. if ($n != '') {
  124. $fieldarr[] = $field[$m];
  125. $valuearr[] = $value[$m];
  126. }
  127. }
  128. return $fieldarr ? array_combine($fieldarr, $valuearr) : [];
  129. }
  130. /**
  131. * 将字符串解析成键值数组
  132. * @param string $text
  133. * @return array
  134. */
  135. public static function decode($text, $split = "\r\n")
  136. {
  137. $content = explode($split, $text);
  138. $arr = [];
  139. foreach ($content as $k => $v) {
  140. if (stripos($v, "|") !== false) {
  141. $item = explode('|', $v);
  142. $arr[$item[0]] = $item[1];
  143. }
  144. }
  145. return $arr;
  146. }
  147. /**
  148. * 将键值数组转换为字符串
  149. * @param array $array
  150. * @return string
  151. */
  152. public static function encode($array, $split = "\r\n")
  153. {
  154. $content = '';
  155. if ($array && is_array($array)) {
  156. $arr = [];
  157. foreach ($array as $k => $v) {
  158. $arr[] = "{$k}|{$v}";
  159. }
  160. $content = implode($split, $arr);
  161. }
  162. return $content;
  163. }
  164. /**
  165. * 本地上传配置信息
  166. * @return array
  167. */
  168. public static function upload()
  169. {
  170. $uploadcfg = config('upload');
  171. $uploadurl = request()->module() ? $uploadcfg['uploadurl'] : ($uploadcfg['uploadurl'] === 'ajax/upload' ? 'index/' . $uploadcfg['uploadurl'] : $uploadcfg['uploadurl']);
  172. if (!preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $uploadurl) && substr($uploadurl, 0, 1) !== '/') {
  173. $uploadurl = url($uploadurl, '', false);
  174. }
  175. $uploadcfg['fullmode'] = isset($uploadcfg['fullmode']) && $uploadcfg['fullmode'];
  176. $uploadcfg['thumbstyle'] = $uploadcfg['thumbstyle'] ?? '';
  177. $upload = [
  178. 'cdnurl' => $uploadcfg['cdnurl'],
  179. 'uploadurl' => $uploadurl,
  180. 'bucket' => 'local',
  181. 'maxsize' => $uploadcfg['maxsize'],
  182. 'mimetype' => $uploadcfg['mimetype'],
  183. 'chunking' => $uploadcfg['chunking'],
  184. 'chunksize' => $uploadcfg['chunksize'],
  185. 'savekey' => $uploadcfg['savekey'],
  186. 'multipart' => [],
  187. 'multiple' => $uploadcfg['multiple'],
  188. 'fullmode' => $uploadcfg['fullmode'],
  189. 'thumbstyle' => $uploadcfg['thumbstyle'],
  190. 'storage' => 'local'
  191. ];
  192. return $upload;
  193. }
  194. /**
  195. * 刷新配置文件
  196. */
  197. public static function refreshFile()
  198. {
  199. //如果没有配置权限无法进行修改
  200. if (!\app\admin\library\Auth::instance()->check('general/config/edit')) {
  201. return false;
  202. }
  203. $config = [];
  204. $configList = self::all();
  205. foreach ($configList as $k => $v) {
  206. $value = $v->toArray();
  207. if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
  208. $value['value'] = explode(',', $value['value']);
  209. }
  210. if ($value['type'] == 'array') {
  211. $value['value'] = (array)json_decode($value['value'], true);
  212. }
  213. $config[$value['name']] = $value['value'];
  214. }
  215. file_put_contents(
  216. CONF_PATH . 'extra' . DS . 'site.php',
  217. '<?php' . "\n\nreturn " . var_export_short($config) . ";\n"
  218. );
  219. return true;
  220. }
  221. }