addAddress.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <!--pages/addAddress/addAddress.wxml-->
  3. <form @submit="formSubmit">
  4. <view class="wxaddr dflex">
  5. <text>获取微信收件地址</text>
  6. <view class="wechat dflex" @tap="chooseAddress">
  7. <image src="/static/pages/images/wxico.png"></image>立即获取
  8. </view>
  9. </view>
  10. <view class="list">
  11. <view class="li">
  12. <text>收货人</text>
  13. <view class="item">
  14. <input name="name" :value="form.userName" placeholder="请填写收货人姓名"></input>
  15. </view>
  16. </view>
  17. <view class="li">
  18. <text>手机号</text>
  19. <view class="item">
  20. <input name="phone" :value="form.telNumber" placeholder="请填写手机号"></input>
  21. </view>
  22. </view>
  23. <view class="li">
  24. <text>所在地区</text>
  25. <picker class="item" mode="region" @change="bindRegionChange" :value="region" :custom-item="customItem">
  26. <view class="picker">
  27. {{region.length > 0 ? region[0]+region[1]+region[2] : '请选择所在地区'}}
  28. </view>
  29. </picker>
  30. <!-- <image src="/static/pages/images/more.png" class="rico"></image> -->
  31. <image src="../static/mine/images/fbico2.png" class="loca" @click="chooseLocation()"></image>
  32. </view>
  33. <view class="li">
  34. <text>详细信息</text>
  35. <view class="item">
  36. <input name="house" :value="form.detailInfo" placeholder="如街道、门牌号"></input>
  37. </view>
  38. </view>
  39. </view>
  40. <view class="list">
  41. <view class="li">
  42. <text>设为默认</text>
  43. <view class="item she" @tap="setDefault">
  44. <image :src="isDefault ? '/static/pages/images/gou_h.png' : '/static/pages/images/gou.png'"
  45. class="check"></image>
  46. </view>
  47. </view>
  48. </view>
  49. <button class="submit" form-type="submit">确定</button>
  50. </form>
  51. </template>
  52. <script>
  53. const req = require("../../utils/request.js"); // 引入SDK核心类
  54. // 引入SDK核心类
  55. var QQMapWX = require("../../utils/qqmap.js");
  56. export default {
  57. data() {
  58. return {
  59. region: [],
  60. isDefault: false,
  61. isChoose: false,
  62. form: {},
  63. once: false,
  64. address: ""
  65. };
  66. },
  67. components: {},
  68. props: {},
  69. onLoad: function(options) {
  70. if (options && options.isChoose) this.isChoose = options.isChoose; // 实例化API核心类
  71. QQMapWX.initMap('3OJBZ-EQEKO-PFNWC-SHHEK-CGWAJ-KRBF7');
  72. },
  73. methods: {
  74. chooseLocation() {
  75. var tha = this;
  76. uni.chooseLocation({
  77. success: function(res) {
  78. if (res.name) {
  79. tha.form.detailInfo = res.name;
  80. tha.reverseGeocoder(res);
  81. console.log('地址数据》》》:', res);
  82. }
  83. }
  84. });
  85. },
  86. reverseGeocoder(location) {
  87. QQMapWX.initMap();
  88. QQMapWX.reverseGeocoder(location, data => {
  89. this.region = [data.ad_info.province, data.ad_info.city, data.ad_info.district]
  90. console.log('解析后的地址地址数据:', data);
  91. });
  92. },
  93. bindRegionChange(event) {
  94. this.setData({
  95. region: event.detail.value
  96. });
  97. // console.log(event);
  98. },
  99. setDefault() {
  100. this.setData({
  101. isDefault: !this.isDefault
  102. });
  103. },
  104. chooseAddress() {
  105. let _ts = this;
  106. req.scopeAddress(data => {
  107. // console.log(data);
  108. _ts.setData({
  109. form: data,
  110. region: [data.provinceName, data.cityName, data.countyName]
  111. });
  112. });
  113. },
  114. formSubmit(event) {
  115. let data = event.detail.value;
  116. // console.log(data);
  117. if (!data.name) return req.msg('请填写收货人姓名');
  118. data.name = req.excludeSpecial(data.name);
  119. if (!data.phone) return req.msg('请填写手机号');
  120. if (this.region.length == 0) return req.msg('请选择收货人地区');
  121. data.address = this.region.join('');
  122. data.region = this.region.join(',');
  123. if (!data.house) return req.msg('请输入详细地址');
  124. data.isDefault = this.isDefault ? 1 : 0;
  125. let that = this; //进行地址解析
  126. QQMapWX.geocoder(data.address + data.house, res => {
  127. // console.log(res);
  128. data.lat = res.location.lat;
  129. data.lng = res.location.lng;
  130. // if (res.deviation < 0 || res.reliability < 7) return req.msg('请输入详细的街道及门牌号');
  131. let isShowLoading = false;
  132. if (this.once) return false;
  133. this.setData({
  134. once: true
  135. });
  136. if (!isShowLoading) {
  137. req.loadIng('保存中');
  138. isShowLoading = true;
  139. }
  140. req.postRequest('/api/address/save', data, dto => {
  141. if (that.isChoose) {
  142. //选择地址
  143. let pages = getCurrentPages();
  144. var prevPage = pages[pages.length - 2];
  145. prevPage.$vm.setData({
  146. address: dto
  147. });
  148. }
  149. if (isShowLoading) {
  150. uni.hideLoading();
  151. uni.navigateBack();
  152. isShowLoading = false;
  153. }
  154. });
  155. });
  156. }
  157. }
  158. };
  159. </script>
  160. <style>
  161. @import "./addAddress.css";
  162. </style>