WechatSubscribeController.php 6.3 KB

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