123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- /**
- * 微信公众号自定义菜单
- * @file:WechatMenuController.php
- * @Date: 2023/7/5
- * @Time: 15:01
- */
- namespace Modules\WechatPlatform\Http\Controllers;
- use Catch\Base\CatchController;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Modules\Channel\Services\WechatOpenPlatform\WechatOpenPlatformService;
- use Modules\Manage\Enmus\MiniprogramType;
- use Modules\User\Http\Controllers\UserTrait;
- use Modules\WechatPlatform\Http\Requests\WechatMenuRequest;
- use Modules\WechatPlatform\Services\WechatMenuService;
- class WechatMenuController extends CatchController
- {
- use UserTrait;
- /**
- * 关键词列表
- * name: List
- * @param Request $request
- * date 2023/07/05 15:10
- */
- public function List(Request $request)
- {
- $param = $request->all();
- $userContext = $this->getUserContext(null);
- $param['user_id'] = $userContext['loginUser']->id;
- $param['puser_id'] = $userContext['loginUser']->pid;
- }
- public function add(WechatMenuRequest $request)
- {
- $param = $this->handelParam($request->validated());
- }
- // 处理参数
- private function handelParam($param)
- {
- $info = DB::table('miniprogram')->where('id', $param['miniprogram_id'])->first();
- if (empty($info)) {
- WechatMenuService::throwErrMsg("小程序不正确");
- }
- if ($info->status != 1) {
- WechatMenuService::throwErrMsg("此小程序暂不提供使用");
- }
- if ($info->type != MiniprogramType::WEIXIN->value()) {
- WechatMenuService::throwErrMsg("关键词回复设置仅支持微信小程序");
- }
- $param['miniprogram_appid'] = $info->appid;
- $info = DB::table('user_has_miniprograms')->where('uid', $this->getLoginUserId())->where('miniprogram_id', $param['miniprogram_id'])->where('is_enabled', 1)->value('id');
- if (empty($info)) {
- WechatMenuService::throwErrMsg("没有此小程序的使用权限");
- }
- $errs = "";
- if (count($param['content']) >3){
- WechatMenuService::throwErrMsg("底部菜单不能超过3个");
- }
- $menus = [];
- foreach ($param['content'] as $val){
- $temp = ['name' => $val['name']];
- if (getProp($val,'sub_button')){
- // 有二级菜单
- if (count($val['sub_button']) > 5){
- $errs .= getProp($val,'name')."的二级菜单超过5个";
- continue ;
- }
- foreach ($val['sub_button'] as $sub){
- $res = $this->checkMenuItem($sub);
- }
- }else{
- // 一级菜单
- $res = $this->checkMenuItem($sub);
- }
- }
- }
- private function checkMenuItem($sub)
- {
- $type = getProp($sub,'type');
- if (!in_array($type,["click",'miniprogram','scancode_waitmsg'])){
- return ['status' => 0,'msg' => "类型非法"];
- }
- }
- public function test(){
- $info = WechatOpenPlatformService::getAppInfoById(8);
- $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($info->component_appid);
- $openPlatform = WechatOpenPlatformService::buildApplication($componentInfo);
- $app = $openPlatform->officialAccount($info->authorizer_appid, getProp($info,'authorizer_refresh_token'));
- unset($appInfo);
- return $app->menu->list();
- }
- }
|