From acd6b0143915433fd4dc80c3970c2aefce097b8a Mon Sep 17 00:00:00 2001 From: zhl Date: Mon, 7 Jun 2021 12:39:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8C=91=E6=88=98=E6=8A=BD?= =?UTF-8?q?=E5=A5=96=E5=88=B8=E7=9A=84=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/api.md | 4 +- src/api/controllers/game_user.controller.ts | 12 ++++- src/models/shop/ShopExam.ts | 7 +++ src/models/user/ItemRecord.ts | 10 +++- src/models/user/UserReward.ts | 55 ++++++++++++++++++--- src/services/JCFW.ts | 24 ++++++++- 6 files changed, 101 insertions(+), 11 deletions(-) diff --git a/doc/api.md b/doc/api.md index 7ccf05f..a64f18a 100644 --- a/doc/api.md +++ b/doc/api.md @@ -6,6 +6,7 @@ 1. [增加用户信息](#212), [抽奖转盘信息](#213), [抽奖](#214), [邀请奖励信息](#215), [挑战详情](#216) 接口 2. 所有接口增加post字段 version(当前版本固定取1.0.1)和sessionid(取自jcfw) 3. 获取店铺信息 增加返回店铺的数字编号 +4. 上报测验题目答案接口的 奖励信息 增加返回rewardType, 0: 表明该奖励为优惠券, 1: 抽奖券 @@ -306,7 +307,8 @@ name: '优惠券名', count: 1, //数量 couponUrl: '优惠券详情图的url', - ids: ['获取记录的短id'] + ids: ['获取记录的短id'], + rewardType: 0 //奖励类型 0: 优惠券 1: 抽奖券 }] } ``` diff --git a/src/api/controllers/game_user.controller.ts b/src/api/controllers/game_user.controller.ts index 361e662..ddc8e59 100644 --- a/src/api/controllers/game_user.controller.ts +++ b/src/api/controllers/game_user.controller.ts @@ -6,6 +6,8 @@ import { getRandom } from '../../utils/number.util' import { UserReward } from '../../models/user/UserReward' import { UserItem } from '../../models/user/UserItem' import { LOTTERY_TICKET } from '../../constants/BaseConst' +import { getShopInviteeNum } from '../../services/JCFW' +import { Shop } from '../../models/shop/Shop' class GameUserController extends BaseController { // TODO:: 增加返回未使用的券 @@ -58,9 +60,17 @@ class GameUserController extends BaseController { @role('anon') @router('post /api/:accountId/info') async userInfo(req: any) { - const { accountId, sid } = req.params + const { accountId, sid, sessionid } = req.params + if (!sid) { + throw new ZError(10, '缺少必要参数: sid') + } + const shop = await Shop.fetchByID(sid) + if (!shop) { + throw new ZError(11, '无法找到对应店铺') + } let result: any = {} result.tocket_lottery = await UserItem.fetchCount({ accountId, shop: sid, item: LOTTERY_TICKET }) + let numInvite = await getShopInviteeNum(accountId, sessionid, shop.numid) return result } diff --git a/src/models/shop/ShopExam.ts b/src/models/shop/ShopExam.ts index 479b296..aefe147 100644 --- a/src/models/shop/ShopExam.ts +++ b/src/models/shop/ShopExam.ts @@ -61,6 +61,13 @@ export class ExamRewardClass extends Base { @prop({ default: 0 }) public count: number + /** + * 奖励类型 + * @type {number} 0: 优惠券, 1: 抽奖券 + */ + @prop({ default: 0 }) + public rewardType: number + /** * 奖励类型 * @type {number} 0: 单局, 1: 累积 diff --git a/src/models/user/ItemRecord.ts b/src/models/user/ItemRecord.ts index e078007..d23cbf3 100644 --- a/src/models/user/ItemRecord.ts +++ b/src/models/user/ItemRecord.ts @@ -1,4 +1,4 @@ -import { getModelForClass, index, modelOptions, mongoose, prop, Severity } from '@typegoose/typegoose' +import { getModelForClass, index, modelOptions, mongoose, post, prop, Severity } from '@typegoose/typegoose' import { dbconn } from '../../decorators/dbconn' import { BaseModule } from '../Base' @@ -25,6 +25,14 @@ class ItemRecordClass extends BaseModule { public count: number @prop() public reason: string + + /** + * 物品类型 + * @type {number} 0: 抽奖券 + */ + @prop() + public type: number + @prop({ type: mongoose.Schema.Types.Mixed }) public data: {} diff --git a/src/models/user/UserReward.ts b/src/models/user/UserReward.ts index 90f2d3c..339ae2f 100644 --- a/src/models/user/UserReward.ts +++ b/src/models/user/UserReward.ts @@ -7,9 +7,17 @@ import { Shop } from '../shop/Shop' import { Coupon } from '../shop/Coupon' import { getCouponUrl } from '../../services/File' import { PuzzleSessionClass } from '../match/PuzzleSession' +import { LOTTERY_TICKET } from '../../constants/BaseConst' const nanoid = customAlphabet('2345678abcdefghjkmnpqrstwxy', 10) +/** + * 用户挑战或活动获得的奖励记录 + * 1. 用于判断某活动或挑战的奖励是否已经领取 + * 2. 用于获取用户的优惠券列表 + * 3. 用于抽奖券的部分获取记录 + */ + @dbconn() @index({ accountId: 1 }, { unique: false }) @index({ accountId: 1, shop: 1 }, { unique: false }) @@ -47,7 +55,10 @@ class UserRewardClass extends BaseModule { */ @prop() public activityId: string - + /** + * PuzzleSession的id + * @type {string} + */ @prop() public sessionId: string /** @@ -56,7 +67,10 @@ class UserRewardClass extends BaseModule { */ @prop() public coupon: string - + /** + * 活动或挑战中奖励配置id + * @type {string} + */ @prop() public rewardId: string @@ -70,6 +84,16 @@ class UserRewardClass extends BaseModule { @prop() public status: number + /** + * 奖励类型 + * @type {number} 0: 优惠券, 1: 抽奖券 + */ + @prop({ default: 0 }) + public rewardType: number + + @prop({ default: 1 }) + public count: number + public static async ticketList(accountId: string, shopId?: string) { let params: any = { accountId } let shopMap: Map = new Map() @@ -124,8 +148,7 @@ class UserRewardClass extends BaseModule { count: number, ) { let ids: string[] = [] - let cdata = await Coupon.findById(coupon) - for (let i = 0; i < count; i++) { + let saveRecord = async function (rewardType: number, cCount: number) { let record = new UserReward({ accountId, shop: history.shop, @@ -133,6 +156,8 @@ class UserRewardClass extends BaseModule { status: 0, rewardId, coupon, + rewardType, + count: cCount, }) if (history.type === 1) { record.activityId = history.activityId @@ -142,12 +167,30 @@ class UserRewardClass extends BaseModule { await record.save() ids.push(record.sid) } + let name = '' + let url = '' + let rewardType = 0 + if (coupon === LOTTERY_TICKET) { + rewardType = 1 + await saveRecord(1, count) + name = '抽奖券' + } else { + rewardType = 0 + let cdata = await Coupon.findById(coupon) + for (let i = 0; i < count; i++) { + await saveRecord(0, 1) + } + name = cdata.name + url = getCouponUrl(history.shop, coupon) + } + return { coupon, - name: cdata.name, + name, count, - couponUrl: getCouponUrl(history.shop, coupon), + couponUrl: url, ids, + rewardType, } } diff --git a/src/services/JCFW.ts b/src/services/JCFW.ts index d8984ea..cafb3d4 100644 --- a/src/services/JCFW.ts +++ b/src/services/JCFW.ts @@ -2,8 +2,7 @@ import axios from 'axios' import { ZError } from '../common/ZError' /** - * 查询接受邀请的人数 - * @return {Promise} + * 查询接受邀请的人数(多店) */ export async function getInviteeNum(accountId: string, sessionId: string, shops: number[]) { const url = 'https://service.kingsome.cn/webapp/index.php?c=AchievementShare&a=getInviteeNum' @@ -22,6 +21,27 @@ export async function getInviteeNum(accountId: string, sessionId: string, shops: }) } +/** + * 查询接受邀请的人数(单店) + * 对上面多店查询接口的封装 + * @param {string} accountId + * @param {string} sessionId + * @param {number} shop + */ +export async function getShopInviteeNum(accountId: string, sessionId: string, shop: number) { + let records = await getInviteeNum(accountId, sessionId, [shop]) + let num = 0 + if (records?.length > 0) { + for (let obj of records) { + if (obj.hasOwnProperty(shop)) { + num = obj[shop] + break + } + } + } + return num +} + /** * 查询接受邀请的好友信息数 * @return {Promise}