lrf 2 年之前
父节点
当前提交
025ee991f5

+ 13 - 0
app/controller/bill.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./config/.bill.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+// 
+class BillController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.bill;
+  }
+}
+module.exports = CrudController(BillController, meta);

+ 43 - 0
app/controller/config/.bill.js

@@ -0,0 +1,43 @@
+module.exports = {
+  create: {
+    requestBody: ['money', 'match_id', 'group_id', 'project_id', 'payer_id', 'pay_id', 'is_pay', 'time', 'type'],
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete',
+  },
+  update: {
+    params: ['!id'],
+    requestBody: ['money', 'match_id', 'group_id', 'project_id', 'payer_id', 'pay_id', 'is_pay', 'time', 'type'],
+  },
+  show: {
+    parameters: {
+      params: ['!id'],
+    },
+    service: 'fetch',
+  },
+  index: {
+    parameters: {
+      query: {
+        'meta.createdAt@start': 'meta.createdAt@start',
+        'meta.createdAt@end': 'meta.createdAt@end',
+        match_id: 'match_id',
+        group_id: 'group_id',
+        project_id: 'project_id',
+        pay_id: 'pay_id',
+        'time@start': 'time@start',
+        'time@end': 'time@end',
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 18 - 1
app/controller/config/.match.js

@@ -1,6 +1,7 @@
 module.exports = {
   create: {
     requestBody: [
+      'money',
       'belong_id',
       'type',
       '!logo',
@@ -23,7 +24,23 @@ module.exports = {
   },
   update: {
     params: ['!id'],
-    requestBody: ['belong_id', 'type', 'logo', 'name', 'start_time', 'end_time', 'address', 'sign_time', 'money_remark', 'money_mode', 'contact', 'explain', 'regular', 'status'],
+    requestBody: [
+      'money',
+      'belong_id',
+      'type',
+      'logo',
+      'name',
+      'start_time',
+      'end_time',
+      'address',
+      'sign_time',
+      'money_remark',
+      'money_mode',
+      'contact',
+      'explain',
+      'regular',
+      'status',
+    ],
   },
   show: {
     parameters: {

+ 40 - 0
app/controller/config/.payOrder.js

@@ -0,0 +1,40 @@
+module.exports = {
+  create: {
+    requestBody: ['money', 'openid', 'match_id', 'group_id', 'project_id', 'payer_id', 'order_no', 'time', 'desc', 'status', 'config'],
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete',
+  },
+  update: {
+    params: ['!id'],
+    requestBody: ['money', 'openid', 'match_id', 'group_id', 'project_id', 'payer_id', 'order_no', 'time', 'desc', 'status', 'config'],
+  },
+  show: {
+    parameters: {
+      params: ['!id'],
+    },
+    service: 'fetch',
+  },
+  index: {
+    parameters: {
+      query: {
+        'meta.createdAt@start': 'meta.createdAt@start',
+        'meta.createdAt@end': 'meta.createdAt@end',
+        payer_id: 'payer_id',
+        order_no: 'order_no',
+        status: 'status',
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 13 - 0
app/controller/payOrder.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./config/.payOrder.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+// 
+class PayOrderController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.payOrder;
+  }
+}
+module.exports = CrudController(PayOrderController, meta);

+ 42 - 0
app/model/race/bill.js

@@ -0,0 +1,42 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+
+const MoneyPlugin = require('naf-framework-mongoose-free/lib/model/type-money-plugin');
+
+// 账单
+const bill = {
+  match_id: { type: String, required: false, zh: '赛事id', ref: 'Match' }, //
+  group_id: { type: String, required: false, zh: '组别id', ref: 'MatchGroup' }, //
+  project_id: { type: String, required: false, zh: '组别项目id', ref: 'MatchProject' }, //
+  payer_id: { type: String, required: false, zh: '支付人id', ref: 'User' }, // 比赛模块用户数据id
+  pay_id: { type: String, required: false, zh: '支付id', ref: 'PayOrder' }, //
+  is_pay: { type: String, required: false, zh: '是否支付' }, //  0:未支付;1:支付成功;-1:支付失败;-3:已退款
+  time: { type: String, required: false, zh: '时间' }, //
+  type: { type: String, required: false, zh: '来源' }, // 1:报名缴费;-1退赛
+};
+const schema = new Schema(bill, { toJSON: { getters: true, virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.index({ match_id: 1 });
+schema.index({ group_id: 1 });
+schema.index({ project_id: 1 });
+schema.index({ pay_id: 1 });
+schema.index({ time: 1 });
+
+schema.plugin(metaPlugin);
+schema.plugin(MoneyPlugin({ zh: '金额', required: false }));
+
+const source = 'race';
+module.exports = app => {
+  const is_multiple = app.mongooseDB.clients;
+  let model;
+  if (is_multiple) {
+    const conn = app.mongooseDB.get(source);
+    model = conn.model('Bill', schema, 'bill');
+  } else {
+    const { mongoose } = app;
+    model = mongoose.model('Bill', schema, 'bill');
+  }
+  return model;
+};

+ 2 - 0
app/model/race/match.js

@@ -1,6 +1,7 @@
 'use strict';
 const Schema = require('mongoose').Schema;
 const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+const MoneyPlugin = require('naf-framework-mongoose-free/lib/model/type-money-plugin');
 // 比赛信息
 const match = {
   belong_id: { type: String, zh: '属于id', ref: 'User', getProp: [ 'name' ] }, // 比赛系统中用户的数据id
@@ -32,6 +33,7 @@ schema.index({ status: 1 });
 
 schema.plugin(metaPlugin);
 
+schema.plugin(MoneyPlugin({ zh: '金额', required: false }));
 const source = 'race';
 module.exports = app => {
   const is_multiple = app.mongooseDB.clients;

+ 41 - 0
app/model/race/payOrder.js

@@ -0,0 +1,41 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+
+const MoneyPlugin = require('naf-framework-mongoose-free/lib/model/type-money-plugin');
+
+// 付款订单表
+const payOrder = {
+  openid: { type: String, required: false, zh: '微信id' }, //
+  match_id: { type: String, required: false, zh: '赛事id', ref: 'Match' }, //
+  group_id: { type: String, required: false, zh: '组别id', ref: 'MatchGroup' }, //
+  project_id: { type: String, required: false, zh: '组别项目id', ref: 'MatchProject' }, //
+  payer_id: { type: String, required: false, zh: '支付用户id', ref: 'User' }, // 比赛模块用户数据id
+  order_no: { type: String, required: false, zh: '订单号' }, //
+  time: { type: String, required: false, zh: '时间' }, //
+  desc: { type: String, required: false, zh: '支付说明' }, //
+  status: { type: String, required: false, zh: '支付状态' }, // 0:未支付;1:已支付;-1:支付失败;-3:已退款
+  config: { type: String, required: false, zh: '设置' }, //
+};
+const schema = new Schema(payOrder, { toJSON: { getters: true, virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.index({ payer_id: 1 });
+schema.index({ order_no: 1 });
+schema.index({ status: 1 });
+
+schema.plugin(metaPlugin);
+schema.plugin(MoneyPlugin({ zh: '金额', required: false }));
+const source = 'race';
+module.exports = app => {
+  const is_multiple = app.mongooseDB.clients;
+  let model;
+  if (is_multiple) {
+    const conn = app.mongooseDB.get(source);
+    model = conn.model('PayOrder', schema, 'payOrder');
+  } else {
+    const { mongoose } = app;
+    model = mongoose.model('PayOrder', schema, 'payOrder');
+  }
+  return model;
+};

+ 2 - 0
app/router.js

@@ -29,4 +29,6 @@ module.exports = app => {
   require('./z_router/matchProject')(app); // 组别项目
   require('./z_router/teamApply')(app); // 组队申请
   require('./z_router/matchSign')(app); // 赛事报名
+  require('./z_router/bill')(app); // 账单
+  require('./z_router/payOrder')(app); // 支付订单
 };

+ 15 - 0
app/service/bill.js

@@ -0,0 +1,15 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+// 
+class BillService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'bill');
+    this.model = this.ctx.model.Race.Bill;
+  }
+}
+
+module.exports = BillService;

+ 44 - 0
app/service/payOrder.js

@@ -0,0 +1,44 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+const moment = require('moment');
+
+//
+class PayOrderService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'payorder');
+    this.model = this.ctx.model.Race.PayOrder;
+    this.payService = this.ctx.service.wxpay;
+  }
+  async beforeCreate(data) {
+    if (!_.get(data, 'order_no')) data.order_no = this.getOrderNo();
+    return data;
+  }
+
+  async afterCreate(body, data) {
+    const wxSign = await this.payService.create(data);
+    return { data, wxSign };
+  }
+
+  async afterUpdate(filter, body, data) {
+    await this.syncData(data);
+    return data;
+  }
+  // 同步修改----针对账单
+  async syncData(data) {
+    const { status } = data;
+    // 未支付状态不做处理
+    if (status === '0') return data;
+    // 支付失败也不做处理
+    if (status === '-1') return data;
+    // 只有支付成功和退款才进行操作
+  }
+
+  getOrderNo() {
+    return `ONCAPPRACE${moment().format('YYYYMMDDHHmmss')}-${_.random(1, 999999)}`;
+  }
+}
+
+module.exports = PayOrderService;

+ 76 - 0
app/service/wxpay.js

@@ -0,0 +1,76 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+//
+class WxpayService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'wxpay');
+    this.appConfig = 'newCourtApp';
+    this.httpUtil = this.ctx.service.util.httpUtil;
+    const { wechat } = this.app.config.httpPrefix;
+    this.wxDomain = wechat;
+  }
+  /**
+   * 查询订单
+   * @param {String} order_no 订单号
+   */
+  async search(order_no) {
+    assert(order_no, '缺少订单号,无法查询订单信息');
+    const params = { config: this.appConfig, order_no };
+    const url = `${this.wxDomain}/pay/searchOrderByOrderNo`;
+    const wxOrderReq = await this.httpUtil.cpost(url, params);
+    return wxOrderReq;
+  }
+
+  /**
+   * 创建订单,获取微信支付签名
+   * @param {Object} data 数据
+   */
+  async create(data) {
+    const { money, openid, order_no, desc } = data;
+    const wxOrderData = { config: this.appConfig, money, openid, order_no, desc };
+    const url = `${this.wxDomain}/pay/payOrder`;
+    const res = await this.httpUtil.cpost(url, wxOrderData);
+    if (res) return res;
+    throw new BusinessError(ErrorCode.SERVICE_FAULT, '微信下单失败!');
+  }
+
+  async createNative(data) {
+    const { money, order_no, desc, notice_url } = data;
+    const wxOrderData = { config: this.appConfig, money, order_no, desc, notice_url };
+    const url = `${this.wxDomain}/pay/payNative`;
+    const res = await this.httpUtil.cpost(url, wxOrderData);
+    if (res) return res;
+    throw new BusinessError(ErrorCode.SERVICE_FAULT, '微信二维码支付下单失败!');
+  }
+
+  /**
+   * 关闭订单
+   * @param {String} order_no 订单号
+   */
+  async close(order_no) {
+    assert(order_no, '缺少订单号,无法查询订单信息');
+    const params = { config: this.appConfig, order_no };
+    const url = `${this.wxDomain}/pay/closeOrder`;
+    const res = await this.httpUtil.cpost(url, params);
+    return res || 'ok';
+  }
+
+  /**
+   * 退款
+   * @param {String} order_no 订单号
+   * @param {String} reason 原因
+   */
+  async refund(order_no, reason) {
+    assert(order_no, '缺少订单号,无法查询订单信息');
+    const url = `${this.wxDomain}/pay/refundOrder`;
+    const params = { config: this.appConfig, order_no, reason };
+    const wxRefundReq = await this.httpUtil.cpost(url, params);
+    return wxRefundReq || 'ok';
+  }
+}
+
+module.exports = WxpayService;

+ 19 - 0
app/z_router/bill.js

@@ -0,0 +1,19 @@
+'use strict';
+// 路由配置
+const path = require('path');
+const regPath = path.resolve('app', 'public', 'routerRegister');
+const routerRegister = require(regPath);
+const rkey = 'bill';
+const ckey = 'bill';
+const keyZh = '账单';
+const routes = [
+  { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
+  { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
+  { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, middleware: [ 'password' ], name: `${ckey}Create`, zh: `创建${keyZh}` },
+  { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, name: `${ckey}Update`, zh: `修改${keyZh}` },
+  { method: 'delete', path: `${rkey}/:id`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
+];
+
+module.exports = app => {
+  routerRegister(app, routes, keyZh, rkey, ckey);
+};

+ 19 - 0
app/z_router/payOrder.js

@@ -0,0 +1,19 @@
+'use strict';
+// 路由配置
+const path = require('path');
+const regPath = path.resolve('app', 'public', 'routerRegister');
+const routerRegister = require(regPath);
+const rkey = 'payOrder';
+const ckey = 'payOrder';
+const keyZh = '支付订单';
+const routes = [
+  { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
+  { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
+  { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, middleware: [ 'password' ], name: `${ckey}Create`, zh: `创建${keyZh}` },
+  { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, name: `${ckey}Update`, zh: `修改${keyZh}` },
+  { method: 'delete', path: `${rkey}/:id`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
+];
+
+module.exports = app => {
+  routerRegister(app, routes, keyZh, rkey, ckey);
+};