index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. this.getUserAddress();
  135. }
  136. },
  137. deep: true
  138. },
  139. addressInfo(val) {
  140. this.isStreet = val.length < 3 ? 0 : 1;
  141. }
  142. },
  143. onLoad(options) {
  144. this.cartId = options.cartId || '';
  145. this.pinkId = options.pinkId || 0;
  146. this.couponId = options.couponId || 0;
  147. this.id = options.id || 0;
  148. this.noCoupon = options.noCoupon || 0;
  149. this.news = options.new || '';
  150. this.deliveryType = options.delivery_type || 1;
  151. this.store_name = options.store_name;
  152. this.storeId = options.store_id;
  153. this.product_id = options.product_id;
  154. this.isCollage = options.isCollage || 0;
  155. uni.setNavigationBarTitle({
  156. title: options.id ? '修改地址' : '添加地址'
  157. })
  158. if (this.isLogin) {
  159. this.getUserAddress();
  160. // this.getCityList();
  161. } else {
  162. toLogin();
  163. }
  164. this.isGroup = options.isGroup;
  165. },
  166. onShow() {
  167. uni.removeStorageSync('form_type_cart');
  168. },
  169. methods: {
  170. onLoadFun(){
  171. this.getUserAddress();
  172. this.isShowAuth = false;
  173. },
  174. // 授权关闭
  175. authColse: function(e) {
  176. this.isShowAuth = e
  177. },
  178. changeRegion(){
  179. this.display = true;
  180. },
  181. OnChangeAddress(address){
  182. this.latitude = ''
  183. this.longitude = ''
  184. this.isStreet = 0
  185. this.addressInfo = address;
  186. },
  187. // 地址数据
  188. // getCityList: function() {
  189. // let that = this;
  190. // getCityData(0).then(res => {
  191. // this.district = res.data
  192. // })
  193. // },
  194. // 关闭地址弹窗;
  195. changeClose: function() {
  196. this.display = false;
  197. },
  198. getUserAddress: function() {
  199. if (!this.id) return false;
  200. let that = this;
  201. getAddressDetail(this.id).then(res => {
  202. let region = [{label:res.data.province}, {label:res.data.city}, {label:res.data.district}, {label:res.data.street}];
  203. that.$set(that, 'userAddress', res.data);
  204. that.addressInfo = res.data.city_list;
  205. that.latitude = res.data.latitude;
  206. that.longitude = res.data.longitude;
  207. that.city_id = res.data.city_id;
  208. });
  209. },
  210. // 获取选中位置
  211. chooseLocation: function() {
  212. let self = this;
  213. uni.chooseLocation({
  214. success: (res) => {
  215. let latitude, longitude;
  216. latitude = res.latitude.toString();
  217. longitude = res.longitude.toString();
  218. this.latitude = res.latitude
  219. this.longitude = res.longitude
  220. getGeocoder({
  221. lat: latitude,
  222. long: longitude
  223. }).then(res => {
  224. const data = res.data;
  225. getCityList(data.address_component.province+'/'+data.address_component.city+'/'+data.address_component.district+'/'+(!data.address_reference.town ? '' : data.address_reference.town.title)).then(res=>{
  226. self.addressInfo = res.data;
  227. self.userAddress.detail = data.formatted_addresses.recommend;
  228. }).catch(err => {
  229. self.$util.Tips({
  230. title: err
  231. });
  232. });
  233. })
  234. },
  235. fail: (err)=>{
  236. console.log(err)
  237. }
  238. })
  239. },
  240. // 自动定位
  241. selfLocation() {
  242. let self = this
  243. uni.showLoading({
  244. title: '定位中',
  245. mask: true,
  246. });
  247. uni.getLocation({
  248. type: 'gcj02',
  249. success: (res) => {
  250. let latitude, longitude;
  251. latitude = res.latitude.toString();
  252. longitude = res.longitude.toString();
  253. this.latitude = res.latitude
  254. this.longitude = res.longitude
  255. getGeocoder({
  256. lat: latitude,
  257. long: longitude
  258. }).then(res => {
  259. const data = res.data;
  260. getCityList(data.address_component.province+'/'+data.address_component.city+'/'+data.address_component.district+'/'+(!data.address_reference.town ? '' : data.address_reference.town.title)).then(res=>{
  261. self.addressInfo = res.data;
  262. self.userAddress.detail = data.formatted_addresses.recommend;
  263. uni.hideLoading();
  264. })
  265. })
  266. },
  267. fail: (res) => {
  268. uni.hideLoading();
  269. uni.showToast({
  270. title: res,
  271. icon: 'none',
  272. duration: 1000
  273. });
  274. }
  275. });
  276. },
  277. // 导入共享地址(小程序)
  278. getWxAddress: function() {
  279. let that = this;
  280. uni.authorize({
  281. scope: 'scope.address',
  282. success: function(res) {
  283. uni.chooseAddress({
  284. success: function(res) {
  285. getCityList(res.provinceName+'/'+res.cityName+'/'+res.countyName).then(res=>{
  286. that.addressInfo = res.data;
  287. })
  288. that.userAddress.real_name = res.userName;
  289. that.userAddress.phone = res.telNumber;
  290. that.userAddress.detail = res.detailInfo;
  291. that.isStreet = 1;
  292. },
  293. fail: function(res) {
  294. if (res.errMsg == 'chooseAddress:cancel') return that.$util
  295. .Tips({
  296. title: '取消选择'
  297. });
  298. },
  299. })
  300. },
  301. fail: function(res) {
  302. uni.showModal({
  303. title: '您已拒绝导入微信地址权限',
  304. content: '是否进入权限管理,调整授权?',
  305. success(res) {
  306. if (res.confirm) {
  307. uni.openSetting({
  308. success: function(res) {}
  309. });
  310. } else if (res.cancel) {
  311. return that.$util.Tips({
  312. title: '已取消!'
  313. });
  314. }
  315. }
  316. })
  317. },
  318. })
  319. },
  320. // 导入共享地址(微信);
  321. getAddress() {
  322. let that = this;
  323. that.$wechat.openAddress().then(res => {
  324. getCityList(res.provinceName+'/'+res.cityName+'/'+res.countryName).then(res=>{
  325. that.addressInfo = res.data;
  326. })
  327. that.userAddress.real_name = res.userName;
  328. that.userAddress.phone = res.telNumber;
  329. that.userAddress.detail = res.detailInfo;
  330. that.isStreet = 1;
  331. }).catch(err => {
  332. that.$util.Tips({
  333. title: err
  334. });
  335. });
  336. },
  337. /**
  338. * 提交用户添加地址
  339. *
  340. */
  341. formSubmit: function(e) {
  342. let that = this,
  343. value = e.detail.value;
  344. if (!value.real_name) return that.$util.Tips({
  345. title: '请填写收货人姓名'
  346. });
  347. if (!value.phone) return that.$util.Tips({
  348. title: '请填写联系电话'
  349. });
  350. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return that.$util.Tips({
  351. title: '请输入正确的手机号码'
  352. });
  353. if (!that.addressInfo.length) return that.$util.Tips({
  354. title: '请选择所在地区'
  355. });
  356. if (that.addressInfo.length < 3) return that.$util.Tips({
  357. title: '请补全所在地区信息'
  358. });
  359. // if (that.region[0] == '省') return that.$util.Tips({
  360. // title: '请选择所在地区'
  361. // });
  362. if (!value.detail) return that.$util.Tips({
  363. title: '请填写详细地址'
  364. });
  365. // if(!that.longitude && !that.latitude) return that.$util.Tips({title:'请定位你的经纬度'})
  366. value.id = that.id;
  367. let regionArray = that.addressInfo;
  368. value.address = {
  369. province: regionArray[0].label,
  370. city: regionArray[1].label,
  371. district: regionArray[2].label,
  372. street: regionArray[3]?regionArray[3].label:'',
  373. city_id: regionArray[regionArray.length-1].id?regionArray[regionArray.length-1].id:that.city_id,
  374. };
  375. value.is_default = that.userAddress.is_default ? 1 : 0;
  376. // 经度
  377. value.longitude = that.longitude;
  378. // 纬度
  379. value.latitude = that.latitude;
  380. uni.showLoading({
  381. title: '保存中',
  382. mask: true
  383. })
  384. editAddress(value).then(res => {
  385. if (that.id)
  386. that.$util.Tips({
  387. title: '修改成功',
  388. icon: 'success'
  389. });
  390. else
  391. that.$util.Tips({
  392. title: '添加成功',
  393. icon: 'success'
  394. });
  395. setTimeout(function() {
  396. if (that.isGroup == 1) {
  397. uni.navigateBack({delta:2})
  398. return;
  399. }
  400. if (that.cartId) {
  401. let cartId = that.cartId;
  402. let pinkId = that.pinkId;
  403. let couponId = that.couponId;
  404. that.cartId = '';
  405. that.pinkId = '';
  406. that.couponId = '';
  407. uni.navigateTo({
  408. url: '/pages/goods/order_confirm/index?new=' + that.news +
  409. '&cartId=' + cartId + '&addressId=' + (that.id ? that.id :
  410. res.data.id) + '&pinkId=' + pinkId + '&couponId=' +
  411. couponId +
  412. '&noCoupon=' + that
  413. .noCoupon +'&delivery_type='+that.deliveryType+'&store_id='+that.storeId+'&store_name='+ that.store_name+'&product_id='+that.product_id
  414. });
  415. } else {
  416. if (that.isCollage) {
  417. uni.navigateTo({
  418. url:'/pages/users/user_address_list/index?isCollage='+that.isCollage+'&store_id='+that.storeId
  419. })
  420. } else{
  421. uni.navigateTo({
  422. url:'/pages/users/user_address_list/index'
  423. })
  424. }
  425. }
  426. }, 1000);
  427. }).catch(err => {
  428. return that.$util.Tips({
  429. title: err
  430. });
  431. })
  432. },
  433. ChangeIsDefault: function(e) {
  434. this.$set(this.userAddress, 'is_default', !this.userAddress.is_default);
  435. },
  436. identify(){
  437. const options = {
  438. type: 0, // 哪种方式解析,0:正则,1:树查找
  439. textFilter: [], // 预清洗的字段
  440. nameMaxLength: 4, // 查找最大的中文名字长度
  441. }
  442. const parseResult = AddressParse(this.addressValue.trim(), options)
  443. // type参数0表示使用正则解析,1表示采用树查找, textFilter地址预清洗过滤字段。
  444. if(this.addressValue.trim()){
  445. getCityList(parseResult.province+'/'+parseResult.city+'/'+parseResult.area).then(res=>{
  446. this.addressInfo = res.data;
  447. this.userAddress.phone = parseResult.phone;
  448. this.userAddress.real_name = parseResult.name;
  449. this.userAddress.detail = parseResult.detail;
  450. }).catch(err=>{
  451. return this.$util.Tips({
  452. title: err
  453. });
  454. })
  455. }
  456. }
  457. }
  458. }
  459. </script>
  460. <style scoped lang="scss">
  461. .color-add {
  462. color:#cdcdcd;
  463. }
  464. .fontcolor{
  465. color: var(--view-theme);
  466. }
  467. .addAddress .list {
  468. background-color: #fff;
  469. }
  470. .addAddress .list .item {
  471. padding: 30rpx;
  472. border-top: 1rpx solid #eee;
  473. position: relative;
  474. }
  475. .addAddress .list .item .detail{
  476. width: 368rpx;
  477. }
  478. .addAddress .list .item .location{
  479. position: absolute;
  480. right: 46rpx;
  481. top: 50%;
  482. margin-top: -40rpx!important;
  483. font-size: 24rpx;
  484. text-align: center;
  485. }
  486. .addAddress .list .item .icon-dizhi{
  487. font-size: 36rpx!important;
  488. }
  489. .addAddress .list .item .name {
  490. width: 195rpx;
  491. font-size: 30rpx;
  492. color: #333;
  493. }
  494. .addAddress .list .item .address {
  495. // width: 412rpx;
  496. flex: 1;
  497. // margin-left: 20rpx;
  498. }
  499. .addAddress .list .item .address .addressCon{
  500. width: 360rpx;
  501. }
  502. .addAddress .list .item .address .addressCon .tip{
  503. font-size: 21rpx;
  504. margin-top: 4rpx;
  505. }
  506. .addAddress .list .item input {
  507. // width: 475rpx;
  508. flex: 1;
  509. font-size: 30rpx;
  510. }
  511. .placeholder {
  512. color: #ccc;
  513. }
  514. // .addAddress .list .item {
  515. // width: 475rpx;
  516. // }
  517. .addAddress .list .item .picker {
  518. width: 430rpx;
  519. font-size: 30rpx;
  520. }
  521. .addAddress .list .item .iconfont {
  522. font-size: 30rpx;
  523. margin-top: 4rpx;
  524. }
  525. .addAddress .default {
  526. padding: 0 30rpx;
  527. height: 90rpx;
  528. background-color: #fff;
  529. margin-top: 23rpx;
  530. }
  531. .addAddress .default checkbox {
  532. margin-right: 15rpx;
  533. }
  534. .addAddress .keepBnt {
  535. width: 690rpx;
  536. height: 86rpx;
  537. border-radius: 50rpx;
  538. text-align: center;
  539. line-height: 86rpx;
  540. margin: 50rpx auto;
  541. font-size: 32rpx;
  542. color: #fff;
  543. }
  544. .addAddress .wechatAddress {
  545. width: 690rpx;
  546. height: 86rpx;
  547. border-radius: 50rpx;
  548. text-align: center;
  549. line-height: 86rpx;
  550. margin: 0 auto;
  551. font-size: 32rpx;
  552. color: var(--view-theme);
  553. border: 1px solid var(--view-theme);
  554. }
  555. .mt-22{
  556. margin-top: 22rpx;
  557. }
  558. </style>