增加获取优惠券列表的接口

This commit is contained in:
zhl 2021-05-20 17:06:50 +08:00
parent d2588bf792
commit 3d261f129e
5 changed files with 130 additions and 0 deletions

View File

@ -402,6 +402,7 @@
| -------- | -------------------------------------- | | -------- | -------------------------------------- |
| nickname | 昵称 | | nickname | 昵称 |
| avatar | 头像 | | avatar | 头像 |
| mobile | 手机号 |
| sex | 性别 | | sex | 性别 |
| country | 国家 | | country | 国家 |
| province | 省份 | | province | 省份 |

View File

@ -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 )
}
} }

View File

@ -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
View 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})

View File

@ -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`
}