微信开放平台(第三方平台)代公众号发起网页授权流程(基于lavarel框架开发)

结合上一篇文章 微信公证号授权给第三方平台开发流程 实现第三方平台代微信公众号起网页授权功能。

一、 公众号权限集 网页服务要授权给第三方,或者全部授权都可以的。
二、配置 公众号开发域名 入下图在这里插入图片描述
三、我同样是用EasyWeChat , 安装 - 按照官网文档配置 文档地址

composer require overtrue/wechat:~4.0 -vvv

四、开发代码
1、创建授权访问路由
2、授权后回调路由获取用户微信信息
根据自己开发 自己定义就行。

Route::get('small_wechat/auth', 'WeChatController@auth'); //小店用户端微信授权
Route::get('get_wechat/get_auth', 'WeChatController@get_auth'); //授权回调绑定微信

五、具体代码如下

<?php

namespace App\Http\Repositories\SmallShop\User;

use Illuminate\Http\Request;
use App\Model\SmallUser\SmallUser;
use App\Model\SmallShop\SmallPlatformAccount;
use Illuminate\Support\Facades\Log;
use EasyWeChat\Factory;

class WeChatRepository
{
    protected $request;
    protected $smallUser;
    protected $openPlatform;
    protected $smallPlatformAccount;

    /**
     * @param Request $request
     * @param SmallUser $smallUser
     * @param SmallPlatformAccount $smallPlatformAccount
     */
    public function __construct(Request $request, SmallUser $smallUser, SmallPlatformAccount $smallPlatformAccount)
    {
        $this->request = $request;
        $this->smallUser = $smallUser;
        $this->smallPlatformAccount = $smallPlatformAccount;
       $oauth = ['oauth' => ['scopes' => ['snsapi_userinfo'], 'callback' => '/api/get_wechat/get_auth']];
        //$config = array_merge(config('wechat.open_platform.default'), $oauth);
        $config = [
            'app_id'  => 'wx5355*******',
            'secret'  => 'e212abd70b********',
            'token'   => 'wsa2qw86b86k********',
            'aes_key' => 'pak2g7mwgz1stw6********',
            'oauth' => [
                'scopes'   => ['snsapi_userinfo'],
                'callback' => '/api/get_wechat/get_auth',
            ],
        ];
        $this->openPlatform = Factory::openPlatform($config);
    }

    /**
     * 基础模型
     * @return false|\Overtrue\Socialite\Providers\WeChatProvider
     */
    public function base_model()
    {
        $account = $this->smallPlatformAccount->where('agent_id', small_agent_id())
            ->orderBy('id', 'desc')->first(['id', 'agent_id', 'appid', 'refresh_token']);
        if ($account) {
            $officialAccount = $this->openPlatform->officialAccount($account->appid, $account->refresh_token);
            return $officialAccount->oauth->scopes(['snsapi_userinfo']);//需要用户确认
            //return $officialAccount->oauth; //静默授权
        }

        return false;
    }

    //用户微信授权
    public function auth()
    {
        $oauth = $this->base_model();
        if ($oauth) {
            //return $oauth->redirect();
            //这里不一定是return,如果你的框架action不是返回内容的话你就得使用
            $oauth->redirect()->send();
        }

        return ["message" => "小店公众号没有授权给第三方平台,微信授权失败!", "code" => 201];
    }

    //授权回调绑定微信
    public function get_auth()
    {
        // 获取 OAuth授权结果用户信息
        $oauth = $this->base_model();
        $user = $oauth->user()->toArray();
        $weixin = [
            'logo' => $user['avatar'],
            'weixin_no' => $user['name'],
            'weixin_openid' => $user['id'],
            'weixin_nickname' => $user['nickname'],
        ];
        $this->smallUser->where('id', small_user_id())->update($weixin);

        header('location:' . '/shop'); //跳转到
    }
}

六、开发者工具授权效果
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值