You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

615 lines
16 KiB

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