index.vue 16 KB

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