Setting.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\common\model\store;
  13. use think\facade\Cache;
  14. use app\common\library\helper;
  15. use cores\BaseModel;
  16. use app\common\enum\Setting as SettingEnum;
  17. use app\common\enum\file\Storage as StorageEnum;
  18. use app\common\enum\order\DeliveryType as DeliveryTypeEnum;
  19. use app\common\enum\setting\sms\Scene as SettingSmsSceneEnum;
  20. use app\common\enum\store\page\category\Style as PageCategoryStyleEnum;
  21. /**
  22. * 系统设置模型
  23. * Class Setting
  24. * @package app\common\model
  25. */
  26. class Setting extends BaseModel
  27. {
  28. // 定义表名
  29. protected $name = 'store_setting';
  30. /**
  31. * 获取器: 转义数组格式
  32. * @param $value
  33. * @return mixed
  34. */
  35. public function getValuesAttr($value)
  36. {
  37. return helper::jsonDecode($value);
  38. }
  39. /**
  40. * 修改器: 转义成json格式
  41. * @param $value
  42. * @return string
  43. */
  44. public function setValuesAttr($value)
  45. {
  46. return helper::jsonEncode($value);
  47. }
  48. /**
  49. * 获取指定项设置
  50. * @param string $key
  51. * @param int|null $storeId
  52. * @return array|mixed
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public static function getItem(string $key, int $storeId = null)
  58. {
  59. $data = self::getAll($storeId);
  60. return isset($data[$key]) ? $data[$key]['values'] : [];
  61. }
  62. /**
  63. * 获取设置项信息
  64. * @param string $key
  65. * @param int|null $storeId
  66. * @return array|\think\Model|null
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\DbException
  69. * @throws \think\db\exception\ModelNotFoundException
  70. */
  71. public static function detail(string $key, int $storeId = null)
  72. {
  73. $query = (new static)->getNewQuery();
  74. $storeId > 0 && $query->where('store_id', '=', $storeId);
  75. return $query->where('key', '=', $key)->find();
  76. }
  77. /**
  78. * 全局缓存: 系统设置
  79. * @param int|null $storeId
  80. * @return array
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. public static function getAll(int $storeId = null)
  86. {
  87. $model = new static;
  88. is_null($storeId) && $storeId = $model::$storeId;
  89. if (!$data = Cache::get("setting_{$storeId}")) {
  90. // 获取商城设置列表
  91. $setting = $model->getList($storeId);
  92. $data = $setting->isEmpty() ? [] : helper::arrayColumn2Key($setting->toArray(), 'key');
  93. // 写入缓存中
  94. Cache::tag('cache')->set("setting_{$storeId}", $data);
  95. }
  96. return $model->getMergeData($data);
  97. }
  98. /**
  99. * 获取商城设置列表
  100. * @param $storeId
  101. * @return \think\Collection
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. private function getList(int $storeId)
  107. {
  108. return $this->where('store_id', '=', $storeId)->select();
  109. }
  110. /**
  111. * 合并用户设置与默认数据
  112. * @param array $actualData
  113. * @return array
  114. */
  115. private function getMergeData(array $actualData)
  116. {
  117. return array_merge_multiple($this->defaultData(), $actualData);
  118. }
  119. /**
  120. * 默认配置
  121. * @return array
  122. */
  123. public function defaultData()
  124. {
  125. return [
  126. // 配送设置
  127. SettingEnum::DELIVERY => [
  128. 'key' => SettingEnum::DELIVERY,
  129. 'describe' => '配送设置',
  130. 'values' => [
  131. // 配送方式
  132. 'delivery_type' => array_keys(DeliveryTypeEnum::data()),
  133. // 快递100
  134. 'kuaidi100' => [
  135. 'customer' => '',
  136. 'key' => '',
  137. ]
  138. ],
  139. ],
  140. // 交易设置
  141. SettingEnum::TRADE => [
  142. 'key' => SettingEnum::TRADE,
  143. 'describe' => '交易设置',
  144. 'values' => [
  145. // 订单流程设置
  146. 'order' => [
  147. 'close_days' => '3',
  148. 'receive_days' => '10',
  149. 'refund_days' => '7'
  150. ],
  151. // 运费组合策略
  152. 'freight_rule' => '10',
  153. ]
  154. ],
  155. // 上传设置
  156. SettingEnum::STORAGE => [
  157. 'key' => SettingEnum::STORAGE,
  158. 'describe' => '上传设置',
  159. 'values' => [
  160. 'default' => StorageEnum::LOCAL,
  161. 'engine' => [
  162. StorageEnum::LOCAL => null,
  163. StorageEnum::QINIU => [
  164. 'bucket' => '',
  165. 'access_key' => '',
  166. 'secret_key' => '',
  167. 'domain' => 'http://'
  168. ],
  169. StorageEnum::ALIYUN => [
  170. 'bucket' => '',
  171. 'access_key_id' => '',
  172. 'access_key_secret' => '',
  173. 'domain' => 'http://'
  174. ],
  175. StorageEnum::QCLOUD => [
  176. 'bucket' => '',
  177. 'region' => '',
  178. 'secret_id' => '',
  179. 'secret_key' => '',
  180. 'domain' => 'http://'
  181. ],
  182. ]
  183. ],
  184. ],
  185. // 短信通知
  186. SettingEnum::SMS => [
  187. 'key' => SettingEnum::SMS,
  188. 'describe' => '短信通知',
  189. 'values' => [
  190. 'default' => 'aliyun',
  191. // 短信服务渠道
  192. 'engine' => [
  193. // 阿里云
  194. 'aliyun' => [
  195. 'AccessKeyId' => '',
  196. 'AccessKeySecret' => '',
  197. 'sign' => '萤火商城' // 短信签名
  198. ]
  199. ],
  200. // 短信通知场景
  201. 'scene' => [
  202. // 短信验证码
  203. SettingSmsSceneEnum::CAPTCHA => [
  204. 'name' => '短信验证码', // 场景名称
  205. 'isEnable' => false, // 是否开启
  206. 'templateCode' => '', // 模板ID
  207. 'content' => '验证码${code},您正在进行身份验证,打死不要告诉别人哦!'
  208. ],
  209. // 新付款订单
  210. SettingSmsSceneEnum::ORDER_PAY => [
  211. 'name' => '新付款订单', // 场景名称
  212. 'isEnable' => false, // 是否开启
  213. 'templateCode' => '', // 模板ID
  214. 'acceptPhone' => '', // 接收手机号
  215. 'content' => '您有一条新订单,订单号为:${order_no},请注意查看'
  216. ]
  217. ]
  218. ],
  219. ],
  220. // 满额包邮设置
  221. SettingEnum::FULL_FREE => [
  222. 'key' => SettingEnum::FULL_FREE,
  223. 'describe' => '满额包邮设置',
  224. 'values' => [
  225. 'is_open' => 0, // 是否开启满额包邮
  226. 'money' => '', // 单笔订单额度
  227. 'excludedRegions' => [ // 不参与包邮的地区
  228. 'cityIds' => [], // 城市ID集
  229. 'selectedText' => '' // 选择的地区(文字)
  230. ],
  231. 'excludedGoodsIds' => [], // 不参与包邮的商品 (ID集)
  232. 'describe' => '' // 满额包邮说明
  233. ],
  234. ],
  235. // 用户充值设置
  236. SettingEnum::RECHARGE => [
  237. 'key' => SettingEnum::RECHARGE,
  238. 'describe' => '用户充值设置',
  239. 'values' => [
  240. 'is_entrance' => 1, // 是否允许用户充值
  241. 'is_custom' => 1, // 是否允许自定义金额
  242. 'is_match_plan' => 1, // 自定义金额是否自动匹配合适的套餐
  243. 'describe' => "1. 账户充值仅限微信在线方式支付,充值金额实时到账;\n" .
  244. "2. 账户充值套餐赠送的金额即时到账;\n" .
  245. "3. 账户余额有效期:自充值日起至用完即止;\n" .
  246. "4. 若有其它疑问,可拨打客服电话400-000-1234", // 充值说明
  247. ],
  248. ],
  249. // 积分设置
  250. SettingEnum::POINTS => [
  251. 'key' => SettingEnum::POINTS,
  252. 'describe' => SettingEnum::data()[SettingEnum::POINTS]['describe'],
  253. 'values' => [
  254. 'points_name' => '积分', // 积分名称自定义
  255. 'is_shopping_gift' => 0, // 是否开启购物送积分
  256. 'gift_ratio' => '100', // 积分赠送比例
  257. 'is_shopping_discount' => 0, // 是否允许下单使用积分抵扣
  258. 'discount' => [ // 积分抵扣
  259. 'discount_ratio' => '0.01', // 积分抵扣比例
  260. 'full_order_price' => '100.00', // 订单满[?]元
  261. 'max_money_ratio' => '10', // 最高可抵扣订单额百分比
  262. ],
  263. // 积分说明
  264. 'describe' => "a) 积分不可兑现、不可转让,仅可在本平台使用;\n" .
  265. "b) 您在本平台参加特定活动也可使用积分,详细使用规则以具体活动时的规则为准;\n" .
  266. "c) 积分的数值精确到个位(小数点后全部舍弃,不进行四舍五入)\n" .
  267. "d) 买家在完成该笔交易(订单状态为“已签收”)后才能得到此笔交易的相应积分,如购买商品参加店铺其他优惠,则优惠的金额部分不享受积分获取;",
  268. ],
  269. ],
  270. // 分类页模板
  271. SettingEnum::PAGE_CATEGORY_TEMPLATE => [
  272. 'key' => SettingEnum::PAGE_CATEGORY_TEMPLATE,
  273. 'describe' => '分类页模板设置',
  274. 'values' => [
  275. 'style' => PageCategoryStyleEnum::TWO_LEVEL, // 分类页样式
  276. 'shareTitle' => '' // 分享标题
  277. ]
  278. ]
  279. ];
  280. }
  281. }