shop.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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分享邀请新用户奖励
  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. const updateBean = await that.app.comoBean.instance({
  112. money: that.app.Sequelize.literal('money + ' + money),
  113. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  114. admin_id,
  115. }, { where: { user_id }, transaction });
  116. // 2022/9/27 用户余额更新 用户表szj_users
  117. const result = await that.update(updateBean, that.app.model.Users, '用户余额更新失败,请稍候重试');
  118. // 2022/9/27 添加余额变化记录szj_users_money_logs
  119. await that.userMoneyLog(user_id, money, transaction, log_desc, admin_id, type, inviter_id, inviter_name, inviter_img);
  120. return result;
  121. }
  122. /**
  123. * [userMoneySub 减少用户余额]
  124. * @param {[type]} user_id [description]
  125. * @param {Number} money [description]
  126. * @param {[type]} transaction [description]
  127. * @return {[type]} [description]
  128. */
  129. async userMoneySub(user_id, money = 0, transaction = null, log_desc = '用户余额减少', admin_id = 0) {
  130. const that = this;
  131. const updateBean = await that.app.comoBean.instance({
  132. money: that.app.Sequelize.literal('money - ' + money),
  133. update_time: that.app.szjcomo.date('Y-m-d H:i:s'), admin_id,
  134. }, { where: { user_id }, transaction });
  135. const result = await that.update(updateBean, that.app.model.Users, '用户余额更新失败,请稍候重试');
  136. await that.userMoneyLog(user_id, -money, transaction, log_desc, admin_id);
  137. return result;
  138. }
  139. /**
  140. * [userMoneyLog 用户余额变动记录]
  141. * @param {[type]} user_id [description]
  142. * @param {Number} money [description]
  143. * @param {[type]} transaction [description]
  144. * @param {String} log_desc [description]
  145. * @param {Number} admin_id 管理员id
  146. * @param {Number} type 充值类型 0管理员充值;1新用户注册奖励;2受邀注册奖励;3分享邀请新用户奖励
  147. * @param {Number} inviter_id 分享邀请者用户id
  148. * @param inviter_name
  149. * @param inviter_img
  150. * @return {[type]} [description]
  151. */
  152. async userMoneyLog(user_id, money = 0, transaction = null
  153. , log_desc = '用户余额变化记录', admin_id = 0, type = 0
  154. , inviter_id = -1, inviter_name = '', inviter_img = '') {
  155. const that = this;
  156. const createBean = await that.app.comoBean.instance({
  157. user_id,
  158. type,
  159. money,
  160. inviter_id,
  161. inviter_name,
  162. inviter_img,
  163. log_desc,
  164. admin_id,
  165. create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  166. }, { transaction });
  167. const result = await that.create(createBean, that.app.model.UsersMoneyLogs, '用户余额变动记录失败,请稍候重试');
  168. return result;
  169. }
  170. /**
  171. * [productSaleCount 统计商品真实销量]
  172. * @param {[type]} product_id [description]
  173. * @return {[type]} [description]
  174. */
  175. async productSaleCount(product_id) {
  176. const that = this;
  177. // const seq = that.app.Sequelize;
  178. const result = await that.app.model.OrdersProducts.sum('product_count', { where: { product_id } });
  179. return result || 0;
  180. }
  181. /**
  182. * [orderPrinter 订单打印]
  183. * @return {[type]} [description]
  184. */
  185. async orderPrinter(order_id, iserr = false) {
  186. const that = this;
  187. try {
  188. const order = await that.service.order.getOrederPrinterInfo(order_id);
  189. const keys = [ 'xiaopiao_token', 'xiaopiao_appname', 'xiaopiao_domain', 'xiaopiao_machinecode', 'xiaopiao_start' ];
  190. const config = await that.service.configs.getConfigMoreValue(keys);
  191. if (!Number(config.xiaopiao_start)) return false;
  192. const respone = await that.app.curl(config.xiaopiao_domain, {
  193. method: 'POST', dataType: 'json', contentType: 'json',
  194. data: {
  195. token: config.xiaopiao_token,
  196. appname: config.xiaopiao_appname,
  197. machineCode: config.xiaopiao_machinecode,
  198. order: that.app.szjcomo.json_encode(order),
  199. },
  200. });
  201. if (respone.data.error !== false) throw new Error(respone.data.message);
  202. if (respone.data.result.error != 0) throw new Error(respone.data.result.error_description);
  203. return respone.data.result.body;
  204. } catch (err) {
  205. if (iserr) throw err;
  206. return false;
  207. }
  208. }
  209. }
  210. module.exports = ShopService;