WechatSubscribeController.php 6.4 KB

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