发布分享图时, 写到redis
This commit is contained in:
parent
53ba957d6b
commit
625d15c0c9
@ -4,12 +4,19 @@ import SystemDic from '../../models/admin/SystemDic'
|
||||
import ChinaArea from '../../models/snoopy/ChinaArea'
|
||||
import {Router} from 'express'
|
||||
import logger from '../../utils/logger'
|
||||
import RedisDao from '../../redis/redis.dao'
|
||||
|
||||
|
||||
const router = new Router()
|
||||
const GameInfo = getGameInfoModel()
|
||||
const GameInfoTest = getGameInfoModel('test')
|
||||
const GameShareImage = getGameShareImage()
|
||||
const GameShareImageTest = getGameShareImage('test')
|
||||
|
||||
const PREFIX = 'game_share_img';
|
||||
|
||||
const redisDao = new RedisDao(true);
|
||||
|
||||
// 获取分享型列表
|
||||
router.get('/sys_dics', async (req, res, next) => {
|
||||
const query = req.query
|
||||
@ -204,69 +211,40 @@ router.post('/save_share', async (req, res, next) => {
|
||||
delete body.isDev
|
||||
|
||||
try {
|
||||
const _id = body._id
|
||||
let _id = body._id;
|
||||
const search = await GameShareImageModel.findById(_id)
|
||||
let result, gameId;
|
||||
if (search) {
|
||||
gameId = search.game_id;
|
||||
// 更新
|
||||
const result = await GameShareImageModel.updateOne({_id: _id}, body)
|
||||
res.send({
|
||||
errcode: 0,
|
||||
result: result,
|
||||
})
|
||||
result = await GameShareImageModel.updateOne({_id: _id}, body)
|
||||
} else {
|
||||
// 新建
|
||||
const newShare = GameShareImageModel(body)
|
||||
const result = await newShare.save()
|
||||
res.send({
|
||||
errcode: 0,
|
||||
result: result,
|
||||
const newShare = GameShareImageModel(body);
|
||||
_id = newShare.id;
|
||||
gameId = newShare.game_id;
|
||||
result = await newShare.save()
|
||||
}
|
||||
if (!isDev) {
|
||||
process.nextTick(async function () {
|
||||
try {
|
||||
let redisStr = await GameShareImageModel.toRedisStr(_id);
|
||||
redisDao.hset(`${PREFIX}:${gameId}`, _id, redisStr);
|
||||
} catch (err) {
|
||||
logger.error(err);
|
||||
}
|
||||
})
|
||||
}
|
||||
} catch (err) {
|
||||
next(err)
|
||||
}
|
||||
})
|
||||
|
||||
// 更新一个分享图
|
||||
router.post('/update_share', async (req, res, next) => {
|
||||
logger.db(req, '游戏管理', '分享图', '更新分享图')
|
||||
// 权限判断
|
||||
const hasPerm =
|
||||
req.user.permissions.includes(`${req.body.uid}-readable`) ||
|
||||
req.user.permissions.includes(`${req.body.uid}-edit`) ||
|
||||
req.user.permissions.includes(`${req.body.uid}-publish`) ||
|
||||
req.user.permissions.includes(`games-writeable`)
|
||||
if (!hasPerm) {
|
||||
res.status(403).send({
|
||||
errcode: 1,
|
||||
errmsg: '用户无此游戏分享图编辑权限!',
|
||||
res.send({
|
||||
errcode: 0,
|
||||
result: result,
|
||||
})
|
||||
return
|
||||
}
|
||||
const body = req.body
|
||||
|
||||
const id = body._id
|
||||
delete body._id
|
||||
delete body.uid
|
||||
|
||||
try {
|
||||
const search = await GameShareImage.findOne({deleted: false, _id: id})
|
||||
if (search) {
|
||||
const result = await GameShareImage.updateOne({_id: id}, body)
|
||||
res.send({
|
||||
errcode: 0,
|
||||
})
|
||||
} else {
|
||||
res.send({
|
||||
errcode: 1,
|
||||
errmsg: '分享图不存在或已被删除!',
|
||||
})
|
||||
}
|
||||
} catch (err) {
|
||||
next(err)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
// 删除分享图
|
||||
router.post('/del_share', async (req, res, next) => {
|
||||
// 权限判断
|
||||
@ -284,7 +262,8 @@ router.post('/del_share', async (req, res, next) => {
|
||||
logger.db(req, '游戏管理', '分享图', '删除分享图')
|
||||
const user = req.user
|
||||
const body = req.body
|
||||
const isDev = body.isDev
|
||||
const isDev = body.isDev;
|
||||
const gameId = body.gameId;
|
||||
const GameShareImageModel = isDev ? GameShareImageTest : GameShareImage
|
||||
delete body.isDev
|
||||
|
||||
@ -300,6 +279,11 @@ router.post('/del_share', async (req, res, next) => {
|
||||
deletedBy: user.username,
|
||||
})
|
||||
.exec()
|
||||
if (!isDev && ids.length > 0) {
|
||||
process.nextTick(function () {
|
||||
redisDao.hdel(`${PREFIX}:${gameId}`, ids);
|
||||
})
|
||||
}
|
||||
res.json({errcode: 0, errmsg: '', count: record.n})
|
||||
} catch (err) {
|
||||
next(err)
|
||||
|
@ -97,6 +97,43 @@ function getGameShareImageModel(type) {
|
||||
return record
|
||||
}
|
||||
|
||||
GameShareImageModel.toRedisStr = function (id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
GameShareImageModel.findById(id).populate('area', 'locations')
|
||||
.then(obj =>{
|
||||
if (!obj) {
|
||||
reject('no record');
|
||||
} else {
|
||||
let result = {
|
||||
game_id: obj.game_id,
|
||||
platform_id: obj.platform_id,
|
||||
default_share: obj.default_share,
|
||||
area: obj.area && obj.area.locations ? obj.area.locations : [],
|
||||
type: obj.share_type,
|
||||
str: obj.share_word,
|
||||
image: obj.share_image,
|
||||
images: obj.share_images,
|
||||
strs: obj.share_words,
|
||||
ad_first: obj.type,
|
||||
share_count: obj.share_count,
|
||||
ad_count: obj.ad_count,
|
||||
ad_cd: obj.ad_cd,
|
||||
ad_id: obj.ad_id
|
||||
};
|
||||
try {
|
||||
let resultStr = JSON.stringify(result);
|
||||
resolve(resultStr);
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
reject(err);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
GameShareImageModel.edit_validate = () => {
|
||||
return {
|
||||
form: '#edit_form',
|
||||
|
@ -1,18 +1,18 @@
|
||||
'use strict';
|
||||
import RedisClient from './redis_client';
|
||||
|
||||
const redis_client = new RedisClient().client;
|
||||
/*
|
||||
* 操作redis的常用方法
|
||||
* */
|
||||
export default class RedisDao {
|
||||
constructor() {
|
||||
|
||||
constructor(isPublish) {
|
||||
isPublish = isPublish || false;
|
||||
this.redisClient = RedisClient.getInstance(isPublish).client;
|
||||
}
|
||||
|
||||
async scanAsync(cursor, pattern, results) {
|
||||
const that = this;
|
||||
const reply = await redis_client.scanAsync(cursor, 'MATCH', pattern, 'COUNT', '1000');
|
||||
const reply = await this.redisClient.scanAsync(cursor, 'MATCH', pattern, 'COUNT', '1000');
|
||||
cursor = reply[0];
|
||||
results = results.concat(reply[1]);
|
||||
if (cursor === '0') {
|
||||
@ -23,14 +23,30 @@ export default class RedisDao {
|
||||
}
|
||||
|
||||
getByKey(key) {
|
||||
return redis_client.getAsync(key);
|
||||
return this.redisClient.getAsync(key);
|
||||
}
|
||||
|
||||
updateOneKey(key, value) {
|
||||
return redis_client.setAsync(key, value);
|
||||
return this.redisClient.setAsync(key, value);
|
||||
}
|
||||
|
||||
deleteKeys(key) {
|
||||
return redis_client.delAsync(key);
|
||||
return this.redisClient.delAsync(key);
|
||||
}
|
||||
|
||||
hset(key, field, value) {
|
||||
return this.redisClient.hsetAsync(key, field, value);
|
||||
}
|
||||
|
||||
hget(key, field) {
|
||||
return this.redisClient.hgetAsync(key, field);
|
||||
}
|
||||
|
||||
hgetall(key) {
|
||||
return this.redisClient.hgetallAsync(key);
|
||||
}
|
||||
|
||||
hdel(key, fields) {
|
||||
return this.redisClient.hdelAsync(key, fields);
|
||||
}
|
||||
}
|
||||
|
@ -9,10 +9,13 @@ Promise.promisifyAll(redis.RedisClient.prototype);
|
||||
Promise.promisifyAll(redis.Multi.prototype);
|
||||
|
||||
export default class RedisClient {
|
||||
constructor() {
|
||||
this.client = redis.createClient({port: config.redis.port,
|
||||
host: config.redis.host,
|
||||
password: config.redis.password,
|
||||
constructor(isPublish) {
|
||||
let port = isPublish ? config.redisPublish.port : config.redis.host;
|
||||
let host = isPublish ? config.redisPublish.host : config.redis.host;
|
||||
let psssword = isPublish ? config.redisPublish.password : config.redis.password;
|
||||
this.client = redis.createClient({port: port,
|
||||
host: host,
|
||||
password: psssword,
|
||||
retry_strategy: function(options) {
|
||||
if (options.error && options.error.code === 'ECONNREFUSED') {
|
||||
// End reconnecting on a specific error and flush all commands with a individual error
|
||||
@ -50,4 +53,11 @@ export default class RedisClient {
|
||||
set client(client) {
|
||||
this._client = client;
|
||||
}
|
||||
// 20200310 改成单例, 保持一个连接
|
||||
static getInstance(isPublish) {
|
||||
if (!this[`instance_${isPublish}`]) {
|
||||
this[`instance_${isPublish}`] = new RedisClient(isPublish);
|
||||
}
|
||||
return this[`instance_${isPublish}`]
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user