mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2024-11-01 22:20:30 +08:00
新增app用户模块
This commit is contained in:
parent
d6fb8338ca
commit
94a97190dd
@ -26,9 +26,6 @@ export class UserInfoEntity extends BaseEntity {
|
|||||||
@Column({ comment: '状态 0-禁用 1-正常', default: 1 })
|
@Column({ comment: '状态 0-禁用 1-正常', default: 1 })
|
||||||
status: number;
|
status: number;
|
||||||
|
|
||||||
@Column({ comment: '登录方式 0-小程序 1-公众号 2-H5 3-APP', default: 0 })
|
@Column({ comment: '登录方式 0-小程序 1-公众号 2-H5', default: 0 })
|
||||||
loginType: number;
|
loginType: number;
|
||||||
|
|
||||||
@Column({ comment: '标签,多个标签按“,”隔开', nullable: true })
|
|
||||||
labels: string;
|
|
||||||
}
|
}
|
||||||
|
@ -99,12 +99,11 @@ export class UserLoginService extends BaseService {
|
|||||||
);
|
);
|
||||||
return this.wxLoginToken(wxUserInfo);
|
return this.wxLoginToken(wxUserInfo);
|
||||||
} else {
|
} else {
|
||||||
throw new CoolCommException('微信登录失败');
|
throw new Error('微信登录失败');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
<<<<<<< HEAD
|
|
||||||
* 保存微信信息
|
* 保存微信信息
|
||||||
* @param wxUserInfo
|
* @param wxUserInfo
|
||||||
* @param type
|
* @param type
|
||||||
@ -121,19 +120,6 @@ export class UserLoginService extends BaseService {
|
|||||||
type,
|
type,
|
||||||
});
|
});
|
||||||
return wxUserInfo;
|
return wxUserInfo;
|
||||||
=======
|
|
||||||
* 保存/更新 微信信息(根据微信规则,用户头像昵称无法在此获取,需要通过chooseAvatar进行获取)
|
|
||||||
* @param wxUserInfo 微信里的用户信息
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async saveWxInfo(wxUserInfo) {
|
|
||||||
const wxInfo = await this.userWxEntity.findOneBy({ openid: wxUserInfo.openid });
|
|
||||||
if (!wxInfo) {
|
|
||||||
await this.userWxEntity.insert(wxInfo);
|
|
||||||
}
|
|
||||||
await this.userWxEntity.save(Object.assign(wxInfo, wxUserInfo));
|
|
||||||
return wxInfo;
|
|
||||||
>>>>>>> 6a9a421c61366184f92aea3f742a577d944d2879
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -149,28 +135,21 @@ export class UserLoginService extends BaseService {
|
|||||||
iv
|
iv
|
||||||
);
|
);
|
||||||
if (wxUserInfo) {
|
if (wxUserInfo) {
|
||||||
<<<<<<< HEAD
|
|
||||||
// 保存
|
// 保存
|
||||||
wxUserInfo = await this.saveWxInfo(wxUserInfo, 0);
|
wxUserInfo = await this.saveWxInfo(wxUserInfo, 0);
|
||||||
return await this.wxLoginToken(wxUserInfo);
|
return await this.wxLoginToken(wxUserInfo);
|
||||||
=======
|
|
||||||
wxUserInfo = await this.saveWxInfo(wxUserInfo);
|
|
||||||
return this.wxLoginToken(wxUserInfo, 0);
|
|
||||||
>>>>>>> 6a9a421c61366184f92aea3f742a577d944d2879
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信登录 获得token
|
* 微信登录 获得token
|
||||||
* @param wxUserInfo 微信用户信息
|
* @param wxUserInfo 微信用户信息
|
||||||
* @param loginType 登录方式 0-小程序 1-公众号 2-H5
|
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
async wxLoginToken(wxUserInfo, loginType) {
|
async wxLoginToken(wxUserInfo) {
|
||||||
const unionid = wxUserInfo.unionid ? wxUserInfo.unionid : wxUserInfo.openid;
|
const unionid = wxUserInfo.unionid ? wxUserInfo.unionid : wxUserInfo.openid;
|
||||||
let userInfo: UserInfoEntity = await this.userInfoEntity.findOneBy({ unionid });
|
let userInfo: any = await this.userInfoEntity.findOneBy({ unionid });
|
||||||
if (!userInfo) {
|
if (!userInfo) {
|
||||||
<<<<<<< HEAD
|
|
||||||
const avatarUrl = await this.file.downAndUpload(
|
const avatarUrl = await this.file.downAndUpload(
|
||||||
wxUserInfo.avatarUrl,
|
wxUserInfo.avatarUrl,
|
||||||
uuid() + '.png'
|
uuid() + '.png'
|
||||||
@ -204,22 +183,7 @@ export class UserLoginService extends BaseService {
|
|||||||
throw new CoolCommException(
|
throw new CoolCommException(
|
||||||
'刷新token失败,请检查refreshToken是否正确或过期'
|
'刷新token失败,请检查refreshToken是否正确或过期'
|
||||||
);
|
);
|
||||||
=======
|
|
||||||
userInfo = new UserInfoEntity();
|
|
||||||
Object.assign(userInfo, {
|
|
||||||
unionid,
|
|
||||||
loginType,
|
|
||||||
...wxUserInfo
|
|
||||||
});
|
|
||||||
await this.userInfoEntity.insert(userInfo);
|
|
||||||
>>>>>>> 6a9a421c61366184f92aea3f742a577d944d2879
|
|
||||||
}
|
}
|
||||||
if (userInfo.status === 0) {
|
|
||||||
throw new CoolCommException('您已违规被禁用');
|
|
||||||
}
|
|
||||||
// 更新登录时间
|
|
||||||
await this.userInfoEntity.save(Object.assign(userInfo, wxUserInfo));
|
|
||||||
return this.token({ userId: userInfo.id });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2,10 +2,6 @@ import { Config, Provide } from '@midwayjs/decorator';
|
|||||||
import { BaseService, CoolCommException } from '@cool-midway/core';
|
import { BaseService, CoolCommException } from '@cool-midway/core';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import * as crypto from 'crypto';
|
import * as crypto from 'crypto';
|
||||||
import * as _ from 'lodash';
|
|
||||||
|
|
||||||
// 微信请求域名
|
|
||||||
const wxBaseUrl = 'https://api.weixin.qq.com';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信
|
* 微信
|
||||||
@ -30,8 +26,9 @@ export class UserWxService extends BaseService {
|
|||||||
* @param secret
|
* @param secret
|
||||||
*/
|
*/
|
||||||
public async getWxToken(type = 'mp') {
|
public async getWxToken(type = 'mp') {
|
||||||
|
//@ts-ignore
|
||||||
const conf = this.config.wx[type];
|
const conf = this.config.wx[type];
|
||||||
return await axios.get(wxBaseUrl + '/cgi-bin/token', {
|
return await axios.get('https://api.weixin.qq.com/cgi-bin/token', {
|
||||||
params: {
|
params: {
|
||||||
grant_type: 'client_credential',
|
grant_type: 'client_credential',
|
||||||
appid: conf.appid,
|
appid: conf.appid,
|
||||||
@ -46,7 +43,7 @@ export class UserWxService extends BaseService {
|
|||||||
*/
|
*/
|
||||||
async openOrMpUserInfo(token) {
|
async openOrMpUserInfo(token) {
|
||||||
return await axios
|
return await axios
|
||||||
.get(wxBaseUrl + '/sns/userinfo', {
|
.get('https://api.weixin.qq.com/sns/userinfo', {
|
||||||
params: {
|
params: {
|
||||||
access_token: token.access_token,
|
access_token: token.access_token,
|
||||||
openid: token.openid,
|
openid: token.openid,
|
||||||
@ -59,13 +56,13 @@ export class UserWxService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得token
|
* 获得token嗯
|
||||||
* @param code
|
* @param code
|
||||||
* @param conf
|
* @param conf
|
||||||
*/
|
*/
|
||||||
async openOrMpToken(code, conf) {
|
async openOrMpToken(code, conf) {
|
||||||
const result = await axios.get(
|
const result = await axios.get(
|
||||||
wxBaseUrl + '/sns/oauth2/access_token',
|
'https://api.weixin.qq.com/sns/oauth2/access_token',
|
||||||
{
|
{
|
||||||
params: {
|
params: {
|
||||||
appid: conf.appid,
|
appid: conf.appid,
|
||||||
@ -86,7 +83,7 @@ export class UserWxService extends BaseService {
|
|||||||
async miniSession(code) {
|
async miniSession(code) {
|
||||||
const { appid, secret } = this.config.wx.mini;
|
const { appid, secret } = this.config.wx.mini;
|
||||||
const result = await axios.get(
|
const result = await axios.get(
|
||||||
wxBaseUrl + '/sns/jscode2session',
|
'https://api.weixin.qq.com/sns/jscode2session',
|
||||||
{
|
{
|
||||||
params: {
|
params: {
|
||||||
appid,
|
appid,
|
||||||
@ -116,10 +113,15 @@ export class UserWxService extends BaseService {
|
|||||||
iv,
|
iv,
|
||||||
session.session_key
|
session.session_key
|
||||||
);
|
);
|
||||||
delete info['watermark'];
|
if (info) {
|
||||||
info.openid = session['openid'];
|
delete info['watermark'];
|
||||||
info.unionid = session['unionid'];
|
return {
|
||||||
return _.pickBy({ ...info });
|
...info,
|
||||||
|
openid: session['openid'],
|
||||||
|
unionid: session['unionid'],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -147,11 +149,17 @@ export class UserWxService extends BaseService {
|
|||||||
encryptedData = Buffer.from(encryptedData, 'base64');
|
encryptedData = Buffer.from(encryptedData, 'base64');
|
||||||
iv = Buffer.from(iv, 'base64');
|
iv = Buffer.from(iv, 'base64');
|
||||||
try {
|
try {
|
||||||
|
// 解密
|
||||||
const decipher = crypto.createDecipheriv('aes-128-cbc', sessionKey, iv);
|
const decipher = crypto.createDecipheriv('aes-128-cbc', sessionKey, iv);
|
||||||
|
// 设置自动 padding 为 true,删除填充补位
|
||||||
decipher.setAutoPadding(true);
|
decipher.setAutoPadding(true);
|
||||||
|
// @ts-ignore
|
||||||
let decoded = decipher.update(encryptedData, 'binary', 'utf8');
|
let decoded = decipher.update(encryptedData, 'binary', 'utf8');
|
||||||
|
// @ts-ignore
|
||||||
decoded += decipher.final('utf8');
|
decoded += decipher.final('utf8');
|
||||||
return JSON.parse(decoded);
|
// @ts-ignore
|
||||||
|
decoded = JSON.parse(decoded);
|
||||||
|
return decoded;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new CoolCommException('获得信息失败');
|
throw new CoolCommException('获得信息失败');
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user