index.vue 15 KB

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