PlatformService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <?php
  2. namespace app\common\Service;
  3. use app\common\model\Config;
  4. use think\Cache;
  5. use think\Db;
  6. /**
  7. * 平台配置服务类
  8. */
  9. class PlatformService
  10. {
  11. /**
  12. * 根据配置名获取配置值
  13. * @param string $name 配置名
  14. * @return mixed
  15. */
  16. public static function getConfigValue($name)
  17. {
  18. $cacheKey = 'config_' . $name;
  19. $value = Cache::get($cacheKey);
  20. if ($value === false) {
  21. $config = Config::where('name', $name)->find();
  22. if ($config) {
  23. $value = $config['value'];
  24. // 处理数组类型
  25. if ($config['type'] === 'array') {
  26. $value = json_decode($value, true) ?: [];
  27. }
  28. // 处理布尔类型
  29. if ($config['type'] === 'boolean') {
  30. $value = (bool)$value;
  31. }
  32. // 缓存配置值
  33. Cache::set($cacheKey, $value, 3600);
  34. } else {
  35. $value = null;
  36. }
  37. }
  38. return $value;
  39. }
  40. /**
  41. * 根据分组获取配置
  42. * @param string $group 分组名
  43. * @return array
  44. */
  45. public static function getConfigByGroup($group)
  46. {
  47. $cacheKey = 'config_group_' . $group;
  48. $configs = Cache::get($cacheKey);
  49. if ($configs === false) {
  50. $list = Config::where('group', $group)
  51. ->where('type', '<>', 'group')
  52. ->select();
  53. $configs = [];
  54. foreach ($list as $config) {
  55. $name = str_replace($group . '.', '', $config['name']);
  56. $value = $config['value'];
  57. // 处理数组类型
  58. if ($config['type'] === 'array') {
  59. $value = json_decode($value, true) ?: [];
  60. }
  61. // 处理布尔类型
  62. if ($config['type'] === 'boolean') {
  63. $value = (bool)$value;
  64. }
  65. $configs[$name] = $value;
  66. }
  67. // 缓存配置
  68. Cache::set($cacheKey, $configs, 3600);
  69. }
  70. return $configs;
  71. }
  72. /**
  73. * 设置配置值
  74. * @param string $name 配置名
  75. * @param mixed $value 配置值
  76. * @return bool
  77. */
  78. public static function setConfigValue($name, $value)
  79. {
  80. $config = Config::where('name', $name)->find();
  81. if (!$config) {
  82. return false;
  83. }
  84. // 处理数组类型
  85. if ($config['type'] === 'array') {
  86. $value = is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value;
  87. }
  88. // 更新配置
  89. $config->value = $value;
  90. $result = $config->save();
  91. if ($result) {
  92. // 清除相关缓存
  93. Cache::rm('config_' . $name);
  94. Cache::rm('config_group_' . $config['group']);
  95. }
  96. return $result;
  97. }
  98. /**
  99. * 批量设置配置值
  100. * @param array $configs 配置数组
  101. * @param string $group 分组名
  102. * @return bool
  103. */
  104. public static function setConfigsByGroup($configs, $group)
  105. {
  106. $result = true;
  107. foreach ($configs as $name => $value) {
  108. $configName = $group . '.' . $name;
  109. if (!self::setConfigValue($configName, $value)) {
  110. $result = false;
  111. }
  112. }
  113. return $result;
  114. }
  115. /**
  116. * 获取所有平台配置
  117. * @return array
  118. */
  119. public static function getAllPlatformConfigs()
  120. {
  121. $cacheKey = 'all_platform_configs';
  122. $configs = Cache::get($cacheKey);
  123. if ($configs === false) {
  124. $platforms = Config::where('name', 'like', 'shop.platform.%')
  125. ->where('type', 'group')
  126. ->where('group', 'shop.platform')
  127. ->select();
  128. $configs = [];
  129. foreach ($platforms as $platform) {
  130. $platformName = str_replace('shop.platform.', '', $platform['name']);
  131. $configs[$platformName] = [
  132. 'name' => $platformName,
  133. 'title' => $platform['title'],
  134. 'tip' => $platform['tip'],
  135. 'status' => self::getConfigValue($platform['name'] . '.status'),
  136. 'config' => self::getConfigByGroup($platform['name'])
  137. ];
  138. }
  139. // 缓存配置
  140. Cache::set($cacheKey, $configs, 3600);
  141. }
  142. return $configs;
  143. }
  144. /**
  145. * 检查配置是否存在
  146. * @param string $name 配置名
  147. * @return bool
  148. */
  149. public static function hasConfig($name)
  150. {
  151. return Config::where('name', $name)->count() > 0;
  152. }
  153. /**
  154. * 创建配置项
  155. * @param array $data 配置数据
  156. * @return bool
  157. */
  158. public static function createConfig($data)
  159. {
  160. // 检查是否已存在
  161. if (self::hasConfig($data['name'])) {
  162. return false;
  163. }
  164. $config = new Config();
  165. $config->data($data);
  166. $result = $config->save();
  167. if ($result) {
  168. // 清除缓存
  169. Cache::clear();
  170. }
  171. return $result;
  172. }
  173. /**
  174. * 删除配置项
  175. * @param string $name 配置名
  176. * @return bool
  177. */
  178. public static function deleteConfig($name)
  179. {
  180. $config = Config::where('name', $name)->find();
  181. if (!$config) {
  182. return false;
  183. }
  184. $result = $config->delete();
  185. if ($result) {
  186. // 清除缓存
  187. Cache::clear();
  188. }
  189. return $result;
  190. }
  191. /**
  192. * 清除所有配置缓存
  193. */
  194. public static function clearConfigCache()
  195. {
  196. Cache::clear();
  197. }
  198. /**
  199. * 获取配置项的显示值
  200. * @param array $config 配置项数组
  201. * @return string
  202. */
  203. public static function getDisplayValue($config)
  204. {
  205. $value = $config['value'];
  206. switch ($config['type']) {
  207. case 'boolean':
  208. return $value ? '是' : '否';
  209. case 'array':
  210. $arrayValue = json_decode($value, true);
  211. return is_array($arrayValue) ? implode(', ', $arrayValue) : $value;
  212. case 'password':
  213. return str_repeat('*', strlen($value));
  214. default:
  215. return $value;
  216. }
  217. }
  218. /**
  219. * 验证配置值
  220. * @param string $type 配置类型
  221. * @param mixed $value 配置值
  222. * @return bool
  223. */
  224. public static function validateValue($type, $value)
  225. {
  226. switch ($type) {
  227. case 'int':
  228. return is_numeric($value);
  229. case 'boolean':
  230. return in_array($value, ['0', '1', 0, 1, true, false], true);
  231. case 'array':
  232. if (is_array($value)) {
  233. return true;
  234. }
  235. $decoded = json_decode($value, true);
  236. return json_last_error() === JSON_ERROR_NONE;
  237. case 'email':
  238. return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
  239. case 'url':
  240. return filter_var($value, FILTER_VALIDATE_URL) !== false;
  241. default:
  242. return true;
  243. }
  244. }
  245. /**
  246. * 获取平台状态
  247. * @param string $platformName 平台名称
  248. * @return bool
  249. */
  250. public static function getPlatformStatus($platformName)
  251. {
  252. $statusConfig = Config::where('name', $platformName . '.status')->find();
  253. return $statusConfig ? (bool)$statusConfig['value'] : false;
  254. }
  255. /**
  256. * 获取平台配置项数量
  257. * @param string $platformName 平台名称
  258. * @return int
  259. */
  260. public static function getConfigCount($platformName)
  261. {
  262. return Config::where('group', $platformName)->count();
  263. }
  264. /**
  265. * 获取平台配置数据
  266. * @param string $platform 平台类型
  267. * @return array
  268. */
  269. public static function getPlatformConfigData($platform)
  270. {
  271. $configs = [];
  272. switch ($platform) {
  273. case 'WechatMiniProgram':
  274. $configs = [
  275. [
  276. 'name' => 'shop.platform.WechatMiniProgram',
  277. 'group' => 'shop.platform',
  278. 'title' => '微信小程序',
  279. 'tip' => '微信小程序平台配置',
  280. 'type' => 'group',
  281. 'value' => '',
  282. 'rule' => '',
  283. 'extend' => ''
  284. ],
  285. [
  286. 'name' => 'shop.platform.WechatMiniProgram.app_id',
  287. 'group' => 'shop.platform.WechatMiniProgram',
  288. 'title' => '小程序AppId',
  289. 'tip' => 'AppID是小程序开发标识码,配合AppSecret可调用小程序的接口能力',
  290. 'type' => 'string',
  291. 'value' => '',
  292. 'rule' => 'required',
  293. 'extend' => ''
  294. ],
  295. [
  296. 'name' => 'shop.platform.WechatMiniProgram.secret',
  297. 'group' => 'shop.platform.WechatMiniProgram',
  298. 'title' => '小程序密钥',
  299. 'tip' => 'AppSecret是校验小程序开发者身份的密钥,具有极高的安全性',
  300. 'type' => 'string',
  301. 'value' => '',
  302. 'rule' => 'required',
  303. 'extend' => ''
  304. ],
  305. [
  306. 'name' => 'shop.platform.WechatMiniProgram.status',
  307. 'group' => 'shop.platform.WechatMiniProgram',
  308. 'title' => '小程序开启状态',
  309. 'tip' => '是否开启微信小程序功能',
  310. 'type' => 'boolean',
  311. 'value' => '0',
  312. 'rule' => '',
  313. 'extend' => ''
  314. ],
  315. [
  316. 'name' => 'shop.platform.WechatMiniProgram.auto_login',
  317. 'group' => 'shop.platform.WechatMiniProgram',
  318. 'title' => '微信自动登录',
  319. 'tip' => '进入应用后,用户将会自动授权登录,未注册用户将会自动注册',
  320. 'type' => 'boolean',
  321. 'value' => '1',
  322. 'rule' => '',
  323. 'extend' => ''
  324. ],
  325. [
  326. 'name' => 'shop.platform.WechatMiniProgram.bind_mobile',
  327. 'group' => 'shop.platform.WechatMiniProgram',
  328. 'title' => '绑定手机号',
  329. 'tip' => '授权登录后,未绑定手机号的用户,将会立即提醒绑定手机号',
  330. 'type' => 'boolean',
  331. 'value' => '1',
  332. 'rule' => '',
  333. 'extend' => ''
  334. ]
  335. ];
  336. break;
  337. case 'DouyinMiniProgram':
  338. $configs = [
  339. [
  340. 'name' => 'shop.platform.DouyinMiniProgram',
  341. 'group' => 'shop.platform',
  342. 'title' => '抖音小程序',
  343. 'tip' => '抖音小程序平台配置',
  344. 'type' => 'group',
  345. 'value' => '',
  346. 'rule' => '',
  347. 'extend' => ''
  348. ],
  349. [
  350. 'name' => 'shop.platform.DouyinMiniProgram.app_id',
  351. 'group' => 'shop.platform.DouyinMiniProgram',
  352. 'title' => '抖音小程序AppId',
  353. 'tip' => '抖音小程序应用ID',
  354. 'type' => 'string',
  355. 'value' => '',
  356. 'rule' => 'required',
  357. 'extend' => ''
  358. ],
  359. [
  360. 'name' => 'shop.platform.DouyinMiniProgram.secret',
  361. 'group' => 'shop.platform.DouyinMiniProgram',
  362. 'title' => '抖音小程序密钥',
  363. 'tip' => '抖音小程序应用密钥',
  364. 'type' => 'string',
  365. 'value' => '',
  366. 'rule' => 'required',
  367. 'extend' => ''
  368. ],
  369. [
  370. 'name' => 'shop.platform.DouyinMiniProgram.status',
  371. 'group' => 'shop.platform.DouyinMiniProgram',
  372. 'title' => '抖音小程序开启状态',
  373. 'tip' => '是否开启抖音小程序功能',
  374. 'type' => 'boolean',
  375. 'value' => '0',
  376. 'rule' => '',
  377. 'extend' => ''
  378. ],
  379. [
  380. 'name' => 'shop.platform.DouyinMiniProgram.auto_login',
  381. 'group' => 'shop.platform.DouyinMiniProgram',
  382. 'title' => '自动登录',
  383. 'tip' => '进入应用后,用户将会自动授权登录,未注册用户将会自动注册',
  384. 'type' => 'boolean',
  385. 'value' => '1',
  386. 'rule' => '',
  387. 'extend' => ''
  388. ],
  389. [
  390. 'name' => 'shop.platform.DouyinMiniProgram.bind_mobile',
  391. 'group' => 'shop.platform.DouyinMiniProgram',
  392. 'title' => '绑定手机号',
  393. 'tip' => '授权登录后,未绑定手机号的用户,将会立即提醒绑定手机号',
  394. 'type' => 'boolean',
  395. 'value' => '1',
  396. 'rule' => '',
  397. 'extend' => ''
  398. ]
  399. ];
  400. break;
  401. }
  402. return $configs;
  403. }
  404. /**
  405. * 初始化平台配置数据
  406. * @param string $platform 平台类型
  407. * @return bool
  408. * @throws \Exception
  409. */
  410. public static function initPlatformConfig($platform)
  411. {
  412. $configs = self::getPlatformConfigData($platform);
  413. Db::startTrans();
  414. try {
  415. foreach ($configs as $config) {
  416. $exists = Config::where('name', $config['name'])->find();
  417. if (!$exists) {
  418. Config::create($config);
  419. }
  420. }
  421. Db::commit();
  422. return true;
  423. } catch (\Exception $e) {
  424. Db::rollback();
  425. throw $e;
  426. }
  427. }
  428. /**
  429. * 根据渠道获取平台配置
  430. * @param string $channel 渠道标识
  431. * @return array
  432. */
  433. public static function getConfigByChannel($channel)
  434. {
  435. $platformMap = [
  436. 'wechat_mini_program' => 'shop.platform.WechatMiniProgram',
  437. 'douyin_mini_program' => 'shop.platform.DouyinMiniProgram',
  438. ];
  439. if (!isset($platformMap[$channel])) {
  440. return [];
  441. }
  442. return self::getConfigByGroup($platformMap[$channel]);
  443. }
  444. /**
  445. * 检查平台是否启用
  446. * @param string $channel 渠道标识
  447. * @return bool
  448. */
  449. public static function isPlatformEnabled($channel)
  450. {
  451. $config = self::getConfigByChannel($channel);
  452. return !empty($config['status']);
  453. }
  454. }