index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. <template>
  2. <view :style="colorStyle">
  3. <form @submit="formSubmit">
  4. <view class='addAddress'>
  5. <view class='list'>
  6. <view class='item acea-row row-between-wrapper'>
  7. <view class='name'>{{$t(`姓名`)}}</view>
  8. <input type='text' :placeholder='$t(`请输入姓名`)' name='real_name' :value="userAddress.real_name"
  9. placeholder-class='placeholder'></input>
  10. </view>
  11. <view class='item acea-row row-between-wrapper'>
  12. <view class='name'>{{$t(`联系电话`)}}</view>
  13. <input type='number' :placeholder='$t(`请输入联系电话`)' name="phone" :value='userAddress.phone'
  14. placeholder-class='placeholder' pattern="\d*"></input>
  15. </view>
  16. <view class='item acea-row row-between-wrapper'>
  17. <view class='name'>{{$t(`所在地区`)}}</view>
  18. <view class="address">
  19. <picker mode="multiSelector" @change="bindRegionChange"
  20. @columnchange="bindMultiPickerColumnChange" :value="valueRegion" :range="multiArray">
  21. <view class='acea-row'>
  22. <view class="picker">{{region[0]}},{{region[1]}},{{region[2]}}</view>
  23. </view>
  24. </picker>
  25. </view>
  26. </view>
  27. <view class='item acea-row row-between-wrapper'>
  28. <view class='name'>{{$t(`详细地址`)}}</view>
  29. <input type='text' :placeholder='$t(`请填写具体地址`)' name='detail' placeholder-class='placeholder'
  30. :value='userAddress.detail'></input>
  31. </view>
  32. </view>
  33. <view class='default acea-row row-middle' @click='ChangeIsDefault'>
  34. <checkbox-group>
  35. <checkbox :checked="userAddress.is_default ? true : false" />{{$t(`设置为默认地址`)}}
  36. </checkbox-group>
  37. </view>
  38. <button class='keepBnt bg-color' form-type="submit">{{$t(`立即保存`)}}</button>
  39. <!-- #ifdef MP -->
  40. <view class="wechatAddress" v-if="!id" @click="getWxAddress">{{$t(`导入微信地址`)}}</view>
  41. <!-- #endif -->
  42. <!-- #ifdef H5 -->
  43. <view class="wechatAddress" v-if="this.$wechat.isWeixin() && !id" @click="getAddress">{{$t(`导入微信地址`)}}
  44. </view>
  45. <!-- #endif -->
  46. </view>
  47. </form>
  48. <!-- #ifdef MP -->
  49. <!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
  50. <!-- #endif -->
  51. <!-- #ifndef MP -->
  52. <home></home>
  53. <!-- #endif -->
  54. </view>
  55. </template>
  56. <script>
  57. import {
  58. editAddress,
  59. getAddressDetail
  60. } from '@/api/user.js';
  61. import {
  62. getCity
  63. } from '@/api/api.js';
  64. import {
  65. toLogin
  66. } from '@/libs/login.js';
  67. import {
  68. mapGetters
  69. } from "vuex";
  70. // #ifdef MP
  71. import authorize from '@/components/Authorize';
  72. // #endif
  73. import home from '@/components/home';
  74. import colors from '@/mixins/color.js';
  75. export default {
  76. components: {
  77. // #ifdef MP
  78. authorize,
  79. // #endif
  80. home,
  81. },
  82. mixins: [colors],
  83. data() {
  84. return {
  85. regionDval: [this.$t(`浙江省`), this.$t(`杭州市`), this.$t(`滨江区`)],
  86. cartId: '', //购物车id
  87. pinkId: 0, //拼团id
  88. couponId: 0, //优惠券id
  89. id: 0, //地址id
  90. userAddress: {
  91. is_default: false
  92. }, //地址详情
  93. region: [this.$t(`省`), this.$t(`市`), this.$t(`区`)],
  94. valueRegion: [0, 0, 0],
  95. isAuto: false, //没有授权的不会自动授权
  96. isShowAuth: false, //是否隐藏授权
  97. district: [],
  98. multiArray: [],
  99. multiIndex: [0, 0, 0],
  100. cityId: 0,
  101. defaultRegion: [this.$t(`广东省`), this.$t(`广州市`), this.$t(`番禺区`)],
  102. defaultRegionCode: '110101',
  103. news: '',
  104. noCoupon: 0,
  105. is_gift: 0,
  106. order_id: 0,
  107. };
  108. },
  109. computed: mapGetters(['isLogin']),
  110. watch: {
  111. isLogin: {
  112. handler: function(newV, oldV) {
  113. if (newV) {
  114. this.getUserAddress();
  115. }
  116. },
  117. deep: true
  118. }
  119. },
  120. onLoad(options) {
  121. if (this.isLogin) {
  122. this.cartId = options.cartId || '';
  123. this.pinkId = options.pinkId || 0;
  124. this.couponId = options.couponId || 0;
  125. this.id = options.id || 0;
  126. this.noCoupon = options.noCoupon || 0;
  127. this.news = options.new || '';
  128. this.is_gift = options.is_gift || '';
  129. this.orderId = options.order_id || '';
  130. uni.setNavigationBarTitle({
  131. title: options.id ? this.$t(`修改地址`) : this.$t(`添加地址`)
  132. })
  133. this.getUserAddress();
  134. this.getCityList();
  135. } else {
  136. toLogin();
  137. }
  138. },
  139. methods: {
  140. // #ifdef APP-PLUS
  141. // 获取选择的地区
  142. handleGetRegion(region) {
  143. this.region = region
  144. },
  145. // #endif
  146. // 回去地址数据
  147. getCityList: function() {
  148. let that = this;
  149. getCity().then(res => {
  150. this.district = res.data
  151. that.initialize();
  152. })
  153. },
  154. initialize() {
  155. let that = this,
  156. province = [],
  157. city = [],
  158. area = [];
  159. let cityChildren = that.district[0].c || [];
  160. let areaChildren = cityChildren.length ? (cityChildren[0].c || []) : [];
  161. that.district.forEach((item, i) => {
  162. province.push(item.n);
  163. if (item.n === this.region[0]) {
  164. this.valueRegion[0] = i
  165. this.multiIndex[0] = i
  166. }
  167. });
  168. that.district[this.valueRegion[0]].c.forEach((item, i) => {
  169. if (this.region[1] == item.c) {
  170. this.valueRegion[1] = i
  171. this.multiIndex[1] = i
  172. }
  173. city.push(item.n);
  174. });
  175. that.district[this.valueRegion[0]].c[this.valueRegion[1]].c.forEach((item, i) => {
  176. if (this.region[2] == item.c) {
  177. this.valueRegion[2] = i
  178. this.multiIndex[2] = i
  179. }
  180. area.push(item.n);
  181. });
  182. this.multiArray = [province, city, area]
  183. },
  184. bindRegionChange: function(e) {
  185. let multiIndex = this.multiIndex,
  186. province = this.district[multiIndex[0]] || {
  187. c: []
  188. },
  189. city = province.c[multiIndex[1]] || {
  190. v: 0
  191. },
  192. multiArray = this.multiArray,
  193. value = e.detail.value;
  194. this.region = [multiArray[0][value[0]], multiArray[1][value[1]], multiArray[2][value[2]]]
  195. // this.$set(this.region,0,multiArray[0][value[0]]);
  196. // this.$set(this.region,1,multiArray[1][value[1]]);
  197. // this.$set(this.region,2,multiArray[2][value[2]]);
  198. this.cityId = city.v
  199. this.valueRegion = [0, 0, 0]
  200. this.initialize();
  201. },
  202. bindMultiPickerColumnChange: function(e) {
  203. let that = this,
  204. column = e.detail.column,
  205. value = e.detail.value,
  206. currentCity = this.district[value] || {
  207. c: []
  208. },
  209. multiArray = that.multiArray,
  210. multiIndex = that.multiIndex;
  211. multiIndex[column] = value;
  212. switch (column) {
  213. case 0:
  214. let areaList = currentCity.c[0] || {
  215. c: []
  216. };
  217. multiArray[1] = currentCity.c.map((item) => {
  218. return item.n;
  219. });
  220. multiArray[2] = areaList.c.map((item) => {
  221. return item.n;
  222. });
  223. break;
  224. case 1:
  225. let cityList = that.district[multiIndex[0]].c[multiIndex[1]].c || [];
  226. multiArray[2] = cityList.map((item) => {
  227. return item.n;
  228. });
  229. break;
  230. case 2:
  231. break;
  232. }
  233. // #ifdef MP || APP-PLUS
  234. this.$set(this.multiArray, 0, multiArray[0]);
  235. this.$set(this.multiArray, 1, multiArray[1]);
  236. this.$set(this.multiArray, 2, multiArray[2]);
  237. // #endif
  238. // #ifdef H5
  239. this.multiArray = multiArray;
  240. // #endif
  241. this.multiIndex = multiIndex
  242. // this.setData({ multiArray: multiArray, multiIndex: multiIndex});
  243. },
  244. // 授权回调
  245. onLoadFun: function() {
  246. this.getUserAddress();
  247. },
  248. // 授权关闭
  249. authColse: function(e) {
  250. this.isShowAuth = e
  251. },
  252. toggleTab(str) {
  253. this.$refs[str].show();
  254. },
  255. // bindRegionChange: function(e) {
  256. // this.$set(this, 'region', e.detail.value);
  257. // },
  258. onConfirm(val) {
  259. this.region = val.checkArr[0] + '-' + val.checkArr[1] + '-' + val.checkArr[2];
  260. },
  261. getUserAddress: function() {
  262. if (!this.id) return false;
  263. let that = this;
  264. getAddressDetail(this.id).then(res => {
  265. // let region = [res.data.province, res.data.city, res.data.district];
  266. let region = [res.data.province, res.data.city, res.data.district];
  267. that.$set(that, 'userAddress', res.data);
  268. that.$set(that, 'region', region);
  269. that.cityId = res.data.city_id
  270. });
  271. },
  272. // 导入共享地址(小程序)
  273. getWxAddress: function() {
  274. let that = this;
  275. uni.authorize({
  276. scope: 'scope.address',
  277. success: function(res) {
  278. uni.chooseAddress({
  279. success: function(res) {
  280. let addressP = {};
  281. addressP.province = res.provinceName;
  282. addressP.city = res.cityName;
  283. addressP.district = res.countyName;
  284. editAddress({
  285. address: addressP,
  286. is_default: 1,
  287. real_name: res.userName,
  288. post_code: res.postalCode,
  289. phone: res.telNumber,
  290. detail: res.detailInfo,
  291. id: 0,
  292. type: 1,
  293. }).then(res => {
  294. setTimeout(function() {
  295. if (that.cartId) {
  296. let cartId = that.cartId;
  297. let pinkId = that.pinkId;
  298. let couponId = that.couponId;
  299. that.cartId = '';
  300. that.pinkId = '';
  301. that.couponId = '';
  302. uni.navigateTo({
  303. url: '/pages/goods/order_confirm/index?cartId=' +
  304. cartId +
  305. '&addressId=' + (
  306. that.id ? that
  307. .id :
  308. res.data
  309. .id) +
  310. '&pinkId=' +
  311. pinkId +
  312. '&couponId=' +
  313. couponId +
  314. '&new=' + that
  315. .news +
  316. '&noCoupon=' + that
  317. .noCoupon + '&is_gift=' + that.is_gift
  318. +
  319. '&order_id=' + that.orderId
  320. });
  321. } else {
  322. uni.navigateBack({
  323. delta: 1
  324. });
  325. }
  326. }, 1000);
  327. return that.$util.Tips({
  328. title: that.$t(`添加成功`),
  329. icon: 'success'
  330. });
  331. }).catch(err => {
  332. return that.$util.Tips({
  333. title: err
  334. });
  335. });
  336. },
  337. fail: function(res) {
  338. if (res.errMsg == 'chooseAddress:cancel') return that.$util
  339. .Tips({
  340. title: that.$t(`取消选择`)
  341. });
  342. },
  343. })
  344. },
  345. fail: function(res) {
  346. uni.showModal({
  347. title: that.$t(`您已拒绝导入微信地址权限`),
  348. content: that.$t(`是否进入权限管理,调整授权?`),
  349. success(res) {
  350. if (res.confirm) {
  351. uni.openSetting({
  352. success: function(res) {}
  353. });
  354. } else if (res.cancel) {
  355. return that.$util.Tips({
  356. title: that.$t(`已取消!`)
  357. });
  358. }
  359. }
  360. })
  361. },
  362. })
  363. },
  364. // 导入共享地址(微信);
  365. getAddress() {
  366. let that = this;
  367. that.$wechat.openAddress().then(userInfo => {
  368. editAddress({
  369. id: this.id,
  370. real_name: userInfo.userName,
  371. phone: userInfo.telNumber,
  372. address: {
  373. province: userInfo.provinceName,
  374. city: userInfo.cityName,
  375. district: userInfo.countryName
  376. },
  377. detail: userInfo.detailInfo,
  378. is_default: 1,
  379. post_code: userInfo.postalCode,
  380. type: 1,
  381. })
  382. .then((res) => {
  383. // close();
  384. that.$util.Tips({
  385. title: that.$t(`添加成功`),
  386. icon: 'success'
  387. }, () => {
  388. if (that.cartId) {
  389. let cartId = that.cartId;
  390. let pinkId = that.pinkId;
  391. let couponId = that.couponId;
  392. that.cartId = '';
  393. that.pinkId = '';
  394. that.couponId = '';
  395. uni.navigateTo({
  396. url: '/pages/goods/order_confirm/index?cartId=' +
  397. cartId + '&addressId=' + (that.id ? that.id :
  398. res.data
  399. .id) + '&pinkId=' + pinkId +
  400. '&couponId=' +
  401. couponId + '&new=' + that.news + '&is_gift=' + that.is_gift +
  402. '&order_id=' + that.orderId
  403. });
  404. } else {
  405. uni.navigateTo({
  406. url: '/pages/users/user_address_list/index'
  407. })
  408. // history.back();
  409. }
  410. });
  411. })
  412. .catch(err => {
  413. // close();
  414. return that.$util.Tips({
  415. title: err || that.$t(`添加失败`)
  416. });
  417. });
  418. }).catch(err => {});
  419. },
  420. /**
  421. * 提交用户添加地址
  422. *
  423. */
  424. formSubmit: function(e) {
  425. let that = this,
  426. value = e.detail.value;
  427. if (!value.real_name.trim()) return that.$util.Tips({
  428. title: that.$t(`请填写收货人姓名`)
  429. });
  430. if (!value.phone) return that.$util.Tips({
  431. title: that.$t(`请填写联系电话`)
  432. });
  433. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return that.$util.Tips({
  434. title: that.$t(`请输入正确的手机号码`)
  435. });
  436. if (that.region[0] == that.$t(`省`)) return that.$util.Tips({
  437. title: that.$t(`请选择所在地区`)
  438. });
  439. if (!value.detail.trim()) return that.$util.Tips({
  440. title: that.$t(`请填写详细地址`)
  441. });
  442. value.id = that.id;
  443. let regionArray = that.region;
  444. value.address = {
  445. province: regionArray[0],
  446. city: regionArray[1],
  447. district: regionArray[2],
  448. city_id: that.cityId,
  449. };
  450. value.is_default = that.userAddress.is_default ? 1 : 0;
  451. uni.showLoading({
  452. title: that.$t(`保存中`),
  453. mask: true
  454. })
  455. editAddress(value).then(res => {
  456. if (that.id)
  457. that.$util.Tips({
  458. title: that.$t(`修改成功`),
  459. icon: 'success'
  460. });
  461. else
  462. that.$util.Tips({
  463. title: that.$t(`添加成功`),
  464. icon: 'success'
  465. });
  466. setTimeout(function() {
  467. if (that.cartId) {
  468. let cartId = that.cartId;
  469. let pinkId = that.pinkId;
  470. let couponId = that.couponId;
  471. that.cartId = '';
  472. that.pinkId = '';
  473. that.couponId = '';
  474. uni.navigateTo({
  475. url: '/pages/goods/order_confirm/index?new=' + that.news +
  476. '&cartId=' + cartId + '&addressId=' + (that.id ? that.id :
  477. res.data.id) + '&pinkId=' + pinkId + '&couponId=' +
  478. couponId +
  479. '&noCoupon=' + that
  480. .noCoupon + '&is_gift=' + that.is_gift +
  481. '&order_id=' + that.orderId
  482. });
  483. } else {
  484. // #ifdef H5
  485. return history.back();
  486. // #endif
  487. // #ifndef H5
  488. return uni.navigateBack({
  489. delta: 1,
  490. })
  491. // #endif
  492. }
  493. }, 1000);
  494. }).catch(err => {
  495. return that.$util.Tips({
  496. title: err
  497. });
  498. })
  499. },
  500. ChangeIsDefault: function(e) {
  501. this.$set(this.userAddress, 'is_default', !this.userAddress.is_default);
  502. }
  503. }
  504. }
  505. </script>
  506. <style scoped lang="scss">
  507. .fontcolor {
  508. color: var(--view-theme);
  509. }
  510. .addAddress .list {
  511. background-color: #fff;
  512. }
  513. .addAddress .list .item {
  514. padding: 30rpx;
  515. border-top: 1rpx solid #eee;
  516. }
  517. .addAddress .list .item .name {
  518. width: 195rpx;
  519. font-size: 30rpx;
  520. color: #333;
  521. }
  522. .addAddress .list .item .address {
  523. // width: 412rpx;
  524. flex: 1;
  525. margin-left: 20rpx;
  526. }
  527. .addAddress .list .item input {
  528. width: 475rpx;
  529. font-size: 30rpx;
  530. }
  531. .addAddress .list .item .placeholder {
  532. color: #ccc;
  533. }
  534. .addAddress .list .item picker {
  535. width: 475rpx;
  536. }
  537. .addAddress .list .item picker .picker {
  538. width: 410rpx;
  539. font-size: 30rpx;
  540. }
  541. .addAddress .list .item picker .iconfont {
  542. font-size: 43rpx;
  543. }
  544. .addAddress .default {
  545. padding: 0 30rpx;
  546. height: 90rpx;
  547. background-color: #fff;
  548. margin-top: 23rpx;
  549. }
  550. .addAddress .default checkbox {
  551. margin-right: 15rpx;
  552. }
  553. .addAddress .keepBnt {
  554. width: 690rpx;
  555. height: 86rpx;
  556. border-radius: 50rpx;
  557. text-align: center;
  558. line-height: 86rpx;
  559. margin: 50rpx auto;
  560. font-size: 32rpx;
  561. color: #fff;
  562. }
  563. .addAddress .wechatAddress {
  564. width: 690rpx;
  565. height: 86rpx;
  566. border-radius: 50rpx;
  567. text-align: center;
  568. line-height: 86rpx;
  569. margin: 0 auto;
  570. font-size: 32rpx;
  571. color: var(--view-theme);
  572. border: 1px solid var(--view-theme);
  573. }
  574. </style>