|
|
'use strict';
|
|
|
const api = global.yoho.API;
|
|
|
const camelCase = global.yoho.camelCase;
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
/* 微信相关 */
|
|
|
const wxCode = {
|
|
|
wxAppId: 'wx75e5a7c0c88e45c2',
|
|
|
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(',')
|
|
|
});
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 合并第三方头像和昵称
|
|
|
* @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 data;
|
|
|
});
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 获取分享页面列表数据
|
|
|
* @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) => {
|
|
|
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, camelCase(result));
|
|
|
});
|
|
|
default:
|
|
|
// 活动状态不正确
|
|
|
firstData.isGo = true;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
return Object.assign(firstData, camelCase(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) => {
|
|
|
// list为空,说明不是分享者本人
|
|
|
if (result.code !== 200 || _.isEmpty(result.data.list)) {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
return mergeBindLogDate(result.data.list).then(data => {
|
|
|
let len = data.length;
|
|
|
|
|
|
// 判断是否满5条分享
|
|
|
listData = 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 = [];
|
|
|
}
|
|
|
|
|
|
firstData.data = listData;
|
|
|
result.data.list = firstData;
|
|
|
|
|
|
// 释放内存
|
|
|
listData = [];
|
|
|
return camelCase(result);
|
|
|
});
|
|
|
});
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 邀请好友赢福利之后领取优惠券
|
|
|
* @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));
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 获取分享列表和用户信息
|
|
|
* @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 = [];
|
|
|
}
|
|
|
|
|
|
firstData.data = listData;
|
|
|
result.data.list = firstData;
|
|
|
|
|
|
mobile = result.data.myProfile.mobile;
|
|
|
result.data.myProfile.encMobile = mobile.replace(mobile.substring(3, 7), '****');
|
|
|
|
|
|
// 释放内存
|
|
|
listData = [];
|
|
|
return camelCase([result.data.list, result.data.myProfile]);
|
|
|
});
|
|
|
});
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 授权后获取微信用户信息
|
|
|
* @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 api._requestFromAPI({
|
|
|
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;
|
|
|
return api._requestFromAPI({
|
|
|
url: url2,
|
|
|
qs: {},
|
|
|
json: true,
|
|
|
gzip: true,
|
|
|
timeout: 3000
|
|
|
})
|
|
|
.then((result2) => {
|
|
|
return result2;
|
|
|
});
|
|
|
});
|
|
|
};
|
|
|
|
|
|
module.exports = {
|
|
|
index,
|
|
|
shareModel,
|
|
|
getBindLogByOpenId,
|
|
|
sendRegCodeToMobile,
|
|
|
checkOldUserCoupon,
|
|
|
validRegCode,
|
|
|
register,
|
|
|
receiveCoupons,
|
|
|
myCoupons,
|
|
|
createShareUrl,
|
|
|
getWxOauthUrl,
|
|
|
getWxUserInfo
|
|
|
}; |
...
|
...
|
|