WechatSubscribeController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * 微信公众号关注回复设置
  4. * @file:WechatSubscribeController.php
  5. * @Date: 2023/7/5
  6. * @Time: 15:02
  7. */
  8. namespace Modules\WechatPlatform\Http\Controllers;
  9. use Catch\Base\CatchController;
  10. use Catch\Exceptions\FailedException;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\DB;
  13. use Modules\Manage\Enmus\MiniprogramType;
  14. use Modules\User\Http\Controllers\UserTrait;
  15. use Modules\WechatPlatform\Http\Requests\WechatSubscribeRequest;
  16. use Modules\WechatPlatform\Services\WechatKeywordsService;
  17. use Modules\WechatPlatform\Services\WechatSubscribeService;
  18. class WechatSubscribeController extends CatchController
  19. {
  20. use UserTrait;
  21. /**
  22. * 关注回复列表
  23. * name: list
  24. * @param Request $request
  25. * date 2023/07/10 09:29
  26. */
  27. public function list(Request $request)
  28. {
  29. }
  30. /**
  31. * 添加关注回复
  32. * name: add
  33. * @param WechatSubscribeRequest $request
  34. * @return string|void
  35. * date 2023/07/10 10:07
  36. */
  37. public function add(WechatSubscribeRequest $request)
  38. {
  39. $param = $request->validated();
  40. $param = $this->handelParam($param);
  41. $userContext = $this->getUserContext(null);
  42. $param['user_id'] = $userContext['loginUser']->id;
  43. $param['puser_id'] = $userContext['loginUser']->pid;
  44. $param['wechat_accounts'] = [];
  45. return WechatSubscribeService::addSubscribe($param);
  46. }
  47. /**
  48. * 编辑
  49. * name: edit
  50. * @param $id
  51. * @param WechatSubscribeRequest $request
  52. * date 2023/07/10 10:51
  53. */
  54. public function edit($id, WechatSubscribeRequest $request)
  55. {
  56. $param = $this->handelParam($request->validated());
  57. return WechatSubscribeService::updateSubscribe($id, $param);
  58. }
  59. /**
  60. * 详情
  61. * name: detail
  62. * @param $id
  63. * date 2023/07/10 10:07
  64. */
  65. public function detail($id)
  66. {
  67. return WechatSubscribeService::getDetail($id);
  68. }
  69. /**
  70. * 分配公众号
  71. * name: allocation
  72. * @param $id
  73. * @param Request $request
  74. * @return string
  75. * date 2023/07/10 10:22
  76. */
  77. public function allocation($id, Request $request)
  78. {
  79. if (!$request->has('wx_auth_ids')) {
  80. throw new FailedException("参数错误");
  81. }
  82. if (!empty($request->input('wx_auth_ids'))) {
  83. $wxAuthIds = explode(',', $request->input('wx_auth_ids'));
  84. } else {
  85. $wxAuthIds = [];
  86. }
  87. return WechatSubscribeService::allocation($id, $wxAuthIds);
  88. }
  89. /**
  90. * 公众号分配列表
  91. * name: authList
  92. * @param $id
  93. * date 2023/07/10 11:08
  94. */
  95. public function authList($id)
  96. {
  97. $userId = $this->getLoginUserId();
  98. return WechatSubscribeService::WechaAccountAuthListInfo($id,$userId);
  99. }
  100. /**
  101. * 删除
  102. * name: del
  103. * @param Request $request
  104. * @return string
  105. * date 2023/07/10 11:08
  106. */
  107. public function del(Request $request)
  108. {
  109. $ids = $request->input('ids');
  110. if (empty($ids)) {
  111. throw new FailedException('要删除的数据参数错误');
  112. }
  113. $ids = explode(',', $ids);
  114. return WechatSubscribeService::del($ids);
  115. }
  116. /**
  117. * 获取设置
  118. * name: getConfig
  119. * @param $miniprogramId
  120. * @return int
  121. * date 2023/07/10 09:33
  122. */
  123. public function getConfig($miniprogramId)
  124. {
  125. $userContext = $this->getUserContext(null);
  126. $userId = $userContext['loginUser']->id;
  127. $puserId = $userContext['loginUser']->pid;
  128. return WechatSubscribeService:: getConfig($userId, $puserId, $miniprogramId);
  129. }
  130. /**
  131. * 更新设置
  132. * name: setConfig
  133. * @param $miniprogramId
  134. * @param Request $request
  135. * @return string
  136. * date 2023/07/10 09:33
  137. */
  138. public function setConfig($miniprogramId, Request $request)
  139. {
  140. if (!$request->has('value')) {
  141. throw new FailedException("参数错误");
  142. }
  143. $param = $request->all(['value']);
  144. if (!in_array($param['value'], [0, 1])) {
  145. throw new FailedException("参数错误");
  146. }
  147. $userContext = $this->getUserContext(null);
  148. $param['miniprogram_id'] = $miniprogramId;
  149. $param['user_id'] = $userContext['loginUser']->id;
  150. $param['puser_id'] = $userContext['loginUser']->pid;
  151. return WechatSubscribeService::setConfig($param);
  152. }
  153. private function handelParam(mixed $param)
  154. {
  155. if (getProp($param, 'type') != 'miniprogram') {
  156. return $param;
  157. }
  158. $info = DB::table('miniprogram')->where('id', $param['miniprogram_id'])->first();
  159. if (empty($info)) {
  160. throw new FailedException("小程序不正确");
  161. }
  162. if ($info->status != 1) {
  163. throw new FailedException("此小程序暂不提供使用");
  164. }
  165. if ($info->type != MiniprogramType::WEIXIN->value()) {
  166. throw new FailedException("关键词回复设置仅支持微信小程序");
  167. }
  168. $param['miniprogram_appid'] = $info->appid;
  169. $info = DB::table('user_has_miniprograms')->where('uid', $this->getLoginUserId())->where('miniprogram_id', $param['miniprogram_id'])->where('is_enabled', 1)->value('id');
  170. if (empty($info)) {
  171. throw new FailedException("没有此小程序的使用权限");
  172. }
  173. $param['send_content'] = "";
  174. foreach ($param['content'] as & $val) {
  175. if (!is_array($val)) {
  176. throw new FailedException("回复内容格式不正确");
  177. }
  178. if (getProp($val, 'url')) {
  179. // 跳转小程序
  180. $val['content'] = "<a href=\"\" data-miniprogram-appid=\"{$param['miniprogram_appid']}\" data-miniprogram-path=\"{$val['url']}\">{$val['title']}</a>";
  181. } else {
  182. $val['content'] = $val['title'];
  183. $val['url'] = "";
  184. }
  185. $param['send_content'] .= $val['content'] . "\n";
  186. }
  187. rtrim($param['send_content'], "\n");
  188. return $param;
  189. }
  190. }