index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 (that.type && that.selectAddr.id != undefined) {
  275. //找出已经的地址
  276. that.addressList.forEach((value,index) => {
  277. if(that.selectAddr.id == value.id) {
  278. that.addressList[index].defaultState = '2'
  279. }else{
  280. that.addressList[index].defaultState = '1'
  281. }
  282. })
  283. }
  284. that.$set(that, 'addressList', that.addressList);
  285. that.loadend = loadend;
  286. that.loadTitle = loadend ? that.$t(`我也是有底线的`) : that.$t(`加载更多`);
  287. that.page = that.page + 1;
  288. that.loading = false;
  289. }).catch(err => {
  290. that.loading = false;
  291. that.loadTitle = that.$t(`加载更多`);
  292. });
  293. },
  294. /**
  295. * 设置默认地址
  296. */
  297. radioChange: function (e) {
  298. let index = parseInt(e.detail.value),
  299. that = this;
  300. let address = this.addressList[index];
  301. if (address == undefined) return that.$util.Tips({
  302. title: that.$t(`您设置的默认地址不存在!`)
  303. });
  304. // 判断是否选择地址
  305. if (this.type) {
  306. this.$store.commit('SETSELECTADDR', address);
  307. }else {
  308. address.defaultState = 2
  309. postUserAddressUpdate(address).then(res => {
  310. for (let i = 0, len = that.addressList.length; i < len; i++) {
  311. if (i == index) that.addressList[i].is_default = true;
  312. else that.addressList[i].is_default = false;
  313. }
  314. that.$util.Tips({
  315. title: that.$t(`设置成功`),
  316. icon: 'success'
  317. }, function () {
  318. that.$set(that, 'addressList', that.addressList);
  319. });
  320. }).catch(err => {
  321. return that.$util.Tips({
  322. title: err
  323. });
  324. });
  325. }
  326. },
  327. /**
  328. * 编辑地址
  329. */
  330. editAddress: function (id) {
  331. let cartId = this.cartId,
  332. pinkId = this.pinkId,
  333. couponId = this.couponId;
  334. this.cartId = '';
  335. this.pinkId = '';
  336. this.couponId = '';
  337. uni.navigateTo({
  338. url: '/pages/users/user_address/index?id=' + id + '&cartId=' + cartId + '&pinkId=' +
  339. pinkId + '&couponId=' +
  340. couponId + '&new=' + this.news
  341. })
  342. },
  343. /**
  344. * 删除地址
  345. */
  346. delAddress: function (index) {
  347. let that = this,
  348. address = this.addressList[index];
  349. if (address == undefined) return that.$util.Tips({
  350. title: that.$t(`您删除的地址不存在!`)
  351. });
  352. postUserAddressRemove({ ids: address.id }).then(res => {
  353. that.$util.Tips({
  354. title: that.$t(`删除成功`),
  355. icon: 'success'
  356. }, function () {
  357. that.addressList.splice(index, 1);
  358. that.$set(that, 'addressList', that.addressList);
  359. });
  360. }).catch(err => {
  361. return that.$util.Tips({
  362. title: err
  363. });
  364. });
  365. },
  366. /**
  367. * 新增地址
  368. */
  369. addAddress: function () {
  370. let cartId = this.cartId,
  371. pinkId = this.pinkId,
  372. couponId = this.couponId;
  373. this.cartId = '';
  374. this.pinkId = '';
  375. this.couponId = '';
  376. uni.navigateTo({
  377. url: '/pages/users/user_address/index?cartId=' + cartId + '&pinkId=' + pinkId +
  378. '&couponId=' + couponId + '&new=' + this.news
  379. })
  380. },
  381. goOrder: function (id) {
  382. let cartId = '';
  383. let pinkId = '';
  384. let couponId = '';
  385. if (this.cartId && id) {
  386. cartId = this.cartId;
  387. pinkId = this.pinkId;
  388. couponId = this.couponId;
  389. this.cartId = '';
  390. this.pinkId = '';
  391. this.couponId = '';
  392. uni.redirectTo({
  393. url: '/pages/goods/order_confirm/index?is_address=1&new=' + this.news + '&cartId=' +
  394. cartId + '&addressId=' + id + '&pinkId=' +
  395. pinkId + '&couponId=' + couponId + '&noCoupon=' + this.noCoupon
  396. })
  397. }
  398. }
  399. },
  400. onReachBottom: function () {
  401. this.getAddressList();
  402. }
  403. }
  404. </script>
  405. <style>
  406. .address-management.fff {
  407. background-color: #fff;
  408. height: 1300rpx
  409. }
  410. .address-management .line {
  411. width: 100%;
  412. height: 3rpx;
  413. }
  414. .address-management .line image {
  415. width: 100%;
  416. height: 100%;
  417. display: block;
  418. }
  419. .address-management .item {
  420. background-color: #fff;
  421. padding: 0 30rpx;
  422. margin-bottom: 12rpx;
  423. }
  424. .address-management .item .address {
  425. padding: 30rpx 0;
  426. border-bottom: 1rpx solid #eee;
  427. font-size: 28rpx;
  428. color: #282828;
  429. }
  430. .address-management .item .address .consignee {
  431. font-size: 28rpx;
  432. font-weight: bold;
  433. margin-bottom: 8rpx;
  434. }
  435. .address-management .item .address .consignee .phone {
  436. margin-left: 25rpx;
  437. }
  438. .address-management .item .operation {
  439. height: 83rpx;
  440. font-size: 28rpx;
  441. color: #282828;
  442. }
  443. .address-management .item .operation .radio text {
  444. margin-left: 13rpx;
  445. }
  446. .address-management .item .operation .iconfont {
  447. color: #2c2c2c;
  448. font-size: 35rpx;
  449. vertical-align: -2rpx;
  450. margin-right: 10rpx;
  451. }
  452. .address-management .item .operation .iconfont.icon-shanchu {
  453. margin-left: 40rpx;
  454. font-size: 38rpx;
  455. }
  456. .address-management .footer {
  457. position: fixed;
  458. width: 100%;
  459. background-color: #fff;
  460. bottom: 0;
  461. height: 106rpx;
  462. padding: 0 30rpx;
  463. box-sizing: border-box;
  464. }
  465. .address-management .footer .addressBnt {
  466. width: 330rpx;
  467. height: 76rpx;
  468. border-radius: 50rpx;
  469. text-align: center;
  470. line-height: 76rpx;
  471. font-size: 30rpx;
  472. color: #fff;
  473. background-color: var(--view-theme);
  474. }
  475. .address-management .footer .addressBnt.on {
  476. width: 690rpx;
  477. margin: 0 auto;
  478. }
  479. .address-management .footer .addressBnt .iconfont {
  480. font-size: 35rpx;
  481. margin-right: 8rpx;
  482. vertical-align: -1rpx;
  483. }
  484. .address-management .footer .addressBnt.wxbnt {
  485. background-color: #FE960F;
  486. }
  487. </style>