Platform.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\Service\PlatformService;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 平台配置管理
  10. */
  11. class Platform extends Backend
  12. {
  13. /**
  14. * 支持的平台配置
  15. * @var array
  16. */
  17. protected $platforms = [
  18. 'WechatMiniProgram' => '微信小程序',
  19. 'DouyinMiniProgram' => '抖音小程序',
  20. ];
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. }
  25. /**
  26. * 查看平台配置
  27. */
  28. public function index()
  29. {
  30. //设置过滤方法
  31. $this->request->filter(['strip_tags', 'trim']);
  32. if ($this->request->isAjax()) {
  33. $list = [];
  34. // 获取所有平台配置
  35. $configs = PlatformService::getAllPlatformConfigs();
  36. foreach ($configs as $platform => $config) {
  37. $list[] = [
  38. 'platform' => $platform,
  39. 'name' => $config['title'],
  40. 'status' => $config['status'] ? '启用' : '禁用',
  41. 'count' => count($config['config']),
  42. 'created_at' => date('Y-m-d H:i:s'),
  43. 'updated_at' => date('Y-m-d H:i:s')
  44. ];
  45. }
  46. $result = array(
  47. "total" => count($list),
  48. "rows" => $list
  49. );
  50. return json($result);
  51. }
  52. return $this->view->fetch();
  53. }
  54. /**
  55. * 微信小程序配置
  56. */
  57. public function wechat_mini_program()
  58. {
  59. $group = 'shop.platform.WechatMiniProgram';
  60. return $this->platformConfig($group, '微信小程序配置');
  61. }
  62. /**
  63. * 抖音小程序配置
  64. */
  65. public function douyin_mini_program()
  66. {
  67. $group = 'shop.platform.DouyinMiniProgram';
  68. return $this->platformConfig($group, '抖音小程序配置');
  69. }
  70. /**
  71. * 平台配置通用方法
  72. */
  73. private function platformConfig($group, $title)
  74. {
  75. if ($this->request->isPost()) {
  76. $params = $this->request->post();
  77. // 验证参数
  78. if (!isset($params['config']) || !is_array($params['config'])) {
  79. $this->error('参数错误');
  80. }
  81. try {
  82. $result = PlatformService::setConfigsByGroup($params['config'], $group);
  83. if ($result) {
  84. $this->success('配置保存成功');
  85. } else {
  86. $this->error('配置保存失败');
  87. }
  88. } catch (Exception $e) {
  89. $this->error($e->getMessage());
  90. }
  91. }
  92. // 获取当前平台配置
  93. $configs = PlatformService::getConfigByGroup($group);
  94. $this->view->assign('configs', $configs);
  95. $this->view->assign('group', $group);
  96. $this->view->assign('title', $title);
  97. return $this->view->fetch('config');
  98. }
  99. /**
  100. * 初始化平台配置
  101. */
  102. public function init_config()
  103. {
  104. if ($this->request->isPost()) {
  105. $platform = $this->request->post('platform');
  106. if (!$platform) {
  107. $this->error('请选择平台');
  108. }
  109. try {
  110. $result = PlatformService::initPlatformConfig($platform);
  111. if ($result) {
  112. $this->success('平台配置初始化成功');
  113. } else {
  114. $this->error('平台配置初始化失败');
  115. }
  116. } catch (Exception $e) {
  117. $this->error($e->getMessage());
  118. }
  119. }
  120. return $this->view->fetch();
  121. }
  122. /**
  123. * 清除配置缓存
  124. */
  125. public function clear()
  126. {
  127. PlatformService::clearConfigCache();
  128. $this->success('缓存清除成功');
  129. }
  130. /**
  131. * 获取配置信息
  132. */
  133. public function get_config()
  134. {
  135. $name = $this->request->param('name');
  136. if (!$name) {
  137. $this->error('参数错误');
  138. }
  139. $config = PlatformService::getConfigValue($name);
  140. return json([
  141. 'code' => 1,
  142. 'msg' => '获取成功',
  143. 'data' => $config
  144. ]);
  145. }
  146. /**
  147. * 设置配置信息
  148. */
  149. public function set_config()
  150. {
  151. if ($this->request->isPost()) {
  152. $name = $this->request->post('name');
  153. $value = $this->request->post('value');
  154. if (!$name) {
  155. $this->error('参数错误');
  156. }
  157. try {
  158. $result = PlatformService::setConfigValue($name, $value);
  159. if ($result) {
  160. $this->success('配置设置成功');
  161. } else {
  162. $this->error('配置设置失败');
  163. }
  164. } catch (Exception $e) {
  165. $this->error($e->getMessage());
  166. }
  167. }
  168. }
  169. }