easyWeChat 前后端分离后,怎么样获取 openId

如题

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 3
wonbin

只要请求参数中有code - value 字段 然后在代码中使用

$account->oauth->user();

就可以了,easywechat 封装的太好了 然后使用很傻瓜,导致很多人不明白

easywechat 源码 vendor/overtrue/socialite/src/Providers/AbstractProvider.php 中user() 方法就是使用code 获取token的

   /**
     * {@inheritdoc}
     */
    public function user(AccessTokenInterface $token = null)
    {
        if (is_null($token) && $this->hasInvalidState()) {
            throw new InvalidStateException();
        }

        $token = $token ?: $this->getAccessToken($this->getCode());

        $user = $this->getUserByToken($token);

        $user = $this->mapUserToObject($user)->merge(['original' => $user]);

        return $user->setToken($token)->setProviderName($this->getName());
    }

    /**
     * Get the code from the request.
     * @return string
     */
 protected function getCode()
{
    return $this->request->get('code');
}
4年前 评论
Epona

前端提交 code 等参数,后端解密处理就可以

4年前 评论
  1. 后台返回url给前端
  2. 前端跳到向到这个url 然后微信会重定向到你设置好的通知url
  3. 这个通知的url你获取到了openid 自行处理 处理好跳回前端

想不跳转可以吗? 估计不行 (h5)

4年前 评论
wonbin

只要请求参数中有code - value 字段 然后在代码中使用

$account->oauth->user();

就可以了,easywechat 封装的太好了 然后使用很傻瓜,导致很多人不明白

easywechat 源码 vendor/overtrue/socialite/src/Providers/AbstractProvider.php 中user() 方法就是使用code 获取token的

   /**
     * {@inheritdoc}
     */
    public function user(AccessTokenInterface $token = null)
    {
        if (is_null($token) && $this->hasInvalidState()) {
            throw new InvalidStateException();
        }

        $token = $token ?: $this->getAccessToken($this->getCode());

        $user = $this->getUserByToken($token);

        $user = $this->mapUserToObject($user)->merge(['original' => $user]);

        return $user->setToken($token)->setProviderName($this->getName());
    }

    /**
     * Get the code from the request.
     * @return string
     */
 protected function getCode()
{
    return $this->request->get('code');
}
4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!