WechatMenuController.php 7.0 KB

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