order.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const _ = require('lodash');
  2. module.exports = class extends think.Model {
  3. /**
  4. * 生成订单的编号order_sn
  5. * @returns {string}
  6. */
  7. generateOrderNumber() {
  8. const date = new Date();
  9. return date.getFullYear() + _.padStart(date.getMonth(), 2, '0') + _.padStart(date.getDay(), 2, '0') + _.padStart(date.getHours(), 2, '0') + _.padStart(date.getMinutes(), 2, '0') + _.padStart(date.getSeconds(), 2, '0') + _.random(100000, 999999);
  10. }
  11. /**
  12. * 获取订单可操作的选项
  13. * @param orderId
  14. * @returns {Promise.<{cancel: boolean, delete: boolean, pay: boolean, comment: boolean, delivery: boolean, confirm: boolean, return: boolean}>}
  15. */
  16. async getOrderHandleOption(orderId) {
  17. const handleOption = {
  18. cancel: false, // 取消操作
  19. delete: false, // 删除操作
  20. pay: false, // 支付操作
  21. comment: false, // 评论操作
  22. delivery: false, // 确认收货操作
  23. confirm: false, // 完成订单操作
  24. return: false, // 退换货操作
  25. buy: false // 再次购买
  26. };
  27. const orderInfo = await this.where({id: orderId}).find();
  28. // 订单流程:下单成功-》支付订单-》发货-》收货-》评论
  29. // 订单相关状态字段设计,采用单个字段表示全部的订单状态
  30. // 1xx表示订单取消和删除等状态 0订单创建成功等待付款,101订单已取消,102订单已删除
  31. // 2xx表示订单支付状态,201订单已付款,等待发货
  32. // 3xx表示订单物流相关状态,300订单已发货,301用户确认收货
  33. // 4xx表示订单退换货相关的状态,401没有发货,退款402,已收货,退款退货
  34. // 如果订单已经取消或是已完成,则可删除和再次购买
  35. if (orderInfo.order_status === 101) {
  36. handleOption.delete = true;
  37. handleOption.buy = true;
  38. }
  39. // 如果订单没有被取消,且没有支付,则可支付,可取消
  40. if (orderInfo.order_status === 0) {
  41. handleOption.cancel = true;
  42. handleOption.pay = true;
  43. }
  44. // 如果订单已付款,没有发货,则可退款操作
  45. if (orderInfo.order_status === 201) {
  46. handleOption.return = true;
  47. }
  48. // 如果订单已经发货,没有收货,则可收货操作和退款、退货操作
  49. if (orderInfo.order_status === 300) {
  50. handleOption.cancel = true;
  51. handleOption.pay = true;
  52. handleOption.return = true;
  53. }
  54. // 如果订单已经支付,且已经收货,则可完成交易、评论和再次购买
  55. if (orderInfo.order_status === 301) {
  56. handleOption.delete = true;
  57. handleOption.comment = true;
  58. handleOption.buy = true;
  59. }
  60. return handleOption;
  61. }
  62. async getOrderStatusText(orderId) {
  63. const orderInfo = await this.where({id: orderId}).find();
  64. let statusText = '未付款';
  65. switch (orderInfo.order_status) {
  66. case 0:
  67. statusText = '未付款';
  68. break;
  69. case 1:
  70. statusText = '未兑换';
  71. break;
  72. case 2:
  73. statusText = '已兑换';
  74. break;
  75. }
  76. return statusText;
  77. }
  78. /**
  79. * 更改订单支付状态
  80. * @param orderId
  81. * @param payStatus
  82. * @returns {Promise.<boolean>}
  83. */
  84. async updatePayStatus(orderId, payStatus = 0) {
  85. return this.where({id: orderId}).limit(1).update({pay_status: parseInt(payStatus)});
  86. }
  87. /**
  88. * 根据订单编号查找订单信息
  89. * @param orderSn
  90. * @returns {Promise.<Promise|Promise<any>|T|*>}
  91. */
  92. async getOrderByOrderSn(orderSn) {
  93. if (think.isEmpty(orderSn)) {
  94. return {};
  95. }
  96. return this.where({order_sn: orderSn}).find();
  97. }
  98. };