后台:礼包码
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { Application, BackendSession, HandlerService, } from "pinus";
|
import { Application, BackendSession, HandlerService, } from "pinus";
|
||||||
import { STATUS, CONSUME_TYPE, DEBUG_MAGIC_WORD } from "../../../consts";
|
import { STATUS, CONSUME_TYPE, DEBUG_MAGIC_WORD, GIFT_GENERATE_TYPE } from "../../../consts";
|
||||||
import { RewardInter } from "../../../pubUtils/interface";
|
import { RewardInter } from "../../../pubUtils/interface";
|
||||||
|
|
||||||
import { resResult } from "../../../pubUtils/util";
|
import { resResult } from "../../../pubUtils/util";
|
||||||
@@ -12,7 +12,7 @@ import { getAp, setAp, setApBuyTimes } from "../../../services/actionPointServic
|
|||||||
import { ActionPointModel } from "../../../db/ActionPoint";
|
import { ActionPointModel } from "../../../db/ActionPoint";
|
||||||
import { GiftCodeDetailModel } from "../../../db/GiftCodeDetail";
|
import { GiftCodeDetailModel } from "../../../db/GiftCodeDetail";
|
||||||
import { GiftCodeType, GiftCodeModel } from "../../../db/GiftCode";
|
import { GiftCodeType, GiftCodeModel } from "../../../db/GiftCode";
|
||||||
import UserGiftCode, { UserGiftCodeModel } from "../../../db/UserGiftCode";
|
import { nowSeconds } from "../../../pubUtils/timeUtil";
|
||||||
|
|
||||||
export default function (app: Application) {
|
export default function (app: Application) {
|
||||||
new HandlerService(app, {});
|
new HandlerService(app, {});
|
||||||
@@ -140,6 +140,7 @@ export class ItemHandler {
|
|||||||
public async useGiftCode(msg: { code: string }, session: BackendSession) {
|
public async useGiftCode(msg: { code: string }, session: BackendSession) {
|
||||||
const roleId: string = session.get('roleId');
|
const roleId: string = session.get('roleId');
|
||||||
const roleName: string = session.get('roleName');
|
const roleName: string = session.get('roleName');
|
||||||
|
const serverId: number = session.get('serverId');
|
||||||
const sid: string = session.get('sid');
|
const sid: string = session.get('sid');
|
||||||
const { code } = msg;
|
const { code } = msg;
|
||||||
|
|
||||||
@@ -147,24 +148,20 @@ export class ItemHandler {
|
|||||||
if(!giftCodeDetail) {
|
if(!giftCodeDetail) {
|
||||||
return resResult(STATUS.GIFT_CODE_NOT_FOUND);
|
return resResult(STATUS.GIFT_CODE_NOT_FOUND);
|
||||||
}
|
}
|
||||||
let giftCode = <GiftCodeType>giftCodeDetail.giftCode;
|
let giftCode = await GiftCodeModel.findByGiftId(giftCodeDetail.giftId);
|
||||||
if(!giftCode) {
|
if(!giftCode) {
|
||||||
return resResult(STATUS.GIFT_CODE_NOT_FOUND);
|
return resResult(STATUS.GIFT_CODE_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!giftCode.isEnable) return resResult(STATUS.GIFT_CODE_HAS_EXPIRED);
|
||||||
|
if (giftCode.beginTime > nowSeconds()) return resResult(STATUS.GIFT_CODE_NOT_START);
|
||||||
|
if (giftCode.endTime < nowSeconds()) return resResult(STATUS.GIFT_CODE_HAS_EXPIRED);
|
||||||
|
|
||||||
if (giftCode.isLimit && giftCodeDetail.usedNum >= giftCode.count) {
|
if (giftCodeDetail.roleIds.indexOf(roleId) != -1) {
|
||||||
return resResult(STATUS.GIFT_CODE_USED_NUM_MAX);
|
|
||||||
}
|
|
||||||
if (giftCode.beginTime > new Date()) return resResult(STATUS.GIFT_CODE_NOT_START);
|
|
||||||
if (giftCode.endTime < new Date()) return resResult(STATUS.GIFT_CODE_HAS_EXPIRED);
|
|
||||||
|
|
||||||
let userGiftCode = await UserGiftCodeModel.findByCode(roleId, code);
|
|
||||||
if (userGiftCode) {
|
|
||||||
return resResult(STATUS.YOU_HAVE_USED_THIS_CODE);
|
return resResult(STATUS.YOU_HAVE_USED_THIS_CODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
await UserGiftCode.createCode(roleId, code, giftCode);
|
await GiftCodeDetailModel.increaseUsedNum(code, roleId, roleName, serverId);
|
||||||
await GiftCodeDetailModel.increaseUsedNum(code);
|
|
||||||
await GiftCodeModel.increaseUsedNum(giftCode.id);
|
await GiftCodeModel.increaseUsedNum(giftCode.id);
|
||||||
|
|
||||||
let goods = await addItems(roleId, roleName, sid, giftCode.goods);
|
let goods = await addItems(roleId, roleName, sid, giftCode.goods);
|
||||||
|
|||||||
@@ -148,37 +148,34 @@ export default class UserController extends Controller {
|
|||||||
ctx.body = await ctx.service.users.getGiftCodeList(page, pageSize, sortField, sortOrder, form);
|
ctx.body = await ctx.service.users.getGiftCodeList(page, pageSize, sortField, sortOrder, form);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async createAndGenerateGift() {
|
||||||
|
const { ctx } = this;
|
||||||
|
const params = ctx.request.body;
|
||||||
|
ctx.body = await ctx.service.users.createAndGenerateGift(params, params.generateCnt);
|
||||||
|
}
|
||||||
|
|
||||||
public async updateGiftCode() {
|
public async updateGiftCode() {
|
||||||
const { ctx } = this;
|
const { ctx } = this;
|
||||||
const { id, values } = ctx.request.body;
|
const params = ctx.request.body;
|
||||||
ctx.body = await ctx.service.users.updateGiftCode(id, values);
|
ctx.body = await ctx.service.users.updateGiftCode(params.id, params.values);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async generateGiftCode() {
|
public async generateGiftCode() {
|
||||||
const { ctx } = this;
|
const { ctx } = this;
|
||||||
const { id, generateType, code, generateNum, codeLen } = ctx.request.body;
|
const params = ctx.request.body;
|
||||||
if(generateType == 1) {
|
ctx.body = await ctx.service.users.generateGiftCode(params.id, params.count);
|
||||||
if(!generateNum) {
|
|
||||||
ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else if (generateType == 2) {
|
|
||||||
if(!code) {
|
|
||||||
ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.body = await ctx.service.users.generateGiftCode(id, generateType, code, generateNum, codeLen);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getGiftCodeDetails() {
|
public async cancelGiftCode() {
|
||||||
|
const { ctx } = this;
|
||||||
|
const params = ctx.request.body;
|
||||||
|
ctx.body = await ctx.service.users.cancelGiftCode(params.id, !!params.isEnable);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getGiftCodeDetailFile() {
|
||||||
const { ctx } = this;
|
const { ctx } = this;
|
||||||
const { id } = ctx.params;
|
const { id } = ctx.params;
|
||||||
ctx.body = await ctx.service.users.getGiftCodeDetails(parseInt(id));
|
ctx.body = await ctx.service.users.getGiftCodeDetailFile(parseInt(id));
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,10 +45,11 @@ export default (app: Application) => {
|
|||||||
router.post('/api/users/deleteitem', tokenParser, controller.users.deleteItem);
|
router.post('/api/users/deleteitem', tokenParser, controller.users.deleteItem);
|
||||||
router.post('/api/users/setitemcount', tokenParser, controller.users.setItemCount);
|
router.post('/api/users/setitemcount', tokenParser, controller.users.setItemCount);
|
||||||
router.post('/api/users/getgiftcodelist', controller.users.getGiftCodeList);
|
router.post('/api/users/getgiftcodelist', controller.users.getGiftCodeList);
|
||||||
|
router.post('/api/users/createandgenerategift', controller.users.createAndGenerateGift)
|
||||||
router.post('/api/users/updategiftcode', controller.users.updateGiftCode);
|
router.post('/api/users/updategiftcode', controller.users.updateGiftCode);
|
||||||
router.post('/api/users/generategiftcode', controller.users.generateGiftCode);
|
router.post('/api/users/generategiftcode', controller.users.generateGiftCode);
|
||||||
// router.post('/api/users/delgiftCode', controller.users.delGiftCode);
|
router.post('/api/users/cancelgiftCode', controller.users.cancelGiftCode);
|
||||||
router.get('/api/users/getgiftcodedetails/:id/:filename', controller.users.getGiftCodeDetails);
|
router.get('/api/users/getgiftcodedetailfile/:id/:filename', controller.users.getGiftCodeDetailFile);
|
||||||
|
|
||||||
// router.post('/api/game/getserverlistbyenv', tokenParser, controller.game.getServerListByEnv);
|
// router.post('/api/game/getserverlistbyenv', tokenParser, controller.game.getServerListByEnv);
|
||||||
// router.post('/api/game/getserverlist', controller.game.getServerList);
|
// router.post('/api/game/getserverlist', controller.game.getServerList);
|
||||||
|
|||||||
@@ -21,7 +21,9 @@ import { CreateHeroes, deletRole } from '@pubUtils/roleUtil';
|
|||||||
import { RScriptRecordModel } from '@db/RScriptRecord';
|
import { RScriptRecordModel } from '@db/RScriptRecord';
|
||||||
import { DicWar } from '@pubUtils/dictionary/DicWar';
|
import { DicWar } from '@pubUtils/dictionary/DicWar';
|
||||||
import { SkinModel } from '@db/Skin';
|
import { SkinModel } from '@db/Skin';
|
||||||
import { SearchEquipParam, SearchHeroParam, SearchUserParam } from '@domain/backEndField/search';
|
import { SearchEquipParam, SearchHeroParam, SearchUserParam, SearchGiftCodeParam } from '@domain/backEndField/search';
|
||||||
|
import { CreateGiftCode, UpdateGiftCode } from '@domain/backEndField/params';
|
||||||
|
import { isNumber } from 'util';
|
||||||
|
|
||||||
// import { resResult } from '@pubUtils/util';
|
// import { resResult } from '@pubUtils/util';
|
||||||
|
|
||||||
@@ -624,70 +626,65 @@ export default class GMUsers extends Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getGiftCodeList(page: number, pageSize: number, sortField: string, sortOrder: string, form: { name?: string, current?: boolean, } = {}) {
|
public async getGiftCodeList(page: number, pageSize: number, sortField: string, sortOrder: string, form: SearchGiftCodeParam = {}) {
|
||||||
const { ctx } = this;
|
const { ctx } = this;
|
||||||
|
|
||||||
const list = await GiftCodeModel.findByCondition(page, pageSize, sortField, sortOrder, form);
|
const list = await GiftCodeModel.findByCondition(page, pageSize, sortField, sortOrder, form);
|
||||||
const total = await GiftCodeModel.countByCondition( form )
|
const total = await GiftCodeModel.countByCondition( form )
|
||||||
return ctx.service.utils.resResult(STATUS.SUCCESS, {
|
return ctx.service.utils.resResult(STATUS.SUCCESS, {
|
||||||
list: list.map(cur => {
|
list: list.map(cur => ({...cur, env: ctx.app.config.realEnv })), total
|
||||||
return { ...cur, beginTime: cur.beginTime.getTime(), endTime: cur.endTime.getTime() }
|
|
||||||
}), total
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateGiftCode(id: string|number, params: any) {
|
public async createAndGenerateGift(obj: any, count: number = 1) {
|
||||||
const { ctx } = this;
|
const { ctx } = this;
|
||||||
if(params.beginTime) params.beginTime = new Date(params.beginTime);
|
let params = new CreateGiftCode(obj);
|
||||||
if(params.endTime) params.endTime = new Date(params.endTime);
|
if(!isNumber(count) || !params.checkParams()) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
||||||
|
|
||||||
|
let giftCode = await GiftCodeModel.createData(params);
|
||||||
|
await GiftCodeDetailModel.generateMany(giftCode, count, ctx.user?.uid);
|
||||||
|
return ctx.service.utils.resResult(STATUS.SUCCESS, giftCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async updateGiftCode(id: number, obj: any) {
|
||||||
|
const { ctx } = this;
|
||||||
|
let params = new UpdateGiftCode(id, obj);
|
||||||
|
if(!params.checkParams()) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
||||||
|
|
||||||
try{
|
|
||||||
params.goods = JSON.parse(params.goods);
|
|
||||||
} catch(e) {
|
|
||||||
console.error(e);
|
|
||||||
return ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
|
||||||
}
|
|
||||||
const result = await GiftCodeModel.updateData(id, params, ctx.user?.uid);
|
const result = await GiftCodeModel.updateData(id, params, ctx.user?.uid);
|
||||||
if(!result) return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
|
if(!result) return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
|
||||||
return ctx.service.utils.resResult(STATUS.SUCCESS);
|
return ctx.service.utils.resResult(STATUS.SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async generateGiftCode(id: number, generateType: 1|2, code: string, generateNum: number, codeLen: number = 8) {
|
public async generateGiftCode(id: number, count: number) {
|
||||||
const { ctx } = this;
|
const { ctx } = this;
|
||||||
|
if(!isNumber(id) || !isNumber(count)) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
||||||
let giftCode = await GiftCodeModel.findData(id);
|
|
||||||
if(!giftCode) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
|
||||||
|
|
||||||
let length = 0, isLimit = false, count = 0, generateCode = '';
|
let giftCode = await GiftCodeModel.findByGiftId(id);
|
||||||
if(generateType == 1) {
|
if(!giftCode) return ctx.service.utils.resResult(STATUS.DB_DATA_NOT_FOUND);
|
||||||
let generateResult = await GiftCodeDetailModel.generateMany(giftCode, generateNum, codeLen, ctx.user?.uid);
|
await GiftCodeDetailModel.generateMany(giftCode, count, ctx.user?.uid);
|
||||||
length = generateResult.length;
|
return ctx.service.utils.resResult(STATUS.SUCCESS, giftCode);
|
||||||
isLimit = true;
|
|
||||||
count = 1;
|
|
||||||
} else if (generateType == 2) {
|
|
||||||
let generateResult = await GiftCodeDetailModel.generateOne(giftCode, code, ctx.user?.uid);
|
|
||||||
length = generateResult.length;
|
|
||||||
generateCode = code;
|
|
||||||
}
|
|
||||||
if(length <= 0) return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
|
|
||||||
giftCode = await GiftCodeModel.updateData(id, { generateCnt: giftCode.generateCnt + length, generateType, isLimit, count, generateCode }, ctx.user?.uid);
|
|
||||||
return ctx.service.utils.resResult(STATUS.SUCCESS, { giftCode })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getGiftCodeDetails(id: number) {
|
public async cancelGiftCode(id: number, isEnable: boolean) {
|
||||||
|
const { ctx } = this;
|
||||||
|
if(!isNumber(id)) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
||||||
|
|
||||||
|
let giftCode = await GiftCodeModel.findByGiftId(id);
|
||||||
|
if(!giftCode) return ctx.service.utils.resResult(STATUS.DB_DATA_NOT_FOUND);
|
||||||
|
await GiftCodeModel.updateData(id, { isEnable }, ctx.user?.uid);
|
||||||
|
return ctx.service.utils.resResult(STATUS.SUCCESS, giftCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getGiftCodeDetailFile(id: number) {
|
||||||
const { ctx } = this;
|
const { ctx } = this;
|
||||||
|
|
||||||
let giftCode = await GiftCodeModel.findData(id);
|
let giftCode = await GiftCodeModel.findByGiftId(id);
|
||||||
if(!giftCode) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
if(!giftCode) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
||||||
let { generateCnt } = giftCode;
|
|
||||||
let limit = 1000;
|
|
||||||
let n = Math.ceil(generateCnt / limit);
|
|
||||||
let codes: string[] = [];
|
let codes: string[] = [];
|
||||||
for(let i = 0; i < n; i++) {
|
let giftCodeDetails = await GiftCodeDetailModel.findAllCodeByGiftId(giftCode.id, giftCode.generateCnt);
|
||||||
let giftCodeDetails = await GiftCodeDetailModel.findByGiftCode(giftCode);
|
for(let {code} of giftCodeDetails) {
|
||||||
for(let {code} of giftCodeDetails) {
|
codes.push(code);
|
||||||
codes.push(code);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
console.log(id, codes.join())
|
console.log(id, codes.join())
|
||||||
|
|
||||||
|
|||||||
@@ -770,4 +770,9 @@ export enum SERVER_TIMER {
|
|||||||
TEN_HALF = 2, // 10:30
|
TEN_HALF = 2, // 10:30
|
||||||
FIFTEEN_HALF = 3, // 15:30
|
FIFTEEN_HALF = 3, // 15:30
|
||||||
NINETEEN_HALF = 4, // 19:30
|
NINETEEN_HALF = 4, // 19:30
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum GIFT_GENERATE_TYPE {
|
||||||
|
ONE_TO_ONE = 1, // 一人一码,一条码只能被用一次
|
||||||
|
ONE_TO_MANY = 2, // 通码,一条码能被多人使用,每个人只能用一次
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
import BaseModel from './BaseModel';
|
import BaseModel from './BaseModel';
|
||||||
import { getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
|
import { getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
|
||||||
import { CounterModel } from './Counter';
|
import { CounterModel } from './Counter';
|
||||||
import { COUNTER } from '../consts';
|
import { COUNTER, GIFT_GENERATE_TYPE } from '../consts';
|
||||||
|
import { SearchGiftCodeParam } from '../domain/backEndField/search';
|
||||||
|
|
||||||
class Rewards {
|
class Rewards {
|
||||||
@prop({ required: true })
|
@prop({ required: true })
|
||||||
@@ -15,81 +16,84 @@ class Rewards {
|
|||||||
**/
|
**/
|
||||||
@modelOptions({ schemaOptions: { id: false } })
|
@modelOptions({ schemaOptions: { id: false } })
|
||||||
export default class GiftCode extends BaseModel {
|
export default class GiftCode extends BaseModel {
|
||||||
@prop({ required: true, default: '' })
|
@prop({ required: true })
|
||||||
id: number; // 唯一id
|
id: number; // 唯一id
|
||||||
|
|
||||||
@prop({ required: true, default: '' })
|
@prop({ required: true, default: '' })
|
||||||
name: string; // 礼包码名
|
name: string; // 礼包码名
|
||||||
|
|
||||||
@prop({ required: true, default: false })
|
|
||||||
isLimit: boolean; // 每个码是否有使用次数限制
|
|
||||||
|
|
||||||
@prop({ required: true, default: 0 })
|
|
||||||
count: number; // 每个码可使用次数
|
|
||||||
|
|
||||||
@prop({ required: true, type: Rewards, _id: false })
|
@prop({ required: true, type: Rewards, _id: false })
|
||||||
goods: Rewards[]; // 奖励
|
goods: Rewards[]; // 奖励
|
||||||
|
|
||||||
@prop({ required: true })
|
@prop({ required: true })
|
||||||
beginTime: Date; // 开始时间
|
beginTime: number; // 开始时间
|
||||||
|
|
||||||
@prop({ required: true })
|
@prop({ required: true })
|
||||||
endTime: Date; // 结束时间
|
endTime: number; // 结束时间
|
||||||
|
|
||||||
|
@prop({ required: true, default: 0 })
|
||||||
|
codeLen: number; // 礼包码位数
|
||||||
|
|
||||||
@prop({ required: true, default: '' })
|
@prop({ required: true, default: '' })
|
||||||
remark: string; // 备注
|
remark: string; // 备注
|
||||||
|
|
||||||
@prop({ required: true, default: 0 })
|
@prop({ required: true, default: 0, enum: GIFT_GENERATE_TYPE })
|
||||||
generateType: number; // 生成类型
|
generateType: GIFT_GENERATE_TYPE; // 生成类型
|
||||||
|
|
||||||
@prop({ required: true, default: 0 })
|
@prop({ required: true, default: 0 })
|
||||||
generateCnt: number; // 已生成条数,giftCodeDetail的数量
|
generateCnt: number; // 生成条数,giftCodeDetail的数量
|
||||||
|
|
||||||
@prop({ required: true, default: '' })
|
|
||||||
generateCode: string; // 单条的生成的那一个code
|
|
||||||
|
|
||||||
@prop({ required: true, default: 0 })
|
@prop({ required: true, default: 0 })
|
||||||
usedNum: number; // 使用次数
|
usedNum: number; // 使用次数
|
||||||
|
|
||||||
public static async findData(id: number) {
|
@prop({ required: true, default: true })
|
||||||
|
isEnable: boolean; // 是否可以使用
|
||||||
|
|
||||||
|
|
||||||
|
public static async findByGiftId(id: number) {
|
||||||
let rec: GiftCodeType = await GiftCodeModel.findOne({ id }).lean();
|
let rec: GiftCodeType = await GiftCodeModel.findOne({ id }).lean();
|
||||||
return rec;
|
return rec;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async updateData(id: string|number, values: GiftCodeParam, uid = 1) {
|
public static async createData(values: GiftCodeParam, uid = 1) {
|
||||||
if(id == 'new') {
|
let id = await CounterModel.getNewCounter(COUNTER.GIFT_CODE);
|
||||||
id = await CounterModel.getNewCounter(COUNTER.GIFT_CODE);
|
|
||||||
}
|
|
||||||
let doc = new GiftCodeModel();
|
let doc = new GiftCodeModel();
|
||||||
let createObj = doc.toJSON();
|
let obj = doc.toJSON();
|
||||||
delete values.id;
|
let update = Object.assign(obj, values, { id });
|
||||||
delete createObj._id;
|
|
||||||
delete createObj.id;
|
|
||||||
for(let key in values) {
|
|
||||||
delete createObj[key];
|
|
||||||
}
|
|
||||||
|
|
||||||
let rec: GiftCodeType = await GiftCodeModel.findOneAndUpdate({ id }, { $set: {...values, updatedBy: uid}, $setOnInsert: { ...createObj, createdBy: uid } },
|
let rec: GiftCodeType = await GiftCodeModel.findOneAndUpdate({ id }, { $set: { updatedBy: uid}, $setOnInsert: { ...update, createdBy: uid } },
|
||||||
{ new: true, upsert: true }).lean(true);
|
{ new: true, upsert: true }).lean(true);
|
||||||
return rec;
|
return rec;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async updateData(id: number, values: GiftCodeParam, uid = 1) {
|
||||||
|
|
||||||
|
let rec: GiftCodeType = await GiftCodeModel.findOneAndUpdate({ id }, { $set: {...values, updatedBy: uid} },
|
||||||
|
{ new: true }).lean(true);
|
||||||
|
return rec;
|
||||||
|
}
|
||||||
|
|
||||||
public static async increaseUsedNum(id: number) {
|
public static async increaseUsedNum(id: number) {
|
||||||
let result: GiftCodeType = await GiftCodeModel.findOneAndUpdate({ id }, { $inc: { usedNum: 1 } }, { new: true }).lean();
|
let result: GiftCodeType = await GiftCodeModel.findOneAndUpdate({ id }, { $inc: { usedNum: 1 } }, { new: true }).lean();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static getSearchObj(form: { name?: string, current?: boolean }) {
|
public static async increateGenerateNum(id: number, count: number) {
|
||||||
|
let result: GiftCodeType = await GiftCodeModel.findOneAndUpdate({ id }, { $inc: { generateCnt: count } }, { new: true }).lean();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static getSearchObj(form: SearchGiftCodeParam) {
|
||||||
let searchObj = {};
|
let searchObj = {};
|
||||||
|
if(form.id) searchObj['id'] = form.id;
|
||||||
if (form.name != undefined) searchObj['name'] = { $regex: new RegExp(form.name.toString(), 'i') };
|
if (form.name != undefined) searchObj['name'] = { $regex: new RegExp(form.name.toString(), 'i') };
|
||||||
if (form.current) {
|
if (form.createTimeStart && form.createTimeEnd) {
|
||||||
searchObj['beginTime'] = { $lte: new Date };
|
searchObj['createdAt'] = { $lte: new Date(form.createTimeEnd * 1000), $gte: new Date(form.createTimeStart * 100) };
|
||||||
searchObj['endTime'] = { $gte: new Date };
|
|
||||||
}
|
}
|
||||||
return searchObj
|
return searchObj
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async findByCondition(page: number, pageSize: number, sortField: string, sortOrder: string, form: { name?: string, current?: boolean } = {}) {
|
public static async findByCondition(page: number, pageSize: number, sortField: string, sortOrder: string, form:SearchGiftCodeParam = {}) {
|
||||||
|
|
||||||
let searchObj = this.getSearchObj(form);
|
let searchObj = this.getSearchObj(form);
|
||||||
let sort = {};
|
let sort = {};
|
||||||
@@ -105,7 +109,7 @@ export default class GiftCode extends BaseModel {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async countByCondition(form: { name?: string, current?: boolean } = {}) {
|
public static async countByCondition(form: SearchGiftCodeParam = {}) {
|
||||||
|
|
||||||
let searchObj = this.getSearchObj(form);
|
let searchObj = this.getSearchObj(form);
|
||||||
const result = await GiftCodeModel.count(searchObj);
|
const result = await GiftCodeModel.count(searchObj);
|
||||||
@@ -113,7 +117,6 @@ export default class GiftCode extends BaseModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GiftCodeModel = getModelForClass(GiftCode);
|
export let GiftCodeModel = getModelForClass(GiftCode);
|
||||||
|
|
||||||
export interface GiftCodeType extends Pick<DocumentType<GiftCode>, keyof GiftCode> { }
|
export interface GiftCodeType extends Pick<DocumentType<GiftCode>, keyof GiftCode> { }
|
||||||
export type GiftCodeParam = Partial<GiftCodeType>;
|
export type GiftCodeParam = Partial<GiftCodeType>;
|
||||||
|
|||||||
@@ -1,7 +1,23 @@
|
|||||||
import BaseModel from './BaseModel';
|
import BaseModel from './BaseModel';
|
||||||
import { index, getModelForClass, prop, DocumentType, modelOptions, Ref, mongoose } from '@typegoose/typegoose';
|
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
|
||||||
import GiftCode, { GiftCodeType } from './GiftCode';
|
import { GIFT_GENERATE_TYPE } from '../consts';
|
||||||
import { genCode } from '../pubUtils/util';
|
import { GiftCodeModel, GiftCodeType } from './GiftCode';
|
||||||
|
import { nowSeconds } from '../pubUtils/timeUtil';
|
||||||
|
import { genCode } from '@pubUtils/util';
|
||||||
|
|
||||||
|
class RoleRecord {
|
||||||
|
@prop({ required: true, default: '' })
|
||||||
|
roleId: string;
|
||||||
|
|
||||||
|
@prop({ required: true, default: '' })
|
||||||
|
roleName: string;
|
||||||
|
|
||||||
|
@prop({ required: true, default: 0 })
|
||||||
|
serverId: number;
|
||||||
|
|
||||||
|
@prop({ required: true, default: 0 })
|
||||||
|
time: number;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 举报记录
|
* 举报记录
|
||||||
@@ -9,15 +25,28 @@ import { genCode } from '../pubUtils/util';
|
|||||||
@modelOptions({ schemaOptions: { id: false } })
|
@modelOptions({ schemaOptions: { id: false } })
|
||||||
@index({ code: 1 })
|
@index({ code: 1 })
|
||||||
export default class GiftCodeDetail extends BaseModel {
|
export default class GiftCodeDetail extends BaseModel {
|
||||||
|
|
||||||
|
@prop({ required: true, default: '' })
|
||||||
|
giftId: number; // giftCode表的id
|
||||||
|
|
||||||
|
@prop({ required: true, default: '' })
|
||||||
|
giftName: string; // giftCode表的name
|
||||||
|
|
||||||
|
@prop({ required: true, default: 0, enum: GIFT_GENERATE_TYPE })
|
||||||
|
generateType: GIFT_GENERATE_TYPE; // 一人一码or通码
|
||||||
|
|
||||||
@prop({ required: true, default: '' })
|
@prop({ required: true, default: '' })
|
||||||
code: string; // 兑换码
|
code: string; // 兑换码
|
||||||
|
|
||||||
@prop({ ref: 'GiftCode', type: mongoose.Schema.Types.ObjectId })
|
|
||||||
giftCode: Ref<GiftCode>;
|
|
||||||
|
|
||||||
@prop({ required: true, default: '' })
|
@prop({ required: true, default: '' })
|
||||||
usedNum: number; // 该码使用次数
|
usedNum: number; // 该码使用次数
|
||||||
|
|
||||||
|
@prop({ required: true, type: String })
|
||||||
|
roleIds: string[];
|
||||||
|
|
||||||
|
@prop({ required: true, type: RoleRecord, _id: false })
|
||||||
|
record: RoleRecord[];
|
||||||
|
|
||||||
|
|
||||||
// 根据code
|
// 根据code
|
||||||
public static async findByCode(code: string) {
|
public static async findByCode(code: string) {
|
||||||
@@ -25,27 +54,33 @@ export default class GiftCodeDetail extends BaseModel {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async findByGiftCode(giftCode: GiftCodeType) {
|
public static async findAllCodeByGiftId(id: number, cnt: number) {
|
||||||
let result: GiftCodeDetailType[] = await GiftCodeDetailModel.find({ giftCode: giftCode._id }).lean();
|
let n = Math.ceil(cnt/1000);
|
||||||
|
let result: GiftCodeDetailType[] = [];
|
||||||
|
for(let i = 0; i < n; i++) {
|
||||||
|
let codes = await GiftCodeDetailModel.find({ giftId: id }).limit(1000).skip(i * 1000).sort({ _id: 1 }).lean();
|
||||||
|
result.push(...codes);
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async increaseUsedNum(code: string) {
|
public static async increaseUsedNum(code: string, roleId: string, roleName: string, serverId: number) {
|
||||||
let result: GiftCodeDetailType = await GiftCodeDetailModel.findOneAndUpdate({ code }, { $inc: { usedNum: 1 } }, { new: true }).lean();
|
let result: GiftCodeDetailType = await GiftCodeDetailModel.findOneAndUpdate({ code }, {
|
||||||
|
$inc: { usedNum: 1 }, $push: { roleIds: roleId, record: { roleId, roleName, serverId, time: nowSeconds() } }
|
||||||
|
}, { new: true }).lean();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async generateOne(giftCode: GiftCodeType, code: string, uid = 1 ) {
|
public static async generateMany(giftCode: GiftCodeType, count = 1, uid = 1) {
|
||||||
let result = await GiftCodeDetailModel.insertMany([{ giftCode: giftCode._id, code, usedNum: 0, createdBy: uid, updatedBy: uid }]);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async generateMany(giftCode: GiftCodeType, generateNum: number, codeLen: number, uid = 1) {
|
|
||||||
let insertArr: GiftCodeDetailParam[] = [];
|
let insertArr: GiftCodeDetailParam[] = [];
|
||||||
for(let i = 0; i < generateNum; i++) {
|
for(let i = 0; i < count; i++) {
|
||||||
insertArr.push({ giftCode: giftCode._id, code: genCode(codeLen), usedNum: 0, createdBy: uid, updatedBy: uid });
|
insertArr.push({
|
||||||
|
giftId: giftCode.id, giftName: giftCode.name, generateType: giftCode.generateType,
|
||||||
|
code: genCode(giftCode.codeLen), usedNum: 0, createdBy: uid, updatedBy: uid
|
||||||
|
});
|
||||||
}
|
}
|
||||||
let result = await GiftCodeDetailModel.insertMany(insertArr);
|
let result = await GiftCodeDetailModel.insertMany(insertArr);
|
||||||
|
await GiftCodeModel.increateGenerateNum(giftCode.id, count);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
import BaseModel from './BaseModel';
|
|
||||||
import { index, getModelForClass, prop, DocumentType, modelOptions, Ref, mongoose } from '@typegoose/typegoose';
|
|
||||||
import GiftCode, { GiftCodeType } from './GiftCode';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 举报记录
|
|
||||||
**/
|
|
||||||
@modelOptions({ schemaOptions: { id: false } })
|
|
||||||
@index({ roleId: 1, code: 1 })
|
|
||||||
export default class UserGiftCode extends BaseModel {
|
|
||||||
|
|
||||||
@prop({ required: true, default: '' })
|
|
||||||
roleId: string; // 玩家id
|
|
||||||
|
|
||||||
@prop({ required: true, default: '' })
|
|
||||||
code: string; // 兑换码
|
|
||||||
|
|
||||||
@prop({ ref: 'GiftCode', type: mongoose.Schema.Types.ObjectId })
|
|
||||||
giftCode: Ref<GiftCode>;
|
|
||||||
|
|
||||||
// 根据code
|
|
||||||
public static async findByCode(roleId: string, code: string) {
|
|
||||||
let result: UserGiftCodeType = await UserGiftCodeModel.findOne({ roleId, code }).lean();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async createCode(roleId: string, code: string, gitCode: GiftCodeType) {
|
|
||||||
let result: UserGiftCodeType = await UserGiftCodeModel.findOneAndUpdate({ roleId, code }, { $setOnInsert: { gitCode: gitCode._id } }, { new: true, upsert: true}).lean()
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const UserGiftCodeModel = getModelForClass(UserGiftCode);
|
|
||||||
|
|
||||||
export interface UserGiftCodeType extends Pick<DocumentType<UserGiftCode>, keyof UserGiftCode> { }
|
|
||||||
export type UserGiftCodeParam = Partial<UserGiftCodeType>;
|
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
import { GM_MAIL_TYPE, MAIL_TIME_TYPE, SERVER_TIMER } from "../../consts";
|
import { GIFT_GENERATE_TYPE, GM_MAIL_TYPE, MAIL_TIME_TYPE, SERVER_TIMER } from "../../consts";
|
||||||
import { isArray } from 'underscore';
|
import { isArray, isNumber, isString } from 'underscore';
|
||||||
import ServerStategy, { GMMail } from "../../db/ServerStategy";
|
import ServerStategy, { GMMail } from "../../db/ServerStategy";
|
||||||
import { RegionType } from "../../db/Region";
|
import { RegionType } from "../../db/Region";
|
||||||
|
import { RewardInter } from "../../pubUtils/interface";
|
||||||
|
import { isTimestamp } from '../../pubUtils/util';
|
||||||
|
|
||||||
export class UpdateMailParams {
|
export class UpdateMailParams {
|
||||||
hasGoods: boolean = false; // 是否有道具
|
hasGoods: boolean = false; // 是否有道具
|
||||||
@@ -156,4 +158,71 @@ export class CreateServerParam {
|
|||||||
if(this.hasCircleMail && !this.circleMail) return false;
|
if(this.hasCircleMail && !this.circleMail) return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CreateGiftCode {
|
||||||
|
name: string; // 礼包码名
|
||||||
|
goods: RewardInter[]; // 奖励
|
||||||
|
beginTime: number; // 开始时间
|
||||||
|
endTime: number; // 结束时间
|
||||||
|
codeLen: number; // 礼包码位数
|
||||||
|
remark: string = ''; // 备注
|
||||||
|
generateType: GIFT_GENERATE_TYPE; // 生成类型
|
||||||
|
|
||||||
|
constructor(obj: any) {
|
||||||
|
this.name = obj.name;
|
||||||
|
this.goods = obj.goods;
|
||||||
|
this.beginTime = obj.beginTime;
|
||||||
|
this.endTime = obj.endTime;
|
||||||
|
this.codeLen = obj.codeLen;
|
||||||
|
this.remark = obj.remark;
|
||||||
|
this.generateType = obj.generateType;
|
||||||
|
}
|
||||||
|
|
||||||
|
checkParams() {
|
||||||
|
if(!isString(this.name)) return false;
|
||||||
|
if(!isArray(this.goods)) return false;
|
||||||
|
for(let { id, count } of this.goods) {
|
||||||
|
if(!isNumber(id) || !isNumber(count)) return false;
|
||||||
|
}
|
||||||
|
if(!isTimestamp(this.beginTime) || !isTimestamp(this.endTime)) return false;
|
||||||
|
if(!isNumber(this.codeLen) || this.codeLen <= 0) return false;
|
||||||
|
|
||||||
|
if(this.generateType != GIFT_GENERATE_TYPE.ONE_TO_MANY && this.generateType != GIFT_GENERATE_TYPE.ONE_TO_ONE) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class UpdateGiftCode {
|
||||||
|
id: number;
|
||||||
|
name: string; // 礼包码名
|
||||||
|
goods: RewardInter[]; // 奖励
|
||||||
|
beginTime: number; // 开始时间
|
||||||
|
endTime: number; // 结束时间
|
||||||
|
codeLen: number; // 礼包码位数
|
||||||
|
remark: string = ''; // 备注
|
||||||
|
|
||||||
|
constructor(id: number, obj: any) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = obj.name;
|
||||||
|
this.goods = obj.goods;
|
||||||
|
this.beginTime = obj.beginTime;
|
||||||
|
this.endTime = obj.endTime;
|
||||||
|
this.codeLen = obj.codeLen;
|
||||||
|
this.remark = obj.remark;
|
||||||
|
}
|
||||||
|
|
||||||
|
checkParams() {
|
||||||
|
if(!isNumber(this.id) || this.id <= 0) return false
|
||||||
|
if(!isString(this.name)) return false;
|
||||||
|
if(!isArray(this.goods)) return false;
|
||||||
|
for(let { id, count } of this.goods) {
|
||||||
|
if(!isNumber(id) || !isNumber(count)) return false;
|
||||||
|
}
|
||||||
|
if(!isTimestamp(this.beginTime) || !isTimestamp(this.endTime)) return false;
|
||||||
|
if(!isNumber(this.codeLen) || this.codeLen <= 0) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -43,4 +43,12 @@ export interface SearchMarqueeParam {
|
|||||||
createTimeEnd?: number;
|
createTimeEnd?: number;
|
||||||
sendTimeStart?: number;
|
sendTimeStart?: number;
|
||||||
sendTimeEnd?: number;
|
sendTimeEnd?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SearchGiftCodeParam {
|
||||||
|
env?: string;
|
||||||
|
id?: number;
|
||||||
|
name?: string;
|
||||||
|
createTimeStart?: number;
|
||||||
|
createTimeEnd?: number;
|
||||||
}
|
}
|
||||||
@@ -90,7 +90,6 @@ import { ScriptBarrageModel } from '../db/ScriptBarrage';
|
|||||||
import { ServerMailModel } from '../db/ServerMail';
|
import { ServerMailModel } from '../db/ServerMail';
|
||||||
import { UserGachaModel } from '../db/UserGacha';
|
import { UserGachaModel } from '../db/UserGacha';
|
||||||
import { UserGachaRecModel } from '../db/UserGachaRec';
|
import { UserGachaRecModel } from '../db/UserGachaRec';
|
||||||
import { UserGiftCodeModel } from '../db/UserGiftCode';
|
|
||||||
import { UserGuildActivityRecModel } from '../db/UserGuildActivityRec';
|
import { UserGuildActivityRecModel } from '../db/UserGuildActivityRec';
|
||||||
import { UserOrderModel } from '../db/UserOrder';
|
import { UserOrderModel } from '../db/UserOrder';
|
||||||
import { UserShopModel } from '../db/UserShop';
|
import { UserShopModel } from '../db/UserShop';
|
||||||
@@ -462,7 +461,6 @@ export async function deletRole(roleId: string) {
|
|||||||
await TowerTaskRecModel.deleteMany({ roleId });
|
await TowerTaskRecModel.deleteMany({ roleId });
|
||||||
await UserGachaModel.deleteMany({ roleId });
|
await UserGachaModel.deleteMany({ roleId });
|
||||||
await UserGachaRecModel.deleteMany({ roleId });
|
await UserGachaRecModel.deleteMany({ roleId });
|
||||||
await UserGiftCodeModel.deleteMany({ roleId });
|
|
||||||
await UserGuildModel.deleteMany({ roleId });
|
await UserGuildModel.deleteMany({ roleId });
|
||||||
await UserGuildActivityRecModel.deleteMany({ roleId });
|
await UserGuildActivityRecModel.deleteMany({ roleId });
|
||||||
await UserGuildApplyModel.deleteMany({ roleId });
|
await UserGuildApplyModel.deleteMany({ roleId });
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { STATUS } from './../consts/statusCode';
|
import { STATUS } from './../consts/statusCode';
|
||||||
|
|
||||||
import { HeroModel, HeroType } from '../db/Hero';
|
import { HeroModel, HeroType } from '../db/Hero';
|
||||||
|
import { isNumber } from 'underscore';
|
||||||
|
|
||||||
const csprng = require('csprng');
|
const csprng = require('csprng');
|
||||||
import fs = require('fs');
|
import fs = require('fs');
|
||||||
@@ -668,4 +669,10 @@ export async function checkWhiteList(env: string, ip: string, uid: number, serve
|
|||||||
if(!!result) return true;
|
if(!!result) return true;
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isTimestamp(time: number, len = 10) {
|
||||||
|
if(!isNumber(time)) return false;
|
||||||
|
if(time.toString().length != len) return false;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user