From 687115a8ab5bf32b43e4bda376941d378abd99e0 Mon Sep 17 00:00:00 2001 From: yulixing Date: Fri, 6 Sep 2019 18:32:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B9=BF=E5=91=8A=E7=A0=81?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E6=AF=94=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/games/ad.js | 72 +++++++++++++++++++++++++++++++++- src/controllers/games/gift.js | 2 + src/controllers/open/ad-uid.js | 55 ++++++++++++++++++++++++++ src/controllers/open/index.js | 2 + src/models/admin/AdUid.js | 3 +- src/models/admin/GameGift.js | 3 ++ 6 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 src/controllers/open/ad-uid.js diff --git a/src/controllers/games/ad.js b/src/controllers/games/ad.js index 119af04..2a2fcbc 100644 --- a/src/controllers/games/ad.js +++ b/src/controllers/games/ad.js @@ -5,6 +5,8 @@ import logger from '../../utils/logger' import AdArea from '../../models/admin/AdArea' import AdUid from '../../models/admin/AdUid' import {builtinModules} from 'module' +import RedisDao from '../../redis/redis.dao' +const redisDao = new RedisDao() const router = new Router() @@ -361,7 +363,8 @@ router.post('/uid', async (req, res, next) => { platform_id: body.platform_id, channel_name: body.channel_name, channel_id: body.channel_id, - appid: body.appid + appid: body.appid, + ratio: body.ratio, }) const result = await newAdUid.save() res.send({ @@ -400,7 +403,8 @@ router.put('/uid', async (req, res, next) => { platform_id: body.platform_id, channel_name: body.channel_name, channel_id: body.channel_id, - appid: body.appid + appid: body.appid, + ratio: body.ratio, } ) res.send({ @@ -451,6 +455,70 @@ router.delete('/uid', async (req, res, next) => { } }) +// 获取Uid显示比例 +router.get('/uid-ratio', async (req, res, next) => { + try { + const query = req.query + const game_id = query.game_id + const platform_id = query.platform_id + const key = `ad-ratio:${game_id}:${platform_id}` + const result = await redisDao.getByKey(key) + + res.send({ + errcode: 0, + result: result || '', + }) + } catch (err) { + next(err) + } +}) + +// 修改Uid显示比例 +router.post('/uid-ratio', async (req, res, next) => { + try { + const body = req.body + const game_id = body.game_id + const platform_id = body.platform_id + const ratio = body.ratio + const key = `ad-ratio:${game_id}:${platform_id}` + const result = await redisDao.updateOneKey(key, ratio) + let cfg = {} + if (ratio) { + const arr = ratio.split('\n') + console.log(arr) + arr.map(item => { + const kv = item.split('-') + cfg[kv[0]] = kv[1] + }) + } + + console.log(cfg) + + for (const key in cfg) { + if (cfg.hasOwnProperty(key)) { + const val = cfg[key] + await AdUid.updateMany( + { + game_id, + platform_id, + deleted: false, + channel_name: key, + }, + { + ratio: val, + } + ) + } + } + + res.send({ + errcode: 0, + }) + } catch (err) { + next(err) + } +}) + // ---------------------------------------------- // export default router diff --git a/src/controllers/games/gift.js b/src/controllers/games/gift.js index 759e7c6..9512f02 100644 --- a/src/controllers/games/gift.js +++ b/src/controllers/games/gift.js @@ -45,6 +45,7 @@ router.post('/', async (req, res, next) => { gift_url: body.gift_url, codes: body.codes, used_codes: body.used_codes, + type: body.type, }) const result = await gift.save() res.send({ @@ -77,6 +78,7 @@ router.put('/', async (req, res, next) => { codes: body.codes, used_codes: body.used_codes, staus: body.staus, + type: body.type, } ) res.send({ diff --git a/src/controllers/open/ad-uid.js b/src/controllers/open/ad-uid.js new file mode 100644 index 0000000..8324003 --- /dev/null +++ b/src/controllers/open/ad-uid.js @@ -0,0 +1,55 @@ +/* 推广系统-广告专用 */ + +import {Router} from 'express' +import AdUid from '../../models/admin/AdUid' +import RedisDao from '../../redis/redis.dao' + +import cors from 'cors' + +const router = new Router() +const redisDao = new RedisDao() + +// 获取广告区域列表 +router.get('/', cors(), async (req, res, next) => { + try { + const query = req.query + const game_id = query.gameid + const platform_id = query.channelid + const key = `ad-ratio:${game_id}:${platform_id}` + const ratio = await redisDao.getByKey(key) + + let records = await AdUid.find({ + game_id, + platform_id, + deleted: false, + }) + + records = JSON.parse(JSON.stringify(records)) + + let cfg = {} + if (ratio) { + const arr = ratio.split('\n') + arr.map(item => { + const kv = item.split('-') + cfg[kv[0]] = kv[1] + }) + } + + const result = records.map(item => { + item.ratio = item.ratio ? item.ratio : cfg[item.channel_name] || 100 + return { + adChannel: item.channel_id, + ratio: parseFloat(item.ratio), + } + }) + + res.send({ + errcode: 0, + result, + }) + } catch (err) { + next(err) + } +}) + +export default router diff --git a/src/controllers/open/index.js b/src/controllers/open/index.js index 340b1fc..e3a956d 100644 --- a/src/controllers/open/index.js +++ b/src/controllers/open/index.js @@ -4,12 +4,14 @@ import {Router} from 'express' import promotionRouter from './promotion' import gamesRouter from './games' +import adUidRouter from './ad-uid' const router = new Router() router.use('/promotion', promotionRouter) router.use('/games', gamesRouter) +router.use('/ad-uid', adUidRouter) export default router diff --git a/src/models/admin/AdUid.js b/src/models/admin/AdUid.js index bbdd203..aa98d8f 100644 --- a/src/models/admin/AdUid.js +++ b/src/models/admin/AdUid.js @@ -13,7 +13,8 @@ const AdUid = new mongoose.Schema( channel_name: {type: String}, channel_id: {type: String}, deleted: {type: Boolean, default: false}, - appid: {type: String} + appid: {type: String}, + ratio: {type: Number} }, { collection: 'ad_uid', diff --git a/src/models/admin/GameGift.js b/src/models/admin/GameGift.js index 047aa9f..e391688 100644 --- a/src/models/admin/GameGift.js +++ b/src/models/admin/GameGift.js @@ -10,6 +10,7 @@ const GameGiftSchema = new mongoose.Schema( gift_name: {type: String}, gift_url: {type: String}, game_id: {type: String}, + type: {type: Number}, // 奖品类型 codes: [{type: String}], // 兑换码 used_codes: [{type: String}], // 已使用兑换码 status: {type: Number, default: 0}, // 0-正常, 1-停用 @@ -59,8 +60,10 @@ const GiftRecordSchema = new mongoose.Schema( gift_id: {type: String}, gift_name: {type: String}, gift_url: {type: String}, + type: {type: Number}, }, code: {type: String}, + used: {type: Boolean, default: false}, }, { collection: 'gift_record',