index.vue 14 KB

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