Authored by 郝肖肖

'invite-ctx'

... ... @@ -18,7 +18,7 @@ const secretKey = '5466ee572bcbc75830d044e66ab429bc';// 秘钥
判断邀请 好友 类型
*/
exports.checkType = function(req, res, next) {
inviteModel.checkType()
req.ctx(inviteModel).checkType()
.then(function(result) {
if (result.code === 200 && result.data) {
res.locals.isVipDay = true;
... ... @@ -40,7 +40,7 @@ exports.index = (req, res, next) => {
let uid = req.user.uid || req.query.uid || 0;
let renderPage = 'invite/list';
inviteModel.index({
req.ctx(inviteModel).index({
uid: uid,
activityId: actId
}).then((result) => {
... ... @@ -70,9 +70,9 @@ exports.share = (req, res, next) => {
let shareUid = req.params[0];
let actId = req.params[1];
let nums = req.params[2];
let shareUrl = inviteModel.createShareUrl(shareUid, actId, nums);
let shareUrl = req.ctx(inviteModel).createShareUrl(shareUid, actId, nums);
let callback = 'http://m.yohobuy.com/activity/invite/getwxinfo?url=' + shareUrl;
let url = inviteModel.getWxOauthUrl(callback);
let url = req.ctx(inviteModel).getWxOauthUrl(callback);
let wxUserInfo = req.cookies.wxUserInfo || {};
if (_.isEmpty(wxUserInfo) || _.isEmpty(wxUserInfo.unionid)) {
... ... @@ -80,7 +80,7 @@ exports.share = (req, res, next) => {
return false;
}
inviteModel.shareModel({
req.ctx(inviteModel).shareModel({
uid: shareUid,
activityId: actId,
nums: nums,
... ... @@ -122,7 +122,7 @@ exports.share = (req, res, next) => {
exports.sendRegCodeToMobile = (req, res, next) => {
let mobile = req.query.mobile || '';
inviteModel.sendRegCodeToMobile({
req.ctx(inviteModel).sendRegCodeToMobile({
area: 86,
mobile: mobile
}).then((result) => {
... ... @@ -135,7 +135,7 @@ exports.checkOldUserCoupon = (req, res, next) => {
let mobile = req.query.mobile || '';
let actId = req.query.actId || '';
inviteModel.checkOldUserCoupon({
req.ctx(inviteModel).checkOldUserCoupon({
mobile: mobile,
activityId: actId
}).then((result) => {
... ... @@ -148,7 +148,7 @@ exports.validRegCode = (req, res, next) => {
let mobile = req.query.mobile || '';
let code = req.query.code || '';
inviteModel.validRegCode({
req.ctx(inviteModel).validRegCode({
area: 86,
mobile: mobile,
code: code
... ... @@ -162,7 +162,7 @@ exports.register = (req, res, next) => {
let mobile = req.query.mobile || '';
let activityName = req.query.activityName || 'invite';
inviteModel.register({
req.ctx(inviteModel).register({
mobile: mobile,
activityName: activityName
}).then((result) => {
... ... @@ -178,7 +178,7 @@ exports.receiveCoupons = (req, res, next) => {
let shareUid = req.query.shareUid;
let openId = req.query.openId;
inviteModel.receiveCoupons({
req.ctx(inviteModel).receiveCoupons({
uid: uid,
activityId: actId,
nums: nums,
... ... @@ -209,7 +209,7 @@ exports.myCoupons = (req, res, next) => {
return false;
}
inviteModel.myCoupons({
req.ctx(inviteModel).myCoupons({
uid: uid,
shareUid: shareUid,
nums: nums,
... ... @@ -250,7 +250,7 @@ exports.getwxinfo = (req, res, next) => {
let url = req.query.url;
let code = req.query.code;
inviteModel.getWxUserInfo({
req.ctx(inviteModel).getWxUserInfo({
code: code
}).then((result) => {
if (result === false) {
... ...
... ... @@ -11,10 +11,7 @@ module.exports = class extends global.yoho.BaseModel {
return this.get({
data: Object.assign({
method: 'app.activity.receviceYohoCoin'
}, params),
param: {
cache: true
}
}, params)
});
}
};
... ...
... ... @@ -83,9 +83,6 @@ class featureModel extends global.yoho.BaseModel {
data: {
method: 'app.activity.template.ignoreCache',
activity_id: params.code
},
param: {
cache: true
}
});
} else {
... ...
'use strict';
const api = global.yoho.API;
const serviceAPI = global.yoho.ServiceAPI;
const _ = require('lodash');
const rp = require('request-promise');
... ... @@ -10,210 +9,227 @@ const wxCode = {
wxAppSecret: 'ce21ae4a3f93852279175a167e54509b'
};
/**
* 获取微信授权地址
* @param callback
* @returns {string}
*/
const getWxOauthUrl = (callback) => {
return 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
wxCode.wxAppId + '&redirect_uri=' + callback +
'&response_type=code&scope=snsapi_userinfo#wechat_redirect';
};
/**
* 生成分享url
* @param shareUid
* @param actId
* @param nums
* @returns {string}
*/
const createShareUrl = (shareUid, actId, nums) => {
return 'http://m.yohobuy.com/activity/invite/share_' + shareUid + '_' + actId + '_' + nums + '.html';
};
/**
* 根据第三方id,查询绑定信息
* @param {[string || array]} openIds 第三方id数组
* @return {[array]}
*/
const getBindLogByOpenId = (openIds) => {
openIds = _.isArray(openIds) ? openIds : [openIds];
return api.get('', {
method: 'wap.invite.getBindLogByOpenId',
openIds: openIds.join(',')
});
};
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 获取微信授权地址
* @param callback
* @returns {string}
*/
getWxOauthUrl(callback) {
return 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
wxCode.wxAppId + '&redirect_uri=' + callback +
'&response_type=code&scope=snsapi_userinfo#wechat_redirect';
}
/**
* 生成分享url
* @param shareUid
* @param actId
* @param nums
* @returns {string}
*/
createShareUrl(shareUid, actId, nums) {
return 'http://m.yohobuy.com/activity/invite/share_' + shareUid + '_' + actId + '_' + nums + '.html';
}
/**
* 根据第三方id,查询绑定信息
* @param {[string || array]} openIds 第三方id数组
* @return {[array]}
*/
getBindLogByOpenId(openIds) {
openIds = _.isArray(openIds) ? openIds : [openIds];
return this.get({
data: {
method: 'wap.invite.getBindLogByOpenId',
openIds: openIds.join(',')
}
});
}
/**
* 合并第三方头像和昵称
* @param {[array]} data 邀请的用户列表,最主要的是openId字段
* @return {[array]}
*/
mergeBindLogDate(data) {
let openIds = [];
let photo = '//static.yohobuy.com/m/v1/activity/newyear/images/108.png';
// 取5条
data = _.slice(data, 0, 5);
_.forEach(data, (req) => {
openIds.push(req.openId);
});
/**
* 合并第三方头像和昵称
* @param {[array]} data 邀请的用户列表,最主要的是openId字段
* @return {[array]}
*/
const mergeBindLogDate = (data) => {
let openIds = [];
let photo = '//static.yohobuy.com/m/v1/activity/newyear/images/108.png';
// 取5条
data = _.slice(data, 0, 5);
_.forEach(data, (req) => {
openIds.push(req.openId);
});
return getBindLogByOpenId(openIds).then(result => {
if (!_.isEmpty(result.data)) {
_.forEach(data, (req, key) => {
data[key].img = photo;
_.forEach(result.data, (bind) => {
if (req.openId === bind.openId) {
data[key].img = _.isEmpty(bind.snsHeadimg) ?
photo : bind.snsHeadimg;
data[key].nick = bind.snsNick;
}
return this.getBindLogByOpenId(openIds).then(result => {
if (!_.isEmpty(result.data)) {
_.forEach(data, (req, key) => {
data[key].img = photo;
_.forEach(result.data, (bind) => {
if (req.openId === bind.openId) {
data[key].img = _.isEmpty(bind.snsHeadimg) ?
photo : bind.snsHeadimg;
data[key].nick = bind.snsNick;
}
});
});
});
}
return data;
});
};
}
return data;
});
}
/**
* 获取分享页面列表数据
* @param {[int]} uid 用户id
* @param {[int]} activityId 活动id
* @return {[array]}
*/
index(params) {
params = params || {};
return this.get({
data: Object.assign({
method: 'wap.invite.index'
}, params)
}).then((result) => {
let firstData = {
isNil: false,
isEmpty: false,
isFive: false,
isGo: false,
remainData: ['', '', '', '', ''],
data: []
};
switch (result.code) {
case 401:
// 没有分享记录
firstData.isNil = true;
break;
case 200:
return this.mergeBindLogDate(result.data).then(data => {
let len = data.length;
// 判断是否否5条分享
result.data = data;
if (len === 0) {
firstData.isEmpty = true;
} else if (len < 5) {
firstData.remainData =
_.slice(firstData.remainData, 0, 5 - len);
} else {
firstData.isFive = true;
firstData.remainData = [];
}
return Object.assign(firstData, result);
});
default:
// 活动状态不正确
firstData.isGo = true;
break;
}
/**
* 获取分享页面列表数据
* @param {[int]} uid 用户id
* @param {[int]} activityId 活动id
* @return {[array]}
*/
const index = (params) => {
params = params || {};
return api.get('', Object.assign({
method: 'wap.invite.index'
}, params
)).then((result) => {
return Object.assign(firstData, result);
});
}
/**
* 通过手机号发送验证码
* @param {[int]} area 区域,中国:86
* @param {[string]} mobile 手机号
* @return {[array]}
*/
sendRegCodeToMobile(params) {
return this.get({
data: Object.assign({
method: 'app.register.sendRegCodeToMobile'
}, params)
});
}
/**
* 发送已注册用户参与活动的优惠券
* @param {[string]} mobile 手机号
* @param {[int]} activityId 活动id
* @return {[array]}
*/
checkOldUserCoupon(params) {
return this.get({
data: Object.assign({
method: 'wap.invite.checkOldUserCoupon'
}, params)
});
}
/**
* 验证手机验证码是否正确
* @param {[int]} area 区域,中国:86
* @param {[string]} mobile 手机号
* @param {[int]} code 验证码
* @return {[array]}
*/
validRegCode(params) {
return this.get({
data: Object.assign({
method: 'app.register.validRegCode'
}, params)
});
}
/**
* 手机账号注册
* @param {[string]} mobile 手机号
* @param {[string]} activityName 活动名称
* @return {[array]}
*/
register(params) {
return this.get({
data: Object.assign({
method: 'wap.invite.register'
}, params)
});
}
/**
* 微信好友获取红包方法(即分享出去的地址)
* @param {[int]} uid 分享用户id
* @param {[int]} activityId 活动id
* @param {[int]} nums 发送优惠券的数量
* @param {[String]} openId 微信的union_id
* @param {[String]} nickName 微信昵称
* @param {[String]} headImgUrl 微信头像
* @returns {[array]}
*/
shareModel(params) {
let firstData = {
isNil: false,
isEmpty: false,
isFive: false,
isGo: false,
remainData: ['', '', '', '', ''],
data: []
};
switch (result.code) {
case 401:
// 没有分享记录
firstData.isNil = true;
break;
case 200:
return mergeBindLogDate(result.data).then(data => {
let len = data.length;
// 判断是否否5条分享
result.data = data;
if (len === 0) {
firstData.isEmpty = true;
} else if (len < 5) {
firstData.remainData =
_.slice(firstData.remainData, 0, 5 - len);
} else {
firstData.isFive = true;
firstData.remainData = [];
}
return Object.assign(firstData, result);
});
default:
// 活动状态不正确
firstData.isGo = true;
break;
}
return Object.assign(firstData, result);
});
};
/**
* 通过手机号发送验证码
* @param {[int]} area 区域,中国:86
* @param {[string]} mobile 手机号
* @return {[array]}
*/
const sendRegCodeToMobile = (params) => {
return api.get('', Object.assign({
method: 'app.register.sendRegCodeToMobile'
}, params));
};
/**
* 发送已注册用户参与活动的优惠券
* @param {[string]} mobile 手机号
* @param {[int]} activityId 活动id
* @return {[array]}
*/
const checkOldUserCoupon = (params) => {
return api.get('', Object.assign({
method: 'wap.invite.checkOldUserCoupon'
}, params));
};
/**
* 验证手机验证码是否正确
* @param {[int]} area 区域,中国:86
* @param {[string]} mobile 手机号
* @param {[int]} code 验证码
* @return {[array]}
*/
const validRegCode = (params) => {
return api.get('', Object.assign({
method: 'app.register.validRegCode'
}, params));
};
/**
* 手机账号注册
* @param {[string]} mobile 手机号
* @param {[string]} activityName 活动名称
* @return {[array]}
*/
const register = (params) => {
return api.get('', Object.assign({
method: 'wap.invite.register'
}, params));
};
/**
* 微信好友获取红包方法(即分享出去的地址)
* @param {[int]} uid 分享用户id
* @param {[int]} activityId 活动id
* @param {[int]} nums 发送优惠券的数量
* @param {[String]} openId 微信的union_id
* @param {[String]} nickName 微信昵称
* @param {[String]} headImgUrl 微信头像
* @returns {[array]}
*/
const shareModel = (params) => {
let firstData = {
isEmpty: false,
isFive: false,
isGo: false,
remainData: ['', '', '', '', ''],
data: []
};
let listData = [];
// 这里面的逻辑就是获取第三方用户的头像和昵称,然后插入数据和更新数据
return api.get('', Object.assign({
method: 'wap.invite.share'
}, params))
.then((result) => {
let listData = [];
// 这里面的逻辑就是获取第三方用户的头像和昵称,然后插入数据和更新数据
return this.get({
data: Object.assign({
method: 'wap.invite.share'
}, params)
}).then((result) => {
// list为空,说明不是分享者本人
if (result.code !== 200 || _.isEmpty(result.data.list)) {
return result;
}
return mergeBindLogDate(result.data.list).then(data => {
return this.mergeBindLogDate(result.data.list).then(data => {
let len = data.length;
// 判断是否满5条分享
... ... @@ -237,134 +253,125 @@ const shareModel = (params) => {
return result;
});
});
};
}
/**
* 邀请好友赢福利之后领取优惠券
* @param {[int]} uid 用户id
* @param {[int]} activityId 活动id
* @param {[int]} nums 发送优惠券的数量
* @param {[int]} shareUid 分享者的uid
* @param {[string]} openId 微信的union_id
* @returns {[array]}
*/
receiveCoupons(params) {
return this.get({
data: Object.assign({
method: 'wap.invite.receiveCoupons'
}, params)
});
}
/**
* 获取分享列表和用户信息
* @param {[int]} uid 用户id
* @param {[int]} shareUid 分享着uid
* @param {[int]} nums 发送优惠券的数量
* @param {[int]} amount 金额
* @param {[int]} activityId 活动id
* @return {[array]}
*/
myCoupons(params) {
let mobile;
let firstData = {
isEmpty: false,
isFive: false,
isGo: false,
remainData: ['', '', '', '', ''],
data: []
};
let listData = [];
/**
* 邀请好友赢福利之后领取优惠券
* @param {[int]} uid 用户id
* @param {[int]} activityId 活动id
* @param {[int]} nums 发送优惠券的数量
* @param {[int]} shareUid 分享者的uid
* @param {[string]} openId 微信的union_id
* @returns {[array]}
*/
const receiveCoupons = (params) => {
return api.get('', Object.assign({
method: 'wap.invite.receiveCoupons'
}, params));
};
return this.get({
data: Object.assign({
method: 'wap.invite.myCoupons'
}, params)
}).then((result) => {
/**
* 获取分享列表和用户信息
* @param {[int]} uid 用户id
* @param {[int]} shareUid 分享着uid
* @param {[int]} nums 发送优惠券的数量
* @param {[int]} amount 金额
* @param {[int]} activityId 活动id
* @return {[array]}
*/
const myCoupons = (params) => {
let mobile;
let firstData = {
isEmpty: false,
isFive: false,
isGo: false,
remainData: ['', '', '', '', ''],
data: []
};
let listData = [];
return api.get('', Object.assign({
method: 'wap.invite.myCoupons'
}, params)).then((result) => {
if (result.code !== 200) {
return result;
}
return mergeBindLogDate(result.data.list).then(data => {
listData = data;
if (listData.length < 5) {
firstData.remainData =
_.slice(firstData.remainData, 0, 5 - listData.length);
} else {
firstData.isFive = true;
firstData.remainData = [];
if (result.code !== 200) {
return result;
}
firstData.data = listData;
result.data.list = firstData;
return this.mergeBindLogDate(result.data.list).then(data => {
listData = data;
mobile = result.data.myProfile.mobile;
result.data.myProfile.encMobile = mobile.replace(mobile.substring(3, 7), '****');
if (listData.length < 5) {
firstData.remainData =
_.slice(firstData.remainData, 0, 5 - listData.length);
} else {
firstData.isFive = true;
firstData.remainData = [];
}
// 释放内存
listData = [];
return [result.data.list, result.data.myProfile];
});
});
};
firstData.data = listData;
result.data.list = firstData;
/**
* 授权后获取微信用户信息
* @return {[array]}
*/
const getWxUserInfo = (params) => {
let url1 = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' +
wxCode.wxAppId + '&secret=' + wxCode.wxAppSecret + '&code=' +
params.code + '&grant_type=authorization_code';
let url2 = 'https://api.weixin.qq.com/sns/userinfo?lang=zh_CN';
return rp({
url: url1,
qs: {},
json: true,
gzip: true,
timeout: 3000
}).then((result) => {
if (_.isEmpty(result.openid)) {
return false;
}
url2 = url2 + '&access_token=' + result.access_token +
'&openid=' + result.openid;
mobile = result.data.myProfile.mobile;
result.data.myProfile.encMobile = mobile.replace(mobile.substring(3, 7), '****');
// 释放内存
listData = [];
return [result.data.list, result.data.myProfile];
});
});
}
/**
* 授权后获取微信用户信息
* @return {[array]}
*/
getWxUserInfo(params) {
let url1 = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' +
wxCode.wxAppId + '&secret=' + wxCode.wxAppSecret + '&code=' +
params.code + '&grant_type=authorization_code';
let url2 = 'https://api.weixin.qq.com/sns/userinfo?lang=zh_CN';
return rp({
url: url2,
url: url1,
qs: {},
json: true,
gzip: true,
timeout: 3000
}).then((result2) => {
return result2;
});
});
};
/**
* 获取 邀请好友赢福利的类型:
* 1. 普通的要求好友赢福利
* 2. 会员日的邀请好友 赢福利
*/
const checkType = () => {
const url = '/activity/UserdaySigninController/isUserday';
return serviceAPI.get(url, {});
};
}).then((result) => {
if (_.isEmpty(result.openid)) {
return false;
}
module.exports = {
index,
checkType,
shareModel,
getBindLogByOpenId,
sendRegCodeToMobile,
checkOldUserCoupon,
validRegCode,
register,
receiveCoupons,
myCoupons,
createShareUrl,
getWxOauthUrl,
getWxUserInfo
url2 = url2 + '&access_token=' + result.access_token +
'&openid=' + result.openid;
return rp({
url: url2,
qs: {},
json: true,
gzip: true,
timeout: 3000
}).then((result2) => {
return result2;
});
});
}
/**
* 获取 邀请好友赢福利的类型:
* 1. 普通的要求好友赢福利
* 2. 会员日的邀请好友 赢福利
*/
checkType() {
return this.get({
api: serviceAPI,
url: '/activity/UserdaySigninController/isUserday',
data: {}
});
}
};
... ...