微信小程序授权登录

授权图片展示

 

php代码实现

public function index()
    {
        // 获取code
        $code = input("code");
        // 获取昵称
        $nickname = input("nickname");
//        // 从微信公众平台获得appid
        $appid = "wxeb284243ebf4674f";
//        // 从微信公众平台获得secret
        $secret = "e3e7a8ef60ee32842ef1e6313ebadfb0";
//        // 发送请求换取openid和sessionkey
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
//        发送请求
        $res=json_decode(file_get_contents($url),true);
//        组装数据
        $info=[
            'nikename'=>$nickname,
            'appid' => $res['openid'],
            'sceret' =>$res['session_key']
        ];
//        判断库里是用户数据是否存在
        $kot=Db::table('sqdl')->where('appid',$info['appid'])->find();
        if ($kot){
            return  json(['data'=>$kot,'msg'=>'success','code'=>200]);
        }

//        判断数据是否入库
        if(Db::table('sqdl')->insert($info)){
               $info['id']=Db::getLastInsID();
               return  json(['data'=>$info,'msg'=>'success','code'=>200]);

        }else{
            return  json(['data'=>$info,'msg'=>'success','code'=>500]);
        }


    }

js实现:

  

bindGetUserInfo(evt){
   
      // // 判断是否获取到用户信息
      if(evt.detail.userInfo){
         // 无线上appid直接跳转到初始页面
         wx.switchTab({
            url: '/pages/dome/dome',
          })

            // 有线上appid可以使用下面代码执行
      //    // console.log(evt);
      //    wx.login({
      //       success (res) {
      //          // console.log(res)
      //         if (res.code) {
      //          //   console.log(res.code)
      //          //  发起网络请求
      //           wx.request({
      //             url: 'http://www.minmin.com/thankapi/public/index.php/admin/good/index',
      //             data: {code: res.code,nickname:evt.detail.userInfo.nickName},
      //             success(e){
      //                // console.log(e.data.code);
      //                if(e.data.code==200){
      //                   wx.switchTab({
      //                     url: '/pages/dome/dome',
      //                   })
      //                }
      //             }
      //           })
      //         } else {
      //           console.log('登录失败!' + res.errMsg)
      //         }
      //       }
      //     })
      }else{
         // 用户拒绝授权登录
         console.log('不让你登录');
      }
   }
   

wxml实现:

<view>
  <view class='headView'>
    <view class='headImageView'>
      <image class='headImage' src='/img/ganguo.png' mode='scaleToFill'></image>
    </view>
    <view class='titleText'>申请获取以下权限</view>
    <view class='contentText'>获得你的公开信息(昵称,头像,手机等)</view>
    <button class='authBtn' type='primary' open-type='getUserInfo' bind:getuserinfo='bindGetUserInfo'>授权登录</button>
  </view>
</view>

wxss实现:

.headView {
   margin: 90rpx 50rpx 90rpx 50rpx;
   /*上右下左*/
}
.headImageView {
   display: block;
   margin-left: 25px;
   margin-top: 25px;
   margin-right: 25px;
   margin-bottom: 0px;
   height: 50px;
}
.headImage {
   display: flex;
   width: 150px;
   height: 80px;
}
.titleText {
   margin-left: 25px;
   margin-top: 25px;
   margin-bottom: 10px;
   font-size: 14px;
   color: #020e0f;
   text-align: center;
}
.contentText {
   margin-left: 25px;
   margin-top: 0px;
   margin-bottom: 0px;
   font-size: 14px;
   color: #666;
   text-align: center;
}
.authBtn {
   margin-top: 35px;
   margin-left: 25px;
   margin-right: 25px;
   height: 45px;
   font-size: 17.5px;
}

 

posted @ 2021-04-13 10:33  jerry_min  阅读(862)  评论(0编辑  收藏  举报