index.vue 14 KB

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