微信开放平台-扫码登录

admin9个月前Laravel362

public function wx_info()
{
   $code = request()->input('code');
   if (!empty($code))  //有code
   {
       //通过code获得 access_token + openid
       $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid
           . "&secret=" . $this->secret . "&code=" . $code . "&grant_type=authorization_code";
       $jsonResult = file_get_contents($url);
       $resultArray = json_decode($jsonResult, true);

       $access_token = $resultArray["access_token"];
       $openid = $resultArray["openid"];
       $infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid;

       $infoResult = file_get_contents($infoUrl);
       $infoArray = json_decode($infoResult, true);

       $data['openid'] = $infoArray['openid'];
       $data['wx_nickname'] = $infoArray['nickname'];
       $data['avatar'] = $infoArray['headimgurl'];

       Db::beginTransaction();
       $info = Db::table('user')->where(array('id' => $this->user_id))->update($data);
       if ($info !== false) {
           Db::commit();
           return $this->success('ok', $infoArray);
       } else {
           Db::rollBack();
           return $this->error('error');
       }
   } else {
       return $this->error('请传入code值');
   }
}

相关文章

微信退款v2

/** * 微信 退款 操作 */ public function refund($out_trade_no, $total_fee) { $parma =...

验证手机号

public function phone($phone){    $pat = '/'        . '^13...

设置项目编号

$max = Db::table('member')->max('id') ?? 0; if ($max < 100000) { $cd = $this->create...

excel 导出

header("Content-Type: application/vnd.ms-excel; name='excel'"); header("Content-Disposition:atta...

关注公众号并登录

/* * 服务端生成带唯一标识二维码并将唯一标识返回给前端 */ public function wx_code() { $scene_str = $this->get_...

抖音小程序登录

$code = request()->input('code');$nickname = request()->input('nickName');$ava...