addAddress.vue 5.8 KB

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