Setting.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 28/02/2017
  6. * Time: 17:00
  7. */
  8. namespace app\common\models;
  9. use Carbon\Carbon;
  10. class Setting extends BaseModel
  11. {
  12. public $table = 'yz_setting';
  13. public $timestamps = false;
  14. public $guarded = [''];
  15. public $defaultGroup = 'shop';
  16. /**
  17. * 获取统一账号配置与值
  18. *
  19. * @param $uniqueAccountId
  20. * @param $key
  21. * @param null $default 默认值
  22. * @return mixed
  23. */
  24. public function getValue($uniqueAccountId, $key, $default = null)
  25. {
  26. if (app('SettingCache')->has($key) || in_array($key, app('SettingCache')->get('nullSettingKeys'))) {
  27. //\Log::debug('-----setting get cache------'.$cacheKey);
  28. $value = app('SettingCache')->get($key);
  29. } else {
  30. //\Log::debug('-----setting get db------'.$key);
  31. list($group, $groupKey) = $this->parseKey($key);
  32. // 如果不存在$groupKey,name判断是否为空,
  33. //\Log::debug('-----setting save cache------' . $cacheKey, $value);
  34. if ('love' == $key) {
  35. $settingGroupItems = $this->getItems($uniqueAccountId, $groupKey);
  36. //如果子key就是全部
  37. if (empty($settingGroupItems)) {
  38. // 如果数据库中不存在记录,需要在缓存中添加这个key,避免重复查库
  39. app('SettingCache')->push('nullSettingKeys', $key, 600);
  40. }
  41. return $settingGroupItems ?: $default;
  42. } else {
  43. $settingGroupItems = $this->getItems($uniqueAccountId, $group);
  44. if (!array_has($settingGroupItems, $groupKey)) {
  45. // 如果数据库中不存在记录,需要在缓存中添加这个key,避免重复查库
  46. app('SettingCache')->push('nullSettingKeys', $key, 600);
  47. }
  48. $value = array_get($settingGroupItems, $groupKey, $default);
  49. }
  50. app('SettingCache')->put($group, $settingGroupItems, 600);
  51. }
  52. return $value;
  53. }
  54. /**
  55. * 设置配置值.
  56. *
  57. * @param $uniqueAccountId
  58. * @param string $key 键 使用.隔开 第一位为group
  59. * @param mixed $value 值
  60. *
  61. * @return mixed
  62. */
  63. public function setValue($uniqueAccountId, $key, $value = null)
  64. {
  65. list($group, $item) = $this->parseKey($key);
  66. $type = $this->getTypeOfValue($value);
  67. $result = $this->setToDatabase($value, $uniqueAccountId, $group, $item, $type);
  68. if ($type == 'array') {
  69. $value = unserialize($value);
  70. }
  71. app('SettingCache')->put($key, $value, 600);
  72. //\Log::debug('-----setting set cache------' . $cacheKey, $value);
  73. return $result;
  74. }
  75. /**
  76. * 获取账号内当前组的所有配置信息
  77. *
  78. * @param $uniqueAccountId
  79. * @param $group
  80. * @return array
  81. */
  82. public function getItems($uniqueAccountId, $group)
  83. {
  84. $items = array();
  85. $settings = self::fetchSettings($uniqueAccountId, $group);
  86. foreach ($settings as $item) {
  87. switch (strtolower($item->type)) {
  88. case 'string':
  89. $items[$item->key] = (string)$item->value;
  90. break;
  91. case 'integer':
  92. $items[$item->key] = (integer)$item->value;
  93. break;
  94. case 'double':
  95. $items[$item->key] = (double)$item->value;
  96. break;
  97. case 'boolean':
  98. $items[$item->key] = (boolean)$item->value;
  99. break;
  100. case 'array':
  101. $items[$item->key] = unserialize($item->value);
  102. break;
  103. case 'null':
  104. $items[$item->key] = null;
  105. break;
  106. default:
  107. $items[$item->key] = $item->value;
  108. }
  109. }
  110. return $items;
  111. }
  112. /**
  113. * 检测是否存在相应配置组
  114. *
  115. * @param $uniqueAccountId
  116. * @param $group
  117. * @return bool
  118. */
  119. public function exists($uniqueAccountId, $group)
  120. {
  121. return !$this->fetchSettings($uniqueAccountId, $group)->isEmpty();
  122. }
  123. /**
  124. * 获取配置组数据
  125. *
  126. * @param $uniqueAccountId
  127. * @param $group
  128. * @return \Illuminate\Database\Eloquent\Collection|static[]
  129. */
  130. public function fetchSettings($uniqueAccountId, $group)
  131. {
  132. return self::where('group', $group)->where('uniacid', $uniqueAccountId)->get();
  133. }
  134. /**
  135. * 解析key
  136. * 分离出 group 与真正的key,其中.分隔第一个为group
  137. *
  138. * @param $key
  139. * @return array
  140. */
  141. protected function parseKey($key)
  142. {
  143. $explodedOnGroup = explode('.', $key);
  144. if (count($explodedOnGroup) > 1) {
  145. $group = array_shift($explodedOnGroup);
  146. $item = implode('.', $explodedOnGroup);
  147. } else {
  148. $group = $this->defaultGroup;
  149. $item = $explodedOnGroup[0];
  150. }
  151. return [$group, $item];
  152. }
  153. /**
  154. * 获取值的类型 并设置值为序列化
  155. *
  156. * @param $value
  157. * @return null|string
  158. */
  159. protected function getTypeOfValue(&$value)
  160. {
  161. $type = null;
  162. $givenType = strtolower(gettype($value));
  163. switch ($givenType) {
  164. case 'string':
  165. case 'integer':
  166. case 'double':
  167. case 'boolean':
  168. case 'null':
  169. $type = $givenType;
  170. break;
  171. case 'array':
  172. $value = serialize($value);
  173. $type = 'array';
  174. break;
  175. default:
  176. $type = null;
  177. }
  178. return $type;
  179. }
  180. /**
  181. * 格式化并保存配置到数据库
  182. * @param $value
  183. * @param $uniqueAccountId
  184. * @param $group
  185. * @param $key
  186. * @param $type
  187. * @return static
  188. */
  189. protected function setToDatabase($value, $uniqueAccountId, $group, $key, $type)
  190. {
  191. //检测数组是否需要特殊操作
  192. $arrayHandling = false;
  193. $keyExploded = explode('.', $key);
  194. if (count($keyExploded) > 1) {
  195. $arrayHandling = true;
  196. $key = array_shift($keyExploded);
  197. if ($type == 'array') {
  198. $value = unserialize($value);
  199. }
  200. }
  201. //如果存在记录则更新
  202. $model = static::where('key', $key)
  203. ->where('group', $group)
  204. ->where('uniacid', $uniqueAccountId)
  205. ->orderBy('id', 'desc');
  206. $model = $model->first();
  207. if (is_null($model)) {
  208. //如果数组需要特殊操作
  209. if ($arrayHandling) {
  210. $array = array();
  211. self::buildArrayPath($keyExploded, $value, $array);
  212. $value = serialize($array);
  213. $type = 'array';
  214. }
  215. $data = [
  216. 'uniacid' => $uniqueAccountId,
  217. 'group' => $group,
  218. 'key' => $key,
  219. 'value' => $value,
  220. 'type' => $type,
  221. ];
  222. return self::create($data);
  223. } else {
  224. //Check if we need to do special array handling
  225. if ($arrayHandling) { // we are setting a subset of an array
  226. $array = array();
  227. self::buildArrayPath($keyExploded, $value, $array);
  228. //如果是数组则合并
  229. if ($model->type == 'array') {
  230. $array = array_replace_recursive(unserialize($model->value), $array);
  231. }
  232. $value = serialize($array);
  233. $type = 'array';
  234. }
  235. $model->value = $value;
  236. $model->type = $type;
  237. return $model->save();
  238. }
  239. }
  240. /**
  241. * 组合数组
  242. *
  243. * @param $map
  244. * @param $value
  245. * @param $array
  246. */
  247. protected static function buildArrayPath($map, $value, &$array)
  248. {
  249. $key = array_shift($map);
  250. if (count($map) !== 0) {
  251. $array[$key] = array();
  252. self::buildArrayPath($map, $value, $array[$key]);
  253. } else {
  254. $array[$key] = $value;
  255. }
  256. }
  257. }