From c04839231bfb6f30d312389b596e15df5ae6fb9d Mon Sep 17 00:00:00 2001 From: snowgitea Date: Wed, 1 Jul 2026 17:57:42 +0800 Subject: [PATCH] chore: src/modules/user/service/info.ts --- src/modules/user/service/info.ts | 124 +++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/modules/user/service/info.ts diff --git a/src/modules/user/service/info.ts b/src/modules/user/service/info.ts new file mode 100644 index 0000000..62f2017 --- /dev/null +++ b/src/modules/user/service/info.ts @@ -0,0 +1,124 @@ +import { BaseService, CoolCommException } from '@cool-midway/core'; +import { Inject, Provide } from '@midwayjs/core'; +import { InjectEntityModel } from '@midwayjs/typeorm'; +import * as md5 from 'md5'; +import { Equal, Repository } from 'typeorm'; +import { v1 as uuid } from 'uuid'; +import { PluginService } from '../../plugin/service/info'; +import { UserInfoEntity } from '../entity/info'; +import { UserSmsService } from './sms'; +import { UserWxService } from './wx'; + +/** + * 用户信息 + */ +@Provide() +export class UserInfoService extends BaseService { + @InjectEntityModel(UserInfoEntity) + userInfoEntity: Repository; + + @Inject() + pluginService: PluginService; + + @Inject() + userSmsService: UserSmsService; + + @Inject() + userWxService: UserWxService; + + /** + * 绑定小程序手机号 + * @param userId + * @param code + * @param encryptedData + * @param iv + */ + async miniPhone(userId: number, code: any, encryptedData: any, iv: any) { + const phone = await this.userWxService.miniPhone(code, encryptedData, iv); + await this.userInfoEntity.update({ id: Equal(userId) }, { phone }); + return phone; + } + + /** + * 获取用户信息 + * @param id + * @returns + */ + async person(id) { + const info = await this.userInfoEntity.findOneBy({ id: Equal(id) }); + delete info.password; + return info; + } + + /** + * 注销 + * @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) { + const info = await this.person(id); + if (!info) throw new CoolCommException('用户不存在'); + try { + // 修改了头像要重新处理 + if (param.avatarUrl && info.avatarUrl != param.avatarUrl) { + const file = await this.pluginService.getInstance('upload'); + param.avatarUrl = await file.downAndUpload( + param.avatarUrl, + uuid() + '.png' + ); + } + } catch (err) {} + try { + return await this.userInfoEntity.update({ id }, param); + } catch (err) { + 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) }); + } + + /** + * 绑定手机号 + * @param userId + * @param phone + * @param code + */ + async bindPhone(userId, phone, code) { + const check = await this.userSmsService.checkCode(phone, code); + if (!check) { + throw new CoolCommException('验证码错误'); + } + await this.userInfoEntity.update({ id: userId }, { phone }); + } +}