Config.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace app\admin\controller\shopro\notification;
  3. use app\admin\controller\shopro\Common;
  4. use app\admin\model\shopro\notification\Config as ConfigModel;
  5. use app\admin\controller\shopro\notification\traits\Notification as NotificationTraits;
  6. use addons\shopro\facade\Wechat;
  7. class Config extends Common
  8. {
  9. use NotificationTraits;
  10. public function _initialize()
  11. {
  12. parent::_initialize();
  13. $this->model = new ConfigModel;
  14. }
  15. /**
  16. * 消息通知配置
  17. */
  18. public function index()
  19. {
  20. if (!$this->request->isAjax()) {
  21. return $this->view->fetch();
  22. }
  23. $receiver_type = $this->request->param('receiver_type');
  24. $notifications = $this->getNotificationsByReceiverType($receiver_type);
  25. $groupConfigs = $this->getGroupConfigs();
  26. foreach ($notifications as $key => &$notification) {
  27. $currentConfigs = $groupConfigs[$notification['event']] ?? [];
  28. foreach ($notification['channels'] as $channel) {
  29. $notification['configs'][$channel] = [
  30. 'status' => isset($currentConfigs[$channel]) ? $currentConfigs[$channel]['status'] : 'disabled',
  31. 'send_num' => isset($currentConfigs[$channel]) ? $currentConfigs[$channel]['send_num'] : 0,
  32. ];
  33. }
  34. }
  35. $this->success('获取成功', null, $notifications);
  36. }
  37. public function detail()
  38. {
  39. $event = $this->request->param('event');
  40. $channel = $this->request->param('channel');
  41. if (!$event || !$channel) {
  42. error_stop('参数错误');
  43. }
  44. $notification = $this->getNotificationByEvent($event);
  45. $notification = $this->formatNotification($notification, $event, $channel);
  46. $this->success('获取成功', null, $notification);
  47. }
  48. // 编辑配置
  49. public function edit($id = null)
  50. {
  51. if (!$this->request->isAjax()) {
  52. return $this->view->fetch();
  53. }
  54. $event = $this->request->param('event');
  55. $channel = $this->request->param('channel');
  56. if ($channel == 'Email') {
  57. $content = $this->request->param('content', '');
  58. } else {
  59. $content = $this->request->param('content/a', []);
  60. }
  61. $type = $this->request->param('type', 'default');
  62. if (!$event || !$channel) {
  63. error_stop('参数错误');
  64. }
  65. $config = $this->model->where('event', $event)->where('channel', $channel)->find();
  66. if (!$config) {
  67. $config = $this->model;
  68. $config->event = $event;
  69. $config->channel = $channel;
  70. }
  71. if (in_array($channel, ['WechatOfficialAccount', 'WechatMiniProgram']) && $type == 'default') {
  72. // 自动组装微信默认模板
  73. $content['fields'] = $this->formatWechatTemplateFields($event, $channel, $content['fields']);
  74. }
  75. $config->type = $type;
  76. $config->content = $content;
  77. $config->save();
  78. $this->success('设置成功');
  79. }
  80. // 配置状态
  81. public function setStatus($event, $channel)
  82. {
  83. $event = $this->request->param('event');
  84. $channel = $this->request->param('channel');
  85. $status = $this->request->param('status', 'disabled');
  86. if (!$event || !$channel) {
  87. $this->error('参数错误');
  88. }
  89. $config = $this->model->where('event', $event)->where('channel', $channel)->find();
  90. if (!$config) {
  91. $config = $this->model;
  92. $config->event = $event;
  93. $config->channel = $channel;
  94. $config->type = 'default';
  95. }
  96. $config->status = $status;
  97. $config->save();
  98. $this->success('设置成功');
  99. }
  100. /**
  101. * 自动获取微信模板 id
  102. */
  103. public function getTemplateId()
  104. {
  105. $event = $this->request->param('event');
  106. $channel = $this->request->param('channel');
  107. $is_delete = $this->request->param('is_delete', 0);
  108. $template_id = $this->request->param('template_id', '');
  109. if (!$event || !$channel) {
  110. error_stop('参数错误');
  111. }
  112. $notification = $this->getNotificationByEvent($event);
  113. $template = $notification['template'][$channel] ?? null;
  114. if (!$template) {
  115. $this->error('模板不存在');
  116. }
  117. // 请求微信接口
  118. switch ($channel) {
  119. case 'WechatMiniProgram': // 小程序订阅消息
  120. $requestParams['tid'] = $template['tid'];
  121. $requestParams['kid'] = $template['kid'];
  122. $requestParams['sceneDesc'] = $template['scene_desc'];
  123. if (!$requestParams['tid'] || !$requestParams['kid']) {
  124. $this->error('缺少模板参数');
  125. }
  126. $wechat = Wechat::miniProgram()->subscribe_message;
  127. $delete_method = 'deleteTemplate';
  128. $result_key = 'priTmplId';
  129. break;
  130. // case 'WechatOfficialAccount': // 公众号模板消息
  131. // $requestParams['template_id'] = $template['temp_no'];
  132. // if (!$requestParams['template_id']) {
  133. // $this->error('缺少模板参数,获取失败');
  134. // }
  135. // $wechat = Wechat::officialAccount()->template_message; // 微信管理
  136. // $result_key = 'template_id';
  137. // $delete_method = 'deletePrivateTemplate';
  138. // break;
  139. case 'WechatOfficialAccount': // 新版公众号模板消息
  140. $requestParams['template_id'] = $template['temp_no'];
  141. $requestParams['keywords'] = $template['keywords'];
  142. if (!$requestParams['template_id']) {
  143. $this->error('公众号类目模板库目前不完善,请自行在公众号后台->模板消息->选择模板配置');
  144. }
  145. if (!$requestParams['keywords']) {
  146. $this->error('缺少模板关键字,获取失败');
  147. }
  148. $wechat = new \addons\shopro\library\easywechatPlus\WechatOfficialTemplate(Wechat::officialAccount());
  149. $result_key = 'template_id';
  150. $delete_method = 'deletePrivateTemplate';
  151. break;
  152. case 'WechatOfficialAccountBizsend': // 公众号订阅消息(待补充)
  153. $requestParams['tid'] = $template['tid'];
  154. $requestParams['kid'] = $template['kid'];
  155. if (!$requestParams['tid'] || !$requestParams['kid']) {
  156. $this->error('缺少模板参数,获取失败');
  157. }
  158. $wechat = Wechat::officialAccount()->subscribe_message; // 微信管理
  159. $result_key = 'priTmplId';
  160. $delete_method = 'deleteTemplate';
  161. break;
  162. default:
  163. $this->error('当前发送渠道不能获取模板');
  164. break;
  165. }
  166. $result = $wechat->addTemplate(...array_values($requestParams));
  167. if ($result['errcode'] != 0) {
  168. $this->error('获取失败: errcode:' . $result['errcode'] . '; errmsg:' . $result['errmsg']);
  169. } else {
  170. if ($is_delete) {
  171. // 删除传入的老模板
  172. if ($template_id) {
  173. $deleteResult = $wechat->{$delete_method}($template_id);
  174. }
  175. // 删除数据库的老模板
  176. $config = $this->model->where('event', $event)->where('channel', $channel)->find();
  177. $template_id = $config ? ($config->content['template_id'] ?? null) : null;
  178. if ($template_id) {
  179. $deleteResult = $wechat->{$delete_method}($template_id);
  180. }
  181. }
  182. }
  183. $this->success('获取成功', null, ($result[$result_key] ?? null));
  184. }
  185. }