小程序开发中如何实现登录功能

这篇“小程序开发中如何实现登录功能”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“小程序开发中如何实现登录功能”文章吧。

为南陵等地区用户提供了全套网页设计制作服务,及南陵网站建设行业解决方案。主营业务为成都网站设计、网站制作、南陵网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

微信小程序登录
一. 小程序不支持cookie会话
1. 通过传递与检验3rd_session来保持会话
2. 3rd_session可以执行‘`head -n 80 /dev/urandom | tr -dc A-Za-z0-9 | head -c 168`该命令生成
3. 使用redis或者数据库存储session
4. 生成的3rd_session发送给客户端,写入storage
5. 客户端的每次请求必须带上3rd_session
二、加密数据解码
1. $iv,$code是被加密过的数据,由于请求过程中因为编码原因+号变成了空格,所以我们需要用下面的方法转换回来

function define_str_replace($data){
    return str_replace(' ','+',$data);
  }


三、例子:
php

// 微信登录
 public function weixin_login(){
   $session_db=D('Session');
   $session_id=I('get.sessionid','');
   $session=$session_db->getSession($session_id);
   if( !empty( $session ) ){
     $this->ajaxReturn(['error_code'=>0,'sessionid'=>$session_id]);
   }else{
     $iv=define_str_replace(I('get.iv')); //把空格转成+
     $encryptedData=urldecode(I('get.encryptedData'));  //解码
     $code=define_str_replace(I('get.code')); //把空格转成+
     $msg=D('Weixin')->getUserInfo($code,$encryptedData,$iv); //获取微信用户信息(openid)
     if($msg['errCode']==0){
       $open_id=$msg['data']->openId;
       $users_db=D('Users');
       $info=$users_db->getUserInfo($open_id);
       if(!$info||empty($info)){
         $users_db->addUser(['open_id'=>$open_id,'last_time'=>['exp','now()']]); //用户信息入库
         $info=$users_db->getUserInfo($open_id);                  //获取用户信息
         $session_id=`head -n 80 /dev/urandom | tr -dc A-Za-z0-9 | head -c 168`;  //生成3rd_session
         $session_db->addSession(['uid'=>$info['id'],'id'=>$session_id]); //保存session
       }
       if($session_id){
         $this->ajaxReturn(['error_code'=>0,'sessionid'=>$session_id]);  //把3rd_session返回给客户端
       }else{
         $this->ajaxReturn(['error_code'=>0,'sessionid'=>$session_db->getSid($info['id'])]);
       }
  }else{
       $this->ajaxReturn(['error_code'=>'用户信息获取失败!']);
     }
      
   }
 }


获取微信信息模型(包括信息解密,官方例子点击下载)

require_once ABS_APP_PATH.'/Addon/Aes/wxBizDataCrypt.php';
class WeixinModel{
  // 获取微信的用户信息(openid)
  public function getUserInfo($code,$encryptedData,$iv){
    $appid=C('appid');
    $secret=C('secret');
    $grant_type='authorization_code';
    $url='https://api.weixin.qq.com/sns/jscode2session';
    $url= sprintf("%s?appid=%s&secret=%s&js_code=%s&grant_type=%",$url,$appid,$secret,$code,$grant_type);
    $user_data=json_decode(file_get_contents($url));
    $session_key= define_str_replace($user_data->session_key);
    $data="";
    $wxBizDataCrypt=new \WXBizDataCrypt($appid,$session_key);
    $errCode=$wxBizDataCrypt->decryptData($encryptedData,$iv,$data);
    return ['errCode'=>$errCode,'data'=>json_decode($data),'session_key'=>$session_key];
  }
  }


javascript

getUserInfo: function(cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function(r) {
          wx.getUserInfo({
            success: function(res) {
              that.login({
                code: r.code,
                iv: res.iv,
                encryptedData: encodeURIComponent(res.encryptedData),
              })
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
 login: function(param) {
    wx.request({
      url: this.requestUrl('Index/weixin_login'),
      data: param,
      header: {
        'content-type': "application/json",
      },
      success: function(res) {
        var data = JSON.parse(res.data.trim());
        wx.setStorageSync('sessionid', data.sessionid);
      }
    })
  },

以上就是关于“小程序开发中如何实现登录功能”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注创新互联行业资讯频道。

网页题目:小程序开发中如何实现登录功能
链接地址:https://www.cdcxhl.com/article37/ggjppj.html

成都网站建设公司_创新互联,为您提供ChatGPT网站设计公司虚拟主机服务器托管企业网站制作网站排名

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站建设