<?php
namespace app\common\model;
use think\Cache;
use app\common\enum\DeliveryType as DeliveryTypeEnum;
/**
* 系统设置模型
* Class Setting
* @package app\common\model
*/
class Setting extends BaseModel
{
protected $name = 'setting';
protected $createTime = false;
/**
* 获取器: 转义数组格式
* @param $value
* @return mixed
*/
public function getValuesAttr($value)
{
return json_decode($value, true);
}
/**
* 修改器: 转义成json格式
* @param $value
* @return string
*/
public function setValuesAttr($value)
{
return json_encode($value);
}
/**
* 获取指定项设置
* @param $key
* @param $wxapp_id
* @return array
*/
public static function getItem($key, $wxapp_id = null)
{
$data = self::getAll($wxapp_id);
return isset($data[$key]) ? $data[$key]['values'] : [];
}
/**
* 获取设置项信息
* @param $key
* @return null|static
* @throws \think\exception\DbException
*/
public static function detail($key)
{
return self::get(compact('key'));
}
/**
* 全局缓存: 系统设置
* @param null $wxapp_id
* @return array|mixed
*/
public static function getAll($wxapp_id = null)
{
$static = new static;
is_null($wxapp_id) && $wxapp_id = $static::$wxapp_id;
if (!$data = Cache::get('setting_' . $wxapp_id)) {
$setting = $static::all(compact('wxapp_id'));
$data = empty($setting) ? [] : array_column(collection($setting)->toArray(), null, 'key');
Cache::tag('cache')->set('setting_' . $wxapp_id, $data);
}
return $static->getMergeData($data);
}
/**
* 合并用户设置与默认数据
* @param $userData
* @return array
*/
private function getMergeData($userData)
{
$defaultData = $this->defaultData();
// 系统设置:配送方式
if (isset($userData['store']['values']['delivery_type'])) {
unset($defaultData['store']['values']['delivery_type']);
}
return array_merge_multiple($defaultData, $userData);
}
/**
* 默认配置
* @param null|string $storeName
* @return array
*/
public function defaultData($storeName = null)
{
return [
'store' => [
'key' => 'store',
'describe' => '系统设置',
'values' => [
// 系统名称
'name' => $storeName ?: 'tpwe系统',
// 配送方式
'delivery_type' => array_keys(DeliveryTypeEnum::data()),
// 快递100
'kuaidi100' => [
'customer' => '',
'key' => '',
]
],
],
'trade' => [
'key' => 'trade',
'describe' => '交易设置',
'values' => [
'order' => [
'close_days' => '3',
'receive_days' => '10',
'refund_days' => '7'
],
'freight_rule' => '10',
]
],
'storage' => [
'key' => 'storage',
'describe' => '上传设置',
'values' => [
'default' => 'local',
'engine' => [
'local' => [],
'qiniu' => [
'bucket' => '',
'access_key' => '',
'secret_key' => '',
'domain' => 'http://'
],
'aliyun' => [
'bucket' => '',
'access_key_id' => '',
'access_key_secret' => '',
'domain' => 'http://'
],
'qcloud' => [
'bucket' => '',
'region' => '',
'secret_id' => '',
'secret_key' => '',
'domain' => 'http://'
],
]
],
],
'sms' => [
'key' => 'sms',
'describe' => '短信通知',
'values' => [
'default' => 'aliyun',
'engine' => [
'aliyun' => [
'AccessKeyId' => '',
'AccessKeySecret' => '',
'sign' => 'TPWE',
'order_pay' => [
'is_enable' => '0',
'template_code' => '',
'accept_phone' => '',
],
],
],
],
],
'tplMsg' => [
'key' => 'tplMsg',
'describe' => '模板消息',
'values' => [
'payment' => [
'is_enable' => '0',
'template_id' => '',
],
'delivery' => [
'is_enable' => '0',
'template_id' => '',
],
'refund' => [
'is_enable' => '0',
'template_id' => '',
],
],
],
];
}
}