Authored by 陈峰

Merge branch 'release/6.9.5' into 'master'

增加分享接口



See merge request !101
  1 +const shareModel = require('../models/share');
  2 +
  3 +const shareController = {
  4 + collect(req, res, next) {
  5 + const {uid, fid} = req.query;
  6 +
  7 + if (!uid || !fid) {
  8 + return res.status(200).end();
  9 + }
  10 + req.ctx(shareModel).collect({uid, fid});
  11 + return res.status(200).end();
  12 + },
  13 + async preshare(req, res, next) {
  14 + const {fid, eventName, shareUrl} = req.query;
  15 +
  16 + if (!eventName || !shareUrl) {
  17 + return res.status(200).end();
  18 + }
  19 + try {
  20 + const id = await req.ctx(shareModel).preshare({fid, eventName, shareUrl, ip: req.yoho.clientIp});
  21 +
  22 + return res.jsonp({
  23 + preid: id
  24 + });
  25 + } catch (error) {
  26 + return next(error);
  27 + }
  28 +
  29 + }
  30 +};
  31 +
  32 +module.exports = shareController;
  1 +const _ = require('lodash');
  2 +const moment = require('moment');
  3 +const logger = global.yoho.logger;
  4 +const mysqlCli = global.yoho.utils.mysqlCli;
  5 +
  6 +
  7 +class ShareModel extends global.yoho.BaseModel {
  8 + constructor(ctx) {
  9 + super(ctx);
  10 + }
  11 + async collect({uid, fid}) {
  12 + return mysqlCli.update('UPDATE `user_share_log` SET `uid` = :uid, `tick` = `tick` + 1 WHERE `id` = :fid', {
  13 + uid,
  14 + fid,
  15 + });
  16 + }
  17 + async preshare({fid, eventName, shareUrl, ip}) {
  18 + let level = -1;
  19 +
  20 + if (fid) {
  21 + const find = await mysqlCli.query('SELECT * FROM `user_share_log` WHERE `id` = :fid', {
  22 + fid
  23 + });
  24 +
  25 + if (find && find[0]) {
  26 + level = find[0].level;
  27 + }
  28 + }
  29 + return mysqlCli.insert('INSERT INTO `user_share_log` (`event_name`, `uid`, `from_id`, `share_url`, `level`, `share_time`, `ip`) VALUES (:eventName, :uid, :fromId, :shareUrl, :level, :shareTime, :ip)', {
  30 + eventName,
  31 + uid: 0,
  32 + fromId: fid || 0,
  33 + shareUrl,
  34 + level: level + 1,
  35 + shareTime: parseInt(new Date().getTime() / 1000, 10),
  36 + ip
  37 + });
  38 + }
  39 +}
  40 +
  41 +module.exports = ShareModel;
@@ -21,6 +21,7 @@ const common = require('./controllers/common'); @@ -21,6 +21,7 @@ const common = require('./controllers/common');
21 const mutilpartMiddleware = multipart(); 21 const mutilpartMiddleware = multipart();
22 const yohoActivitys = require('./controllers/yoho-activitys'); 22 const yohoActivitys = require('./controllers/yoho-activitys');
23 const dailyCheckIn = require('./controllers/daily-check-in'); 23 const dailyCheckIn = require('./controllers/daily-check-in');
  24 +const share = require('./controllers/share');
24 25
25 router.get('/gettoken', qiniu.getToken); 26 router.get('/gettoken', qiniu.getToken);
26 router.get('/share/getSignPackage', wechat.getSignPackage); 27 router.get('/share/getSignPackage', wechat.getSignPackage);
@@ -105,4 +106,6 @@ router.post('/activity/checkIn/saveUserInfo', dailyCheckIn.saveUserInfo); // 保 @@ -105,4 +106,6 @@ router.post('/activity/checkIn/saveUserInfo', dailyCheckIn.saveUserInfo); // 保
105 router.get('/activity/checkIn/getWinnerList', dailyCheckIn.getWinnerList); // 获取实物奖品中奖用户 106 router.get('/activity/checkIn/getWinnerList', dailyCheckIn.getWinnerList); // 获取实物奖品中奖用户
106 router.get('/activity/checkIn/getUserInfo', dailyCheckIn.getUserInfo); // 获取用户信息 107 router.get('/activity/checkIn/getUserInfo', dailyCheckIn.getUserInfo); // 获取用户信息
107 108
  109 +router.get('/share/collect', share.collect); // 分享
  110 +router.get('/share/preshare', share.preshare); // 分享
108 module.exports = router; 111 module.exports = router;
  1 +CREATE TABLE IF NOT EXISTS user_share_log (
  2 + `id` INT(8) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  3 + `event_name` VARCHAR(100) NOT NULL,
  4 + `uid` VARCHAR(50) NOT NULL,
  5 + `from_id` VARCHAR(50) NOT NULL DEFAULT '',
  6 + `share_url` VARCHAR(500) NOT NULL DEFAULT '',
  7 + `level` INT(8) NOT NULL DEFAULT 0,
  8 + `tick` INT(8) NOT NULL DEFAULT 0,
  9 + `share_time` INT(10) NOT NULL ,
  10 + `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
  11 +) DEFAULT CHARSET=utf8mb4;
  12 +
  13 +ALTER TABLE user_share_log ADD INDEX INDEX_EVENT_NAME (`event_name`);
  14 +ALTER TABLE user_share_log ADD INDEX INDEX_UID (`uid`);
  15 +ALTER TABLE user_share_log ADD INDEX INDEX_FROM_ID (`from_id`);
  16 +ALTER TABLE user_share_log ADD INDEX INDEX_SHARE_URL (`share_url`);