WechatKeywordsController.php 6.1 KB

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