Config.php 5.3 KB

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