增加获取优惠券列表的接口
This commit is contained in:
parent
d2588bf792
commit
3d261f129e
@ -402,6 +402,7 @@
|
|||||||
| -------- | -------------------------------------- |
|
| -------- | -------------------------------------- |
|
||||||
| nickname | 昵称 |
|
| nickname | 昵称 |
|
||||||
| avatar | 头像 |
|
| avatar | 头像 |
|
||||||
|
| mobile | 手机号 |
|
||||||
| sex | 性别 |
|
| sex | 性别 |
|
||||||
| country | 国家 |
|
| country | 国家 |
|
||||||
| province | 省份 |
|
| province | 省份 |
|
||||||
|
@ -3,6 +3,7 @@ import { role, router } from '../../decorators/router'
|
|||||||
import { GameUser } from '../../models/GameUser'
|
import { GameUser } from '../../models/GameUser'
|
||||||
import { ZError } from '../../common/ZError'
|
import { ZError } from '../../common/ZError'
|
||||||
import { getRandom } from '../../utils/number.util'
|
import { getRandom } from '../../utils/number.util'
|
||||||
|
import { UserReward } from '../../models/UserReward'
|
||||||
|
|
||||||
class GameUserController extends BaseController {
|
class GameUserController extends BaseController {
|
||||||
// TODO:: 增加返回未使用的券
|
// TODO:: 增加返回未使用的券
|
||||||
@ -21,6 +22,7 @@ class GameUserController extends BaseController {
|
|||||||
return {token}
|
return {token}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO:: 根据真实的配表获取数据
|
||||||
@role('anon')
|
@role('anon')
|
||||||
@router('post /api/:accountid/stats')
|
@router('post /api/:accountid/stats')
|
||||||
async userStatic(req: any) {
|
async userStatic(req: any) {
|
||||||
@ -43,4 +45,11 @@ class GameUserController extends BaseController {
|
|||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@role('anon')
|
||||||
|
@router('post /api/:accountid/tickets')
|
||||||
|
async userTickets(req: any) {
|
||||||
|
const { accountid } = req.params
|
||||||
|
return await UserReward.ticketList( accountid )
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,14 @@ class GameUserClass extends BaseModule {
|
|||||||
@prop()
|
@prop()
|
||||||
public avatar: string
|
public avatar: string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 手机号
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
|
@prop()
|
||||||
|
public mobile: string
|
||||||
|
|
||||||
|
|
||||||
@prop({default: 0})
|
@prop({default: 0})
|
||||||
public sex?: string;
|
public sex?: string;
|
||||||
@prop({default: 'CN'})
|
@prop({default: 'CN'})
|
||||||
|
107
src/models/UserReward.ts
Normal file
107
src/models/UserReward.ts
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
import { dbconn } from '../decorators/dbconn'
|
||||||
|
import {
|
||||||
|
getModelForClass,
|
||||||
|
index,
|
||||||
|
modelOptions, pre,
|
||||||
|
prop
|
||||||
|
} from '@typegoose/typegoose'
|
||||||
|
import { BaseModule } from './Base'
|
||||||
|
import { customAlphabet, } from 'nanoid'
|
||||||
|
import { Shop } from './shop/Shop'
|
||||||
|
import { Coupon } from './shop/Coupon'
|
||||||
|
import { getCouponUrl } from '../services/File'
|
||||||
|
|
||||||
|
const nanoid = customAlphabet('2345678abcdefghjkmnpqrstwxy', 10)
|
||||||
|
|
||||||
|
@dbconn()
|
||||||
|
@index({ accountId: 1}, { unique: false })
|
||||||
|
@index({ accountId: 1, shop: 1}, { unique: false })
|
||||||
|
@index({sid: 1}, {unique: true})
|
||||||
|
@modelOptions({ schemaOptions: { collection: 'game_user_reward', timestamps: true } })
|
||||||
|
@pre<UserRewardClass>('save', async function() {
|
||||||
|
if (!this.sid) {
|
||||||
|
let sid = ''
|
||||||
|
while (!sid) {
|
||||||
|
sid = nanoid()
|
||||||
|
const records = await UserReward.find({sid}, {_id: 1}).limit(1);
|
||||||
|
(records.length > 0) && (sid = '')
|
||||||
|
}
|
||||||
|
this.sid = sid
|
||||||
|
}
|
||||||
|
})
|
||||||
|
class UserRewardClass extends BaseModule {
|
||||||
|
@prop()
|
||||||
|
public sid: string
|
||||||
|
@prop()
|
||||||
|
public accountId: string
|
||||||
|
|
||||||
|
@prop()
|
||||||
|
public shop: string
|
||||||
|
/**
|
||||||
|
* 挑战id
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
|
@prop()
|
||||||
|
public examId: string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 活动id
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
|
@prop()
|
||||||
|
public activityId: string
|
||||||
|
|
||||||
|
@prop()
|
||||||
|
public sessionId: string
|
||||||
|
/**
|
||||||
|
* 券的id
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
|
@prop()
|
||||||
|
public coupon: string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 状态
|
||||||
|
* 0: 未使用
|
||||||
|
* 1: 已使用
|
||||||
|
* 9: 已过期
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
@prop()
|
||||||
|
public status: number
|
||||||
|
|
||||||
|
public static async ticketList(accountId: string, shopId?: string) {
|
||||||
|
let params:any = { accountId }
|
||||||
|
if (shopId) {
|
||||||
|
params.shop = shopId
|
||||||
|
}
|
||||||
|
let records = await UserReward.find(params)
|
||||||
|
let results: any = []
|
||||||
|
let shopMap: Map<string, string> = new Map()
|
||||||
|
let couponMap : Map<string, any> = new Map()
|
||||||
|
for (let record of records) {
|
||||||
|
let sid = record.shop
|
||||||
|
if (!shopMap.has(record.shop)) {
|
||||||
|
let shop = await Shop.fetchByID(record.shop)
|
||||||
|
shopMap.set(record.shop, shop.showName)
|
||||||
|
sid = shop.id
|
||||||
|
}
|
||||||
|
if (!couponMap.has(record.coupon)) {
|
||||||
|
let coupon = await Coupon.findById(record.coupon)
|
||||||
|
couponMap.set(coupon.id, coupon)
|
||||||
|
}
|
||||||
|
results.push({
|
||||||
|
id: record.sid,
|
||||||
|
shop: record.shop,
|
||||||
|
shopName: shopMap.get(record.shop),
|
||||||
|
coupon: record.coupon,
|
||||||
|
couponName: couponMap.get(record.coupon),
|
||||||
|
couponUrl: getCouponUrl(sid, record.coupon),
|
||||||
|
status: record.status
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export const UserReward = getModelForClass(UserRewardClass, {existingConnection: UserRewardClass.db})
|
@ -81,3 +81,8 @@ export function generateCouponImg(shop: string, couponId: string, content: strin
|
|||||||
}}
|
}}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getCouponUrl(shop: string, couponId: string) {
|
||||||
|
let subPath = `/coupon/${shop}/`
|
||||||
|
return `${config.file.show_url}${subPath}/${couponId}.png`
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user