123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- 'use strict';
- const BaseService = require('./base.js');
- // 商城服务类
- class ShopService extends BaseService {
- /**
- * [getWebUser 获取前端登录的用户]
- * @return {[type]} [description]
- */
- getWebUser() {
- const that = this;
- const token = that.ctx.request.header.weblogintoken;
- const user = that.app.jwt.verify(token, that.app.config.jwt.secret);
- return user;
- }
- /**
- * [getWebUserId 获取用户ID]
- * @return {[type]} [description]
- */
- getWebUserId() {
- const that = this;
- const user = that.getWebUser();
- return user.user_id || 0;
- }
- /**
- * [payConfig 获取支付配置]
- * @return {[type]} [description]
- */
- async payConfig(attributes = []) {
- const that = this;
- const res = await that.app.model.PaysConfig.findAll({
- order: [ [ 'pay_sort', 'asc' ] ],
- attributes: [ 'pay_id', 'pay_name', 'pay_logo', 'pay_params_index', 'is_default' ].concat(attributes),
- where: { pay_status: 1 },
- });
- return res || [];
- }
- /**
- * [getUserMoney 获取用户余额]
- * @param {[type]} user_id [description]
- * @return {[type]} [description]
- */
- async getUserMoney(user_id, transaction = null) {
- const that = this;
- const result = await that.app.model.Users.findOne({
- where: { user_id },
- transaction,
- attributes: [ 'money' ],
- });
- if (!result) return 0;
- return result.money;
- }
- /**
- * [getUserMoney 获取用户账户余额]
- * @param {[type]} user_id [description]
- * @return {[type]} [description]
- */
- async getUserAccount(user_id, transaction = null) {
- const that = this;
- const result = await that.app.model.Users.findOne({
- where: { user_id },
- transaction,
- attributes: [ 'money', 'commission' ],
- });
- if (!result) {
- throw new Error('获取账户余额信息失败,请稍后重试');
- }
- // 2023/2/28 补充查询餐币钱包余额diningCoin
- const seq = that.app.Sequelize;
- const selectBean = await that.app.comoBean.instance({}, {
- where: { user_id, expired: false },
- attributes: [ [ seq.fn('sum', seq.col('account')), 'account' ] ],
- });
- const res = await that.service.base.select(selectBean, that.app.model.DinnerCoins, '查询餐饮币账户余额失败,请稍候重试', false, false);
- const resultTemp = JSON.parse(JSON.stringify(result));
- resultTemp.diningCoin = res.account;
- return resultTemp;
- }
- /**
- * [createPaymentLog 写入支付记录]
- * @param {Object} data [description]
- * @param {[type]} transaction [description]
- * @return {[type]} [description]
- */
- async createPaymentLog(data = {}, transaction = null) {
- const that = this;
- const defaults = {
- out_trade_no: `Y${that.app.szjcomo.date('YmdHis')}${data.pay_id}${data.user_id}${that.app.szjcomo.mt_rand(100, 999)}`,
- total_fee: 0, trade_type: '余额支付', transaction_id: '', time_end: that.app.szjcomo.date('YmdHis'),
- openid: '', fee_type: '余额', bank_type: '商城余额', create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
- };
- const createBean = await that.app.comoBean.instance(Object.assign(defaults, data), { transaction });
- const result = await that.create(createBean, that.app.model.Payments, '支付记录写入失败,请稍候重试');
- return result;
- }
- /**
- * [userMoneyAdd 添加用户余额]
- * @param {[type]} user_id [description]
- * @param {Number} money [description]
- * @param transaction
- * @param log_desc
- * @param {Number} admin_id 管理员id
- * @param {Number} type 充值类型 0管理员充值;1新用户注册奖励;2受邀注册奖励;3分享邀请新用户奖励;4每日抽奖红包
- * @param {Number} inviter_id 分享邀请者用户id
- * @param inviter_name
- * @param inviter_img
- * @return {[type]} [description]
- */
- async userMoneyAdd(user_id, money = 0, transaction = null, log_desc = '用户余额增加'
- , admin_id = 0, type = 0, inviter_id = -1, inviter_name = '', inviter_img = '') {
- const that = this;
- let updateBean;
- if (type === 4) {
- updateBean = await that.app.comoBean.instance({
- money: that.app.Sequelize.literal('money + ' + money),
- update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
- lucky_time: that.app.szjcomo.date('Y-m-d H:i:s'),
- admin_id,
- }, { where: { user_id }, transaction });
- } else {
- updateBean = await that.app.comoBean.instance({
- money: that.app.Sequelize.literal('money + ' + money),
- update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
- admin_id,
- }, { where: { user_id }, transaction });
- }
- // 2022/9/27 用户余额更新 用户表szj_users
- const result = await that.update(updateBean, that.app.model.Users, '用户红包余额更新失败,请稍候重试');
- // 2022/9/27 添加余额变化记录szj_users_money_logs
- await that.userMoneyLog(user_id, money, transaction, log_desc, admin_id, type, inviter_id, inviter_name, inviter_img);
- return result;
- }
- /**
- * [userMoneySub 减少用户余额]
- * @param {[type]} user_id [description]
- * @param {Number} money [description]
- * @param {[type]} transaction [description]
- * @return {[type]} [description]
- */
- async userMoneySub(user_id, money = 0, transaction = null, log_desc = '用户余额减少', admin_id = 0) {
- const that = this;
- const updateBean = await that.app.comoBean.instance({
- money: that.app.Sequelize.literal('money - ' + money),
- update_time: that.app.szjcomo.date('Y-m-d H:i:s'), admin_id,
- }, { where: { user_id }, transaction });
- const result = await that.update(updateBean, that.app.model.Users, '用户余额更新失败,请稍候重试');
- await that.userMoneyLog(user_id, -money, transaction, log_desc, admin_id);
- return result;
- }
- /**
- * [userMoneyLog 用户余额变动记录]
- * @param {[type]} user_id [description]
- * @param {Number} money [description]
- * @param {[type]} transaction [description]
- * @param {String} log_desc [description]
- * @param {Number} admin_id 管理员id
- * @param {Number} type 充值类型 0管理员充值;1新用户注册奖励;2受邀注册奖励;3分享邀请新用户奖励
- * @param {Number} inviter_id 分享邀请者用户id
- * @param inviter_name
- * @param inviter_img
- * @return {[type]} [description]
- */
- async userMoneyLog(user_id, money = 0, transaction = null
- , log_desc = '用户余额变化记录', admin_id = 0, type = 0
- , inviter_id = -1, inviter_name = '', inviter_img = '') {
- const that = this;
- const createBean = await that.app.comoBean.instance({
- user_id,
- type,
- money,
- inviter_id,
- inviter_name,
- inviter_img,
- log_desc,
- admin_id,
- create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
- }, { transaction });
- const result = await that.create(createBean, that.app.model.UsersMoneyLogs, '用户余额变动记录失败,请稍候重试');
- return result;
- }
- /**
- * [productSaleCount 统计商品真实销量]
- * @param {[type]} product_id [description]
- * @return {[type]} [description]
- */
- async productSaleCount(product_id) {
- const that = this;
- // const seq = that.app.Sequelize;
- const result = await that.app.model.OrdersProducts.sum('product_count', { where: { product_id } });
- return result || 0;
- }
- /**
- * [orderPrinter 订单打印]
- * @return {[type]} [description]
- */
- async orderPrinter(order_id, iserr = false) {
- const that = this;
- try {
- const order = await that.service.order.getOrederPrinterInfo(order_id);
- const keys = [ 'xiaopiao_token', 'xiaopiao_appname', 'xiaopiao_domain', 'xiaopiao_machinecode', 'xiaopiao_start' ];
- const config = await that.service.configs.getConfigMoreValue(keys);
- if (!Number(config.xiaopiao_start)) return false;
- const respone = await that.app.curl(config.xiaopiao_domain, {
- method: 'POST', dataType: 'json', contentType: 'json',
- data: {
- token: config.xiaopiao_token,
- appname: config.xiaopiao_appname,
- machineCode: config.xiaopiao_machinecode,
- order: that.app.szjcomo.json_encode(order),
- },
- });
- if (respone.data.error !== false) throw new Error(respone.data.message);
- if (respone.data.result.error != 0) throw new Error(respone.data.result.error_description);
- return respone.data.result.body;
- } catch (err) {
- if (iserr) throw err;
- return false;
- }
- }
- }
- module.exports = ShopService;
|