order.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. 'use strict';
  2. const shopController = require('./shop.js');
  3. // 订单控制器
  4. module.exports = class OrderController extends shopController {
  5. /**
  6. * [useModel 使用模型]
  7. * @return {[type]} [description]
  8. */
  9. get useModel() {
  10. const that = this;
  11. return that.app.model.Orders;
  12. }
  13. /**
  14. * [addressModel 用户地址模型]
  15. * @return {[type]} [description]
  16. */
  17. get useAddressModel() {
  18. const that = this;
  19. return that.app.model.Address;
  20. }
  21. /**
  22. * [useCartModel 使用购物车模型]
  23. * @return {[type]} [description]
  24. */
  25. get useCartModel() {
  26. const that = this;
  27. return that.app.model.Carts;
  28. }
  29. /**
  30. * [usePaysConfigModel 使用支付模型]
  31. * @return {[type]} [description]
  32. */
  33. get usePaysConfigModel() {
  34. const that = this;
  35. return that.app.model.PaysConfig;
  36. }
  37. /**
  38. * [useOrdersProductsModel 订单商品模型]
  39. * @return {[type]} [description]
  40. */
  41. get useOrdersProductsModel() {
  42. const that = this;
  43. return that.app.model.OrdersProducts;
  44. }
  45. /**
  46. * [createValidate 用户下单验证器]
  47. * @return {[type]} [description]
  48. */
  49. get createValidate() {
  50. const that = this;
  51. return {
  52. pay_id: that.ctx.rules.name('支付方式')
  53. .required()
  54. .notEmpty()
  55. .number(),
  56. pid: that.ctx.rules.name('家具分类父级ID')
  57. .required()
  58. .notEmpty()
  59. .number(),
  60. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  61. .number(),
  62. user_remarks: that.ctx.rules.default('')
  63. .required(),
  64. order_source: that.ctx.rules.default(1)
  65. .number(),
  66. order_sn: that.ctx.rules.default(`W${that.app.szjcomo.date('YmdHis')}${that.app.szjcomo.mt_rand(100000, 999999)}`)
  67. .required(),
  68. create_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s'))
  69. .required(),
  70. };
  71. }
  72. /**
  73. * [selectValidate 获取订单列表]
  74. * @return {[type]} [description]
  75. */
  76. get selectValidate() {
  77. const that = this;
  78. return {
  79. page: that.ctx.rules.default(1)
  80. .number(),
  81. limit: that.ctx.rules.default(20)
  82. .number(),
  83. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  84. .number(),
  85. order_status: that.ctx.rules.default(-1)
  86. .number(),
  87. order_id: that.ctx.rules.default(0)
  88. .number(),
  89. comment_id: that.ctx.rules.default(0)
  90. .number(),
  91. };
  92. }
  93. /**
  94. * [selectInfoValidate 订单详情验证器]
  95. * @return {[type]} [description]
  96. */
  97. get selectInfoValidate() {
  98. const that = this;
  99. return {
  100. order_id: that.ctx.rules.name('订单ID')
  101. .required()
  102. .notEmpty()
  103. .number(),
  104. };
  105. }
  106. /**
  107. * [orderCountValidate 获取订单数量统计]
  108. * @return {[type]} [description]
  109. */
  110. get orderCountValidate() {
  111. const that = this;
  112. return {
  113. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  114. .number(),
  115. };
  116. }
  117. /**
  118. * [cancelValidate 用户取消订单]
  119. * @return {[type]} [description]
  120. */
  121. get cancelValidate() {
  122. const that = this;
  123. return {
  124. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  125. .number(),
  126. order_id: that.ctx.rules.name('订单ID')
  127. .required()
  128. .notEmpty()
  129. .number(),
  130. };
  131. }
  132. /**
  133. * [deliverValidate 查看物流配送信息]
  134. * @return {[type]} [description]
  135. */
  136. get deliverValidate() {
  137. const that = this;
  138. return {
  139. order_id: that.ctx.rules.name('订单ID')
  140. .required()
  141. .notEmpty()
  142. .number(),
  143. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  144. .number(),
  145. };
  146. }
  147. /**
  148. * [orderCount 订单数量统计]
  149. * @return {[type]} [description]
  150. */
  151. async orderCount() {
  152. const that = this;
  153. try {
  154. const data = await that.ctx.validate(that.orderCountValidate, await that.ctx.getParse());
  155. const seq = that.app.Sequelize;
  156. const options = {
  157. where: {
  158. user_id: data.user_id, order_status: {
  159. [seq.Op.in]: [ 0, 1, 2, 3 ],
  160. },
  161. }, group: [ 'order_status' ],
  162. };
  163. const result = await that.useModel.count(options);
  164. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  165. } catch (err) {
  166. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  167. }
  168. }
  169. /**
  170. * [select 查询订单]
  171. * @return {[type]} [description]
  172. */
  173. async select() {
  174. const that = this;
  175. try {
  176. const data = await that.ctx.validate(that.selectValidate, await that.ctx.getParse());
  177. let result;
  178. if (data.order_id) {
  179. result = await that.orderInfo(data);
  180. } else {
  181. result = await that.orderList(data);
  182. }
  183. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  184. } catch (err) {
  185. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  186. }
  187. }
  188. /**
  189. * [selectInfo 查询订单详情]
  190. * @return {[type]} [description]
  191. */
  192. async selectInfo() {
  193. const that = this;
  194. try {
  195. const data = await that.ctx.validate(that.selectInfoValidate, await that.ctx.getParse());
  196. const result = await that.orderInfo(data);
  197. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  198. } catch (err) {
  199. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  200. }
  201. }
  202. /**
  203. * [orderInfo 订单详情]
  204. * @return {[type]} [description]
  205. */
  206. async orderInfo(data = {}) {
  207. const that = this;
  208. const seq = that.app.Sequelize;
  209. let commentInclude = [];
  210. if (data.comment_id) {
  211. commentInclude = [ {
  212. model: that.app.model.ProductComment,
  213. as: 'product_comment',
  214. attributes: [ 'comment' ],
  215. where: { order_id: data.order_id },
  216. required: false,
  217. } ];
  218. }
  219. const options = {
  220. where: { order_id: data.order_id },
  221. include: [
  222. {
  223. model: that.app.model.OrdersProducts,
  224. as: 'orders_products',
  225. attributes: [ 'product_count', 'product_name', 'product_image', 'product_id', 'category_id', 'shop_price', 'volume', 'price' ],
  226. include: commentInclude,
  227. },
  228. { model: that.app.model.PaysConfig, as: 'pays_config', attributes: [] },
  229. ],
  230. attributes: [ 'order_sn', 'create_time', 'order_status', 'order_amount', 'order_id', [ seq.col('pays_config.pay_name'), 'pay_name' ], 'consignee', 'mobile', 'address', 'is_urge', 'pay_id', 'surplus_amout' ],
  231. };
  232. const selectBean = await that.app.comoBean.instance(data, options);
  233. const result = await that.service.base.select(selectBean, that.useModel, '订单详情查询失败,请稍候重试', false);
  234. return result;
  235. }
  236. /**
  237. * [orderList 订单列表]
  238. * @param {Object} data [description]
  239. * @return {[type]} [description]
  240. */
  241. async orderList(data = {}) {
  242. const that = this;
  243. // const seq = that.app.Sequelize;
  244. const options = {
  245. offset: (data.page - 1) * data.limit, limit: data.limit, where: { user_id: data.user_id },
  246. include: [
  247. {
  248. model: that.app.model.OrdersProducts,
  249. as: 'orders_products',
  250. attributes: [ 'product_count', 'product_name', 'product_image', 'product_id', 'category_id', 'shop_price', 'volume', 'price' ],
  251. order: [ [ 'rec_id', 'asc' ] ],
  252. },
  253. ],
  254. attributes: [ 'order_sn', 'create_time', 'order_status', 'order_amount', 'surplus_amout', 'order_id', 'is_urge' ],
  255. order: [ [ 'order_id', 'desc' ] ],
  256. };
  257. if (data.order_status > -1) options.where.order_status = data.order_status;
  258. const selectBean = await that.app.comoBean.instance(data, options);
  259. const result = await that.service.base.select(selectBean, that.useModel, '订单列表查询失败,请稍候重试', true);
  260. return result;
  261. }
  262. /**
  263. * [cancel 用户取消订单]
  264. * @return {[type]} [description]
  265. */
  266. async cancel() {
  267. const that = this;
  268. let transaction;
  269. try {
  270. const data = await that.ctx.validate(that.cancelValidate, await that.ctx.anyParse());
  271. transaction = await that.app.model.transaction();
  272. const info = await that.useModel.findOne({
  273. where: { order_id: data.order_id, user_id: data.user_id },
  274. attributes: [ 'surplus_amout', 'order_sn' ],
  275. transaction,
  276. raw: true,
  277. });
  278. if (!info) throw new Error('订单信息不存在,请勿非法操作');
  279. const opts = { where: { user_id: data.user_id, order_id: data.order_id }, transaction };
  280. const updateBean = await that.app.comoBean.instance({ order_status: 6, surplus_amout: 0 }, opts);
  281. const result = await that.service.base.update(updateBean, that.useModel, '取消订单操作失败,请重试');
  282. if (Number(info.surplus_amout) > 0) await that.service.shop.userMoneyAdd(data.user_id, Number(info.surplus_amout), transaction, `订单取消,退回订单抵扣的余额,订单编号是:${info.order_sn}`);
  283. await that.service.order.orderAction({
  284. admin_id: 0,
  285. order_id: data.order_id,
  286. action_desc: '用户取消订单',
  287. }, transaction);
  288. await transaction.commit();
  289. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  290. } catch (err) {
  291. if (transaction) await transaction.rollback();
  292. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  293. }
  294. }
  295. /**
  296. * [urge 用户催发货]
  297. * @return {[type]} [description]
  298. */
  299. async urge() {
  300. const that = this;
  301. let transaction;
  302. try {
  303. const data = await that.ctx.validate(that.cancelValidate, await that.ctx.anyParse());
  304. transaction = await that.app.model.transaction();
  305. const opts = { where: { user_id: data.user_id, order_id: data.order_id }, transaction };
  306. const updateBean = await that.app.comoBean.instance({ is_urge: that.app.Sequelize.literal('is_urge + 1') }, opts);
  307. const result = await that.service.base.update(updateBean, that.useModel, '催发货失败,请稍候重试');
  308. await that.service.order.orderAction({
  309. admin_id: 0,
  310. order_id: data.order_id,
  311. action_desc: '用户催发货',
  312. }, transaction);
  313. await transaction.commit();
  314. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  315. } catch (err) {
  316. if (transaction) await transaction.rollback();
  317. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  318. }
  319. }
  320. /**
  321. * [confirm 用户确认收货]
  322. * @return {[type]} [description]
  323. */
  324. async confirm() {
  325. const that = this;
  326. let transaction;
  327. try {
  328. const data = await that.ctx.validate(that.cancelValidate, await that.ctx.anyParse());
  329. transaction = await that.app.model.transaction();
  330. const opts = { where: { user_id: data.user_id, order_id: data.order_id }, transaction };
  331. const updateBean = await that.app.comoBean.instance({
  332. order_status: 3,
  333. query_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  334. }, opts);
  335. const result = await that.service.base.update(updateBean, that.useModel, '确认收货更新失败,请稍候重试');
  336. await that.service.order.orderAction({
  337. admin_id: 0,
  338. order_id: data.order_id,
  339. action_desc: '用户已收货',
  340. }, transaction);
  341. await transaction.commit();
  342. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  343. } catch (err) {
  344. if (transaction) await transaction.rollback();
  345. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  346. }
  347. }
  348. // ======================以下为用户下单所需要方法==============================
  349. /**
  350. * [createV2 用户下单]
  351. * @return {[type]} [description]
  352. */
  353. async createV2() {
  354. const that = this;
  355. let transaction;
  356. try {
  357. const data = await that.ctx.validate(that.createValidate, await that.ctx.postParse());
  358. const inviteCode = data.inviteCode ? Number(data.inviteCode) : -1;
  359. const isUseMoney = data.isUseMoney ? data.isUseMoney : false;
  360. that.logger.error('用户下单绑定的邀请码: %s ', inviteCode);
  361. transaction = await that.app.model.transaction();
  362. const products = await that.getCartProducts(data.user_id, data.pid, transaction);
  363. const orderAmount = await that.caclOrderAmountV2(products, data.pid);
  364. const result = await that.orderCreateBeforeV2(data, orderAmount, transaction, inviteCode, isUseMoney);
  365. const cartIds = await that.orderProductsAfter(result.order.order_id, products, data, transaction);
  366. await that.clearCartProducts(cartIds, transaction);
  367. await transaction.commit();
  368. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result.payParmas, false));
  369. } catch (err) {
  370. if (transaction) await transaction.rollback();
  371. await that.logs('order.js', err);
  372. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  373. }
  374. }
  375. /**
  376. * [caclOrderAmount 计算订单总价]
  377. * @return {[type]} [description]
  378. * @param products
  379. * @param pid
  380. */
  381. async caclOrderAmountV2(products, pid = 4) {
  382. const that = this;
  383. const keys = [ 'can_discount_category' ];
  384. const config = await that.service.configs.getConfigMoreValue(keys);
  385. const temA = config.can_discount_category.replace('[', '');
  386. const temB = temA.replace(']', '');
  387. let canDiscountCategory = temB.split(',');
  388. canDiscountCategory = canDiscountCategory.map(Number);
  389. const orderAmount = { orderPriceCanDiscount: 0, orderPriceNotDiscount: 0 };
  390. // 2023/10/12 todo 订单价格办公家具全场八折
  391. products.forEach(item => {
  392. if (!canDiscountCategory.includes(item.category_id)) {
  393. orderAmount.orderPriceNotDiscount += ((Number(item.price > 0 ? item.price : item.shop_price) * item.product_count) * (pid === 1 ? 0.8 : 1));
  394. } else {
  395. orderAmount.orderPriceCanDiscount += ((Number(item.price > 0 ? item.price : item.shop_price) * item.product_count) * (pid === 1 ? 0.8 : 1));
  396. }
  397. });
  398. return orderAmount;
  399. }
  400. /**
  401. * [orderCreateBefore 用户下单前置]
  402. * @param data data [description]
  403. * @param orderAmount
  404. * @param transaction
  405. * @param inviteCode
  406. * @param isUseMoney
  407. * @return {[type]} [description]
  408. */
  409. async orderCreateBeforeV2(data = {}, orderAmount = {
  410. orderPriceCanDiscount: 0,
  411. orderPriceNotDiscount: 0,
  412. }, transaction = null, inviteCode = -1, isUseMoney = false) {
  413. const that = this;
  414. const address = await that.getUserAddress(data.user_id, transaction);
  415. if (!address && !data.isSelfPickUp) throw new Error('您还没有完善收货信息,请完善收货地址或者选择门店自提再下单吧!');
  416. // 计算用户需要支付的金额
  417. const tmpPayMentMoney = await that.service.order.caclPaymentMoneyV2(data, orderAmount, transaction, isUseMoney);
  418. const opts = {
  419. order_sn: data.order_sn,
  420. user_id: data.user_id,
  421. order_status: 0,
  422. order_source: data.order_source,
  423. surplus_amout: tmpPayMentMoney.surplus_amout,
  424. consignee: address ? address.uname : '门店自提',
  425. mobile: address ? address.mobile : '',
  426. order_amount: orderAmount.orderPriceCanDiscount + orderAmount.orderPriceNotDiscount,
  427. province_id: address ? address.province_id : '',
  428. city_id: address ? address.city_id : '',
  429. user_remarks: data.user_remarks,
  430. county_id: address ? address.county_id : '',
  431. address: address ? [ address.province_name, address.city_name, address.county_name, address.address ].join('') : '门店自提',
  432. create_time: data.create_time,
  433. pay_id: data.pay_id,
  434. shipping_fee: 0,
  435. is_urge: 0,
  436. inviter_id: inviteCode,
  437. };
  438. const orderBean = await that.app.comoBean.instance(opts, { transaction });
  439. // 2022/11/11 创建订单
  440. const order = await that.service.base.create(orderBean, that.useModel, '订单提交失败,请重试');
  441. if (tmpPayMentMoney.surplus_amout) await that.service.shop.userMoneySub(data.user_id, tmpPayMentMoney.surplus_amout, transaction, `商品下单时余额抵扣,订单编号是:${data.order_sn}`);
  442. // 用户全部使用余额支付
  443. if (!tmpPayMentMoney.payment_money) {
  444. const payParmas = await that.service.order.getSurplusPayParams(order.order_id, data.user_id, tmpPayMentMoney.surplus_amout, transaction);
  445. // 订单发货时扣除库存,受控于后台配置的扣除时机
  446. await that.service.order.productStockSub(order.order_id, transaction, 1);
  447. return { order, payParmas };
  448. }
  449. let payParmas = {};
  450. const userInfo = await that.app.model.Users.findOne({
  451. where: { user_id: data.user_id },
  452. attributes: [ 'openid' ],
  453. transaction,
  454. raw: true,
  455. });
  456. if (data.order_source === 1) {
  457. payParmas = await that.service.wechat.wechatAccountJsApiPay({
  458. body: '订单支付',
  459. openid: userInfo.openid,
  460. total_fee: (tmpPayMentMoney.payment_money * 100),
  461. trade_type: 'JSAPI',
  462. attach: that.app.szjcomo.base64_encode(that.app.szjcomo.json_encode({
  463. user_id: data.user_id,
  464. order_id: order.order_id,
  465. pay_id: data.pay_id,
  466. })),
  467. });
  468. }
  469. return {
  470. order,
  471. payParmas: { payment_money: tmpPayMentMoney.payment_money, params: payParmas, order_id: order.order_id },
  472. };
  473. }
  474. /**
  475. * [orderProductsAfter description]
  476. * @param {[type]} order_id [description]
  477. * @param {Array} products [description]
  478. * @param data
  479. * @param {[type]} transaction [description]
  480. * @return {[type]} [description]
  481. */
  482. async orderProductsAfter(order_id, products = [], data = {}, transaction = null) {
  483. const that = this;
  484. const cartIds = [];
  485. // const seq = that.app.Sequelize;
  486. const orderProducts = products.map(item => {
  487. const tmp = Object.assign({
  488. order_id, total_price: Number(item.price > 0 ? item.price : item.shop_price) * item.product_count,
  489. // 2023/1/9 默认出货数量是订单商品的购买数量
  490. deliver_count: Number(item.product_count),
  491. create_time: data.create_time,
  492. }, item);
  493. cartIds.push(item.cart_id);
  494. return tmp;
  495. });
  496. const result = await that.useOrdersProductsModel.bulkCreate(orderProducts, { transaction });
  497. if (!result) throw new Error('订单商品写入失败,请重试');
  498. return cartIds;
  499. }
  500. /**
  501. * [clearCartProducts 清除购物车商品]
  502. * @param {Array} cartIds [description]
  503. * @return {[type]} [description]
  504. */
  505. async clearCartProducts(cartIds = [], transaction = null) {
  506. const that = this;
  507. const seq = that.app.Sequelize;
  508. const deleteBean = await that.app.comoBean.instance({}, {
  509. where: { cart_id: { [seq.Op.in]: cartIds } },
  510. transaction,
  511. });
  512. await that.service.base.delete(deleteBean, that.useCartModel, '订单提交成功,清除购物车失败,请重试');
  513. }
  514. /**
  515. * [getCartProducts 下单时获取购物车商品]
  516. * @param {Number} user_id [description]
  517. * @return {[type]} [description]
  518. */
  519. async getCartProducts(user_id = 0, pid = 4, transaction = null) {
  520. const that = this;
  521. const seq = that.app.Sequelize;
  522. const options = {
  523. where: { user_id, is_select: 1, pid }, transaction, raw: true,
  524. include: [
  525. { model: that.app.model.Products, as: 'products', attributes: [] },
  526. ],
  527. attributes: [
  528. [ seq.col('products.product_id'), 'product_id' ],
  529. [ seq.col('products.category_id'), 'category_id' ],
  530. [ seq.col('products.product_name'), 'product_name' ],
  531. [ seq.col('products.product_sn'), 'product_sn' ],
  532. [ seq.col('products.product_image'), 'product_image' ],
  533. [ seq.col('products.shop_price'), 'shop_price' ],
  534. [ seq.col('products.dinning_coin_amount'), 'dinning_coin_amount' ],
  535. [ seq.col('products.dining_partner_id'), 'dining_partner_id' ],
  536. [ seq.col('products.goods_type'), 'goods_type' ],
  537. [ seq.col('products.activity_id'), 'activity_id' ], 'product_count', 'cart_id', 'volume', 'price',
  538. ],
  539. };
  540. const res = await that.useCartModel.findAll(options);
  541. return res;
  542. }
  543. /**
  544. * [getUserAddress 获取收货地址]
  545. * @param {Number} user_id [description]
  546. * @return {[type]} [description]
  547. */
  548. async getUserAddress(user_id = 0, transaction = null) {
  549. const that = this;
  550. const options = {
  551. where: { user_id, is_use: 1 }, raw: true, transaction, attributes: {
  552. exclude: [ 'user_id', 'address_id', 'is_use', 'update_time', 'create_time' ],
  553. },
  554. };
  555. const res = await that.useAddressModel.findOne(options);
  556. return res;
  557. }
  558. /**
  559. * [orderDeliver 查看物流配送信息]
  560. * @return {[type]} [description]
  561. */
  562. async orderDeliver() {
  563. const that = this;
  564. try {
  565. const data = await that.ctx.validate(that.deliverValidate, await that.ctx.getParse());
  566. const selectBean = await that.app.comoBean.instance(data, {
  567. where: { order_id: data.order_id, user_id: data.user_id },
  568. include: [
  569. {
  570. model: that.app.model.OrdersProducts,
  571. as: 'orders_products',
  572. attributes: [ 'product_count', 'product_name', 'product_image', 'product_id', 'category_id', 'shop_price', 'deliver_count', 'is_deliver', 'volume', 'price' ],
  573. },
  574. ],
  575. attributes: [ 'order_sn', 'create_time', 'order_status', 'order_amount', 'order_id', 'consignee', 'mobile', 'address', 'is_urge', 'pay_id', 'surplus_amout', 'is_express', 'express_sn', 'express_id', 'deliver_desc', 'deliver_time' ],
  576. });
  577. const result = await that.service.base.select(selectBean, that.useModel, '查询订单详情失败,请稍候重试', false, false);
  578. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  579. } catch (err) {
  580. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  581. }
  582. }
  583. /**
  584. * [orderPayFind 查询验证订单是否已经支付]
  585. * @return {[type]} [description]
  586. */
  587. async orderPayFind() {
  588. const that = this;
  589. try {
  590. const data = await that.ctx.validate(that.cancelValidate, await that.ctx.getParse());
  591. const selectBean = await that.app.comoBean.instance(data, {
  592. where: {
  593. order_id: data.order_id,
  594. user_id: data.user_id,
  595. }, attributes: [ 'order_status' ],
  596. });
  597. const result = await that.service.base.select(selectBean, that.useModel, '查询订单信息失败,请勿非法操作', false, false);
  598. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  599. } catch (err) {
  600. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  601. }
  602. }
  603. /**
  604. * [orderAgainPay 订单继续支付]
  605. * @return {[type]} [description]
  606. */
  607. async orderAgainPay() {
  608. const that = this;
  609. try {
  610. const data = await that.ctx.validate(that.cancelValidate, await that.ctx.getParse());
  611. const seq = that.app.Sequelize;
  612. const info = await that.useModel.findOne({
  613. include: [ { model: that.app.model.Users, as: 'users', attributes: [] } ],
  614. where: { order_id: data.order_id }, raw: true,
  615. attributes: [ 'order_amount', 'surplus_amout', 'pay_id', [ seq.col('users.openid'), 'openid' ] ],
  616. });
  617. if (!info) throw new Error('订单信息不存在,请勿非法操作');
  618. const result = await that.service.wechat.wechatAccountJsApiPay({
  619. body: '订单支付',
  620. openid: info.openid,
  621. total_fee: ((Number(info.order_amount) - Number(info.surplus_amout)) * 100),
  622. trade_type: 'JSAPI',
  623. attach: that.app.szjcomo.base64_encode(that.app.szjcomo.json_encode({
  624. user_id: data.user_id,
  625. order_id: data.order_id,
  626. pay_id: data.pay_id,
  627. })),
  628. });
  629. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  630. } catch (err) {
  631. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  632. }
  633. }
  634. };