index.vue 15 KB

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