WechatKeywordsController.php 6.2 KB

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