index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import Dialog from 'tdesign-miniprogram/dialog/index';
  2. import Toast from 'tdesign-miniprogram/toast/index';
  3. import reasonSheet from '../components/reason-sheet/reasonSheet';
  4. import { getDeliverCompanyList, create, update } from './api';
  5. Page({
  6. deliveryCompanyList: [],
  7. data: {
  8. trackingNo: '',
  9. remark: '',
  10. deliveryCompany: null,
  11. submitActived: false,
  12. submitting: false,
  13. },
  14. onLoad(query) {
  15. const {
  16. rightsNo = '',
  17. logisticsNo = '',
  18. logisticsCompanyName = '',
  19. logisticsCompanyCode = '',
  20. remark = '',
  21. } = query;
  22. if (!rightsNo) {
  23. Dialog.confirm({
  24. title: '请选择售后单?',
  25. content: '',
  26. confirmBtn: '确认',
  27. }).then(() => {
  28. wx.navigateBack({ backRefresh: true });
  29. });
  30. }
  31. this.rightsNo = rightsNo;
  32. if (logisticsNo) {
  33. wx.setNavigationBarTitle({
  34. title: '修改运单号',
  35. fail() {},
  36. });
  37. this.isChange = true;
  38. this.setData({
  39. deliveryCompany: {
  40. name: logisticsCompanyName,
  41. code: logisticsCompanyCode,
  42. },
  43. trackingNo: logisticsNo,
  44. remark,
  45. submitActived: true,
  46. });
  47. }
  48. this.setWatcher('trackingNo', this.checkParams.bind(this));
  49. this.setWatcher('deliveryCompany', this.checkParams.bind(this));
  50. },
  51. setWatcher(key, callback) {
  52. let lastData = this.data;
  53. const keys = key.split('.');
  54. keys.slice(0, -1).forEach((k) => {
  55. lastData = lastData[k];
  56. });
  57. const lastKey = keys[keys.length - 1];
  58. this.observe(lastData, lastKey, callback);
  59. },
  60. observe(data, k, callback) {
  61. let val = data[k];
  62. Object.defineProperty(data, k, {
  63. configurable: true,
  64. enumerable: true,
  65. set: (value) => {
  66. val = value;
  67. callback();
  68. },
  69. get: () => {
  70. return val;
  71. },
  72. });
  73. },
  74. getDeliveryCompanyList() {
  75. if (this.deliveryCompanyList.length > 0) {
  76. return Promise.resolve(this.deliveryCompanyList);
  77. }
  78. return getDeliverCompanyList().then((res) => {
  79. this.deliveryCompanyList = res.data || [];
  80. return this.deliveryCompanyList;
  81. });
  82. },
  83. onInput(e) {
  84. const { key } = e.currentTarget.dataset;
  85. const { value } = e.detail;
  86. this.setData({ [key]: value });
  87. },
  88. onCompanyTap() {
  89. this.getDeliveryCompanyList().then((deliveryCompanyList) => {
  90. reasonSheet({
  91. show: true,
  92. title: '选择物流公司',
  93. options: deliveryCompanyList.map((company) => ({
  94. title: company.name,
  95. checked: this.data.deliveryCompany
  96. ? company.code === this.data.deliveryCompany.code
  97. : false,
  98. })),
  99. showConfirmButton: true,
  100. showCancelButton: true,
  101. emptyTip: '请选择物流公司',
  102. }).then((indexes) => {
  103. this.setData({
  104. deliveryCompany: deliveryCompanyList[indexes[0]],
  105. });
  106. });
  107. });
  108. },
  109. checkParams() {
  110. const res = { errMsg: '', require: false };
  111. if (!this.data.trackingNo) {
  112. res.errMsg = '请填写运单号';
  113. res.require = true;
  114. } else if (!this.data.deliveryCompany) {
  115. res.errMsg = '请选择物流公司';
  116. res.require = true;
  117. }
  118. this.setData({ submitActived: !res.require });
  119. return res;
  120. },
  121. onSubmit() {
  122. const checkRes = this.checkParams();
  123. if (checkRes.errMsg) {
  124. Toast({
  125. context: this,
  126. selector: '#t-toast',
  127. message: checkRes.errMsg,
  128. icon: '',
  129. });
  130. return;
  131. }
  132. const {
  133. trackingNo,
  134. remark,
  135. deliveryCompany: { code, name },
  136. } = this.data;
  137. const params = {
  138. rightsNo: this.rightsNo,
  139. logisticsCompanyCode: code,
  140. logisticsCompanyName: name,
  141. logisticsNo: trackingNo,
  142. remark,
  143. };
  144. const api = this.isChange ? create : update;
  145. this.setData({ submitting: true });
  146. api(params)
  147. .then(() => {
  148. this.setData({ submitting: false });
  149. Toast({
  150. context: this,
  151. selector: '#t-toast',
  152. message: '保存成功',
  153. icon: '',
  154. });
  155. setTimeout(() => wx.navigateBack({ backRefresh: true }), 1000);
  156. })
  157. .catch(() => {
  158. this.setData({ submitting: false });
  159. });
  160. },
  161. onScanTap() {
  162. wx.scanCode({
  163. scanType: ['barCode'],
  164. success: (res) => {
  165. Toast({
  166. context: this,
  167. selector: '#t-toast',
  168. message: '扫码成功',
  169. icon: '',
  170. });
  171. this.setData({ trackingNo: res.result });
  172. },
  173. fail: () => {},
  174. });
  175. },
  176. });