index.vue 14 KB

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