WechatMenuController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * 微信公众号自定义菜单
  4. * @file:WechatMenuController.php
  5. * @Date: 2023/7/5
  6. * @Time: 15:01
  7. */
  8. namespace Modules\WechatPlatform\Http\Controllers;
  9. use Catch\Base\CatchController;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\DB;
  12. use Modules\Channel\Services\WechatOpenPlatform\WechatOpenPlatformService;
  13. use Modules\Manage\Enmus\MiniprogramType;
  14. use Modules\User\Http\Controllers\UserTrait;
  15. use Modules\WechatPlatform\Http\Requests\WechatMenuRequest;
  16. use Modules\WechatPlatform\Services\WechatMenuService;
  17. class WechatMenuController 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 WechatMenuService::list($param);
  33. }
  34. public function add(WechatMenuRequest $request)
  35. {
  36. $param = $this->handleParam($request->validated());
  37. $userContext = $this->getUserContext(null);
  38. $param['user_id'] = $userContext['loginUser']->id;
  39. $param['puser_id'] = $userContext['loginUser']->pid;
  40. $param['wechat_accounts'] = [];
  41. return WechatMenuService::addMenu($param);
  42. }
  43. public function edit($id,WechatMenuRequest $request)
  44. {
  45. $param = $this->handleParam($request->validated());
  46. return WechatMenuService::updateMenu($id,$param);
  47. }
  48. public function detail($id){
  49. return WechatMenuService::detail($id);
  50. }
  51. public function authList($id){
  52. $userId = $this->getLoginUserId();
  53. return WechatMenuService::authList($id, $userId);
  54. }
  55. public function del(Request $request)
  56. {
  57. $ids = $request->input('ids');
  58. if (empty($ids)) {
  59. WechatMenuService::throwErrMsg('要删除的数据参数错误');
  60. }
  61. $ids = explode(',', $ids);
  62. return WechatMenuService::del($ids);
  63. }
  64. /**
  65. * 分配
  66. * name: allocation
  67. * @param $id
  68. * date 2023/07/12 07:18
  69. */
  70. public function allocation($id,Request $request)
  71. {
  72. if (!$request->has('wx_auth_ids')) {
  73. WechatMenuService::throwErrMsg("参数错误");
  74. }
  75. if (!empty($request->input('wx_auth_ids'))) {
  76. $wxAuthIds = explode(',', $request->input('wx_auth_ids'));
  77. } else {
  78. $wxAuthIds = [];
  79. }
  80. return WechatMenuService::allocation($id,$wxAuthIds);
  81. }
  82. // 处理参数
  83. private function handleParam($param)
  84. {
  85. $info = DB::table('miniprogram')->where('id', $param['miniprogram_id'])->first();
  86. if (empty($info)) {
  87. WechatMenuService::throwErrMsg("小程序不正确");
  88. }
  89. if ($info->status != 1) {
  90. WechatMenuService::throwErrMsg("此小程序暂不提供使用");
  91. }
  92. if ($info->type != MiniprogramType::WEIXIN->value()) {
  93. WechatMenuService::throwErrMsg("关键词回复设置仅支持微信小程序");
  94. }
  95. $param['miniprogram_appid'] = $info->appid;
  96. $info = DB::table('user_has_miniprograms')->where('uid', $this->getLoginUserId())->where('miniprogram_id', $param['miniprogram_id'])->where('is_enabled', 1)->value('id');
  97. if (empty($info)) {
  98. WechatMenuService::throwErrMsg("没有此小程序的使用权限");
  99. }
  100. if (count($param['content']) >3){
  101. WechatMenuService::throwErrMsg("底部菜单不能超过3个");
  102. }
  103. $clicks = [];
  104. foreach ($param['content'] as $val){
  105. if (!empty(getProp($val,'sub_button'))){
  106. foreach ($val['sub_button'] as $sub){
  107. if (getProp($sub,'type') == 'miniprogram'){
  108. $sub['appid'] = $param['miniprogram_appid'];
  109. $sub['pagepath'] = $sub['url'];
  110. }
  111. if (getProp($sub,'type') == 'click'){
  112. $clicks[] = $sub['key'];
  113. }
  114. }
  115. }else{
  116. if (getProp($val,'type') == 'miniprogram'){
  117. $val['appid'] = $param['miniprogram_appid'];
  118. $val['pagepath'] = $val['url'];
  119. }
  120. if (getProp($val,'type') == 'click'){
  121. $clicks[] = $val['key'];
  122. }
  123. }
  124. }
  125. unset($val,$sub,$info);
  126. $param['msg_content']= [];
  127. // 有点击事件
  128. if (!empty($clicks)){
  129. $msgContent = [];
  130. $msg = getProp($param,'msg_content');
  131. if (empty($msg) || !is_array($msg)){
  132. WechatMenuService::throwErrMsg("发送的文本信息不能为空");
  133. }
  134. foreach ($clicks as $val){
  135. $temp = getProp($msg,$val);
  136. if (empty($temp) || !is_array($temp)){
  137. WechatMenuService::throwErrMsg("发送的文本信息格式不正确");
  138. }
  139. $contents = "";
  140. foreach ($temp as & $item){
  141. if (getProp($item,'url')){
  142. $item['content'] = "<a href=\"\" data-miniprogram-appid=\"{$param['miniprogram_appid']}\" data-miniprogram-path=\"{$val['url']}\">{$val['title']}</a>";
  143. }else{
  144. $item['content'] = $item['tittle'];
  145. }
  146. $contents .= $item['content']."\n";
  147. }
  148. rtrim($contents, "\n");
  149. $temp['content'] = $contents;
  150. $msgContent = [$val => $temp];
  151. }
  152. $param['msg_content'] = $msgContent;
  153. unset($msgContent);
  154. unset($temp,$val,$item);
  155. }
  156. return $param;
  157. }
  158. public function test(){
  159. $info = WechatOpenPlatformService::getAppInfoById(8);
  160. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($info->component_appid);
  161. $openPlatform = WechatOpenPlatformService::buildApplication($componentInfo);
  162. $app = $openPlatform->officialAccount($info->authorizer_appid, getProp($info,'authorizer_refresh_token'));
  163. unset($appInfo);
  164. return $app->menu->list();
  165. }
  166. }