import { Provide, Scope, ScopeEnum } from "@midwayjs/decorator"; import AppError from "@/utils/common/AppError"; import ErrNo from "@/constants/errorEnum"; import BaseService from "@/apis/service/base"; import ModelName from "@/constants/dbEnum"; import { UserDocument } from "@/apis/model/User"; // 单例 @Provide() @Scope(ScopeEnum.Singleton) export default class UserService extends BaseService { private get UserModel() { return this.getModel(ModelName.User); } async login(username: string, password: string) { if (username === "zhangsan" && password === "123") { // 测试 redis await this.redisService.set("foo", "bar123"); const result = await this.redisService.get("foo"); // 测试 cache await this.cacheService.set("user-123", "张三"); const name = await this.cacheService.get("user-123"); return `userID:zhangsan-123_${result}_${name}`; } throw new AppError("用户名或密码错误", ErrNo.LOGIN_FAIL); } async test() { const doc = new this.UserModel({ name: "Bill", email: "bill@initech.com", avatar: "https://i.imgur.com/dM7Thhn.png", }); await doc.save(); } }