123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- 'use strict';
- const BaseService = require('./base.js');
- class ShopService extends BaseService {
-
- 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() {
- const that = this;
- const user = that.getWebUser();
- return user.user_id || 0;
- }
-
- 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 || [];
- }
-
- 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;
- }
-
- 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('获取账户余额信息失败,请稍后重试');
- }
-
- 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;
- }
-
- 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;
- }
-
- 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 });
- }
-
- const result = await that.update(updateBean, that.app.model.Users, '用户红包余额更新失败,请稍候重试');
-
- await that.userMoneyLog(user_id, money, transaction, log_desc, admin_id, type, inviter_id, inviter_name, inviter_img);
- return result;
- }
-
- 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;
- }
-
- 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;
- }
-
- async productSaleCount(product_id) {
- const that = this;
-
- const result = await that.app.model.OrdersProducts.sum('product_count', { where: { product_id } });
- return result || 0;
- }
-
- 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;
|