WechatMenuController.php 6.2 KB

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