| | |
| | | import { InjectEntityModel } from '@midwayjs/typeorm'; |
| | | import { Equal, Repository } from 'typeorm'; |
| | | import { UserInfoEntity } from '../entity/info'; |
| | | import { BasicdataSorterEntity } from '../../basicdata/entity/sorter'; |
| | | import { UserWxService } from './wx'; |
| | | import * as jwt from 'jsonwebtoken'; |
| | | import { UserWxEntity } from '../entity/wx'; |
| | |
| | | export class UserLoginService extends BaseService { |
| | | @InjectEntityModel(UserInfoEntity) |
| | | userInfoEntity: Repository<UserInfoEntity>; |
| | | |
| | | @InjectEntityModel(BasicdataSorterEntity) |
| | | basicdataSorterEntity: Repository<BasicdataSorterEntity>; |
| | | |
| | | @InjectEntityModel(UserWxEntity) |
| | | userWxEntity: Repository<UserWxEntity>; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 分拣人员-密码登录 |
| | | * @param phone |
| | | * @param password |
| | | */ |
| | | async sorterPassword(phone, password) { |
| | | const user = await this.basicdataSorterEntity.findOneBy({ phone }); |
| | | |
| | | if (user && user.password == md5(password)) { |
| | | |
| | | const info = { |
| | | id: user.businessId, |
| | | } |
| | | const { expire, refreshExpire } = this.jwtConfig; |
| | | return { |
| | | expire, |
| | | token: await this.generateSorterToken(info), |
| | | refreshExpire, |
| | | refreshToken: await this.generateSorterToken(info, true), |
| | | }; |
| | | |
| | | } else { |
| | | throw new CoolCommException('账号或密码错误'); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获得token |
| | | * @param info |
| | | * @returns |
| | |
| | | expiresIn: isRefresh ? refreshExpire : expire, |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 生成token |
| | | * @param tokenInfo 信息 |
| | | * @param roleIds 角色集合 |
| | | */ |
| | | async generateSorterToken(info, isRefresh = false) { |
| | | const { expire, refreshExpire, secret } = this.jwtConfig; |
| | | const user = await this.basicdataSorterEntity.findOneBy({ businessId: Equal(info.id) }); |
| | | const tokenInfo = { |
| | | isRefresh, |
| | | ...info, |
| | | tenantId: user?.tenantId, |
| | | }; |
| | | return jwt.sign(tokenInfo, secret, { |
| | | expiresIn: isRefresh ? refreshExpire : expire, |
| | | }); |
| | | } |
| | | } |