123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- 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;
-
- public function List(Request $request)
- {
- $param = $request->all();
- $userContext = $this->getUserContext(null);
- $param['user_id'] = $userContext['loginUser']->id;
- $param['puser_id'] = $userContext['loginUser']->pid;
- return WechatMenuService::list($param);
- }
- public function add(WechatMenuRequest $request)
- {
- $param = $request->validated();
- $param['msg_content'] = $request->input('msg_content');
- $param = $this->handleParam($param);
- $userContext = $this->getUserContext(null);
- $param['user_id'] = $userContext['loginUser']->id;
- $param['puser_id'] = $userContext['loginUser']->pid;
- $param['wechat_accounts'] = [];
- return WechatMenuService::addMenu($param);
- }
- public function edit($id,WechatMenuRequest $request)
- {
- $param = $request->validated();
- $param['msg_content'] = $request->input('msg_content');
- $param = $this->handleParam($param);
- return WechatMenuService::updateMenu($id,$param);
- }
- public function detail($id){
- return WechatMenuService::detail($id);
- }
- public function authList($id){
- $userId = $this->getLoginUserId();
- return WechatMenuService::authList($id, $userId);
- }
- public function del(Request $request)
- {
- $ids = $request->input('ids');
- if (empty($ids)) {
- WechatMenuService::throwErrMsg('要删除的数据参数错误');
- }
- $ids = explode(',', $ids);
- return WechatMenuService::del($ids);
- }
-
- public function allocation($id,Request $request)
- {
- if (!$request->has('wx_auth_ids')) {
- WechatMenuService::throwErrMsg("参数错误");
- }
- if (!empty($request->input('wx_auth_ids'))) {
- $wxAuthIds = explode(',', $request->input('wx_auth_ids'));
- } else {
- $wxAuthIds = [];
- }
- return WechatMenuService::allocation($id,$wxAuthIds);
- }
-
- private function handleParam($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("没有此小程序的使用权限");
- }
- if (count($param['content']) >3){
- WechatMenuService::throwErrMsg("底部菜单不能超过3个");
- }
-
- $msgContent = [];
- foreach ($param['content'] as &$val){
- if (!empty(getProp($val,'sub_button'))){
- foreach ($val['sub_button'] as &$sub){
- if (getProp($sub,'type') == 'miniprogram'){
- $sub['appid'] = $param['miniprogram_appid'];
- $sub['pagepath'] = $sub['url'];
- }
- if (getProp($sub,'type') == 'click'){
-
- $msgContent[] =[$sub['key'] => $sub['content']];
- }
- if (!in_array(getProp($sub,'type') ,['view','miniprogram'])){
- unset($sub['url']);
- }
- }
- }else{
- if (getProp($val,'type') == 'miniprogram'){
- $val['appid'] = $param['miniprogram_appid'];
- $val['pagepath'] = $val['url'];
- }
- if (getProp($val,'type') == 'click'){
-
- $msgContent[] =[$val['key'] => $val['content']];
- }
- if (!in_array(getProp($val,'type') ,['view','miniprogram'])){
- unset($val['url']);
- }
- }
- }
- $param['msg_content']= $msgContent;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return $param;
- }
- 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();
- }
- }
|