index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { getPermission } from '../../../../utils/getPermission';
  2. import { phoneRegCheck } from '../../../../utils/util';
  3. import Toast from 'tdesign-miniprogram/toast/index';
  4. import { addressParse } from '../../../../utils/addressParse';
  5. import { resolveAddress, rejectAddress } from '../../../../services/address/list';
  6. Component({
  7. externalClasses: ['t-class'],
  8. properties: {
  9. title: {
  10. type: String,
  11. },
  12. navigateUrl: {
  13. type: String,
  14. },
  15. navigateEvent: {
  16. type: String,
  17. },
  18. isCustomStyle: {
  19. type: Boolean,
  20. value: false,
  21. },
  22. isDisabledBtn: {
  23. type: Boolean,
  24. value: false,
  25. },
  26. isOrderSure: {
  27. type: Boolean,
  28. value: false,
  29. },
  30. },
  31. methods: {
  32. getWxLocation() {
  33. if (this.properties.isDisabledBtn) return;
  34. getPermission({ code: 'scope.address', name: '通讯地址' }).then(() => {
  35. wx.chooseAddress({
  36. success: async (options) => {
  37. const { provinceName, cityName, countyName, detailInfo, userName, telNumber } = options;
  38. if (!phoneRegCheck(telNumber)) {
  39. Toast({
  40. context: this,
  41. selector: '#t-toast',
  42. message: '请填写正确的手机号',
  43. });
  44. return;
  45. }
  46. const target = {
  47. name: userName,
  48. phone: telNumber,
  49. countryName: '中国',
  50. countryCode: 'chn',
  51. detailAddress: detailInfo,
  52. provinceName: provinceName,
  53. cityName: cityName,
  54. districtName: countyName,
  55. isDefault: false,
  56. isOrderSure: this.properties.isOrderSure,
  57. };
  58. try {
  59. const { provinceCode, cityCode, districtCode } = await addressParse(provinceName, cityName, countyName);
  60. const params = Object.assign(target, {
  61. provinceCode,
  62. cityCode,
  63. districtCode,
  64. });
  65. if (this.properties.isOrderSure) {
  66. this.onHandleSubmit(params);
  67. } else if (this.properties.navigateUrl != '') {
  68. const { navigateEvent } = this.properties;
  69. this.triggerEvent('navigate');
  70. wx.navigateTo({
  71. url: this.properties.navigateUrl,
  72. success: function (res) {
  73. res.eventChannel.emit(navigateEvent, params);
  74. },
  75. });
  76. } else {
  77. this.triggerEvent('change', params);
  78. }
  79. } catch (error) {
  80. wx.showToast({ title: '地址解析出错,请稍后再试', icon: 'none' });
  81. }
  82. },
  83. fail(err) {
  84. console.warn('未选择微信收货地址', err);
  85. },
  86. });
  87. });
  88. },
  89. async queryAddress(addressId) {
  90. try {
  91. const { data } = await apis.userInfo.queryAddress({ addressId });
  92. return data.userAddressVO;
  93. } catch (err) {
  94. console.error('查询地址错误', err);
  95. throw err;
  96. }
  97. },
  98. findPage(pageRouteUrl) {
  99. const currentRoutes = getCurrentPages().map((v) => v.route);
  100. return currentRoutes.indexOf(pageRouteUrl);
  101. },
  102. async onHandleSubmit(params) {
  103. try {
  104. const orderPageDeltaNum = this.findPage('pages/order/order-confirm/index');
  105. if (orderPageDeltaNum > -1) {
  106. wx.navigateBack({ delta: 1 });
  107. resolveAddress(params);
  108. return;
  109. }
  110. } catch (err) {
  111. rejectAddress(params);
  112. console.error(err);
  113. }
  114. },
  115. },
  116. });