微信小程序授权登录
发布于 2 年前 作者 wyan 1048 次浏览 来自 分享

wxml:

<view>
<image src="{{userInfo.img}}" class="img"></image>
</view>
<view class="name">
{{userInfo.nickname}}
</view>
<view wx:if="{{userInfo.sex==0}}" class="sex">男</view>
<view wx:else="{{userInfo.sex==1}}" class="sex"></view>
<button open-type="getUserInfo" bindtap="login" type="default">登录</button>

js:

  login(e){
      var that = this 
      wx.getUserProfile({
        desc: 'desc',
        success:res=>{
          console.log(res)
          var userInfo = res.userInfo
          wx.login({
            success (res) {
              console.log(res)
              if (res.code) {
                //发起网络请求
                wx.request({
                  url: 'http://www.exam2.com/api/login',
                  data: {
                    code: res.code,
                    userInfo:userInfo
                  },
                  method:"POST",
                  header: {
                    'content-type''application/json' // 默认值
                  },
                  success (res) {
                    console.log(res.data)
                    if(res.data.code==203){
                      wx.showToast({
                        title: res.data.msg,
                        icon:"error"
                      })
                      return;
                    }
                    if(res.data.code==200){
                      wx.setStorageSync('openid',res.data.data.openid)
                      wx.setStorageSync('session_key',res.data.data.session_key)
                      that.setData({
                        userInfo:res.data.data
                      })
                      wx.showToast({
                        title: res.data.msg,
                      })
                      wx.navigateTo({
                        url: '/pages/phone/phone',
                      })
                    }
                  }
                  
                })
              } else {
                console.log('登录失败!' + res.errMsg)
              }
            }
          }) 
        }
      })
  },

php:

<?php

namespace App\Http\Controllers;

use App\Jwt\Token;
use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    //
    /**
     * 登录
     */
    public function loginDo(request $request)
    {
        $code = $request->post('code');
        $openid = config('openid.openid');
        $session_key = config('openid.session_key');

        $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$openid}&secret={$session_key}&js_code={$code}&grant_type=authorization_code";

        $res = $this->geturl($url);

        //return ['code'=>200,'msg'=>'成功','data'=>$res];

        $arr = [
            'openid'=>$res['openid'],
            'session_key'=>$res['session_key']
        ];
        $data = User::where('openid',$res['openid'])->first();

        if (empty($data)){
            $add = User::create($arr,true);
            if ($add){
               $token = (new Token())->createToken($res['openid']);
               return ['code'=>200,'msg'=>'登录成功','data'=>$token];
            }
        }else{
            $up = User::where('openid',$res['openid'])->update(['session_key'=>$res['session_key']]);
            if ($up){
                $token = (new Token())->createToken($res['openid']);
                return ['code'=>200,'msg'=>'登录成功','data'=>$token];
            }
        }

    }

    function geturl($url){
        $headerArray =array("Content-type:application/json;","Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);
        return $output;
    }

}

回到顶部