Platform.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace app\admin\controller\shop;
  3. use app\common\Service\ShopConfigService;
  4. use app\common\controller\Backend;
  5. use app\common\Enum\ChannelEnum;
  6. use think\Db;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. use app\common\Enum\StatusEnum;
  10. /**
  11. * 平台配置管理
  12. */
  13. class Platform extends Backend
  14. {
  15. /**
  16. * 获取平台配置映射(枚举常量 -> 显示配置)
  17. */
  18. private function getPlatformDisplayMap()
  19. {
  20. return [
  21. ChannelEnum::CHANNEL_WECHAT_MINI_PROGRAM => [
  22. 'key' => 'wechat_mini_program',
  23. 'name' => '微信小程序',
  24. 'desc' => '一键生成,连接微信庞大用户群及微信生态',
  25. 'icon' => '/assets/img/platfom/icon_we_chat_applet.png',
  26. 'class' => 'wechat-mini',
  27. 'btn_class' => 'btn-success'
  28. ],
  29. ChannelEnum::CHANNEL_DOUYIN_MINI_PROGRAM => [
  30. 'key' => 'douyin_mini_program',
  31. 'name' => '抖音小程序',
  32. 'desc' => '抖音小程序,抖音生态',
  33. 'icon' => '/assets/img/platfom/icon-douyin.png',
  34. 'class' => 'toutiao-mini',
  35. 'btn_class' => 'btn-warning'
  36. ],
  37. ChannelEnum::CHANNEL_IOS_APP => [
  38. 'key' => 'ios_app',
  39. 'name' => 'IOS APP',
  40. 'desc' => '支持IOS程序下载,助力品牌传播',
  41. 'icon' => '/assets/img/platfom/icon_uniapp.png',
  42. 'class' => 'app-platform',
  43. 'btn_class' => 'btn-info'
  44. ],
  45. ChannelEnum::CHANNEL_ANDROID_APP => [
  46. 'key' => 'android_app',
  47. 'name' => '安卓APP',
  48. 'desc' => '支持安卓程序下载,助力品牌传播',
  49. 'icon' => '/assets/img/platfom/icon_uniapp.png',
  50. 'class' => 'app-platform',
  51. 'btn_class' => 'btn-info'
  52. ],
  53. // ChannelEnum::CHANNEL_WECHAT_OFFICIAL_ACCOUNT => [
  54. // 'key' => 'wechat_official_account',
  55. // 'name' => '微信公众号',
  56. // 'desc' => '借助海量流量,玩转微信生态',
  57. // 'icon' => '/assets/img/platfom/icon_we_chat.png',
  58. // 'class' => 'wechat-official',
  59. // 'btn_class' => 'btn-success'
  60. // ],
  61. // ChannelEnum::CHANNEL_H5 => [
  62. // 'key' => 'h5',
  63. // 'name' => '手机浏览器H5',
  64. // 'desc' => 'WAP移动端页面,广泛覆盖移动市场',
  65. // 'icon' => '/assets/img/platfom/icon_browser.png',
  66. // 'class' => 'mobile-h5',
  67. // 'btn_class' => 'btn-primary'
  68. // ],
  69. // ChannelEnum::CHANNEL_PC => [
  70. // 'key' => 'pc',
  71. // 'name' => 'PC商城',
  72. // 'desc' => '开启你的个人PC端的独立商城',
  73. // 'icon' => '/assets/img/platfom/icon_pc.png',
  74. // 'class' => 'pc-platform',
  75. // 'btn_class' => 'btn-info'
  76. // ],
  77. // ChannelEnum::CHANNEL_QQ_MINI_PROGRAM => [
  78. // 'key' => 'qq_mini_program',
  79. // 'name' => 'QQ小程序',
  80. // 'desc' => 'QQ小程序,连接QQ用户生态',
  81. // 'icon' => '/assets/img/platfom/icon_qq.png',
  82. // 'class' => 'qq-mini',
  83. // 'btn_class' => 'btn-primary'
  84. // ],
  85. // ChannelEnum::CHANNEL_ALIPAY_MINI_PROGRAM => [
  86. // 'key' => 'alipay_mini_program',
  87. // 'name' => '支付宝小程序',
  88. // 'desc' => '支付宝小程序,支付宝生态',
  89. // 'icon' => '/assets/img/platfom/icon_zhifubao.png',
  90. // 'class' => 'alipay-mini',
  91. // 'btn_class' => 'btn-success'
  92. // ]
  93. ];
  94. }
  95. /**
  96. * 获取所有支持的平台
  97. */
  98. private function getSupportedPlatforms()
  99. {
  100. return array_keys($this->getPlatformDisplayMap());
  101. }
  102. /**
  103. * 根据枚举常量获取平台Key
  104. */
  105. private function getPlatformKey($channelConstant)
  106. {
  107. $map = $this->getPlatformDisplayMap();
  108. return $map[$channelConstant]['key'] ?? null;
  109. }
  110. /**
  111. * 根据平台Key获取枚举常量
  112. */
  113. private function getChannelConstant($platformKey)
  114. {
  115. $map = $this->getPlatformDisplayMap();
  116. foreach ($map as $constant => $config) {
  117. if ($config['key'] === $platformKey) {
  118. return $constant;
  119. }
  120. }
  121. return null;
  122. }
  123. protected $model = null;
  124. protected $noNeedRight = ['select', 'selectpage_type', 'check_element_available'];
  125. public function _initialize()
  126. {
  127. parent::_initialize();
  128. $this->model = new \app\common\model\ShopConfig;
  129. $this->view->assign('statusList', StatusEnum::getMap());
  130. }
  131. /**
  132. * 平台状态
  133. */
  134. public function index()
  135. {
  136. if ($this->request->isAjax()) {
  137. // AJAX请求返回状态数据
  138. $status = [];
  139. $platformMap = $this->getPlatformDisplayMap();
  140. foreach ($platformMap as $channelConstant => $config) {
  141. $platformKey = $config['key'];
  142. $status[$platformKey] = ShopConfigService::getConfigField("shop.platform.{$platformKey}.status", false) ?: 0;
  143. }
  144. $this->success('操作成功', null, $status);
  145. } else {
  146. // 普通请求返回视图
  147. $status = [];
  148. $platformMap = $this->getPlatformDisplayMap();
  149. foreach ($platformMap as $channelConstant => $config) {
  150. $platformKey = $config['key'];
  151. $status[$platformKey] = ShopConfigService::getConfigField("shop.platform.{$platformKey}.status", false) ?: 0;
  152. }
  153. // 为每个平台预处理功能支持情况
  154. $platformFeatures = [];
  155. foreach ($platformMap as $channelConstant => $config) {
  156. $platformKey = $config['key'];
  157. $platformFeatures[$channelConstant] = [
  158. 'payment' => ChannelEnum::channelSupportsFeature($channelConstant, 'payment'),
  159. 'share' => ChannelEnum::channelSupportsFeature($channelConstant, 'share'),
  160. // 'push' => ChannelEnum::channelSupportsFeature($channelConstant, 'push'),
  161. ];
  162. }
  163. $this->view->assign('platformStatus', $status);
  164. $this->view->assign('platformList', $platformMap);
  165. $this->view->assign('platformFeatures', $platformFeatures);
  166. $this->view->assign('channelEnum', ChannelEnum::class);
  167. return $this->view->fetch();
  168. }
  169. }
  170. /**
  171. * 平台配置
  172. */
  173. public function edit($ids = null)
  174. {
  175. $platform = $this->request->param('platform');
  176. // 验证平台是否支持
  177. $channelConstant = $this->getChannelConstant($platform);
  178. if (!ChannelEnum::isValidChannel($channelConstant)) {
  179. $this->error('平台不支持');
  180. }
  181. if ($this->request->isAjax()) {
  182. $params = $this->request->param();
  183. // 兼容config和row两种参数名
  184. $config = isset($params['config']) ? $params['config'] : (isset($params['row']) ? $params['row'] : []);
  185. if (empty($config)) {
  186. $this->error('配置参数错误');
  187. }
  188. if (!isset($params['share']['methods'])) {
  189. $params['share']['methods'] = [];
  190. }
  191. if (!isset($params['payment']['methods'])) {
  192. $params['payment']['methods'] = [];
  193. }
  194. // echo "<pre>";
  195. // print_r($config);
  196. // echo "</pre>";
  197. // exit;
  198. // try {
  199. $configs = ShopConfigService::setConfigs('shop.platform.' . $platform, $config);
  200. $this->success('配置保存成功', null, ['platform' => $platform]);
  201. // } catch (\Exception $e) {
  202. // $this->error('配置保存失败:' . $e->getMessage());
  203. // }
  204. }
  205. $configs = ShopConfigService::getConfigs('shop.platform.' . $platform, false);
  206. $platformMap = $this->getPlatformDisplayMap();
  207. $platformConfig = null;
  208. $currentChannelConstant = null;
  209. // 获取当前平台的配置信息
  210. foreach ($platformMap as $channelConstant => $config) {
  211. if ($config['key'] === $platform) {
  212. $platformConfig = $config;
  213. $platformConfig['channel'] = $channelConstant;
  214. $currentChannelConstant = $channelConstant;
  215. break;
  216. }
  217. }
  218. // 预处理支付方式支持情况
  219. $supportedPayments = [];
  220. if ($currentChannelConstant) {
  221. $supportedPayments = [
  222. 'wechat' => ChannelEnum::channelSupportsPayment($currentChannelConstant, 'wechat'),
  223. 'alipay' => ChannelEnum::channelSupportsPayment($currentChannelConstant, 'alipay'),
  224. 'balance' => ChannelEnum::channelSupportsPayment($currentChannelConstant, 'balance'),
  225. 'offline' => ChannelEnum::channelSupportsPayment($currentChannelConstant, 'offline'),
  226. 'cod' => ChannelEnum::channelSupportsPayment($currentChannelConstant, 'cod'),
  227. ];
  228. }
  229. // 预处理功能支持情况
  230. $supportedFeatures = [];
  231. if ($currentChannelConstant) {
  232. $supportedFeatures = [
  233. 'payment' => ChannelEnum::channelSupportsFeature($currentChannelConstant, 'payment'),
  234. 'share' => ChannelEnum::channelSupportsFeature($currentChannelConstant, 'share'),
  235. 'push' => ChannelEnum::channelSupportsFeature($currentChannelConstant, 'push'),
  236. 'location' => ChannelEnum::channelSupportsFeature($currentChannelConstant, 'location'),
  237. 'camera' => ChannelEnum::channelSupportsFeature($currentChannelConstant, 'camera'),
  238. ];
  239. }
  240. // 获取当前平台支持的支付方式列表
  241. $supportedPaymentList = [];
  242. if ($currentChannelConstant) {
  243. $paymentCodes = ChannelEnum::getChannelSupportedPayments($currentChannelConstant);
  244. $paymentMap = ChannelEnum::getPaymentMap();
  245. foreach ($paymentCodes as $paymentCode) {
  246. $supportedPaymentList[$paymentCode] = $paymentMap[$paymentCode] ?? $paymentCode;
  247. }
  248. }
  249. $this->view->assign('configs', $configs);
  250. $this->view->assign('platform', $platform);
  251. $this->view->assign('platformConfig', $platformConfig);
  252. $this->view->assign('channelConstant', $currentChannelConstant);
  253. $this->view->assign('channelEnum', ChannelEnum::class);
  254. $this->view->assign('supportedPayments', $supportedPayments);
  255. $this->view->assign('supportedFeatures', $supportedFeatures);
  256. $this->view->assign('supportedPaymentList', $supportedPaymentList);
  257. $this->view->assign('group', 'shop.platform.' . $platform);
  258. return $this->view->fetch();
  259. }
  260. }