Config.php 7.4 KB

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