index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <template>
  2. <!-- 添加新地址 -->
  3. <view :style="colorStyle">
  4. <form @submit="formSubmit">
  5. <view class='addAddress'>
  6. <view class="pad30">
  7. <view class='default acea-row row-middle borderRadius15'>
  8. <input v-model="addressValue" type="text" placeholder="粘贴地址信息,自动拆分姓名、电话和地址"
  9. placeholder-class='placeholder' style="width:100%;"
  10. @blur="identify()">
  11. </view>
  12. </view>
  13. <view class="pad30 mt-22">
  14. <view class='list borderRadius15'>
  15. <view class='item acea-row row-between-wrapper'>
  16. <view class='name'>姓名</view>
  17. <input type='text' placeholder='请输入姓名' name='real_name' :value="userAddress.real_name"
  18. placeholder-class='placeholder'></input>
  19. </view>
  20. <view class='item acea-row row-between-wrapper'>
  21. <view class='name'>联系电话</view>
  22. <input type='number' maxlength="11" placeholder='请输入联系电话' name="phone" :value='userAddress.phone'
  23. placeholder-class='placeholder' pattern="\d*"></input>
  24. </view>
  25. <view class='item acea-row row-between-wrapper'>
  26. <view class='name'>所在地区</view>
  27. <view class="address acea-row row-between">
  28. <view class="addressCon acea-row" @click="changeRegion">
  29. <text class="picker color-add" v-if="!addressInfo.length">请选择地址</text>
  30. <view v-else>
  31. <text class="picker">{{addressText}}</text>
  32. <view class="font-num tip" v-if="isStreet">请补充县/区信息</view>
  33. </view>
  34. </view>
  35. <!-- <text class="iconfont icon-dizhi fontcolor" @click="chooseLocation"></text> -->
  36. </view>
  37. </view>
  38. <view class='item acea-row row-between-wrapper'>
  39. <view class='name'>详细地址</view>
  40. <view class="address">
  41. <input type='text' placeholder='请填写具体地址' name='detail' placeholder-class='placeholder'
  42. :value='userAddress.detail' class="detail"></input>
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. <view class="pad30">
  48. <view class='default acea-row row-middle borderRadius15'>
  49. <checkbox-group @change='ChangeIsDefault'>
  50. <checkbox :checked="userAddress.is_default ? true : false" />设置为默认地址
  51. </checkbox-group>
  52. </view>
  53. </view>
  54. <button class='keepBnt bg-color' form-type="submit">立即保存</button>
  55. <!-- #ifdef MP -->
  56. <view class="wechatAddress" v-if="!id" @click="getWxAddress">导入微信地址</view>
  57. <!-- #endif -->
  58. <!-- #ifdef H5 -->
  59. <view class="wechatAddress" v-if="this.$wechat.isWeixin() && !id" @click="getAddress">导入微信地址</view>
  60. <!-- #endif -->
  61. </view>
  62. </form>
  63. <areaWindow ref="areaWindow" :display="display" :address="addressInfo"
  64. @submit="OnChangeAddress" @changeClose="changeClose"></areaWindow>
  65. <home v-if="navigation"></home>
  66. <!-- #ifdef MP -->
  67. <authorize v-if="isShowAuth" @authColse="authColse" @onLoadFun="onLoadFun"></authorize>
  68. <!-- #endif -->
  69. </view>
  70. </template>
  71. <script>
  72. import {
  73. editAddress,
  74. getAddressDetail,
  75. getGeocoder,
  76. getCityList
  77. } from '@/api/user.js';
  78. import {
  79. getCityData
  80. } from '@/api/api.js';
  81. import {
  82. toLogin
  83. } from '@/libs/login.js';
  84. import {
  85. mapGetters
  86. } from "vuex";
  87. import home from '@/components/home';
  88. import colors from '@/mixins/color.js';
  89. import areaWindow from '../components/areaWindow';
  90. import AddressParse from '../components/zh-address-parse.min.js'
  91. export default {
  92. components: {
  93. areaWindow,
  94. home
  95. },
  96. mixins:[colors],
  97. data() {
  98. return {
  99. cartId: '', //购物车id
  100. pinkId: 0, //拼团id
  101. couponId: 0, //优惠券id
  102. id: 0, //地址id
  103. userAddress: {
  104. is_default: false
  105. }, //地址详情
  106. isAuto: false, //没有授权的不会自动授权
  107. isShowAuth: false, //是否隐藏授权
  108. district: [],
  109. news: '',
  110. noCoupon: 0,
  111. display: false,
  112. addressInfo:[],
  113. addressVal:'',
  114. latitude:'',
  115. longitude:'',
  116. city_id:0,
  117. isStreet:0,
  118. addressValue:"",
  119. deliveryType:1,//配送方式
  120. store_name:'',//门店名称
  121. storeId:0,//门店id
  122. product_id:0//商品id
  123. };
  124. },
  125. computed: {...mapGetters(['isLogin']),
  126. addressText(){
  127. return this.addressInfo.map(v=>v.label).join('/');
  128. }
  129. },
  130. watch: {
  131. isLogin: {
  132. handler: function(newV, oldV) {
  133. if (newV) {
  134. //#ifndef MP
  135. this.getUserAddress();
  136. //#endif
  137. }
  138. },
  139. deep: true
  140. }
  141. },
  142. onLoad(options) {
  143. this.cartId = options.cartId || '';
  144. this.pinkId = options.pinkId || 0;
  145. this.couponId = options.couponId || 0;
  146. this.id = options.id || 0;
  147. this.noCoupon = options.noCoupon || 0;
  148. this.news = options.new || '';
  149. this.deliveryType = options.delivery_type || 1;
  150. this.store_name = options.store_name;
  151. this.storeId = options.store_id;
  152. this.product_id = options.product_id;
  153. uni.setNavigationBarTitle({
  154. title: options.id ? '修改地址' : '添加地址'
  155. })
  156. if (this.isLogin) {
  157. this.getUserAddress();
  158. // this.getCityList();
  159. } else {
  160. //#ifndef MP
  161. toLogin();
  162. //#endif
  163. //#ifdef MP
  164. this.isShowAuth = true;
  165. //#endif
  166. }
  167. },
  168. onShow() {
  169. uni.removeStorageSync('form_type_cart');
  170. },
  171. methods: {
  172. onLoadFun(){
  173. this.getUserAddress();
  174. this.isShowAuth = false;
  175. },
  176. // 授权关闭
  177. authColse: function(e) {
  178. this.isShowAuth = e
  179. },
  180. changeRegion(){
  181. this.display = true;
  182. },
  183. OnChangeAddress(address){
  184. this.latitude = ''
  185. this.longitude = ''
  186. this.isStreet = 0
  187. this.addressInfo = address;
  188. },
  189. // 地址数据
  190. // getCityList: function() {
  191. // let that = this;
  192. // getCityData(0).then(res => {
  193. // this.district = res.data
  194. // })
  195. // },
  196. // 关闭地址弹窗;
  197. changeClose: function() {
  198. this.display = false;
  199. },
  200. getUserAddress: function() {
  201. if (!this.id) return false;
  202. let that = this;
  203. getAddressDetail(this.id).then(res => {
  204. let region = [{label:res.data.province}, {label:res.data.city}, {label:res.data.district}, {label:res.data.street}];
  205. that.$set(that, 'userAddress', res.data);
  206. that.addressInfo = res.data.city_list;
  207. that.latitude = res.data.latitude;
  208. that.longitude = res.data.longitude;
  209. that.city_id = res.data.city_id;
  210. });
  211. },
  212. // 获取选中位置
  213. chooseLocation: function() {
  214. let self = this;
  215. uni.chooseLocation({
  216. success: (res) => {
  217. let latitude, longitude;
  218. latitude = res.latitude.toString();
  219. longitude = res.longitude.toString();
  220. this.latitude = res.latitude
  221. this.longitude = res.longitude
  222. getGeocoder({
  223. lat: latitude,
  224. long: longitude
  225. }).then(res => {
  226. const data = res.data;
  227. getCityList(data.address_component.province+'/'+data.address_component.city+'/'+data.address_component.district+'/'+(!data.address_reference.town ? '' : data.address_reference.town.title)).then(res=>{
  228. self.addressInfo = res.data;
  229. self.userAddress.detail = data.formatted_addresses.recommend;
  230. }).catch(err => {
  231. self.$util.Tips({
  232. title: err
  233. });
  234. });
  235. })
  236. },
  237. fail: (err)=>{
  238. console.log(err)
  239. }
  240. })
  241. },
  242. // 自动定位
  243. selfLocation() {
  244. let self = this
  245. uni.showLoading({
  246. title: '定位中',
  247. mask: true,
  248. });
  249. uni.getLocation({
  250. type: 'gcj02',
  251. success: (res) => {
  252. let latitude, longitude;
  253. latitude = res.latitude.toString();
  254. longitude = res.longitude.toString();
  255. this.latitude = res.latitude
  256. this.longitude = res.longitude
  257. getGeocoder({
  258. lat: latitude,
  259. long: longitude
  260. }).then(res => {
  261. const data = res.data;
  262. getCityList(data.address_component.province+'/'+data.address_component.city+'/'+data.address_component.district+'/'+(!data.address_reference.town ? '' : data.address_reference.town.title)).then(res=>{
  263. self.addressInfo = res.data;
  264. self.userAddress.detail = data.formatted_addresses.recommend;
  265. uni.hideLoading();
  266. })
  267. })
  268. },
  269. fail: (res) => {
  270. uni.hideLoading();
  271. uni.showToast({
  272. title: res,
  273. icon: 'none',
  274. duration: 1000
  275. });
  276. }
  277. });
  278. },
  279. // 导入共享地址(小程序)
  280. getWxAddress: function() {
  281. let that = this;
  282. uni.authorize({
  283. scope: 'scope.address',
  284. success: function(res) {
  285. uni.chooseAddress({
  286. success: function(res) {
  287. getCityList(res.provinceName+'/'+res.cityName+'/'+res.countyName).then(res=>{
  288. that.addressInfo = res.data;
  289. })
  290. that.userAddress.real_name = res.userName;
  291. that.userAddress.phone = res.telNumber;
  292. that.userAddress.detail = res.detailInfo;
  293. that.isStreet = 1;
  294. },
  295. fail: function(res) {
  296. if (res.errMsg == 'chooseAddress:cancel') return that.$util
  297. .Tips({
  298. title: '取消选择'
  299. });
  300. },
  301. })
  302. },
  303. fail: function(res) {
  304. uni.showModal({
  305. title: '您已拒绝导入微信地址权限',
  306. content: '是否进入权限管理,调整授权?',
  307. success(res) {
  308. if (res.confirm) {
  309. uni.openSetting({
  310. success: function(res) {}
  311. });
  312. } else if (res.cancel) {
  313. return that.$util.Tips({
  314. title: '已取消!'
  315. });
  316. }
  317. }
  318. })
  319. },
  320. })
  321. },
  322. // 导入共享地址(微信);
  323. getAddress() {
  324. let that = this;
  325. that.$wechat.openAddress().then(res => {
  326. getCityList(res.provinceName+'/'+res.cityName+'/'+res.countryName).then(res=>{
  327. that.addressInfo = res.data;
  328. })
  329. that.userAddress.real_name = res.userName;
  330. that.userAddress.phone = res.telNumber;
  331. that.userAddress.detail = res.detailInfo;
  332. that.isStreet = 1;
  333. }).catch(err => {
  334. that.$util.Tips({
  335. title: err
  336. });
  337. });
  338. },
  339. /**
  340. * 提交用户添加地址
  341. *
  342. */
  343. formSubmit: function(e) {
  344. let that = this,
  345. value = e.detail.value;
  346. if (!value.real_name) return that.$util.Tips({
  347. title: '请填写收货人姓名'
  348. });
  349. if (!value.phone) return that.$util.Tips({
  350. title: '请填写联系电话'
  351. });
  352. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return that.$util.Tips({
  353. title: '请输入正确的手机号码'
  354. });
  355. if (!that.addressInfo.length) return that.$util.Tips({
  356. title: '请选择所在地区'
  357. });
  358. // if (that.region[0] == '省') return that.$util.Tips({
  359. // title: '请选择所在地区'
  360. // });
  361. if (!value.detail) return that.$util.Tips({
  362. title: '请填写详细地址'
  363. });
  364. // if(!that.longitude && !that.latitude) return that.$util.Tips({title:'请定位你的经纬度'})
  365. value.id = that.id;
  366. let regionArray = that.addressInfo;
  367. value.address = {
  368. province: regionArray[0].label,
  369. city: regionArray[1].label,
  370. district: regionArray[2].label,
  371. street: regionArray[3]?regionArray[3].label:'',
  372. city_id: regionArray[regionArray.length-1].id?regionArray[regionArray.length-1].id:that.city_id,
  373. };
  374. value.is_default = that.userAddress.is_default ? 1 : 0;
  375. // 经度
  376. value.longitude = that.longitude;
  377. // 纬度
  378. value.latitude = that.latitude;
  379. uni.showLoading({
  380. title: '保存中',
  381. mask: true
  382. })
  383. editAddress(value).then(res => {
  384. if (that.id)
  385. that.$util.Tips({
  386. title: '修改成功',
  387. icon: 'success'
  388. });
  389. else
  390. that.$util.Tips({
  391. title: '添加成功',
  392. icon: 'success'
  393. });
  394. setTimeout(function() {
  395. if (that.cartId) {
  396. let cartId = that.cartId;
  397. let pinkId = that.pinkId;
  398. let couponId = that.couponId;
  399. that.cartId = '';
  400. that.pinkId = '';
  401. that.couponId = '';
  402. uni.navigateTo({
  403. url: '/pages/goods/order_confirm/index?new=' + that.news +
  404. '&cartId=' + cartId + '&addressId=' + (that.id ? that.id :
  405. res.data.id) + '&pinkId=' + pinkId + '&couponId=' +
  406. couponId +
  407. '&noCoupon=' + that
  408. .noCoupon +'&delivery_type='+that.deliveryType+'&store_id='+that.storeId+'&store_name='+ that.store_name+'&product_id='+that.product_id
  409. });
  410. } else {
  411. uni.navigateTo({
  412. url:'/pages/users/user_address_list/index'
  413. })
  414. }
  415. }, 1000);
  416. }).catch(err => {
  417. return that.$util.Tips({
  418. title: err
  419. });
  420. })
  421. },
  422. ChangeIsDefault: function(e) {
  423. this.$set(this.userAddress, 'is_default', !this.userAddress.is_default);
  424. },
  425. identify(){
  426. const options = {
  427. type: 0, // 哪种方式解析,0:正则,1:树查找
  428. textFilter: [], // 预清洗的字段
  429. nameMaxLength: 4, // 查找最大的中文名字长度
  430. }
  431. const parseResult = AddressParse(this.addressValue.trim(), options)
  432. // type参数0表示使用正则解析,1表示采用树查找, textFilter地址预清洗过滤字段。
  433. if(this.addressValue.trim()){
  434. getCityList(parseResult.province+'/'+parseResult.city+'/'+parseResult.area).then(res=>{
  435. this.addressInfo = res.data;
  436. this.userAddress.phone = parseResult.phone;
  437. this.userAddress.real_name = parseResult.name;
  438. this.userAddress.detail = parseResult.detail;
  439. }).catch(err=>{
  440. return this.$util.Tips({
  441. title: err
  442. });
  443. })
  444. }
  445. }
  446. }
  447. }
  448. </script>
  449. <style scoped lang="scss">
  450. .color-add {
  451. color:#cdcdcd;
  452. }
  453. .fontcolor{
  454. color: var(--view-theme);
  455. }
  456. .addAddress .list {
  457. background-color: #fff;
  458. }
  459. .addAddress .list .item {
  460. padding: 30rpx;
  461. border-top: 1rpx solid #eee;
  462. position: relative;
  463. }
  464. .addAddress .list .item .detail{
  465. width: 368rpx;
  466. }
  467. .addAddress .list .item .location{
  468. position: absolute;
  469. right: 46rpx;
  470. top: 50%;
  471. margin-top: -40rpx!important;
  472. font-size: 24rpx;
  473. text-align: center;
  474. }
  475. .addAddress .list .item .icon-dizhi{
  476. font-size: 36rpx!important;
  477. }
  478. .addAddress .list .item .name {
  479. width: 195rpx;
  480. font-size: 30rpx;
  481. color: #333;
  482. }
  483. .addAddress .list .item .address {
  484. // width: 412rpx;
  485. flex: 1;
  486. margin-left: 20rpx;
  487. }
  488. .addAddress .list .item .address .addressCon{
  489. width: 360rpx;
  490. }
  491. .addAddress .list .item .address .addressCon .tip{
  492. font-size: 21rpx;
  493. margin-top: 4rpx;
  494. }
  495. .addAddress .list .item input {
  496. // width: 475rpx;
  497. flex: 1;
  498. font-size: 30rpx;
  499. }
  500. .placeholder {
  501. color: #ccc;
  502. }
  503. // .addAddress .list .item {
  504. // width: 475rpx;
  505. // }
  506. .addAddress .list .item .picker {
  507. width: 430rpx;
  508. font-size: 30rpx;
  509. }
  510. .addAddress .list .item .iconfont {
  511. font-size: 30rpx;
  512. margin-top: 4rpx;
  513. }
  514. .addAddress .default {
  515. padding: 0 30rpx;
  516. height: 90rpx;
  517. background-color: #fff;
  518. margin-top: 23rpx;
  519. }
  520. .addAddress .default checkbox {
  521. margin-right: 15rpx;
  522. }
  523. .addAddress .keepBnt {
  524. width: 690rpx;
  525. height: 86rpx;
  526. border-radius: 50rpx;
  527. text-align: center;
  528. line-height: 86rpx;
  529. margin: 50rpx auto;
  530. font-size: 32rpx;
  531. color: #fff;
  532. }
  533. .addAddress .wechatAddress {
  534. width: 690rpx;
  535. height: 86rpx;
  536. border-radius: 50rpx;
  537. text-align: center;
  538. line-height: 86rpx;
  539. margin: 0 auto;
  540. font-size: 32rpx;
  541. color: var(--view-theme);
  542. border: 1px solid var(--view-theme);
  543. }
  544. .mt-22{
  545. margin-top: 22rpx;
  546. }
  547. </style>