PHP微信小程序授权登录

wxml

<button type="primary" bindtap="login">授权登录</button>

js

// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.login({
      success: (res) => {
        wx.request({
          url: 'http://www.cs.com/api/demo/openid',
          method: "GET",
          data: {
            code: res.code
          },
          success: (result) => {
            that.setData({
              openid: result.data.data
            })
          }
        })
      }
    })
  },
  login(){
    var that=this
    var openid=that.data.openid
    wx.getUserProfile({
      desc: '授权登录',
      success:(res)=>{
            var nick_name=res.userInfo.nickName
            var avatar_url=res.userInfo.avatarUrl
           wx.request({
             url: 'http://www.cs.com/api/demo/login',
             method:"POST",
             data:{nick_name,avatar_url,openid},
             success:(result)=>{
                      wx.setStorageSync('token',result.data.data)
             }
           })
      }
    })
  }
})

.php  获取openid,composer 安装  :"guzzlehttp/guzzle": "~6.0",

 public function openid(Request $request)
    {
        $code = $request->get("code");
        $appid = "";
        $secret = "";
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        $client = new Client(['timeout' => 2.0, 'verify' => false]);
        $response = $client->get($url);
        $res = (string)$response->getBody();
        $result = json_decode($res, true);
        return json(["error_code"=>0,"data"=>$result["openid"],"msg"=>"返回openid"]);
    }

.php 登录处理: 

 public function login(Request $request){
         $data=$request->post();
         $res=UserModel::getInfo(["openid"=>$data["openid"]]);
         if (!$res){
             $result=UserModel::saveInfo($data);
             $userInfo=UserModel::getInfo(["id",$result]);
//         jwt 获取Token
             $token=Token::signToken($userInfo["id"]);
             return json(["error_code"=>0,"data"=>$token,"msg"=>"登录成功"]);
         }elseif ($res){
             $token=Token::signToken($res["id"]);
             return json(["error_code"=>0,"data"=>$token,"msg"=>"登录成功"]);
         }else{
             return json(["error_code"=>1,"data"=>"","msg"=>"登录失败"]);
         }
    }

jwt请参考:

https://blog.csdn.net/qq_22630169/article/details/104004670?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162496773416780255283747%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162496773416780255283747&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-1-104004670.first_rank_v2_pc_rank_v29&utm_term=tp6%E4%B8%AD%E9%97%B4%E4%BB%B6%E4%BD%BF%E7%94%A8%E9%85%8D%E5%90%88token&spm=1018.2226.3001.4187 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值