index.vue 15 KB

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