当前位置:网站首页>Wechat applet (authorized login of TP5)

Wechat applet (authorized login of TP5)

2022-06-11 06:16:00 Results of persistence and effort

Wechat applet Authorized landing PHP tp5 frame _ When you're alone, dogs accompany you ! The blog of -CSDN Blog

HTML page 

<button open-type="getUserInfo" bindtap="login" wx:if="{
   { showThis }}"> Authorized login </button>

<image src="/pages/image/2.png" style="width: 150rpx;height: 150rpx;padding-left: 300rpx;" wx:if="{
   { pic }}"></image>

<text>\r\n</text>

<text wx:if="{
   { pic }}" style="padding-left: 330rpx;"> full name </text>



JS: commentary 

// pages/login/login.js

Page({

    //  Initial data of the page 

    data: {

        showThis:true,

        pic:false

    },

    onLoad: function (options) {

    },

    login(){

        // adopt wx.getUserProfile The authorization box pops up 

        let that = this;

        wx.getUserProfile({

          desc: 'desc',

          success:res=>{

            // adopt wx.login obtain code

            wx.login({

              success:d=>{

                let code = d.code;

                // send out ajax request , And will code Carry the past 

                wx.request({

                  url: 'http://www.weekpic.com/loginDo',

                  data:{code:code},

                  success:function(r){

                        if(r.data.code==200)

                        {

                            let id=r.data.data.id;

                            let token=r.data.data.token;

                            wx.setStorage({

                                key:"uid",

                                data:id

                              })

                              that.setData({

                                  showThis:false,

                                  pic:true

                              })

                              wx.setStorage({

                                key:"token",

                                data:token

                              })

                            wx.showToast({

                              title: ' Login successful !',

                            })

                            wx.switchTab({

                              url: '/pages/type/type',

                            })

                        }

                  }

                })

              }

            })

          },

          fail:res=>{

            wx.showToast({

              title: " privilege grant failed ",

            })

          }

        })

      }

})



php commentary :


public function loginDo(Request $request)
   {
        $code=$request->get('code');
        $SECRET="";//SECRET
        $APPID="";//appid
        $url="https://api.weixin.qq.com/sns/jscode2session?appid=$APPID&secret=$SECRET&js_code=$code&grant_type=authorization_code";
        $url=json_decode(file_get_contents($url,true),true);

        $findRes=login::where('appid',$APPID)->find();

        if($findRes)
        {
             $token=Token::createToken(1);
                $data=[
                    'token'=>$token,
                    'id'=>$findRes['id']
                ];
             return ['code'=>200,'msg'=>'success','data'=>$data];
        }
        $createRes=login::create(['appid'=>$APPID]);
        if($createRes)
        {
//             $token=Token::createToken($createRes['id']);
                $data=[
                    'token'=>$token,
                    'id'=>$createRes['id']
                ];
             return ['code'=>200,'msg'=>'success','data'=>$data];
        }
   }
原网站

版权声明
本文为[Results of persistence and effort]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020528544156.html