Config.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. 'int' => __('Int'),
  54. 'enum' => __('Enum'),
  55. 'varchar' => __('Varchar'),
  56. 'mediumint' => __('Mediumint'),
  57. 'smallint' => __('Smallint'),
  58. 'tinyint' => __('Tinyint'),
  59. 'bigint' => __('Bigint'),
  60. 'decimal' => __('Decimal'),
  61. 'mediumtext' => __('Mediumtext'),
  62. 'longtext' => __('Longtext'),
  63. ];
  64. return $typeList;
  65. }
  66. public static function getRegexList()
  67. {
  68. $regexList = [
  69. 'required' => '必选',
  70. 'digits' => '数字',
  71. 'letters' => '字母',
  72. 'date' => '日期',
  73. 'time' => '时间',
  74. 'email' => '邮箱',
  75. 'url' => '网址',
  76. 'qq' => 'QQ号',
  77. 'IDcard' => '身份证',
  78. 'tel' => '座机电话',
  79. 'mobile' => '手机号',
  80. 'zipcode' => '邮编',
  81. 'chinese' => '中文',
  82. 'username' => '用户名',
  83. 'password' => '密码'
  84. ];
  85. return $regexList;
  86. }
  87. public function getExtendHtmlAttr($value, $data)
  88. {
  89. $result = preg_replace_callback("/\{([a-zA-Z]+)\}/", function ($matches) use ($data) {
  90. if (isset($data[$matches[1]])) {
  91. return $data[$matches[1]];
  92. }
  93. }, $data['extend']);
  94. return $result;
  95. }
  96. /**
  97. * 读取分类分组列表
  98. * @return array
  99. */
  100. public static function getGroupList()
  101. {
  102. $groupList = config('site.configgroup');
  103. foreach ($groupList as $k => &$v) {
  104. $v = __($v);
  105. }
  106. return $groupList;
  107. }
  108. public static function getArrayData($data)
  109. {
  110. if (!isset($data['value'])) {
  111. $result = [];
  112. foreach ($data as $index => $datum) {
  113. $result['field'][$index] = $datum['key'];
  114. $result['value'][$index] = $datum['value'];
  115. }
  116. $data = $result;
  117. }
  118. $fieldarr = $valuearr = [];
  119. $field = isset($data['field']) ? $data['field'] : (isset($data['key']) ? $data['key'] : []);
  120. $value = isset($data['value']) ? $data['value'] : [];
  121. foreach ($field as $m => $n) {
  122. if ($n != '') {
  123. $fieldarr[] = $field[$m];
  124. $valuearr[] = $value[$m];
  125. }
  126. }
  127. return $fieldarr ? array_combine($fieldarr, $valuearr) : [];
  128. }
  129. /**
  130. * 将字符串解析成键值数组
  131. * @param string $text
  132. * @return array
  133. */
  134. public static function decode($text, $split = "\r\n")
  135. {
  136. $content = explode($split, $text);
  137. $arr = [];
  138. foreach ($content as $k => $v) {
  139. if (stripos($v, "|") !== false) {
  140. $item = explode('|', $v);
  141. $arr[$item[0]] = $item[1];
  142. }
  143. }
  144. return $arr;
  145. }
  146. /**
  147. * 将键值数组转换为字符串
  148. * @param array $array
  149. * @return string
  150. */
  151. public static function encode($array, $split = "\r\n")
  152. {
  153. $content = '';
  154. if ($array && is_array($array)) {
  155. $arr = [];
  156. foreach ($array as $k => $v) {
  157. $arr[] = "{$k}|{$v}";
  158. }
  159. $content = implode($split, $arr);
  160. }
  161. return $content;
  162. }
  163. /**
  164. * 本地上传配置信息
  165. * @return array
  166. */
  167. public static function upload()
  168. {
  169. $uploadcfg = config('upload');
  170. $uploadurl = request()->module() ? $uploadcfg['uploadurl'] : ($uploadcfg['uploadurl'] === 'ajax/upload' ? 'index/' . $uploadcfg['uploadurl'] : $uploadcfg['uploadurl']);
  171. if (!preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $uploadurl) && substr($uploadurl, 0, 1) !== '/') {
  172. $uploadurl = url($uploadurl, '', false);
  173. }
  174. $uploadcfg['fullmode'] = isset($uploadcfg['fullmode']) && $uploadcfg['fullmode'] ? true : false;
  175. $uploadcfg['thumbstyle'] = $uploadcfg['thumbstyle'] ?? '';
  176. $upload = [
  177. 'cdnurl' => $uploadcfg['cdnurl'],
  178. 'uploadurl' => $uploadurl,
  179. 'bucket' => 'local',
  180. 'maxsize' => $uploadcfg['maxsize'],
  181. 'mimetype' => $uploadcfg['mimetype'],
  182. 'chunking' => $uploadcfg['chunking'],
  183. 'chunksize' => $uploadcfg['chunksize'],
  184. 'savekey' => $uploadcfg['savekey'],
  185. 'multipart' => [],
  186. 'multiple' => $uploadcfg['multiple'],
  187. 'fullmode' => $uploadcfg['fullmode'],
  188. 'thumbstyle' => $uploadcfg['thumbstyle'],
  189. 'storage' => 'local'
  190. ];
  191. return $upload;
  192. }
  193. /**
  194. * 刷新配置文件
  195. */
  196. public static function refreshFile()
  197. {
  198. //如果没有配置权限无法进行修改
  199. if (!\app\admin\library\Auth::instance()->check('general/config/edit')) {
  200. return false;
  201. }
  202. $config = [];
  203. $configList = self::all();
  204. foreach ($configList as $k => $v) {
  205. $value = $v->toArray();
  206. if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
  207. $value['value'] = explode(',', $value['value']);
  208. }
  209. if ($value['type'] == 'array') {
  210. $value['value'] = (array)json_decode($value['value'], true);
  211. }
  212. $config[$value['name']] = $value['value'];
  213. }
  214. file_put_contents(
  215. CONF_PATH . 'extra' . DS . 'site.php',
  216. '<?php' . "\n\nreturn " . var_export_short($config) . ";\n"
  217. );
  218. return true;
  219. }
  220. }