Config.php 6.9 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. 'text' => __('Text'),
  32. 'editor' => __('Editor'),
  33. 'number' => __('Number'),
  34. 'date' => __('Date'),
  35. 'time' => __('Time'),
  36. 'datetime' => __('Datetime'),
  37. 'datetimerange' => __('Datetimerange'),
  38. 'select' => __('Select'),
  39. 'selects' => __('Selects'),
  40. 'image' => __('Image'),
  41. 'images' => __('Images'),
  42. 'file' => __('File'),
  43. 'files' => __('Files'),
  44. 'switch' => __('Switch'),
  45. 'checkbox' => __('Checkbox'),
  46. 'radio' => __('Radio'),
  47. 'city' => __('City'),
  48. 'selectpage' => __('Selectpage'),
  49. 'selectpages' => __('Selectpages'),
  50. 'array' => __('Array'),
  51. 'custom' => __('Custom'),
  52. ];
  53. return $typeList;
  54. }
  55. public static function getRegexList()
  56. {
  57. $regexList = [
  58. 'required' => '必选',
  59. 'digits' => '数字',
  60. 'letters' => '字母',
  61. 'date' => '日期',
  62. 'time' => '时间',
  63. 'email' => '邮箱',
  64. 'url' => '网址',
  65. 'qq' => 'QQ号',
  66. 'IDcard' => '身份证',
  67. 'tel' => '座机电话',
  68. 'mobile' => '手机号',
  69. 'zipcode' => '邮编',
  70. 'chinese' => '中文',
  71. 'username' => '用户名',
  72. 'password' => '密码'
  73. ];
  74. return $regexList;
  75. }
  76. public function getExtendHtmlAttr($value, $data)
  77. {
  78. $result = preg_replace_callback("/\{([a-zA-Z]+)\}/", function ($matches) use ($data) {
  79. if (isset($data[$matches[1]])) {
  80. return $data[$matches[1]];
  81. }
  82. }, $data['extend']);
  83. return $result;
  84. }
  85. /**
  86. * 读取分类分组列表
  87. * @return array
  88. */
  89. public static function getGroupList()
  90. {
  91. $groupList = config('site.configgroup');
  92. foreach ($groupList as $k => &$v) {
  93. $v = __($v);
  94. }
  95. return $groupList;
  96. }
  97. public static function newgetGroupList()
  98. {
  99. $groupList = array (
  100. // 'basic' => '基础配置',
  101. 'website' => 'APP配置',
  102. 'takecash' => '提现配置',
  103. 'indexpipei' => '匹配设置',
  104. 'contentaudit' => '全站审核',
  105. 'question' => '问答设置',
  106. );
  107. foreach ($groupList as $k => &$v) {
  108. $v = __($v);
  109. }
  110. return $groupList;
  111. }
  112. public static function getArrayData($data)
  113. {
  114. if (!isset($data['value'])) {
  115. $result = [];
  116. foreach ($data as $index => $datum) {
  117. $result['field'][$index] = $datum['key'];
  118. $result['value'][$index] = $datum['value'];
  119. }
  120. $data = $result;
  121. }
  122. $fieldarr = $valuearr = [];
  123. $field = isset($data['field']) ? $data['field'] : (isset($data['key']) ? $data['key'] : []);
  124. $value = isset($data['value']) ? $data['value'] : [];
  125. foreach ($field as $m => $n) {
  126. if ($n != '') {
  127. $fieldarr[] = $field[$m];
  128. $valuearr[] = $value[$m];
  129. }
  130. }
  131. return $fieldarr ? array_combine($fieldarr, $valuearr) : [];
  132. }
  133. /**
  134. * 将字符串解析成键值数组
  135. * @param string $text
  136. * @return array
  137. */
  138. public static function decode($text, $split = "\r\n")
  139. {
  140. $content = explode($split, $text);
  141. $arr = [];
  142. foreach ($content as $k => $v) {
  143. if (stripos($v, "|") !== false) {
  144. $item = explode('|', $v);
  145. $arr[$item[0]] = $item[1];
  146. }
  147. }
  148. return $arr;
  149. }
  150. /**
  151. * 将键值数组转换为字符串
  152. * @param array $array
  153. * @return string
  154. */
  155. public static function encode($array, $split = "\r\n")
  156. {
  157. $content = '';
  158. if ($array && is_array($array)) {
  159. $arr = [];
  160. foreach ($array as $k => $v) {
  161. $arr[] = "{$k}|{$v}";
  162. }
  163. $content = implode($split, $arr);
  164. }
  165. return $content;
  166. }
  167. /**
  168. * 本地上传配置信息
  169. * @return array
  170. */
  171. public static function upload()
  172. {
  173. $uploadcfg = config('upload');
  174. $uploadurl = request()->module() ? $uploadcfg['uploadurl'] : ($uploadcfg['uploadurl'] === 'ajax/upload' ? 'index/' . $uploadcfg['uploadurl'] : $uploadcfg['uploadurl']);
  175. if (!preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $uploadurl) && substr($uploadurl, 0, 1) !== '/') {
  176. $uploadurl = url($uploadurl, '', false);
  177. }
  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. '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. }