index.vue 13 KB

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