Config.php 5.2 KB

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