index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <template>
  2. <view :style="colorStyle">
  3. <view class='address-management' :class='addressList.length < 1 && page > 1 ? "fff" : ""'>
  4. <view class='line'>
  5. <image src='../../../static/images/line.jpg' v-if="addressList.length"></image>
  6. </view>
  7. <radio-group class="radio-group" @change="radioChange" v-if="addressList.length">
  8. <view class='item' v-for="(item, index) in addressList" :key="index">
  9. <view class='address' @click='goOrder(item.id)'>
  10. <view class='consignee'>{{ $t(`收货人`) }}:{{ item.realName }}<text class='phone'>{{ item.phone }}
  11. </text></view>
  12. <view>{{ $t(`收货地址`) }}:{{ item.province }}{{ item.city }}{{ item.district }}{{ item.detail }}</view>
  13. </view>
  14. <view class='operation acea-row row-between-wrapper'>
  15. <!-- #ifdef MP -->
  16. <radio class="radio" :value="index" :checked="item.defaultState == '2' ? true : false">
  17. <text>{{ $t(typeTitle) }}</text>
  18. </radio>
  19. <!-- #endif -->
  20. <view class='acea-row row-middle'>
  21. <view @click='editAddress(item.id)'><text class='iconfont icon-bianji'></text>{{ $t(`编辑`) }}
  22. </view>
  23. <view @click='delAddress(index)'><text class='iconfont icon-shanchu'></text>{{ $t(`删除`) }}
  24. </view>
  25. </view>
  26. </view>
  27. </view>
  28. </radio-group>
  29. <view class='loadingicon acea-row row-center-wrapper' v-if="addressList.length">
  30. <text class='loading iconfont icon-jiazai' :hidden='loading == false'></text>{{ loadTitle }}
  31. </view>
  32. <view class='noCommodity' v-if="addressList.length < 1 && page > 1">
  33. <view class='pictrue'>
  34. <image :src="imgHost + '/statics/images/noAddress.png'"></image>
  35. </view>
  36. </view>
  37. <view style='height:120rpx;'></view>
  38. <view class='footer acea-row row-between-wrapper'>
  39. <!-- #ifdef APP-PLUS -->
  40. <view class='addressBnt on' @click='addAddress'><text
  41. class='iconfont icon-tianjiadizhi'></text>{{ $t(`添加新地址`) }}</view>
  42. <!-- #endif -->
  43. <!-- #ifdef MP-->
  44. <view class='addressBnt on' @click='addAddress'><text
  45. class='iconfont icon-tianjiadizhi'></text>{{ $t(`添加新地址`) }}</view>
  46. <!-- <view class='addressBnt' @click='getWxAddress'><text class='iconfont icon-weixin2'></text>{{$t(`导入微信地址`)}} -->
  47. </view>
  48. <!-- #endif -->
  49. <!-- #ifdef H5-->
  50. <view class='addressBnt' :class="this.$wechat.isWeixin() ? 'wxbnt' : 'on'" @click='addAddress'><text
  51. class='iconfont icon-tianjiadizhi'></text>{{ $t(`添加新地址`) }}</view>
  52. <view class=""></view>
  53. <view class='addressBnt' @click='getAddress' v-if="this.$wechat.isWeixin()"><text
  54. class='iconfont icon-weixin2'></text>{{ $t(`导入微信地址`) }}</view>
  55. <!-- #endif -->
  56. </view>
  57. </view>
  58. <!-- #ifdef MP -->
  59. <!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
  60. <!-- #endif -->
  61. <!-- #ifndef MP -->
  62. <home></home>
  63. <!-- #endif -->
  64. </view>
  65. </template>
  66. <script>
  67. import {
  68. getUserAddressPage,
  69. postUserAddressUpdate,
  70. postUserAddressRemove
  71. } from '@/api/home.js';
  72. import {
  73. getAddressList,
  74. setAddressDefault,
  75. delAddress,
  76. editAddress,
  77. postAddress
  78. } from '@/api/user.js';
  79. import {
  80. toLogin
  81. } from '@/libs/login.js';
  82. import {
  83. mapGetters
  84. } from "vuex";
  85. // #ifdef MP
  86. import authorize from '@/components/Authorize';
  87. // #endif
  88. import home from '@/components/home';
  89. import colors from '@/mixins/color.js';
  90. import { HTTP_REQUEST_URL } from '@/config/app';
  91. export default {
  92. components: {
  93. // #ifdef MP
  94. authorize,
  95. // #endif
  96. home
  97. },
  98. mixins: [colors],
  99. data() {
  100. return {
  101. type: 0,
  102. imgHost: HTTP_REQUEST_URL,
  103. addressList: [],
  104. cartId: '',
  105. pinkId: 0,
  106. couponId: 0,
  107. loading: false,
  108. loadend: false,
  109. loadTitle: this.$t(`加载更多`),
  110. page: 1,
  111. typeTitle: `默认地址`,
  112. limit: 20,
  113. isAuto: false, //没有授权的不会自动授权
  114. isShowAuth: false, //是否隐藏授权
  115. news: '',
  116. noCoupon: 0
  117. };
  118. },
  119. computed: mapGetters(['isLogin',"selectAddr"]),
  120. onLoad(options) {
  121. if (options.type) {
  122. this.type = options.type
  123. this.typeTitle = '选择地址'
  124. uni.setNavigationBarTitle({
  125. title: "选择地址"
  126. });
  127. }
  128. if (this.isLogin) {
  129. this.cartId = options.cartId || '';
  130. this.noCoupon = options.noCoupon || 0;
  131. this.pinkId = options.pinkId || 0;
  132. this.couponId = options.couponId || 0;
  133. this.news = options.news || 0;
  134. this.getAddressList(true);
  135. } else {
  136. toLogin();
  137. }
  138. },
  139. onShow: function () {
  140. let that = this;
  141. that.getAddressList(true);
  142. },
  143. methods: {
  144. onLoadFun: function () {
  145. this.getAddressList();
  146. },
  147. // 授权关闭
  148. authColse: function (e) {
  149. this.isShowAuth = e
  150. },
  151. /*
  152. * 导入微信地址(小程序)
  153. */
  154. getWxAddress: function () {
  155. let that = this;
  156. uni.authorize({
  157. scope: 'scope.address',
  158. success: function (res) {
  159. uni.chooseAddress({
  160. success: function (res) {
  161. let addressP = {};
  162. addressP.province = res.provinceName;
  163. addressP.city = res.cityName;
  164. addressP.district = res.countyName;
  165. editAddress({
  166. address: addressP,
  167. is_default: 1,
  168. realName: res.userName,
  169. post_code: res.postalCode,
  170. phone: res.telNumber,
  171. detail: res.detailInfo,
  172. id: 0,
  173. type: 1
  174. }).then(res => {
  175. that.$util.Tips({
  176. title: that.$t(`添加成功`),
  177. icon: 'success'
  178. }, function () {
  179. that.getAddressList(true);
  180. });
  181. }).catch(err => {
  182. return that.$util.Tips({
  183. title: err
  184. });
  185. });
  186. },
  187. fail: function (res) {
  188. if (res.errMsg == 'chooseAddress:cancel') return that.$util
  189. .Tips({
  190. title: that.$t(`取消选择`)
  191. });
  192. },
  193. })
  194. },
  195. fail: function (res) {
  196. uni.showModal({
  197. title: that.$t(`您已拒绝导入微信地址权限`),
  198. content: that.$t(`是否进入权限管理,调整授权?`),
  199. success(res) {
  200. if (res.confirm) {
  201. uni.openSetting({
  202. success: function (res) { }
  203. });
  204. } else if (res.cancel) {
  205. return that.$util.Tips({
  206. title: that.$t(`已取消!`)
  207. });
  208. }
  209. }
  210. })
  211. }
  212. })
  213. },
  214. /*
  215. * 导入微信地址(公众号)
  216. */
  217. getAddress() {
  218. let that = this;
  219. that.$wechat.openAddress().then(userInfo => {
  220. // open();
  221. editAddress({
  222. realName: userInfo.userName,
  223. phone: userInfo.telNumber,
  224. address: {
  225. province: userInfo.provinceName,
  226. city: userInfo.cityName,
  227. district: userInfo.countryName
  228. },
  229. detail: userInfo.detailInfo,
  230. post_code: userInfo.postalCode,
  231. is_default: 1,
  232. type: 1
  233. })
  234. .then(() => {
  235. that.$util.Tips({
  236. title: that.$t(`添加成功`),
  237. icon: 'success'
  238. }, function () {
  239. // close();
  240. that.getAddressList(true);
  241. });
  242. })
  243. .catch(err => {
  244. // close();
  245. return that.$util.Tips({
  246. title: err || that.$t(`添加失败`)
  247. });
  248. });
  249. });
  250. },
  251. /**
  252. * 获取地址列表
  253. *
  254. */
  255. getAddressList: function (isPage) {
  256. let that = this;
  257. if (isPage) {
  258. that.loadend = false;
  259. that.page = 1;
  260. that.$set(that, 'addressList', []);
  261. };
  262. if (that.loading) return;
  263. if (that.loadend) return;
  264. that.loading = true;
  265. that.loadTitle = '';
  266. getUserAddressPage({
  267. userId: this.$store.state.app.uid,
  268. current: that.page,
  269. size: that.limit
  270. }).then(res => {
  271. let list = res.data;
  272. let loadend = list.length < that.limit;
  273. that.addressList = that.$util.SplitArray(list, that.addressList);// 判断是否选择地址
  274. if(this.type && that.addressList.length == 0){
  275. this.$store.commit('SETSELECTADDR', {});
  276. }
  277. if(this.type && that.addressList.length == 1){
  278. that.addressList[0].defaultState = 2
  279. this.$store.commit('SETSELECTADDR', that.addressList[0]);
  280. }
  281. if (that.type && that.selectAddr.id != undefined) {
  282. //找出已经的地址
  283. that.addressList.forEach((value,index) => {
  284. if(that.selectAddr.id == value.id) {
  285. that.addressList[index].defaultState = 2
  286. }else{
  287. that.addressList[index].defaultState = 1
  288. }
  289. })
  290. }
  291. that.$set(that, 'addressList', that.addressList);
  292. that.loadend = loadend;
  293. that.loadTitle = loadend ? that.$t(`我也是有底线的`) : that.$t(`加载更多`);
  294. that.page = that.page + 1;
  295. that.loading = false;
  296. }).catch(err => {
  297. that.loading = false;
  298. that.loadTitle = that.$t(`加载更多`);
  299. });
  300. },
  301. /**
  302. * 设置默认地址
  303. */
  304. radioChange: function (e) {
  305. let index = parseInt(e.detail.value),
  306. that = this;
  307. let address = this.addressList[index];
  308. if (address == undefined) return that.$util.Tips({
  309. title: that.$t(`您设置的默认地址不存在!`)
  310. });
  311. // 判断是否选择地址
  312. if (this.type) {
  313. this.$store.commit('SETSELECTADDR', address);
  314. }else {
  315. address.defaultState = 2
  316. postUserAddressUpdate(address).then(res => {
  317. for (let i = 0, len = that.addressList.length; i < len; i++) {
  318. if (i == index) that.addressList[i].is_default = true;
  319. else that.addressList[i].is_default = false;
  320. }
  321. that.$util.Tips({
  322. title: that.$t(`设置成功`),
  323. icon: 'success'
  324. }, function () {
  325. that.$set(that, 'addressList', that.addressList);
  326. });
  327. }).catch(err => {
  328. return that.$util.Tips({
  329. title: err
  330. });
  331. });
  332. }
  333. },
  334. /**
  335. * 编辑地址
  336. */
  337. editAddress: function (id) {
  338. let cartId = this.cartId,
  339. pinkId = this.pinkId,
  340. couponId = this.couponId;
  341. this.cartId = '';
  342. this.pinkId = '';
  343. this.couponId = '';
  344. uni.navigateTo({
  345. url: '/pages/users/user_address/index?id=' + id + '&cartId=' + cartId + '&pinkId=' +
  346. pinkId + '&couponId=' +
  347. couponId + '&new=' + this.news
  348. })
  349. },
  350. /**
  351. * 删除地址
  352. */
  353. delAddress: function (index) {
  354. let that = this,
  355. address = this.addressList[index];
  356. if (address == undefined) return that.$util.Tips({
  357. title: that.$t(`您删除的地址不存在!`)
  358. });
  359. that.$set(that, 'addressList', that.addressList);
  360. postUserAddressRemove({ ids: address.id }).then(res => {
  361. that.$util.Tips({
  362. title: that.$t(`删除成功`),
  363. icon: 'success'
  364. }, function () {
  365. that.addressList.splice(index, 1);
  366. if(that.type && address.id == that.selectAddr.id){
  367. that.$store.commit('SETSELECTADDR', {});
  368. }
  369. that.$set(that, 'addressList', that.addressList);
  370. });
  371. }).catch(err => {
  372. return that.$util.Tips({
  373. title: err
  374. });
  375. });
  376. },
  377. /**
  378. * 新增地址
  379. */
  380. addAddress: function () {
  381. let cartId = this.cartId,
  382. pinkId = this.pinkId,
  383. couponId = this.couponId;
  384. this.cartId = '';
  385. this.pinkId = '';
  386. this.couponId = '';
  387. uni.navigateTo({
  388. url: '/pages/users/user_address/index?cartId=' + cartId + '&pinkId=' + pinkId +
  389. '&couponId=' + couponId + '&new=' + this.news
  390. })
  391. },
  392. goOrder: function (id) {
  393. let cartId = '';
  394. let pinkId = '';
  395. let couponId = '';
  396. if (this.cartId && id) {
  397. cartId = this.cartId;
  398. pinkId = this.pinkId;
  399. couponId = this.couponId;
  400. this.cartId = '';
  401. this.pinkId = '';
  402. this.couponId = '';
  403. uni.redirectTo({
  404. url: '/pages/goods/order_confirm/index?is_address=1&new=' + this.news + '&cartId=' +
  405. cartId + '&addressId=' + id + '&pinkId=' +
  406. pinkId + '&couponId=' + couponId + '&noCoupon=' + this.noCoupon
  407. })
  408. }
  409. }
  410. },
  411. onReachBottom: function () {
  412. this.getAddressList();
  413. }
  414. }
  415. </script>
  416. <style>
  417. .address-management.fff {
  418. background-color: #fff;
  419. height: 1300rpx
  420. }
  421. .address-management .line {
  422. width: 100%;
  423. height: 3rpx;
  424. }
  425. .address-management .line image {
  426. width: 100%;
  427. height: 100%;
  428. display: block;
  429. }
  430. .address-management .item {
  431. background-color: #fff;
  432. padding: 0 30rpx;
  433. margin-bottom: 12rpx;
  434. }
  435. .address-management .item .address {
  436. padding: 30rpx 0;
  437. border-bottom: 1rpx solid #eee;
  438. font-size: 28rpx;
  439. color: #282828;
  440. }
  441. .address-management .item .address .consignee {
  442. font-size: 28rpx;
  443. font-weight: bold;
  444. margin-bottom: 8rpx;
  445. }
  446. .address-management .item .address .consignee .phone {
  447. margin-left: 25rpx;
  448. }
  449. .address-management .item .operation {
  450. height: 83rpx;
  451. font-size: 28rpx;
  452. color: #282828;
  453. }
  454. .address-management .item .operation .radio text {
  455. margin-left: 13rpx;
  456. }
  457. .address-management .item .operation .iconfont {
  458. color: #2c2c2c;
  459. font-size: 35rpx;
  460. vertical-align: -2rpx;
  461. margin-right: 10rpx;
  462. }
  463. .address-management .item .operation .iconfont.icon-shanchu {
  464. margin-left: 40rpx;
  465. font-size: 38rpx;
  466. }
  467. .address-management .footer {
  468. position: fixed;
  469. width: 100%;
  470. background-color: #fff;
  471. bottom: 0;
  472. height: 106rpx;
  473. padding: 0 30rpx;
  474. box-sizing: border-box;
  475. }
  476. .address-management .footer .addressBnt {
  477. width: 330rpx;
  478. height: 76rpx;
  479. border-radius: 50rpx;
  480. text-align: center;
  481. line-height: 76rpx;
  482. font-size: 30rpx;
  483. color: #fff;
  484. background-color: var(--view-theme);
  485. }
  486. .address-management .footer .addressBnt.on {
  487. width: 690rpx;
  488. margin: 0 auto;
  489. }
  490. .address-management .footer .addressBnt .iconfont {
  491. font-size: 35rpx;
  492. margin-right: 8rpx;
  493. vertical-align: -1rpx;
  494. }
  495. .address-management .footer .addressBnt.wxbnt {
  496. background-color: #FE960F;
  497. }
  498. </style>