index.vue 13 KB

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