登录流程
- 获取密钥
小程序管理->开发->开发设置
- 密钥应该存在后端的constant.service.ts中,如下:
- 创建wechatConfig
- 代码说明流程图
```typescript
// 1. 小程序登录 获取code
uni.login({
success: ({ code }) => {
userService
}, fail: res => { reject(res); }, });.getTokenByCode(code)
.pipe(
switchMap(() => {
return this._startUp();
}),
)
.subscribe(
() => {
// uni.hideLoading();
this.activate$.next(true);
resolve(true);
},
err => {
console.log(err);
},
);
// 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
```