欢迎光临散文网 会员登陆 & 注册

uniapp登录微信小程序

2023-04-13 14:35 作者:学酥两支笔  | 我要投稿

一 小程序服务端文档链接

https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getStableAccessToken.html

https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html


二 登录流程时序为

小程序 wx.login()获取code

小程序 wx.request()发送code 开发者服务器

开发者服务器 登录凭证校验接口 appid+appsecret+code 微信接口服务

微信接口服务 返回session_key+openid等 开发者服务器

开发者服务器 自定义登录态 与openid,session_key关联

开发者服务器 返回自动以登录态 小程序

小程序 自定义登录态 存入storage

小程序 wx.request()发起业务请求 携带自定义登录态

开发者服务器 通过自定义登录态 查询openid和session_key

开发者服务器 返回业务数据


说明:

前端调用 wx.login() 获取 临时登录凭证code ,并回传到开发者服务器。

后端调用 auth.code2Session 接口,换取 用户唯一标识 OpenID 、 用户在微信开放平台帐号下的唯一标识UnionID(若当前小程序已绑定到微信开放平台帐号) 和 会话密钥 session_key。


注意事项:

会话密钥 session_key 是对用户数据进行 加密签名 的密钥。为了应用自身的数据安全,开发者服务器不应该把会话密钥下发到小程序,也不应该对外提供这个密钥。

临时登录凭证 code 只能使用一次


三 其他项目解决方案

需要传token的接口统统在header中加

Authorization   

terminalType 1:iOS,2:安卓    

version版本号


header中添加Authorization带token值,获取员工信息

即小程序request的header改为

header: {'content-type':'application/json', 'Authorization': token},


注:fastadmin做后端的话,Authorization关键词改为token


四 uniapp登录思路

1 前端获取code 

2 后端获取OpenID 

3 后端注册 方法在FASTADMIN的auth类

4 后端登录 返回userinfo,其中有token

5 前端获取userinfo,存入globalData改变登录状态

6 header中声明token进行前后端交互


五 前台小程序源生登录

wx.login方法会返回用户登录凭证(有效期五分钟)。

开发者需要在开发者服务器后台调用 code2Session,使用 code 换取 openid、unionid、session_key 等信息

```

wx.login({

  success (res) {

    if (res.code) {

      //发起网络请求

      wx.request({

        url: 'https://example.com/onLogin',

        data: {

          code: res.code

        }

      })

    } else {

      console.log('登录失败!' + res.errMsg)

    }

  }

})

```


六 后台jscode2session接口根据code,获取unionid等

GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code 


七 uniapp封装的登录方法

文档链接:

https://uniapp.dcloud.net.cn/api/plugins/login.html

```

uni.login({

  provider: 'weixin', //使用微信登录

  success: function (loginRes) {

    console.log(loginRes.authResult);

  }

});

```


八 后端FASTADMIN的登录认证

FASTADMIN使用了基于Auth验证的权限管理系统

配置项路径为:

application\common\controller\backend.php 里的 protected $dataLimit

相关表为:

auth_group

auth_rule

引用:

use app\common\library\Auth;

注册:

$auth = new Auth();

$auth ->register('test', 'test123456789');


九 auth、token、cookie、session等

1 this->auth是保存在服务器端的,类似session

2 token是保存在客户端的,类似cookie

3 auth是每次api访问后生成的,里边保存包括用户的信息.

4 session和cookie是本次访问本台服务器记录下来的会话信息,无法用于api接口开发方式,因为每一次请求都会是新的session 会导致本次存储的session和上次的不一样,到时候会读不到你想要的.

5 token是用户登录后生成的,也可以是其它途径生成.token生成后会存入数据库(对于fastadmin来说,是这样实现的,一个用户有多个token),在访问api的时候,fastadmin会通过token来获取用户信息.


十 论坛查询token相关问答

关于fastadmin后台操作检测权限使用说明

页面

    <table id="table" class="table table-striped table-bordered table-hover" 

           data-operate-edit="{:$auth->check('user/user/edit')}" 

           data-operate-del="{:$auth->check('user/user/del')}" 

           width="100%">

    </table>

基类文件路径:

app\common\backend.php

引用Auth:

use app\admin\library\Auth;

初始化auth:

$this->auth = Auth::instance();

app\admin\library\Auth继承于extend/fast/Auth类

$uid加载获取登录的账号所有权限

$uid = $uid ? $uid : $this->id

因为引入了admin模型

use app\admin\model\Admin;

$uid = $uid ? $uid : $this->id

在登录成功时候会把用户信息进行写入session

通过TP自动调用魔术方法__get获取admin成员属性

直接访问$this->id就是去找admin模型的属性。


十一 改造user表

新增openid

新增unionid

php think crud -t adminuser -u -f 1


十二 api中新增方法jscode2session

```

public function jscode2session()

{

        $jscode = $this->request->get("jscode");

        $url = "https://api.weixin.qq.com/sns/jscode2session";

        $res = Http::sendRequest($url); //array类型 直接返回报错 $res = json_encode($res);

        $msg = $res['msg']; //string类型

        $msg = json_decode($msg, true);

        $openid = '';

        if($msg['openid']){

            $openid = $msg['openid'];

            //api登录拷贝自user.php。在app/common/library/Auth.php中新增loginbyopenid

            $ret = $this->auth->loginByOpenid($openid); 

            if ($ret) {

                $data = ['userinfo' => $this->auth->getUserinfo()];

                return json_encode($data);

            } else {

                return "注册失败";

            }

        }

        else{

            return "登录失败";

        }

    }

```


十三 auth类中新增方法loginByOpenid

略 修改自原有的注册和登录方法


十四 uniapp新增小程序登录

1 先获取系统类型

uni.getSystemInfo

2 调用登录方法

uni.login

3 回调中调用后端注册登录方法

uni.request

4 以上部分是异步方法,最终的回调中同步存入本地并更新数据

uni.setStorageSync


十五 上传和修改头像

前端上传使用

uni.uploadFile

新建方法

```

    public function upload_avatar()

    {

        $user = $this->auth->getUser();

        $avatar = $this->request->file('image');

        $info = $avatar->move(ROOT_PATH . 'public' . DS . 'uploads');

        if($info){

            // 成功上传后 获取上传信息

            //$info = $info->getExtension(); // 输出文件类型

            $info = $info->getSaveName();//输出 20160820/42a79759f284b767dfcb2a0197904287.jpg

            //$info = $info->getFilename();// 输出 42a79759f284b767dfcb2a0197904287.jpg

        }else{

            // 上传失败获取错误信息

            $info = $file->getError();

        }

        return $info;

    }

```


uniapp登录微信小程序的评论 (共 条)

分享到微博请遵守国家法律