shop.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. 'use strict';
  2. const BaseService = require('./base.js');
  3. // 商城服务类
  4. class ShopService extends BaseService {
  5. /**
  6. * [getWebUser 获取前端登录的用户]
  7. * @return {[type]} [description]
  8. */
  9. getWebUser() {
  10. const that = this;
  11. const token = that.ctx.request.header.weblogintoken;
  12. const user = that.app.jwt.verify(token, that.app.config.jwt.secret);
  13. return user;
  14. }
  15. /**
  16. * [getWebUserId 获取用户ID]
  17. * @return {[type]} [description]
  18. */
  19. getWebUserId() {
  20. const that = this;
  21. const user = that.getWebUser();
  22. return user.user_id || 0;
  23. }
  24. /**
  25. * [payConfig 获取支付配置]
  26. * @return {[type]} [description]
  27. */
  28. async payConfig(attributes = []) {
  29. const that = this;
  30. const res = await that.app.model.PaysConfig.findAll({
  31. order: [ [ 'pay_sort', 'asc' ] ],
  32. attributes: [ 'pay_id', 'pay_name', 'pay_logo', 'pay_params_index', 'is_default' ].concat(attributes),
  33. where: { pay_status: 1 },
  34. });
  35. return res || [];
  36. }
  37. /**
  38. * [getUserMoney 获取用户余额]
  39. * @param {[type]} user_id [description]
  40. * @return {[type]} [description]
  41. */
  42. async getUserMoney(user_id, transaction = null) {
  43. const that = this;
  44. const result = await that.app.model.Users.findOne({
  45. where: { user_id },
  46. transaction,
  47. attributes: [ 'money' ],
  48. });
  49. if (!result) return 0;
  50. return result.money;
  51. }
  52. /**
  53. * [getUserMoney 获取用户账户余额]
  54. * @param {[type]} user_id [description]
  55. * @return {[type]} [description]
  56. */
  57. async getUserAccount(user_id, transaction = null) {
  58. const that = this;
  59. const result = await that.app.model.Users.findOne({
  60. where: { user_id },
  61. transaction,
  62. attributes: [ 'money', 'commission' ],
  63. });
  64. if (!result) {
  65. throw new Error('获取账户余额信息失败,请稍后重试');
  66. }
  67. // 2023/2/28 补充查询餐币钱包余额diningCoin
  68. const seq = that.app.Sequelize;
  69. const selectBean = await that.app.comoBean.instance({}, {
  70. where: { user_id, expired: false },
  71. attributes: [ [ seq.fn('sum', seq.col('account')), 'account' ] ],
  72. });
  73. const res = await that.service.base.select(selectBean, that.app.model.DinnerCoins, '查询餐饮币账户余额失败,请稍候重试', false, false);
  74. const resultTemp = JSON.parse(JSON.stringify(result));
  75. resultTemp.diningCoin = res.account;
  76. return resultTemp;
  77. }
  78. /**
  79. * [createPaymentLog 写入支付记录]
  80. * @param {Object} data [description]
  81. * @param {[type]} transaction [description]
  82. * @return {[type]} [description]
  83. */
  84. async createPaymentLog(data = {}, transaction = null) {
  85. const that = this;
  86. const defaults = {
  87. out_trade_no: `Y${that.app.szjcomo.date('YmdHis')}${data.pay_id}${data.user_id}${that.app.szjcomo.mt_rand(100, 999)}`,
  88. total_fee: 0, trade_type: '余额支付', transaction_id: '', time_end: that.app.szjcomo.date('YmdHis'),
  89. openid: '', fee_type: '余额', bank_type: '商城余额', create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  90. };
  91. const createBean = await that.app.comoBean.instance(Object.assign(defaults, data), { transaction });
  92. const result = await that.create(createBean, that.app.model.Payments, '支付记录写入失败,请稍候重试');
  93. return result;
  94. }
  95. /**
  96. * [userMoneyAdd 添加用户余额]
  97. * @param {[type]} user_id [description]
  98. * @param {Number} money [description]
  99. * @param transaction
  100. * @param log_desc
  101. * @param {Number} admin_id 管理员id
  102. * @param {Number} type 充值类型 0管理员充值;1新用户注册奖励;2受邀注册奖励;3分享邀请新用户奖励;4每日抽奖红包
  103. * @param {Number} inviter_id 分享邀请者用户id
  104. * @param inviter_name
  105. * @param inviter_img
  106. * @return {[type]} [description]
  107. */
  108. async userMoneyAdd(user_id, money = 0, transaction = null, log_desc = '用户余额增加'
  109. , admin_id = 0, type = 0, inviter_id = -1, inviter_name = '', inviter_img = '') {
  110. const that = this;
  111. let updateBean;
  112. if (type === 4) {
  113. updateBean = await that.app.comoBean.instance({
  114. money: that.app.Sequelize.literal('money + ' + money),
  115. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  116. lucky_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  117. admin_id,
  118. }, { where: { user_id }, transaction });
  119. } else {
  120. updateBean = await that.app.comoBean.instance({
  121. money: that.app.Sequelize.literal('money + ' + money),
  122. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  123. admin_id,
  124. }, { where: { user_id }, transaction });
  125. }
  126. // 2022/9/27 用户余额更新 用户表szj_users
  127. const result = await that.update(updateBean, that.app.model.Users, '用户红包余额更新失败,请稍候重试');
  128. // 2022/9/27 添加余额变化记录szj_users_money_logs
  129. await that.userMoneyLog(user_id, money, transaction, log_desc, admin_id, type, inviter_id, inviter_name, inviter_img);
  130. return result;
  131. }
  132. /**
  133. * [userMoneySub 减少用户余额]
  134. * @param {[type]} user_id [description]
  135. * @param {Number} money [description]
  136. * @param {[type]} transaction [description]
  137. * @return {[type]} [description]
  138. */
  139. async userMoneySub(user_id, money = 0, transaction = null, log_desc = '用户余额减少', admin_id = 0) {
  140. const that = this;
  141. const updateBean = await that.app.comoBean.instance({
  142. money: that.app.Sequelize.literal('money - ' + money),
  143. update_time: that.app.szjcomo.date('Y-m-d H:i:s'), admin_id,
  144. }, { where: { user_id }, transaction });
  145. const result = await that.update(updateBean, that.app.model.Users, '用户余额更新失败,请稍候重试');
  146. await that.userMoneyLog(user_id, -money, transaction, log_desc, admin_id);
  147. return result;
  148. }
  149. /**
  150. * [userMoneyLog 用户余额变动记录]
  151. * @param {[type]} user_id [description]
  152. * @param {Number} money [description]
  153. * @param {[type]} transaction [description]
  154. * @param {String} log_desc [description]
  155. * @param {Number} admin_id 管理员id
  156. * @param {Number} type 充值类型 0管理员充值;1新用户注册奖励;2受邀注册奖励;3分享邀请新用户奖励
  157. * @param {Number} inviter_id 分享邀请者用户id
  158. * @param inviter_name
  159. * @param inviter_img
  160. * @return {[type]} [description]
  161. */
  162. async userMoneyLog(user_id, money = 0, transaction = null
  163. , log_desc = '用户余额变化记录', admin_id = 0, type = 0
  164. , inviter_id = -1, inviter_name = '', inviter_img = '') {
  165. const that = this;
  166. const createBean = await that.app.comoBean.instance({
  167. user_id,
  168. type,
  169. money,
  170. inviter_id,
  171. inviter_name,
  172. inviter_img,
  173. log_desc,
  174. admin_id,
  175. create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  176. }, { transaction });
  177. const result = await that.create(createBean, that.app.model.UsersMoneyLogs, '用户余额变动记录失败,请稍候重试');
  178. return result;
  179. }
  180. /**
  181. * [productSaleCount 统计商品真实销量]
  182. * @param {[type]} product_id [description]
  183. * @return {[type]} [description]
  184. */
  185. async productSaleCount(product_id) {
  186. const that = this;
  187. // const seq = that.app.Sequelize;
  188. const result = await that.app.model.OrdersProducts.sum('product_count', { where: { product_id } });
  189. return result || 0;
  190. }
  191. /**
  192. * [orderPrinter 订单打印]
  193. * @return {[type]} [description]
  194. */
  195. async orderPrinter(order_id, iserr = false) {
  196. const that = this;
  197. try {
  198. const order = await that.service.order.getOrederPrinterInfo(order_id);
  199. const keys = [ 'xiaopiao_token', 'xiaopiao_appname', 'xiaopiao_domain', 'xiaopiao_machinecode', 'xiaopiao_start' ];
  200. const config = await that.service.configs.getConfigMoreValue(keys);
  201. if (!Number(config.xiaopiao_start)) return false;
  202. const respone = await that.app.curl(config.xiaopiao_domain, {
  203. method: 'POST', dataType: 'json', contentType: 'json',
  204. data: {
  205. token: config.xiaopiao_token,
  206. appname: config.xiaopiao_appname,
  207. machineCode: config.xiaopiao_machinecode,
  208. order: that.app.szjcomo.json_encode(order),
  209. },
  210. });
  211. if (respone.data.error !== false) throw new Error(respone.data.message);
  212. if (respone.data.result.error != 0) throw new Error(respone.data.result.error_description);
  213. return respone.data.result.body;
  214. } catch (err) {
  215. if (iserr) throw err;
  216. return false;
  217. }
  218. }
  219. }
  220. module.exports = ShopService;