mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2024-11-01 22:20:30 +08:00
新增密码登录方式,去除wps文件空间
This commit is contained in:
parent
f06fe943d3
commit
9a7f1e73ba
@ -15,12 +15,4 @@ import { SpaceInfoService } from '../../service/info';
|
||||
fieldEq: ['type', 'classifyId'],
|
||||
},
|
||||
})
|
||||
export class BaseAppSpaceInfoController extends BaseController {
|
||||
@Config('module.space.wps')
|
||||
config;
|
||||
|
||||
@Get('/getConfig', { summary: '获得WPS配置' })
|
||||
async getConfig() {
|
||||
return this.ok({ appId: this.config.appId });
|
||||
}
|
||||
}
|
||||
export class BaseAppSpaceInfoController extends BaseController {}
|
||||
|
@ -1,65 +0,0 @@
|
||||
import {
|
||||
ALL,
|
||||
Body,
|
||||
Files,
|
||||
Get,
|
||||
Inject,
|
||||
Param,
|
||||
Post,
|
||||
Provide,
|
||||
Query,
|
||||
} from '@midwayjs/decorator';
|
||||
import { CoolController, BaseController } from '@cool-midway/core';
|
||||
import { SpaceWpsService } from '../../service/wps';
|
||||
|
||||
/**
|
||||
* wps回调
|
||||
*/
|
||||
@Provide()
|
||||
@CoolController('/wps')
|
||||
export class AppSpaceWpsController extends BaseController {
|
||||
@Inject()
|
||||
spaceWpsService: SpaceWpsService;
|
||||
|
||||
@Get('/v3/3rd/files/:file_id', { summary: '获取文件信息' })
|
||||
async files(@Param('file_id') file_id: string) {
|
||||
return await this.spaceWpsService.getFiles(file_id);
|
||||
}
|
||||
|
||||
@Get('/v3/3rd/files/:file_id/download', { summary: '获取文件下载地址' })
|
||||
async download(@Param('file_id') file_id: string) {
|
||||
return await this.spaceWpsService.download(file_id);
|
||||
}
|
||||
|
||||
@Get('/v3/3rd/files/:file_id/permission', { summary: '获取文档用户权限' })
|
||||
async permission(@Param('file_id') file_id: string) {
|
||||
return await this.spaceWpsService.permission(file_id);
|
||||
}
|
||||
|
||||
@Post('/v3/3rd/files/:file_id/upload', { summary: '文件上传' })
|
||||
async upload(@Param('file_id') file_id: string, @Files() files) {
|
||||
return await this.spaceWpsService.upload(file_id, files);
|
||||
}
|
||||
|
||||
@Get('/v3/3rd/files/:file_id/upload/prepare', { summary: '准备上传阶段' })
|
||||
async uploadPrepare(@Param('file_id') file_id: string) {
|
||||
return await this.spaceWpsService.uploadPrepare(file_id);
|
||||
}
|
||||
|
||||
@Post('/v3/3rd/files/:file_id/upload/address', { summary: '获取上传地址' })
|
||||
async uploadAddress(@Param('file_id') file_id: string, @Body(ALL) body) {
|
||||
return await this.spaceWpsService.uploadAddress(file_id, body);
|
||||
}
|
||||
|
||||
@Post('/v3/3rd/files/:file_id/upload/complete', {
|
||||
summary: '上传完成后,回调通知上传结果',
|
||||
})
|
||||
async uploadComplete(@Param('file_id') file_id: string, @Body(ALL) body) {
|
||||
return await this.spaceWpsService.uploadComplete(file_id, body);
|
||||
}
|
||||
|
||||
@Get('/v3/3rd/users', { summary: '用户信息' })
|
||||
async users(@Query('user_ids') user_ids: string[]) {
|
||||
return await this.spaceWpsService.users(user_ids);
|
||||
}
|
||||
}
|
@ -1,271 +0,0 @@
|
||||
import { SpaceTypeEntity } from './../entity/type';
|
||||
import { SpaceInfoEntity } from './../entity/info';
|
||||
import { Config, Inject, Provide } from '@midwayjs/decorator';
|
||||
import { BaseService } from '@cool-midway/core';
|
||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||
import { In, Repository } from 'typeorm';
|
||||
import * as _ from 'lodash';
|
||||
import { BaseSysUserService } from '../../base/service/sys/user';
|
||||
import * as moment from 'moment';
|
||||
import * as fs from 'fs';
|
||||
import { CacheManager } from '@midwayjs/cache';
|
||||
import * as jwt from 'jsonwebtoken';
|
||||
import { BaseSysUserEntity } from '../../base/entity/sys/user';
|
||||
import { CoolFile } from '@cool-midway/file';
|
||||
|
||||
/**
|
||||
* 使用wps在线文档
|
||||
*/
|
||||
@Provide()
|
||||
export class SpaceWpsService extends BaseService {
|
||||
@InjectEntityModel(SpaceInfoEntity)
|
||||
spaceInfoEntity: Repository<SpaceInfoEntity>;
|
||||
|
||||
@InjectEntityModel(SpaceTypeEntity)
|
||||
spaceTypeEntity: Repository<SpaceTypeEntity>;
|
||||
|
||||
@Inject()
|
||||
baseSysUserService: BaseSysUserService;
|
||||
|
||||
@InjectEntityModel(BaseSysUserEntity)
|
||||
baseSysUserEntity: Repository<BaseSysUserEntity>;
|
||||
|
||||
@Inject()
|
||||
ctx;
|
||||
|
||||
@Inject()
|
||||
coolFile: CoolFile;
|
||||
|
||||
@Inject()
|
||||
cacheManager: CacheManager;
|
||||
|
||||
@Config('module.base')
|
||||
jwtConfig;
|
||||
|
||||
/**
|
||||
* 根据filedId获取数据库文件信息
|
||||
* @param fileId
|
||||
*/
|
||||
async getFileInfo(fileId) {
|
||||
return await this.spaceInfoEntity.findOneBy({
|
||||
fileId,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验文档权限
|
||||
*/
|
||||
async verifyUser() {
|
||||
const token = this.ctx.request.header['x-weboffice-token'];
|
||||
let tokenData;
|
||||
try {
|
||||
tokenData = jwt.verify(token, this.jwtConfig.jwt.secret);
|
||||
} catch (err) {}
|
||||
let userInfo;
|
||||
if (tokenData) {
|
||||
userInfo = await this.baseSysUserEntity.findOneBy({
|
||||
id: tokenData.userId,
|
||||
});
|
||||
}
|
||||
if (_.isEmpty(userInfo)) {
|
||||
throw new Error('用户不存在');
|
||||
}
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得文件信息
|
||||
* @param file_id
|
||||
*/
|
||||
async getFiles(file_id: string) {
|
||||
const userInfo = await this.verifyUser();
|
||||
const fileInfo = await this.getFileInfo(file_id);
|
||||
if (_.isEmpty(fileInfo)) {
|
||||
return {
|
||||
code: '40004',
|
||||
msg: '文档不存在',
|
||||
};
|
||||
}
|
||||
return {
|
||||
code: 0,
|
||||
data: {
|
||||
id: fileInfo.fileId,
|
||||
name: fileInfo.name,
|
||||
version: fileInfo.version,
|
||||
size: fileInfo.size,
|
||||
create_time: moment(fileInfo.createTime).valueOf(),
|
||||
modify_time: moment(fileInfo.updateTime).valueOf(),
|
||||
creator_id: String(userInfo.id),
|
||||
modifier_id: String(userInfo.id),
|
||||
attrs: { _w_third_file_id: fileInfo.fileId },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件下载地址
|
||||
* @param file_id
|
||||
* @returns
|
||||
*/
|
||||
async download(file_id: string) {
|
||||
await this.verifyUser();
|
||||
const fileInfo = await this.getFileInfo(file_id);
|
||||
return {
|
||||
code: 0,
|
||||
data: {
|
||||
url: fileInfo.url,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文档用户权限
|
||||
* @param file_id
|
||||
* @returns
|
||||
*/
|
||||
async permission(file_id: string) {
|
||||
const userInfo = await this.verifyUser();
|
||||
return {
|
||||
code: 0,
|
||||
data: {
|
||||
user_id: String(userInfo.id),
|
||||
read: 1,
|
||||
update: 1,
|
||||
download: 1,
|
||||
rename: 0,
|
||||
history: 0,
|
||||
copy: 1,
|
||||
print: 1,
|
||||
saveas: 1,
|
||||
comment: 1,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param file_id
|
||||
* @param files
|
||||
* @param body
|
||||
* @returns
|
||||
*/
|
||||
async upload(file_id: string, files) {
|
||||
const userInfo = await this.verifyUser();
|
||||
const fileInfo = await this.getFileInfo(file_id);
|
||||
const data = files[0].data;
|
||||
const stat = fs.statSync(data);
|
||||
await this.coolFile.uploadWithKey(files[0], fileInfo.key);
|
||||
|
||||
fileInfo.version++;
|
||||
fileInfo.size = stat.size;
|
||||
await this.spaceInfoEntity.save(fileInfo);
|
||||
return {
|
||||
code: 0,
|
||||
data: {
|
||||
id: fileInfo.fileId,
|
||||
name: fileInfo.name,
|
||||
version: fileInfo.version,
|
||||
size: fileInfo.size,
|
||||
create_time: moment(fileInfo.createTime).valueOf(),
|
||||
modify_time: moment(fileInfo.updateTime).valueOf(),
|
||||
creator_id: String(userInfo.id),
|
||||
modifier_id: String(userInfo.id),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户信息
|
||||
* @param user_ids
|
||||
* @returns
|
||||
*/
|
||||
async users(user_ids: string[]) {
|
||||
const userInfos = await this.baseSysUserEntity.find({
|
||||
where: {
|
||||
id: In(user_ids),
|
||||
},
|
||||
});
|
||||
return {
|
||||
code: 0,
|
||||
data: userInfos.map(userInfo => {
|
||||
return {
|
||||
id: String(userInfo.id),
|
||||
name: userInfo.name,
|
||||
avatar_url: userInfo.headImg,
|
||||
};
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 准备上传阶段
|
||||
* @param file_id
|
||||
* @returns
|
||||
*/
|
||||
async uploadPrepare(file_id: string) {
|
||||
console.log('准备上传阶段:' + file_id);
|
||||
return {
|
||||
code: 0,
|
||||
data: {
|
||||
digest_types: ['sha1'],
|
||||
},
|
||||
msg: '',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上传地址
|
||||
* @param file_id
|
||||
* @param body
|
||||
* @returns
|
||||
*/
|
||||
async uploadAddress(file_id: string, body: any) {
|
||||
console.log('获取上传地址:' + file_id);
|
||||
console.log(body);
|
||||
const fileInfo = await this.getFileInfo(file_id);
|
||||
const uploadRes = await this.coolFile.downAndUpload(
|
||||
body.url,
|
||||
fileInfo.name
|
||||
);
|
||||
if (!uploadRes) {
|
||||
return {
|
||||
code: '41001',
|
||||
msg: '文件未正确上传',
|
||||
};
|
||||
}
|
||||
return {
|
||||
code: 0,
|
||||
data: {
|
||||
method: 'PUT',
|
||||
url: uploadRes,
|
||||
},
|
||||
msg: '',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传完成后,回调通知上传结果
|
||||
* @param file_id
|
||||
* @param body
|
||||
* @returns
|
||||
*/
|
||||
async uploadComplete(file_id: string, body: any) {
|
||||
console.log('上传完成后,回调通知上传结果:' + file_id);
|
||||
console.log(body);
|
||||
const userInfo = await this.baseSysUserService.person();
|
||||
const fileInfo = await this.getFileInfo(file_id);
|
||||
return {
|
||||
code: 0,
|
||||
data: {
|
||||
id: fileInfo.fileId,
|
||||
name: fileInfo.name,
|
||||
version: fileInfo.version,
|
||||
size: fileInfo.size,
|
||||
create_time: moment(fileInfo.createTime).valueOf(),
|
||||
modify_time: moment(fileInfo.updateTime).valueOf(),
|
||||
creator_id: String(userInfo.id),
|
||||
modifier_id: String(userInfo.id),
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
@ -28,4 +28,19 @@ export class AppUserInfoController extends BaseController {
|
||||
await this.userInfoService.updatePerson(this.ctx.user.id, body)
|
||||
);
|
||||
}
|
||||
|
||||
@Post('/updatePassword', { summary: '更新用户密码' })
|
||||
async updatePassword(
|
||||
@Body('password') password: string,
|
||||
@Body('code') code: string
|
||||
) {
|
||||
await this.userInfoService.updatePassword(this.ctx.user.id, password, code);
|
||||
return this.ok();
|
||||
}
|
||||
|
||||
@Post('/logoff', { summary: '注销' })
|
||||
async logoff() {
|
||||
await this.userInfoService.logoff(this.ctx.user.id);
|
||||
return this.ok();
|
||||
}
|
||||
}
|
||||
|
@ -68,4 +68,13 @@ export class AppUserLoginController extends BaseController {
|
||||
public async refreshToken(@Body('refreshToken') refreshToken) {
|
||||
return this.ok(await this.userLoginService.refreshToken(refreshToken));
|
||||
}
|
||||
|
||||
@CoolTag(TagTypes.IGNORE_TOKEN)
|
||||
@Post('/password', { summary: '密码登录' })
|
||||
async password(
|
||||
@Body('phone') phone: string,
|
||||
@Body('password') password: string
|
||||
) {
|
||||
return this.ok(await this.userLoginService.password(phone, password));
|
||||
}
|
||||
}
|
||||
|
@ -23,9 +23,12 @@ export class UserInfoEntity extends BaseEntity {
|
||||
@Column({ comment: '性别 0-未知 1-男 2-女', default: 0 })
|
||||
gender: number;
|
||||
|
||||
@Column({ comment: '状态 0-禁用 1-正常', default: 1 })
|
||||
@Column({ comment: '状态 0-禁用 1-正常 2-已注销', default: 1 })
|
||||
status: number;
|
||||
|
||||
@Column({ comment: '登录方式 0-小程序 1-公众号 2-H5', default: 0 })
|
||||
loginType: number;
|
||||
|
||||
@Column({ comment: '密码', nullable: true })
|
||||
password: string;
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ import { Repository } from 'typeorm';
|
||||
import { UserInfoEntity } from '../entity/info';
|
||||
import { CoolFile } from '@cool-midway/file';
|
||||
import { v1 as uuid } from 'uuid';
|
||||
import { UserSmsService } from './sms';
|
||||
import * as md5 from 'md5';
|
||||
|
||||
/**
|
||||
* 用户信息
|
||||
@ -17,6 +19,9 @@ export class UserInfoService extends BaseService {
|
||||
@Inject()
|
||||
file: CoolFile;
|
||||
|
||||
@Inject()
|
||||
userSmsService: UserSmsService;
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
* @param id
|
||||
@ -26,6 +31,29 @@ export class UserInfoService extends BaseService {
|
||||
return await this.userInfoEntity.findOneBy({ id });
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销
|
||||
* @param userId
|
||||
*/
|
||||
async logoff(userId: number) {
|
||||
await this.userInfoEntity.update(
|
||||
{ id: userId },
|
||||
{
|
||||
status: 2,
|
||||
phone: null,
|
||||
unionid: null,
|
||||
nickName: `已注销-00${userId}`,
|
||||
avatarUrl: null,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户信息
|
||||
* @param id
|
||||
* @param param
|
||||
* @returns
|
||||
*/
|
||||
async updatePerson(id, param) {
|
||||
try {
|
||||
const info = await this.person(id);
|
||||
@ -41,4 +69,19 @@ export class UserInfoService extends BaseService {
|
||||
throw new CoolCommException('更新失败,参数错误或者手机号已存在');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新密码
|
||||
* @param userId
|
||||
* @param password
|
||||
* @param 验证码
|
||||
*/
|
||||
async updatePassword(userId, password, code) {
|
||||
const user = await this.userInfoEntity.findOneBy({ id: userId });
|
||||
const check = await this.userSmsService.checkCode(user.phone, code);
|
||||
if (!check) {
|
||||
throw new CoolCommException('验证码错误');
|
||||
}
|
||||
await this.userInfoEntity.update(user.id, { password: md5(password) });
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import { CoolFile } from '@cool-midway/file';
|
||||
import { BaseSysLoginService } from '../../base/service/sys/login';
|
||||
import { UserSmsService } from './sms';
|
||||
import { v1 as uuid } from 'uuid';
|
||||
import * as md5 from 'md5';
|
||||
|
||||
/**
|
||||
* 登录
|
||||
@ -187,6 +188,23 @@ export class UserLoginService extends BaseService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 密码登录
|
||||
* @param phone
|
||||
* @param password
|
||||
*/
|
||||
async password(phone, password) {
|
||||
const user = await this.userInfoEntity.findOneBy({ phone });
|
||||
|
||||
if (user && user.password == md5(password)) {
|
||||
return this.token({
|
||||
id: user.id,
|
||||
});
|
||||
} else {
|
||||
throw new CoolCommException('账号或密码错误');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得token
|
||||
* @param info
|
||||
|
Loading…
Reference in New Issue
Block a user