activityController.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import moment from 'moment';
  2. import activity from '../../model/activity.js';
  3. import { stringUtils } from '../../util/stringClass.js';
  4. import { globalCache } from '../../util/GlobalCache.js';
  5. import config from '../../config/index.js';
  6. import { query } from '../../util/db.js';
  7. // 以下模型在旧项目中独立存在,新项目暂用 query 直接调用
  8. // PayInfo.GetActivityMoneyList, Users.GetUsersInfoByUserID, Users.UpdateUsersIsRebateByUserID
  9. /**
  10. * 得到当前活动信息
  11. * 旧: exports.GetCurrentActivityInfo
  12. */
  13. export async function GetCurrentActivityInfo(ctx) {
  14. let param = {
  15. UserID: ctx.query.UserID || 0,
  16. };
  17. if (param.UserID === "undefined")
  18. param.UserID = 0;
  19. const list = await activity.GetCurrentActivityInfo(param);
  20. if (list && list.length > 0) {
  21. const result = {};
  22. result.MoneyNotPay = 0; //未领取金额
  23. result.MoneyTotal = 0; //累计回馈
  24. result.MoneyPay = 0; //领取金额
  25. result.ActivityCurrent = null;
  26. result.ActivityHistory = [];
  27. for (let i = 0; i < list.length; i++) {
  28. const item = list[i];
  29. item.UserCreateTime = moment(item.UserCreateTime).format('YYYY年MM月DD日 HH:mm');
  30. item.DateStartCHS = moment(item.DateStart).format('YYYY年MM月DD日');
  31. item.DateEndCHS = moment(item.DateEnd).format('YYYY年MM月DD日');
  32. item.DateStart = moment(item.DateStart).format('MM.DD');
  33. item.DateEnd = moment(item.DateEnd).format('MM.DD');
  34. const param2 = {
  35. UserID: param.UserID,
  36. ActivityInfoID: item.ActivityInfoID,
  37. };
  38. item.MoneyPlan = 0; //预计回馈
  39. const detailList = await activity.GetUserActivityInfo(param2);
  40. for (let j = 0; j < detailList.length; j++) {
  41. const itemChild = detailList[j];
  42. let payMoney = 0;
  43. if (item.RuleCompute1 === "*") {
  44. payMoney = Math.round(itemChild.Money * Number(item.RuleCompute2));
  45. }
  46. else if (item.RuleCompute1 === "-") {
  47. payMoney = itemChild.Money - Number(item.RuleCompute2);
  48. }
  49. if (item.Status === -1 && itemChild.IsRebate === 0) {
  50. result.MoneyNotPay += payMoney;
  51. result.MoneyTotal += payMoney;
  52. }
  53. else if (item.Status === -1 && itemChild.IsRebate === 1) {
  54. result.MoneyPay += payMoney;
  55. result.MoneyTotal += payMoney;
  56. }
  57. item.MoneyPlan += payMoney;
  58. }
  59. item.MoneyPlan = stringUtils.FormatMoney(item.MoneyPlan);
  60. item.List = detailList;
  61. if (item.Status === 1) {
  62. result.ActivityCurrent = item;
  63. }
  64. else {
  65. result.ActivityHistory.push(item);
  66. }
  67. }
  68. result.MoneyNotPay = stringUtils.FormatMoney(result.MoneyNotPay);
  69. result.MoneyTotal = stringUtils.FormatMoney(result.MoneyTotal);
  70. result.MoneyPay = stringUtils.FormatMoney(result.MoneyPay);
  71. ctx.body = { "errcode": 10000, result: result };
  72. }
  73. else
  74. ctx.body = { "errcode": 10000 };
  75. }
  76. /**
  77. * 得到当前活动
  78. * 旧: exports.GetCurrentActivity
  79. */
  80. export async function GetCurrentActivity(ctx) {
  81. const list = await activity.GetCurrentActivity();
  82. if (list && list.length > 0) {
  83. const item = list[0];
  84. ctx.body = { "errcode": 10000, result: item };
  85. }
  86. else
  87. ctx.body = { "errcode": 10000 };
  88. }
  89. /**
  90. * 参加活动
  91. * 旧: exports.AddActivity
  92. */
  93. export async function AddActivity(ctx) {
  94. let param = {
  95. UserID: ctx.query.UserID || 0,
  96. ActivityInfoID: ctx.query.ActivityInfoID || 0,
  97. };
  98. if (param.UserID === "undefined")
  99. param.UserID = 0;
  100. const list = await activity.GetUserActivityInfo4(param);
  101. if (list && list.length > 0) {
  102. ctx.body = { "errcode": 10000 };
  103. }
  104. else {
  105. param.CreateTime = new Date();
  106. param.Status = 0;
  107. param.Flag = 0;
  108. await activity.AddActivity(param);
  109. ctx.body = { "errcode": 10000 };
  110. }
  111. }
  112. /**
  113. * 支付活动经费
  114. * 旧: exports.PayActivity
  115. * 使用 globalCache 替代旧的 BufferMemory 实现节流
  116. */
  117. export async function PayActivity(ctx) {
  118. let param = {
  119. UserID: ctx.query.UserID || 0,
  120. };
  121. if (param.UserID === "undefined")
  122. param.UserID = 0;
  123. const cacheKey = "PayActivity?UserID=" + param.UserID;
  124. const canPay = globalCache.get(cacheKey);
  125. if (!canPay) {
  126. //计算费用
  127. const list = await activity.GetCurrentActivityInfo(param);
  128. if (list && list.length > 0) {
  129. const result = {};
  130. result.MoneyNotPay = 0; //未领取金额
  131. const userIDArr = [];
  132. for (let i = 0; i < list.length; i++) {
  133. const item = list[i];
  134. const param2 = {
  135. UserID: param.UserID,
  136. ActivityInfoID: item.ActivityInfoID,
  137. };
  138. const detailList = await activity.GetUserActivityInfo(param2);
  139. for (let j = 0; j < detailList.length; j++) {
  140. const itemChild = detailList[j];
  141. let payMoney = 0;
  142. if (item.RuleCompute1 === "*") {
  143. payMoney = Math.round(itemChild.Money * Number(item.RuleCompute2));
  144. }
  145. else if (item.RuleCompute1 === "-") {
  146. payMoney = itemChild.Money - Number(item.RuleCompute2);
  147. }
  148. if (item.Status === -1 && itemChild.IsRebate === 0) {
  149. result.MoneyNotPay += payMoney;
  150. userIDArr.push(itemChild.UserID);
  151. }
  152. }
  153. }
  154. // 获取用户信息
  155. const sql = "select * from WXUsers where UserID=?;";
  156. const userInfo = await query(sql, [param.UserID]);
  157. if (userInfo && userInfo.length > 0 && result.MoneyNotPay > 0) {
  158. const text = "领取推广回馈,详见《数学练习小学口算版》小程序";
  159. const url = "http://localhost:" + config.port + "/api/PayUserRebate/?PayType=2&UserID=" + param.UserID + "&OpenID=" + userInfo[0].OpenID + "&Money=" + result.MoneyNotPay + "&Text=" + text;
  160. console.log("url:" + url);
  161. // 异步调用支付接口(非阻塞)
  162. import('axios').then(axios => {
  163. axios.default.get(encodeURI(url))
  164. .then(response => {
  165. if (response.data) {
  166. console.log("send ok:" + response.data.status);
  167. if (userIDArr.length > 0) {
  168. const sqlUpdate = "Update WXUsers SET IsRebate=1 where UserID in (" + userIDArr.join(",") + ");";
  169. query(sqlUpdate).catch(err => console.error(err));
  170. }
  171. }
  172. })
  173. .catch(err => console.error(err));
  174. });
  175. }
  176. }
  177. // 设置缓存节流
  178. globalCache.set(cacheKey, true, config.BufferMemoryTimeLowNormal);
  179. }
  180. else {
  181. console.log("CanNotPay");
  182. }
  183. ctx.body = { "errcode": 10000 };
  184. }
  185. /**
  186. * 得到领取记录
  187. * 旧: exports.GetActivityMoneyList
  188. */
  189. export async function GetActivityMoneyList(ctx) {
  190. let param = {
  191. UserID: ctx.query.UserID || 0,
  192. };
  193. if (param.UserID === "undefined")
  194. param.UserID = 0;
  195. const result = {
  196. MoneyTotal: 0,
  197. List: [],
  198. };
  199. // 从 ProductPayInfo 和 PaybackInfo 获取活动领取记录
  200. const sql = "select ID,TradeNo,CreateTime,Body,Money from PaybackInfo where UserID=? order by ID desc";
  201. const list = await query(sql, [param.UserID]);
  202. if (list && list.length > 0) {
  203. for (let i = 0; i < list.length; i++) {
  204. result.MoneyTotal += list[i].Money;
  205. list[i].Money = stringUtils.FormatMoney(list[i].Money);
  206. list[i].CreateTime = moment(list[i].CreateTime).format('YYYY年MM月DD日 HH:mm');
  207. }
  208. result.MoneyTotal = stringUtils.FormatMoney(result.MoneyTotal);
  209. result.List = list;
  210. }
  211. ctx.body = { "errcode": 10000, result: result };
  212. }