Config.php 7.0 KB

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