index.vue 16 KB

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