Config.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use think\Model;
  5. /**
  6. * 配置模型
  7. */
  8. class Config extends Model
  9. {
  10. // 表名,不含前缀
  11. protected $name = 'config';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = false;
  14. // 定义时间戳字段名
  15. protected $createTime = false;
  16. protected $updateTime = false;
  17. // 追加属性
  18. protected $append = [
  19. ];
  20. /**
  21. * 读取配置类型
  22. * @return array
  23. */
  24. public static function getTypeList()
  25. {
  26. $typeList = [
  27. 'string' => __('String'),
  28. 'text' => __('Text'),
  29. 'editor' => __('Editor'),
  30. 'number' => __('Number'),
  31. 'date' => __('Date'),
  32. 'time' => __('Time'),
  33. 'datetime' => __('Datetime'),
  34. 'select' => __('Select'),
  35. 'selects' => __('Selects'),
  36. 'image' => __('Image'),
  37. 'images' => __('Images'),
  38. 'file' => __('File'),
  39. 'files' => __('Files'),
  40. 'checkbox' => __('Checkbox'),
  41. 'radio' => __('Radio'),
  42. 'array' => __('Array'),
  43. 'custom' => __('Custom'),
  44. ];
  45. return $typeList;
  46. }
  47. public static function getRegexList()
  48. {
  49. $regexList = [
  50. 'required' => '必选',
  51. 'digits' => '数字',
  52. 'letters' => '字母',
  53. 'date' => '日期',
  54. 'time' => '时间',
  55. 'email' => '邮箱',
  56. 'url' => '网址',
  57. 'qq' => 'QQ号',
  58. 'IDcard' => '身份证',
  59. 'tel' => '座机电话',
  60. 'mobile' => '手机号',
  61. 'zipcode' => '邮编',
  62. 'chinese' => '中文',
  63. 'username' => '用户名',
  64. 'password' => '密码'
  65. ];
  66. return $regexList;
  67. }
  68. /**
  69. * 读取分类分组列表
  70. * @return array
  71. */
  72. public static function getGroupList()
  73. {
  74. $groupList = config('site.configgroup');
  75. foreach ($groupList as $k => &$v)
  76. {
  77. $v = __($v);
  78. }
  79. return $groupList;
  80. }
  81. public static function getArrayData($data)
  82. {
  83. $fieldarr = $valuearr = [];
  84. $field = isset($data['field']) ? $data['field'] : [];
  85. $value = isset($data['value']) ? $data['value'] : [];
  86. foreach ($field as $m => $n)
  87. {
  88. if ($n != '')
  89. {
  90. $fieldarr[] = $field[$m];
  91. $valuearr[] = $value[$m];
  92. }
  93. }
  94. return $fieldarr ? array_combine($fieldarr, $valuearr) : [];
  95. }
  96. /**
  97. * 将字符串解析成键值数组
  98. * @param string $text
  99. * @return array
  100. */
  101. public static function decode($text, $split = "\r\n")
  102. {
  103. $content = explode($split, $text);
  104. $arr = [];
  105. foreach ($content as $k => $v)
  106. {
  107. if (stripos($v, "|") !== false)
  108. {
  109. $item = explode('|', $v);
  110. $arr[$item[0]] = $item[1];
  111. }
  112. }
  113. return $arr;
  114. }
  115. /**
  116. * 将键值数组转换为字符串
  117. * @param array $array
  118. * @return string
  119. */
  120. public static function encode($array, $split = "\r\n")
  121. {
  122. $content = '';
  123. if ($array && is_array($array))
  124. {
  125. $arr = [];
  126. foreach ($array as $k => $v)
  127. {
  128. $arr[] = "{$k}|{$v}";
  129. }
  130. $content = implode($split, $arr);
  131. }
  132. return $content;
  133. }
  134. /**
  135. * 本地上传配置信息
  136. * @return array
  137. */
  138. public static function upload()
  139. {
  140. $uploadcfg = config('upload');
  141. $upload = [
  142. 'cdnurl' => $uploadcfg['cdnurl'],
  143. 'uploadurl' => $uploadcfg['uploadurl'],
  144. 'bucket' => 'local',
  145. 'maxsize' => $uploadcfg['maxsize'],
  146. 'mimetype' => $uploadcfg['mimetype'],
  147. 'multipart' => [],
  148. 'multiple' => $uploadcfg['multiple'],
  149. ];
  150. return $upload;
  151. }
  152. /**
  153. * 获取config.site格式的数组
  154. */
  155. public function getConfigSiteArr()
  156. {
  157. $redis = Redis::instance();
  158. $key = 'site';
  159. if ($ret = $redis->get($key)) {
  160. return json_decode($ret, true);
  161. } else {
  162. $config = [];
  163. foreach ($this->all() as $k => $v) {
  164. $value = $v->toArray();
  165. if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
  166. $value['value'] = explode(',', $value['value']);
  167. }
  168. if ($value['type'] == 'array') {
  169. $value['value'] = (array)json_decode($value['value'], true);
  170. }
  171. $config[$value['name']] = $value['value'];
  172. }
  173. if (!empty($config)) { // 防止空数组被写入缓存
  174. $redis->setex($key, 86400, json_encode($config, JSON_UNESCAPED_UNICODE));
  175. }
  176. return $config;
  177. }
  178. }
  179. }