index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. Component({
  2. props: {
  3. componentData: {},
  4. addressData: null,
  5. btnBgColor: '#1677FF',
  6. onComplete: null,
  7. maxLen: 20,
  8. isShowDefault: true,
  9. btnMarginTop: '300rpx',
  10. regionType: 'map'
  11. },
  12. data: {
  13. addressInfo: {
  14. name: '',
  15. mobile: '',
  16. region: [],
  17. regionCode: [],
  18. address: '',
  19. isDefault: false,
  20. latitude: '',
  21. longitude: '',
  22. addressNumber: ''
  23. } // 收货地址信息
  24. },
  25. didMount() {
  26. if (!this.isEmptyObject(this.props.addressData)) {
  27. this.setData({
  28. addressInfo: { ...this.props.addressData
  29. }
  30. });
  31. }
  32. },
  33. methods: {
  34. isEmptyObject(obj) {
  35. return this.isObject(obj) && !Object.keys(obj).length;
  36. },
  37. isObject(val) {
  38. return Object.prototype.toString.call(val) === '[object Object]';
  39. },
  40. defaultChange(e) {
  41. this.setData({
  42. 'addressInfo.isDefault': e.detail.value
  43. });
  44. },
  45. chooseContact() {
  46. my.choosePhoneContact({
  47. success: res => {
  48. const mobile = res.mobile.replace(/-|\s/g, '');
  49. this.setData({
  50. 'addressInfo.name': res.name,
  51. 'addressInfo.mobile': mobile
  52. });
  53. }
  54. });
  55. },
  56. chooseRegion() {
  57. const {
  58. regionType = ''
  59. } = this.props; // 选择区域为地图
  60. if (regionType === 'map') {
  61. my.chooseLocation({
  62. success: res => {
  63. const {
  64. adName = '',
  65. address = '',
  66. cityName = '',
  67. latitude = 0,
  68. longitude = 0,
  69. provinceName = '',
  70. name = '',
  71. adCode = ''
  72. } = res || {};
  73. let resAddress = address;
  74. [provinceName, cityName, adName].forEach(v => {
  75. resAddress = resAddress.replace(v, '');
  76. });
  77. const tempSet = new Set([provinceName, cityName, adName]);
  78. this.setData({
  79. 'addressInfo.region': Array.from(tempSet),
  80. 'addressInfo.address': `${resAddress}${name}`,
  81. 'addressInfo.latitude': latitude,
  82. 'addressInfo.longitude': longitude,
  83. 'addressInfo.regionCode': ['', '', adCode]
  84. });
  85. },
  86. fail: () => {}
  87. });
  88. } else {
  89. // 选择区域为regionPicker
  90. this.selRegion();
  91. }
  92. },
  93. selRegion() {
  94. if (my.regionPicker) {
  95. my.regionPicker({
  96. selectedItem: this.data.addressInfo.region || [],
  97. success: res => {
  98. this.setData({
  99. 'addressInfo.region': res.data,
  100. 'addressInfo.regionCode': res.code
  101. });
  102. }
  103. });
  104. } else {
  105. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样提示
  106. my.alert({
  107. title: '提示',
  108. content: '当前支付宝版本过低,无法使用此功能,请升级最新版本支付宝'
  109. });
  110. }
  111. },
  112. checkMobile(mobile) {
  113. const reg = new RegExp(/^1\d{10}$/);
  114. return reg.test(mobile);
  115. },
  116. saveAddress(e) {
  117. const obj = e.detail.value;
  118. console.log(obj, 'obj');
  119. if (!obj.name) {
  120. return my.showToast({
  121. content: '请输入收货人'
  122. });
  123. }
  124. if (!obj.mobile) {
  125. return my.showToast({
  126. content: '请输入手机号码'
  127. });
  128. }
  129. if (!this.checkMobile(obj.mobile)) {
  130. return my.showToast({
  131. content: '手机号码格式不正确'
  132. });
  133. }
  134. if (this.props.regionType === 'map') {
  135. // 采用map 校验
  136. const {
  137. latitude = '',
  138. longitude = ''
  139. } = this.data.addressInfo;
  140. if (!latitude || !longitude) {
  141. return my.showToast({
  142. content: '请选择所在地区'
  143. });
  144. }
  145. } else {
  146. // 采用region picker 校验
  147. if (Object.prototype.toString.call(this.data.addressInfo.regionCode) === '[object Array]' && this.data.addressInfo.regionCode.length !== 3) {
  148. return my.showToast({
  149. content: '请选择所在地区'
  150. });
  151. }
  152. if (Object.prototype.toString.call(this.data.addressInfo.region) === '[object Array]' && this.data.addressInfo.region.length !== 3) {
  153. return my.showToast({
  154. content: '请选择所在地区'
  155. });
  156. }
  157. }
  158. if (!obj.addressNumber) {
  159. return my.showToast({
  160. content: '请输入详细地址'
  161. });
  162. }
  163. const region = { ...obj,
  164. region: this.data.addressInfo.region,
  165. regionCode: this.data.addressInfo.regionCode,
  166. latitude: this.data.addressInfo.latitude,
  167. longitude: this.data.addressInfo.longitude,
  168. address: this.data.addressInfo.address
  169. };
  170. if (Object.prototype.toString.call(this.props.onComplete) === '[object Function]') {
  171. this.props.onComplete(region);
  172. }
  173. }
  174. }
  175. });