100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 微信开放平台(第三方平台)代公众号发起网页授权流程(基于lavarel框架开发)

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

时间:2019-08-06 06:41:22

相关推荐

微信开放平台(第三方平台)代公众号发起网页授权流程(基于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'); //授权回调绑定微信

五、具体代码如下

<?phpnamespace 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'); //跳转到}}

六、开发者工具授权效果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。