index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. <template>
  2. <view>
  3. <z-paging ref="paging" class="list-container" v-model="addressList" @query="queryList" loading-more-no-more-text="我也是有底线的">
  4. <template #empty>
  5. <uni-empty-view />
  6. </template>
  7. <view class="address-management">
  8. <view class="item" v-for="(item, index) in addressList" :key="index">
  9. <view class="address" @click="goOrder(item.id)">
  10. <view class="consignee">{{ item.province }}{{ item.city }}{{ item.district
  11. }}{{ item.detail }}</view>
  12. <view class="">{{ item.realName
  13. }}<text class="phone">{{ item.phone }}</text></view>
  14. </view>
  15. <view class="operation acea-row row-between-wrapper">
  16. <view @click="radioChange(item,index)" class="radio" :class="{active:item.isDefault}">
  17. <image v-if="item.isDefault" src="@/static/images/select-active@2x.png" mode=""></image>
  18. <image v-else src="@/static/images/select@2x.png" mode=""></image>
  19. <view class="">设为默认</view>
  20. </view>
  21. <view class="operation-right">
  22. <view @click="editAddressFn(item.id)">编辑</view>
  23. <view @click="delAddressFn(index)">删除</view>
  24. </view>
  25. </view>
  26. </view>
  27. </view>
  28. <template #bottom>
  29. <view class="footer acea-row row-between-wrapper">
  30. <!-- #ifdef MP-->
  31. <view class="addressBnt" @click="addAddress"><text class="iconfont icon-tianjiadizhi"></text>添加新地址
  32. </view>
  33. <view class="addressBnt wxbnt" @click="getWxAddress"><text class="iconfont icon-weixin2"></text>导入微信地址</view>
  34. <!-- #endif -->
  35. <!-- #ifdef H5-->
  36. <view class="addressBnt" :class="wechat.isWeixin() ? '' : 'on'" @click="addAddress"><text
  37. class="iconfont icon-tianjiadizhi"></text>添加新地址</view>
  38. <view v-if="wechat.isWeixin()" class="addressBnt wxbnt" @click="getAddress"><text
  39. class="iconfont icon-weixin2"></text>导入微信地址</view>
  40. <!-- #endif -->
  41. <!-- #ifdef APP-->
  42. <view class="addressBnt on" @click="addAddress"><text class="iconfont icon-tianjiadizhi"></text>添加新地址
  43. </view>
  44. <!-- #endif -->
  45. </view>
  46. </template>
  47. </z-paging>
  48. </view>
  49. </template>
  50. <script setup>
  51. import {
  52. ref,
  53. computed,
  54. watch
  55. } from "vue";
  56. import {
  57. useAppStore
  58. } from "@/stores/app.js";
  59. import {
  60. onLoad,
  61. onShow,
  62. onReachBottom
  63. } from "@dcloudio/uni-app";
  64. import {
  65. getAddressList,
  66. setAddressDefault,
  67. delAddress,
  68. editAddress,
  69. } from "@/api/user.js";
  70. import {
  71. toLogin
  72. } from "@/libs/login.js";
  73. import {
  74. useToast
  75. } from "@/hooks/useToast.js";
  76. import wechat from "@/libs/wechat.js";
  77. const {
  78. Toast
  79. } = useToast();
  80. const appStore = useAppStore();
  81. const addressList = ref([]);
  82. const cartId = ref("");
  83. const pinkId = ref(0);
  84. const couponId = ref(0);
  85. const loading = ref(false);
  86. const loadend = ref(false);
  87. const loadTitle = ref("加载更多");
  88. const page = ref(1);
  89. const limit = ref(20);
  90. const isAuto = ref(false);
  91. const isShowAuth = ref(false);
  92. const bargain = ref(false);
  93. const combination = ref(false);
  94. const secKill = ref(false);
  95. const preOrderNo = ref(0);
  96. const isFlashSale = ref(false); // 是否为秒杀订单
  97. const isGroupBuy = ref(false); // 是否为团购订单
  98. watch(
  99. () => appStore.isLogin,
  100. (newV) => {
  101. if (newV) {
  102. getUserAddress(true);
  103. }
  104. }
  105. );
  106. function getUserAddress(isPage) {
  107. getAddressListFn(isPage);
  108. }
  109. onLoad((options) => {
  110. if (appStore.isLogin) {
  111. preOrderNo.value = options.preOrderNo || 0;
  112. isFlashSale.value = options.isFlashSale === "1"; // 接收秒杀订单标识
  113. isGroupBuy.value = options.isGroupBuy === "1"; // 接收团购订单标识
  114. getAddressListFn(true);
  115. } else {
  116. toLogin();
  117. }
  118. });
  119. onShow(() => {
  120. getAddressListFn(true);
  121. });
  122. function onLoadFun() {
  123. getAddressListFn();
  124. }
  125. function authColse(e) {
  126. isShowAuth.value = e;
  127. }
  128. // 导入微信地址(小程序)
  129. function getWxAddress() {
  130. uni.authorize({
  131. scope: "scope.address",
  132. success: function() {
  133. uni.chooseAddress({
  134. success: function(res) {
  135. let addressP = {
  136. province: res.provinceName,
  137. city: res.cityName,
  138. district: res.countyName,
  139. cityId: 0,
  140. };
  141. editAddress({
  142. address: addressP,
  143. isDefault: true,
  144. realName: res.userName,
  145. postCode: res.postalCode,
  146. phone: res.telNumber,
  147. detail: res.detailInfo,
  148. id: 0,
  149. })
  150. .then(() => {
  151. Toast({
  152. title: "添加成功",
  153. icon: "success",
  154. },
  155. () => {
  156. getAddressListFn(true);
  157. }
  158. );
  159. })
  160. .catch((err) => {
  161. return Toast({
  162. title: err,
  163. });
  164. });
  165. },
  166. fail: function(res) {
  167. if (res.errMsg == "chooseAddress:cancel")
  168. return Toast({
  169. title: "取消选择",
  170. });
  171. },
  172. });
  173. },
  174. fail: function() {
  175. uni.showModal({
  176. title: "您已拒绝导入微信地址权限",
  177. content: "是否进入权限管理,调整授权?",
  178. success(res) {
  179. if (res.confirm) {
  180. uni.openSetting({
  181. success: function(res) {
  182. console.log(res.authSetting);
  183. },
  184. });
  185. } else if (res.cancel) {
  186. return Toast({
  187. title: "已取消!",
  188. });
  189. }
  190. },
  191. });
  192. },
  193. });
  194. }
  195. // 导入微信地址(公众号)
  196. function getAddress() {
  197. wechat.openAddress().then((userInfo) => {
  198. editAddress({
  199. realName: userInfo.userName,
  200. phone: userInfo.telNumber,
  201. address: {
  202. province: userInfo.provinceName,
  203. city: userInfo.cityName,
  204. district: userInfo.countryName,
  205. cityId: 0,
  206. },
  207. detail: userInfo.detailInfo,
  208. postCode: userInfo.postalCode,
  209. isDefault: true,
  210. })
  211. .then(() => {
  212. Toast({
  213. title: "添加成功",
  214. icon: "success",
  215. },
  216. () => {
  217. getAddressListFn(true);
  218. }
  219. );
  220. })
  221. .catch((err) => {
  222. Toast({
  223. title: err || "添加失败",
  224. });
  225. });
  226. });
  227. }
  228. const paging = ref(null)
  229. const queryList = async (pageNo, pageSize) => {
  230. try {
  231. let res = await getAddressList({
  232. page: pageNo,
  233. limit: pageSize
  234. })
  235. paging.value.complete(res.data.list);
  236. } catch (err) {
  237. paging.value.complete(false);
  238. }
  239. }
  240. function getAddressListFn(isPage) {
  241. if (isPage) {
  242. loadend.value = false;
  243. page.value = 1;
  244. addressList.value = [];
  245. }
  246. if (loading.value || loadend.value) return;
  247. loading.value = true;
  248. loadTitle.value = "";
  249. getAddressList({
  250. page: page.value,
  251. limit: limit.value,
  252. })
  253. .then((res) => {
  254. let list = res.data.list;
  255. let isLoadend = list.length < limit.value;
  256. addressList.value = [...addressList.value, ...list];
  257. loadend.value = isLoadend;
  258. loadTitle.value = isLoadend ? "我也是有底线的" : "加载更多";
  259. page.value = page.value + 1;
  260. loading.value = false;
  261. })
  262. .catch(() => {
  263. loading.value = false;
  264. loadTitle.value = "加载更多";
  265. });
  266. }
  267. // 设置默认地址
  268. function radioChange(item, index) {
  269. let address = addressList.value[index];
  270. if (address == undefined) return Toast({
  271. title: "您设置的默认地址不存在!"
  272. });
  273. setAddressDefault(address.id)
  274. .then(() => {
  275. addressList.value.forEach((item, i) => {
  276. item.isDefault = i === index;
  277. });
  278. Toast({
  279. title: "设置成功",
  280. icon: "success",
  281. },
  282. () => {
  283. addressList.value = [...addressList.value];
  284. }
  285. );
  286. })
  287. .catch((err) => {
  288. return Toast({
  289. title: err,
  290. });
  291. });
  292. }
  293. // 编辑地址
  294. function editAddressFn(id) {
  295. const cart = cartId.value;
  296. const pink = pinkId.value;
  297. const coupon = couponId.value;
  298. cartId.value = "";
  299. pinkId.value = "";
  300. couponId.value = "";
  301. const flashSaleParam = isFlashSale.value ? "&isFlashSale=1" : "";
  302. uni.navigateTo({
  303. url: `/pages/users/user_address/index?id=${id}&cartId=${cart}&pinkId=${pink}&couponId=${coupon}&secKill=${secKill.value}&combination=${combination.value}&bargain=${bargain.value}&preOrderNo=${preOrderNo.value}${flashSaleParam}`,
  304. });
  305. }
  306. // 删除地址
  307. function delAddressFn(index) {
  308. let address = addressList.value[index];
  309. if (address == undefined) return Toast({
  310. title: "您删除的地址不存在!"
  311. });
  312. delAddress(address.id)
  313. .then(() => {
  314. Toast({
  315. title: "删除成功",
  316. icon: "success",
  317. },
  318. () => {
  319. addressList.value.splice(index, 1);
  320. addressList.value = [...addressList.value];
  321. }
  322. );
  323. })
  324. .catch((err) => {
  325. return Toast({
  326. title: err,
  327. });
  328. });
  329. }
  330. // 新增地址
  331. function addAddress() {
  332. cartId.value = "";
  333. pinkId.value = "";
  334. couponId.value = "";
  335. const flashSaleParam = isFlashSale.value ? "&isFlashSale=1" : "";
  336. const groupBuyParam = isGroupBuy.value ? "&isGroupBuy=1" : "";
  337. uni.navigateTo({
  338. url: `/pages/users/user_address/index?preOrderNo=${preOrderNo.value}${flashSaleParam}${groupBuyParam}`,
  339. });
  340. }
  341. function goOrder(id) {
  342. if (preOrderNo.value) {
  343. // 根据订单类型跳转到不同的确认页面
  344. if (isFlashSale.value) {
  345. // 秒杀订单跳转到秒杀确认页面
  346. uni.redirectTo({
  347. url: `/pages/users/utils/flashSale/confirmOrder?is_address=1&preOrderNo=${preOrderNo.value}&addressId=${id}`,
  348. });
  349. } else if (isGroupBuy.value) {
  350. // 团购支付返回到支付详情页
  351. uni.redirectTo({
  352. url: `/pages/group_buying/paydetail?is_address=1&preOrderNo=${preOrderNo.value}&addressId=${id}`,
  353. });
  354. } else {
  355. // 普通订单跳转到普通确认页面
  356. uni.redirectTo({
  357. url: `/pages/users/order_confirm/index?is_address=1&preOrderNo=${preOrderNo.value}&addressId=${id}`,
  358. });
  359. }
  360. }
  361. }
  362. onReachBottom(() => {
  363. getAddressListFn();
  364. });
  365. </script>
  366. <style>
  367. page {
  368. background: #F9F7F0;
  369. }
  370. </style>
  371. <style lang="scss" scoped>
  372. .list-container{
  373. background-color: #f9f7f0;
  374. height: 100%;
  375. }
  376. .address-management {
  377. padding: 16rpx 16rpx 0;
  378. }
  379. .address-management .item {
  380. background-color: #FFFFFF;
  381. padding: 16rpx;
  382. border-radius: 16rpx;
  383. margin-bottom: 16rpx;
  384. &:last-child {
  385. margin-bottom: 0;
  386. }
  387. }
  388. .address-management .item .address {
  389. font-size: 28rpx;
  390. color: #333333;
  391. }
  392. .address-management .item .address .consignee {
  393. font-size: 32rpx;
  394. font-weight: bold;
  395. margin-bottom: 16rpx;
  396. }
  397. .address-management .item .address .phone {
  398. margin-left: 32rpx;
  399. }
  400. .address-management .item .operation-right {
  401. display: flex;
  402. align-items: center;
  403. view {
  404. font-size: 28rpx;
  405. color: #333333;
  406. width: 120rpx;
  407. text-align: center;
  408. line-height: 60rpx;
  409. background: rgba(248, 192, 8, 0.1);
  410. border-radius: 8rpx;
  411. &:last-child {
  412. background: #F8C008;
  413. margin-left: 16rpx;
  414. }
  415. }
  416. }
  417. .address-management .item .operation .radio {
  418. display: flex;
  419. align-items: center;
  420. color: #333333;
  421. font-size: 28rpx;
  422. image {
  423. width: 28rpx;
  424. height: 28rpx;
  425. margin-right: 16rpx;
  426. }
  427. }
  428. .address-management .item .operation .radio.active {
  429. color: #F8C008;
  430. }
  431. .address-management .item .operation .iconfont {
  432. color: #2c2c2c;
  433. font-size: 35rpx;
  434. vertical-align: -2rpx;
  435. margin-right: 10rpx;
  436. }
  437. .address-management .item .operation .iconfont.icon-shanchu {
  438. margin-left: 35rpx;
  439. font-size: 38rpx;
  440. }
  441. .footer {
  442. width: 100%;
  443. background-color: #FFFFFF;
  444. padding: 22rpx 32rpx 56rpx;
  445. }
  446. .footer .addressBnt {
  447. line-height: 76rpx;
  448. width: 330rpx;
  449. border-radius: 16rpx;
  450. text-align: center;
  451. font-size: 32rpx;
  452. color: #333333;
  453. font-weight: bold;
  454. background: #F8C008;
  455. }
  456. .footer .addressBnt.on {
  457. width: 690rpx;
  458. margin: 0 auto;
  459. }
  460. .footer .addressBnt.wxbnt {
  461. background-color: #fe960f;
  462. }
  463. .footer .addressBnt .iconfont {
  464. font-size: 35rpx;
  465. margin-right: 8rpx;
  466. vertical-align: -1rpx;
  467. }
  468. </style>