登录流程

小程序登录搭建 - 图1

  • 获取密钥

小程序管理->开发->开发设置
image.png

  • 密钥应该存在后端的constant.service.ts中,如下:

image.png

  • 创建wechatConfig

image.png

  • 代码说明流程图 ```typescript // 1. 小程序登录 获取code uni.login({ success: ({ code }) => { userService
    1. .getTokenByCode(code)
    2. .pipe(
    3. switchMap(() => {
    4. return this._startUp();
    5. }),
    6. )
    7. .subscribe(
    8. () => {
    9. // uni.hideLoading();
    10. this.activate$.next(true);
    11. resolve(true);
    12. },
    13. err => {
    14. console.log(err);
    15. },
    16. );
    }, fail: res => { reject(res); }, });

// 2. 后端使用code 获取key // 使用用户登录的code去请求openid和session_key async getContent({ appid, secret, js_code, grant_type = ‘authorization_code’, }: { appid: string; secret: string; js_code: string; grant_type?: string; }) { const res = await this.httpService .get<{ openid: string; session_key: string; unionid: string; errcode: number; errmsg: string; }>(‘https://api.weixin.qq.com/sns/jscode2session‘, { params: { appid, secret, js_code, grant_type, }, }) .toPromise(); if (res.data.openid) { return res.data; } else { throw new BizException({ code: BizExceptionAuthCodeEnum.WEAPP_AUTH_ERROR, msg: res.data.errmsg, extraData: res.data, system: ‘’, }); } }

// 3. 将openid存入用户的auth

```