Config.php 4.7 KB

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