Config.php 6.7 KB

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