Config.php 7.1 KB

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