123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?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->handleParam($request->validated());
- $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 = $this->handleParam($request->validated());
- 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);
- }
- /**
- * 分配
- * name: allocation
- * @param $id
- * date 2023/07/12 07:18
- */
- 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个");
- }
- $clicks = [];
- 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'){
- $clicks[] = $sub['key'];
- }
- }
- }else{
- if (getProp($val,'type') == 'miniprogram'){
- $val['appid'] = $param['miniprogram_appid'];
- $val['pagepath'] = $val['url'];
- }
- if (getProp($val,'type') == 'click'){
- $clicks[] = $val['key'];
- }
- }
- }
- unset($val,$sub,$info);
- $param['msg_content']= [];
- // 有点击事件
- if (!empty($clicks)){
- $msgContent = [];
- $msg = getProp($param,'msg_content');
- if (empty($msg) || !is_array($msg)){
- WechatMenuService::throwErrMsg("发送的文本信息不能为空");
- }
- foreach ($clicks as $val){
- $temp = getProp($msg,$val);
- if (empty($temp) || !is_array($temp)){
- WechatMenuService::throwErrMsg("发送的文本信息格式不正确");
- }
- $contents = "";
- foreach ($temp as & $item){
- if (getProp($item,'url')){
- $item['content'] = "<a href=\"\" data-miniprogram-appid=\"{$param['miniprogram_appid']}\" data-miniprogram-path=\"{$val['url']}\">{$val['title']}</a>";
- }else{
- $item['content'] = $item['tittle'];
- }
- $contents .= $item['content']."\n";
- }
- rtrim($contents, "\n");
- $temp['content'] = $contents;
- $msgContent = [$val => $temp];
- }
- $param['msg_content'] = $msgContent;
- unset($msgContent);
- unset($temp,$val,$item);
- }
- 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();
- }
- }
|