Authored by 陈峰

merge

const redbagModel = require('../models/red-envelope');
const headerModel = require('../../../doraemon/models/header'); // 头部model
const valid = require('../../../utils/validator');
const logger = global.yoho.logger;
const redbagPage = async(req, res, next) => {
try {
... ... @@ -85,10 +86,36 @@ const receiveRedBag = async(req, res, next) => {
return next(error);
}
};
const submitWxCode = async(req, res) => {
try {
let obj = {uid: req.user.uid || null,
shareCode: req.body.shareCode || '',
userCode: req.body.userCode || ''};
obj = valid(obj, {
uid: {type: 'uid', empty: false},
shareCode: {type: 'String', empty: false},
userCode: {type: 'String', empty: false}
});
let userInfo = await req.ctx(redbagModel).renderByCs(obj);
obj.userInfo = userInfo.data;
let result = await req.ctx(redbagModel).submitWxCode(obj);
return res.json({code: 200, data: result, result: true});
} catch (e) {
logger.error(e);
return res.json(e);
}
};
module.exports = {
redbagPage,
receiveRedBag
receiveRedBag,
submitWxCode
};
... ...
const logger = global.yoho.logger;
class redEnvelopeModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
... ... @@ -24,13 +26,48 @@ class redEnvelopeModel extends global.yoho.BaseModel {
}
async renderByCs(obj) {
return this.get({
data: {
method: 'app.passport.getUserInfoByShareCode',
uid: obj.uid,
shareCode: obj.shareCode
try {
let result = this.get({
data: {
method: 'app.passport.getUserInfoByShareCode',
uid: obj.uid,
shareCode: obj.shareCode
}
});
if (result.code) {
return Promise.resolve({code: 200, result: true, data: result.data});
} else {
logger.error(result);
return Promise.reject({code: 301, result: false, msg: '数据错误'});
}
});
} catch (e) {
logger.error(e);
return Promise.reject({code: 301, result: false, msg: '数据错误'});
}
}
async submitWxCode(obj) {
try {
let result = this.get({
data: {
method: 'app.passport.submitUserWechatCode',
uid: obj.userInfo,
wechat_uid: obj.uid,
userCode: obj.userCode
}
});
if (result.code) {
return Promise.resolve({code: 200, result: true, data: result.data});
} else {
logger.error(result);
return Promise.reject({code: 301, result: false, msg: '数据错误'});
}
} catch (e) {
logger.error(e);
return Promise.reject({code: 301, result: false, msg: '数据错误'});
}
}
async renderByUser({uid, shareCode}) {
... ...
... ... @@ -341,4 +341,5 @@ router.post('/have-gain/submitApply', auth, haveGain.submitApply); // 有货有
router.get('/red-envelope/index', auth, redPack.redbagPage);
router.post('/red-envelope/receive', auth, redPack.receiveRedBag);
router.post('/red-envelope/submitWxCode', auth, redPack.submitWxCode);
module.exports = router;
... ...
... ... @@ -222,6 +222,37 @@ const _booleanValid = (option) => {
}
};
const _objectValid = (option) => {
const value = option.value;
// 设置默认值
option.require = option.require || false;
option.empty = option.empty || true;
if (value === undefined || value === null) {
if (option.require === true) {
throw _errJson(`${option.param} 参数是必须的`);
} else {
return null;
}
}
if (typeof value === 'object') {
let valueArr = Object.keys(value);
if (!valueArr.length) {
if (option.empty === false) {
throw _errJson(`${option.param} 参数不能为空`);
} else {
return null;
}
} else {
return value;
}
} else {
throw _errJson(`${option.param} 参数格式错误`);
}
};
/*
* data: 需要校验的数据对象
* option: 校验规则对象,eg: {'name', {type: 'string', require: true}}
... ... @@ -253,6 +284,12 @@ const valid = (data, option) => {
delete data[item];
}
break;
case 'object':
data[item] = _objectValid(opt);
if (data[item] === undefined) {
delete data[item];
}
break;
}
});
... ...