Config.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. ];
  19. /**
  20. * 读取配置类型
  21. * @return array
  22. */
  23. public static function getTypeList()
  24. {
  25. $typeList = [
  26. 'string' => __('String'),
  27. 'text' => __('Text'),
  28. 'editor' => __('Editor'),
  29. 'number' => __('Number'),
  30. 'date' => __('Date'),
  31. 'time' => __('Time'),
  32. 'datetime' => __('Datetime'),
  33. 'select' => __('Select'),
  34. 'selects' => __('Selects'),
  35. 'image' => __('Image'),
  36. 'images' => __('Images'),
  37. 'file' => __('File'),
  38. 'files' => __('Files'),
  39. 'switch' => __('Switch'),
  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. public function getExtendAttr($value, $data)
  69. {
  70. $result = preg_replace_callback("/\{([a-zA-Z]+)\}/", function ($matches) use ($data) {
  71. if (isset($data[$matches[1]])) {
  72. return $data[$matches[1]];
  73. }
  74. }, $data['extend']);
  75. return $result;
  76. }
  77. /**
  78. * 读取分类分组列表
  79. * @return array
  80. */
  81. public static function getGroupList()
  82. {
  83. $groupList = config('site.configgroup');
  84. foreach ($groupList as $k => &$v) {
  85. $v = __($v);
  86. }
  87. return $groupList;
  88. }
  89. public static function getArrayData($data)
  90. {
  91. if (!isset($data['value'])) {
  92. $result = [];
  93. foreach ($data as $index => $datum) {
  94. $result['field'][$index] = $datum['key'];
  95. $result['value'][$index] = $datum['value'];
  96. }
  97. $data = $result;
  98. }
  99. $fieldarr = $valuearr = [];
  100. $field = isset($data['field']) ? $data['field'] : (isset($data['key']) ? $data['key'] : []);
  101. $value = isset($data['value']) ? $data['value'] : [];
  102. foreach ($field as $m => $n) {
  103. if ($n != '') {
  104. $fieldarr[] = $field[$m];
  105. $valuearr[] = $value[$m];
  106. }
  107. }
  108. return $fieldarr ? array_combine($fieldarr, $valuearr) : [];
  109. }
  110. /**
  111. * 将字符串解析成键值数组
  112. * @param string $text
  113. * @return array
  114. */
  115. public static function decode($text, $split = "\r\n")
  116. {
  117. $content = explode($split, $text);
  118. $arr = [];
  119. foreach ($content as $k => $v) {
  120. if (stripos($v, "|") !== false) {
  121. $item = explode('|', $v);
  122. $arr[$item[0]] = $item[1];
  123. }
  124. }
  125. return $arr;
  126. }
  127. /**
  128. * 将键值数组转换为字符串
  129. * @param array $array
  130. * @return string
  131. */
  132. public static function encode($array, $split = "\r\n")
  133. {
  134. $content = '';
  135. if ($array && is_array($array)) {
  136. $arr = [];
  137. foreach ($array as $k => $v) {
  138. $arr[] = "{$k}|{$v}";
  139. }
  140. $content = implode($split, $arr);
  141. }
  142. return $content;
  143. }
  144. /**
  145. * 本地上传配置信息
  146. * @return array
  147. */
  148. public static function upload()
  149. {
  150. $uploadcfg = config('upload');
  151. $upload = [
  152. 'cdnurl' => $uploadcfg['cdnurl'],
  153. 'uploadurl' => $uploadcfg['uploadurl'],
  154. 'bucket' => 'local',
  155. 'maxsize' => $uploadcfg['maxsize'],
  156. 'mimetype' => $uploadcfg['mimetype'],
  157. 'multipart' => [],
  158. 'multiple' => $uploadcfg['multiple'],
  159. ];
  160. return $upload;
  161. }
  162. }