Config.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace app\admin\model\shopro;
  3. use think\Model;
  4. /**
  5. * 配置模型
  6. */
  7. class Config extends Model
  8. {
  9. // 表名,不含前缀
  10. protected $name = 'shopro_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. public static function load($name)
  78. {
  79. $value = self::where(['name' => $name])->value('value');
  80. return json_decode($value, true);
  81. }
  82. /**
  83. * 读取分类分组列表
  84. * @return array
  85. */
  86. public static function getGroupList()
  87. {
  88. $group = self::group('group')->column('group');
  89. $groupList = [];
  90. foreach ($group as $v) {
  91. $groupList[$v] = __($v);
  92. }
  93. return $groupList;
  94. }
  95. public static function getArrayData($data)
  96. {
  97. if (!isset($data['value'])) {
  98. $result = [];
  99. foreach ($data as $index => $datum) {
  100. $result['field'][$index] = $datum['key'];
  101. $result['value'][$index] = $datum['value'];
  102. }
  103. $data = $result;
  104. }
  105. $fieldarr = $valuearr = [];
  106. $field = isset($data['field']) ? $data['field'] : (isset($data['key']) ? $data['key'] : []);
  107. $value = isset($data['value']) ? $data['value'] : [];
  108. foreach ($field as $m => $n) {
  109. if ($n != '') {
  110. $fieldarr[] = $field[$m];
  111. $valuearr[] = $value[$m];
  112. }
  113. }
  114. return $fieldarr ? array_combine($fieldarr, $valuearr) : [];
  115. }
  116. /**
  117. * 将字符串解析成键值数组
  118. * @param string $text
  119. * @return array
  120. */
  121. public static function decode($text, $split = "\r\n")
  122. {
  123. $content = explode($split, $text);
  124. $arr = [];
  125. foreach ($content as $k => $v) {
  126. if (stripos($v, "|") !== false) {
  127. $item = explode('|', $v);
  128. $arr[$item[0]] = $item[1];
  129. }
  130. }
  131. return $arr;
  132. }
  133. /**
  134. * 将键值数组转换为字符串
  135. * @param array $array
  136. * @return string
  137. */
  138. public static function encode($array, $split = "\r\n")
  139. {
  140. $content = '';
  141. if ($array && is_array($array)) {
  142. $arr = [];
  143. foreach ($array as $k => $v) {
  144. $arr[] = "{$k}|{$v}";
  145. }
  146. $content = implode($split, $arr);
  147. }
  148. return $content;
  149. }
  150. /**
  151. * 本地上传配置信息
  152. * @return array
  153. */
  154. public static function upload()
  155. {
  156. $uploadcfg = config('upload');
  157. $upload = [
  158. 'cdnurl' => $uploadcfg['cdnurl'],
  159. 'uploadurl' => $uploadcfg['uploadurl'],
  160. 'bucket' => 'local',
  161. 'maxsize' => $uploadcfg['maxsize'],
  162. 'mimetype' => $uploadcfg['mimetype'],
  163. 'multipart' => [],
  164. 'multiple' => $uploadcfg['multiple'],
  165. ];
  166. return $upload;
  167. }
  168. }