index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <template>
  2. <view>
  3. <view class="my-order">
  4. <view class="nav">
  5. <up-tabs
  6. :scrollable="false"
  7. lineColor="#F8C008"
  8. :activeStyle="{ color: '#F8C008', fontSize: '28rpx' }"
  9. :inactiveStyle="{ color: '#333333', fontSize: '28rpx' }"
  10. :list="list"
  11. @click="statusClick"
  12. :current="oldOrderStatus"
  13. ></up-tabs>
  14. </view>
  15. <view class="list" v-if="orderList.length">
  16. <OrderListCard
  17. v-for="(item, index) in orderList"
  18. :key="index"
  19. :order="item"
  20. :index="index"
  21. @cancelOrder="cancelOrder"
  22. @goPay="goPay"
  23. @goOrderDetails="goOrderDetails"
  24. :mallType="0"
  25. @delOrder="delOrder"
  26. @refreshStatus="refreshOrderStatus"
  27. />
  28. </view>
  29. <view
  30. class="loadingicon acea-row row-center-wrapper"
  31. v-if="orderList.length > 0"
  32. >
  33. <text
  34. class="loading iconfont icon-jiazai"
  35. :hidden="loading == false"
  36. ></text
  37. >{{ loadTitle }}
  38. </view>
  39. <view class="empty" v-if="orderList.length == 0">
  40. <uni-empty-view />
  41. </view>
  42. </view>
  43. <payment
  44. @closePopup="closePayPopup"
  45. :payMode="payMode"
  46. :showPopup="showPopup"
  47. @payComplete="payComplete"
  48. @payFail="payFail"
  49. @onChangeFun="onChangeFun"
  50. :order_id="pay_order_id"
  51. :totalPrice="totalPrice"
  52. ></payment>
  53. </view>
  54. </template>
  55. <script setup>
  56. import { ref, onMounted } from "vue";
  57. import { onLoad, onShow, onReachBottom } from "@dcloudio/uni-app";
  58. import OrderListCard from "@/components/OrderListCard";
  59. import { useAppStore } from "@/stores/app";
  60. import { getOrderList, orderData, orderCancel, orderDel } from "@/api/order.js";
  61. import payment from "@/components/payment";
  62. import { queryPaymentStatus } from "@/api/payment.js";
  63. import { toLogin } from "@/libs/login.js";
  64. import emptyPage from "@/components/emptyPage.vue";
  65. import { useToast } from "@/hooks/useToast";
  66. const appStore = useAppStore();
  67. const { Toast } = useToast();
  68. // 状态
  69. const loading = ref(false);
  70. const loadend = ref(false);
  71. const loadTitle = ref("加载更多");
  72. const orderList = ref([]);
  73. const orderDataState = ref({});
  74. const orderStatus = ref(0);
  75. const oldOrderStatus = ref(0); //老数据
  76. const page = ref(1);
  77. const limit = ref(10);
  78. const payMode = ref([
  79. {
  80. name: "微信支付",
  81. icon: "icon-weixinzhifu",
  82. value: "weixin",
  83. title: "微信快捷支付",
  84. },
  85. {
  86. name: "余额支付",
  87. icon: "icon-yuezhifu",
  88. value: "yue",
  89. title: "可用余额:",
  90. number: 0,
  91. },
  92. ]);
  93. const showPopup = ref(false);
  94. const pay_order_id = ref("");
  95. const totalPrice = ref("0");
  96. const list = ref([
  97. {
  98. name: "待付款",
  99. orderStatus: 0,
  100. value: 0,
  101. },
  102. {
  103. name: "待发货",
  104. orderStatus: 1,
  105. value: 1,
  106. },
  107. {
  108. name: "待收货",
  109. orderStatus: 2,
  110. value: 2,
  111. },
  112. {
  113. name: "退款/换货",
  114. orderStatus: -3,
  115. value: 3,
  116. },
  117. {
  118. name: "已完成",
  119. orderStatus: 4,
  120. value: 4,
  121. },
  122. {
  123. name: "已取消",
  124. orderStatus: 6,
  125. value: 6,
  126. },
  127. ]); //头部导航
  128. // 生命周期
  129. onMounted(() => {
  130. if (appStore.isLogin) {
  131. loadend.value = false;
  132. page.value = 1;
  133. orderList.value = [];
  134. getOrderData();
  135. getOrderListFn();
  136. } else {
  137. toLogin();
  138. }
  139. });
  140. // 页面显示时
  141. onShow(() => {
  142. if (appStore.isLogin) {
  143. loadend.value = false;
  144. page.value = 1;
  145. orderList.value = [];
  146. getOrderData();
  147. getOrderListFn();
  148. } else {
  149. toLogin();
  150. }
  151. });
  152. onLoad((options) => {
  153. if (options.status) {
  154. const statusValue = parseInt(options.status);
  155. // 验证状态是否有效
  156. const validStatuses = [0, 1, 2, 4, -3];
  157. if (validStatuses.includes(statusValue)) {
  158. // 设置状态
  159. orderStatus.value = statusValue;
  160. // 找到对应的索引
  161. const index = list.value.findIndex(
  162. (item) => item.orderStatus === statusValue
  163. );
  164. if (index !== -1) {
  165. oldOrderStatus.value = index; // 设置索引值
  166. }
  167. console.log("设置订单状态为:", statusValue, "索引为:", index);
  168. } else {
  169. console.warn("无效的状态参数:", statusValue);
  170. }
  171. }
  172. });
  173. // 下拉加载
  174. onReachBottom(() => {
  175. getOrderListFn();
  176. });
  177. function onChangeFun(state) {
  178. let action = state.action || null;
  179. let value = state.value !== undefined ? state.value : null;
  180. if (action && typeof [action] === "function") {
  181. [action](value);
  182. }
  183. }
  184. function getOrderData() {
  185. orderData().then((res) => {
  186. orderDataState.value = res.data;
  187. });
  188. }
  189. function cancelOrder(index, order_id) {
  190. if (!order_id)
  191. return Toast({
  192. title: "缺少订单号无法取消订单",
  193. });
  194. uni.showLoading({
  195. title: "正在删除中",
  196. });
  197. orderCancel(order_id)
  198. .then((res) => {
  199. uni.hideLoading();
  200. Toast(
  201. {
  202. title: "删除成功",
  203. icon: "success",
  204. },
  205. () => {
  206. orderList.value.splice(index, 1);
  207. orderDataState.value.unpaid_count--;
  208. getOrderData();
  209. }
  210. );
  211. })
  212. .catch((err) => {
  213. Toast({
  214. title: err,
  215. });
  216. });
  217. }
  218. function goPay(pay_price, order_id) {
  219. appStore.USERINFO().then((res) => {
  220. if (res.nowMoney) {
  221. showPopup.value = true;
  222. pay_order_id.value = order_id;
  223. totalPrice.value = pay_price;
  224. }
  225. });
  226. }
  227. const closePayPopup = () => {
  228. console.log("payClose");
  229. showPopup.value = false;
  230. };
  231. function payComplete() {
  232. console.log("payComplete");
  233. loadend.value = false;
  234. page.value = 1;
  235. orderList.value = [];
  236. showPopup.value = false;
  237. getOrderData();
  238. getOrderListFn();
  239. }
  240. function payFail() {
  241. showPopup.value = false;
  242. }
  243. function goOrderDetails(order_id) {
  244. if (!order_id)
  245. return Toast({
  246. title: "缺少订单号无法查看订单详情",
  247. });
  248. uni.navigateTo({
  249. url: "/pages/order_details/index?order_id=" + order_id,
  250. });
  251. }
  252. function toReturnList() {
  253. uni.navigateTo({
  254. url: "/pages/users/user_return_list/index",
  255. });
  256. }
  257. const changeStatus = (item) => {
  258. // oldOrderStatus.value = item.index;
  259. };
  260. function statusClick(item) {
  261. console.log(item);
  262. if (item.orderStatus === orderStatus.value) return;
  263. orderStatus.value = item.orderStatus;
  264. loadend.value = false;
  265. page.value = 1;
  266. orderList.value = [];
  267. getOrderListFn();
  268. }
  269. function getOrderListFn() {
  270. if (loadend.value || loading.value) return;
  271. loading.value = true;
  272. loadTitle.value = "加载更多";
  273. getOrderList({
  274. type: orderStatus.value,
  275. page: page.value,
  276. limit: limit.value,
  277. mallType: 0,
  278. })
  279. .then((res) => {
  280. let list = res.data.list || [];
  281. let isLoadend = list.length < limit.value;
  282. // orderList.value = uni.$util.SplitArray(list, orderList.value)
  283. orderList.value = [...list, ...orderList.value];
  284. loadend.value = isLoadend;
  285. loading.value = false;
  286. loadTitle.value = isLoadend ? "我也是有底线的" : "加载更多";
  287. page.value += 1;
  288. })
  289. .catch(() => {
  290. loading.value = false;
  291. loadTitle.value = "加载更多";
  292. });
  293. }
  294. function refreshOrderStatus(orderId, index) {
  295. if (!orderId) return Toast({ title: "缺少订单号" });
  296. uni.showLoading({ title: "查询中..." });
  297. queryPaymentStatus(orderId)
  298. .then((res) => {
  299. uni.hideLoading();
  300. const isPaid = res.data?.tradeState === "SUCCESS";
  301. if (isPaid) {
  302. Toast({ title: "支付成功,订单已更新", icon: "success" }, () => {
  303. loadend.value = false;
  304. page.value = 1;
  305. orderList.value = [];
  306. getOrderData();
  307. getOrderListFn();
  308. });
  309. } else {
  310. Toast({ title: "订单未支付,状态未变更" });
  311. }
  312. })
  313. .catch((err) => {
  314. uni.hideLoading();
  315. Toast({ title: err || "查询失败,请稍后重试" });
  316. });
  317. }
  318. function delOrder(order_id, index) {
  319. orderDel(order_id)
  320. .then((res) => {
  321. orderList.value.splice(index, 1);
  322. orderDataState.value.unpaid_count--;
  323. getOrderData();
  324. Toast({
  325. title: "删除成功",
  326. icon: "success",
  327. });
  328. })
  329. .catch((err) => {
  330. Toast({
  331. title: err,
  332. });
  333. });
  334. }
  335. </script>
  336. <style>
  337. page {
  338. background: #f9f7f0;
  339. }
  340. </style>
  341. <style scoped lang="scss">
  342. .empty {
  343. height: calc(100vh - 44px);
  344. /* #ifdef H5 */
  345. height: calc(100vh - 88px);
  346. /* #endif */
  347. display: flex;
  348. align-items: center;
  349. justify-content: center;
  350. }
  351. .my-order {
  352. .nav {
  353. background: #ffffff;
  354. // height: 140rpx;
  355. background-color: #fff;
  356. .item {
  357. text-align: center;
  358. font-size: 26rpx;
  359. color: #282828;
  360. padding: 26rpx 0;
  361. &.on {
  362. font-weight: bold;
  363. border-bottom: 5rpx solid $border-color;
  364. }
  365. .num {
  366. margin-top: 18rpx;
  367. }
  368. }
  369. }
  370. .list {
  371. width: 690rpx;
  372. margin: 16rpx auto 0 auto;
  373. }
  374. }
  375. .noCart {
  376. margin-top: 171rpx;
  377. padding-top: 0.1rpx;
  378. .pictrue {
  379. width: 414rpx;
  380. height: 336rpx;
  381. margin: 78rpx auto 56rpx auto;
  382. image {
  383. width: 100%;
  384. height: 100%;
  385. }
  386. }
  387. }
  388. </style>