index.vue 14 KB

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