order.js 27 KB

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