logic.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. import { getMyOrders } from '@/api/fulfiller'
  2. import { getServiceList } from '@/api/service'
  3. import { cancelOrderApi } from '@/api/order/subOrder/index'
  4. import customTabbar from '@/components/custom-tabbar/index.vue'
  5. export default {
  6. components: {
  7. customTabbar
  8. },
  9. data() {
  10. return {
  11. currentTab: 0,
  12. tabs: ['待接送/服务', '配送/服务中', '已完成', '已拒绝'],
  13. typeFilterOptions: ['全部类型'],
  14. currentTypeFilterIdx: 0,
  15. activeDropdown: 0,
  16. hasTimeFilter: false,
  17. currentMonth: '2026年2月',
  18. weekDays: ['日', '一', '二', '三', '四', '五', '六'],
  19. calendarDays: [],
  20. selectedDateRange: [],
  21. allOrderList: [],
  22. serviceList: [],
  23. searchContent: '',
  24. startServiceTime: '',
  25. endServiceTime: '',
  26. showPetModal: false,
  27. currentPetInfo: {},
  28. showNavModal: false,
  29. navTargetItem: null,
  30. navTargetPointType: '',
  31. activeCallItem: null,
  32. showRemarkInput: false,
  33. remarkText: ''
  34. }
  35. },
  36. created() {
  37. this.initCalendar();
  38. },
  39. async onLoad() {
  40. await this.loadServiceList()
  41. await this.loadOrders()
  42. },
  43. onShow() {
  44. uni.hideTabBar()
  45. // 此处不需要重复调用,因为逻辑可能在onLoad已处理,
  46. // 或者如果需要每次进入都刷新,可以保留,但需确保顺序
  47. this.loadOrders()
  48. },
  49. async onPullDownRefresh() {
  50. try {
  51. await this.loadServiceList()
  52. await this.loadOrders()
  53. } finally {
  54. uni.stopPullDownRefresh()
  55. }
  56. },
  57. watch: {
  58. currentTab() {
  59. this.loadOrders()
  60. },
  61. currentTypeFilterIdx() {
  62. this.loadOrders()
  63. },
  64. searchContent() {
  65. // 搜索内容变化时,自动重新加载订单
  66. this.loadOrders()
  67. }
  68. },
  69. computed: {
  70. orderList() {
  71. return this.allOrderList;
  72. }
  73. },
  74. methods: {
  75. async loadServiceList() {
  76. try {
  77. const res = await getServiceList()
  78. this.serviceList = res.data || []
  79. this.typeFilterOptions = ['全部类型', ...this.serviceList.map(s => s.name)]
  80. } catch (err) {
  81. console.error('获取服务类型失败:', err)
  82. }
  83. },
  84. async loadOrders() {
  85. try {
  86. const statusMap = { 0: 2, 1: 3, 2: 4, 3: 5 }
  87. const serviceId = this.currentTypeFilterIdx > 0 ? this.serviceList[this.currentTypeFilterIdx - 1]?.id : undefined
  88. const params = {
  89. status: statusMap[this.currentTab],
  90. content: this.searchContent || undefined,
  91. service: serviceId,
  92. startServiceTime: this.startServiceTime || undefined,
  93. endServiceTime: this.endServiceTime || undefined
  94. }
  95. console.log('订单列表请求参数:', params)
  96. const res = await getMyOrders(params)
  97. console.log('订单列表响应:', res)
  98. const orders = res.rows || []
  99. console.log('订单数量:', orders.length)
  100. this.allOrderList = orders.map(order => this.transformOrder(order, this.currentTab))
  101. } catch (err) {
  102. console.error('获取订单列表失败:', err)
  103. this.allOrderList = []
  104. }
  105. },
  106. transformOrder(order, tabIndex) {
  107. const service = this.serviceList.find(s => s.id === order.service)
  108. const serviceText = service?.name || '未知'
  109. const serviceIcon = service?.iconUrl || ''
  110. const mode = service?.mode || 0
  111. const isRoundTrip = mode === 1
  112. // 根据 Tab 索引强制指定状态文字,忽略后端缺失的 status 字段
  113. let statusText = '接单'
  114. if (tabIndex === 0) {
  115. statusText = '接单' // 待接送/服务
  116. } else if (tabIndex === 1) {
  117. statusText = isRoundTrip ? '出发' : '开始' // 配送/服务中
  118. } else if (tabIndex === 2) {
  119. statusText = '已完成' // 已完成
  120. } else if (tabIndex === 3) {
  121. statusText = '已拒绝' // 已拒绝
  122. }
  123. return {
  124. id: order.id,
  125. status: order.status, // 保存原始 status 用于判断权限
  126. type: isRoundTrip ? 1 : 2,
  127. typeText: serviceText,
  128. typeIcon: serviceIcon,
  129. statusText: statusText,
  130. price: (order.price / 100).toFixed(2),
  131. timeLabel: '服务时间',
  132. time: order.serviceTime || '',
  133. petAvatar: order.petAvatar || '/static/dog.png',
  134. petAvatarUrl: order.petAvatarUrl || '',
  135. petName: order.petName || '',
  136. petBreed: order.breed || '',
  137. startLocation: order.fromAddress || '',
  138. startAddress: order.fromAddress || '',
  139. startDistance: '0km',
  140. endLocation: (order.customerName || '') + ' ' + (order.customerPhone || ''),
  141. endAddress: order.toAddress || '',
  142. customerPhone: order.customerPhone || '',
  143. endDistance: '0km',
  144. serviceContent: order.remark || '',
  145. remark: order.remark || ''
  146. }
  147. },
  148. getDisplayStatus(item) {
  149. if (item.statusText === '已完成') return '已完成';
  150. if (item.statusText === '已拒绝') return '已拒绝';
  151. if (item.statusText === '接单') {
  152. return item.type === 1 ? '待接送' : '待服务';
  153. }
  154. return item.type === 1 ? '配送中' : '服务中';
  155. },
  156. getStatusClass(item) {
  157. let display = this.getDisplayStatus(item);
  158. if (display === '已完成') return 'finish';
  159. if (display === '已拒绝') return 'reject';
  160. if (display === '配送中' || display === '服务中') return 'processing';
  161. return 'highlight';
  162. },
  163. goToDetail(item) {
  164. uni.navigateTo({ url: `/pages/orders/detail?id=${item.id}` });
  165. },
  166. showPetProfile(item) {
  167. this.currentPetInfo = {
  168. ...item,
  169. petGender: 'M',
  170. petAge: '2岁',
  171. petWeight: '15kg',
  172. petPersonality: '活泼亲人,精力旺盛',
  173. petHobby: '喜欢追飞盘,爱吃肉干',
  174. petRemark: '肠胃较弱,不能乱喂零食;出门易爆冲,请拉紧牵引绳。',
  175. petTags: ['拉响警报', '不能吃鸡肉', '精力旺盛'],
  176. petLogs: [
  177. { date: '2026-02-09 14:00', content: '今天遛弯拉了两次粑粑,精神状态很好。', recorder: '王阿姨' },
  178. { date: '2026-02-08 10:30', content: '有些挑食,剩了小半碗狗粮。', recorder: '李师傅' },
  179. { date: '2026-02-05 09:00', content: '建档。', recorder: '系统记录' }
  180. ]
  181. };
  182. this.showPetModal = true;
  183. },
  184. closePetProfile() {
  185. this.showPetModal = false;
  186. },
  187. openNavigation(item, pointType) {
  188. this.navTargetItem = item;
  189. this.navTargetPointType = pointType;
  190. this.showNavModal = true;
  191. },
  192. closeNavModal() {
  193. this.showNavModal = false;
  194. },
  195. chooseMap(mapType) {
  196. let item = this.navTargetItem;
  197. let pointType = this.navTargetPointType;
  198. let name = pointType === 'start' ? item.startLocation : item.endLocation;
  199. let address = pointType === 'start' ? item.startAddress : item.endAddress;
  200. this.showNavModal = false;
  201. uni.openLocation({
  202. latitude: 30.52,
  203. longitude: 114.31,
  204. name: name || '目的地',
  205. address: address || '默认地址',
  206. success: function () {
  207. console.log('打开导航成功: ' + mapType);
  208. }
  209. });
  210. },
  211. toggleCallMenu(item) {
  212. if (this.activeCallItem === item) {
  213. this.activeCallItem = null;
  214. } else {
  215. this.activeCallItem = item;
  216. }
  217. },
  218. closeCallMenu() {
  219. this.activeCallItem = null;
  220. },
  221. doCall(type, item) {
  222. let phoneNum = ''
  223. const targetItem = item || this.activeCallItem
  224. if (type === 'merchant') {
  225. phoneNum = '18900008451'
  226. } else if (type === 'customer') {
  227. phoneNum = targetItem?.customerPhone || '13800000001'
  228. }
  229. if (phoneNum) {
  230. uni.makePhoneCall({ phoneNumber: phoneNum })
  231. }
  232. this.activeCallItem = null;
  233. },
  234. reportAbnormal(item) {
  235. uni.navigateTo({ url: '/pages/orders/anomaly?orderId=' + (item.id || '') });
  236. },
  237. toggleDropdown(idx) {
  238. if (this.activeDropdown === idx) {
  239. this.activeDropdown = 0;
  240. } else {
  241. this.activeDropdown = idx;
  242. }
  243. },
  244. closeDropdown() {
  245. this.activeDropdown = 0;
  246. },
  247. selectType(index) {
  248. this.currentTypeFilterIdx = index;
  249. this.closeDropdown();
  250. },
  251. initCalendar() {
  252. let days = [];
  253. for (let i = 1; i <= 28; i++) {
  254. days.push(i);
  255. }
  256. this.calendarDays = days;
  257. this.selectedDateRange = [2, 4];
  258. },
  259. prevMonth() { uni.showToast({ title: '上个月', icon: 'none' }); },
  260. nextMonth() { uni.showToast({ title: '下个月', icon: 'none' }); },
  261. selectDateItem(day) {
  262. if (this.selectedDateRange.length === 2) {
  263. this.selectedDateRange = [day];
  264. } else if (this.selectedDateRange.length === 1) {
  265. let start = this.selectedDateRange[0];
  266. if (day > start) {
  267. this.selectedDateRange = [start, day];
  268. } else if (day < start) {
  269. this.selectedDateRange = [day, start];
  270. } else {
  271. this.selectedDateRange = [];
  272. }
  273. } else {
  274. this.selectedDateRange = [day];
  275. }
  276. },
  277. getDateClass(day) {
  278. if (this.selectedDateRange.length === 0) return '';
  279. if (this.selectedDateRange.length === 1) {
  280. return day === this.selectedDateRange[0] ? 'is-start' : '';
  281. }
  282. let start = this.selectedDateRange[0];
  283. let end = this.selectedDateRange[1];
  284. if (day === start) return 'is-start';
  285. if (day === end) return 'is-end';
  286. if (day > start && day < end) return 'is-between';
  287. return '';
  288. },
  289. resetTimeFilter() {
  290. this.hasTimeFilter = false;
  291. this.selectedDateRange = [];
  292. this.startServiceTime = '';
  293. this.endServiceTime = '';
  294. this.closeDropdown();
  295. this.loadOrders();
  296. },
  297. confirmTimeFilter() {
  298. if (this.selectedDateRange.length === 0) {
  299. uni.showToast({ title: '请先选择日期', icon: 'none' });
  300. return;
  301. }
  302. // 构建时间范围参数
  303. const year = this.currentMonth.replace(/[^0-9]/g, '').substring(0, 4);
  304. const month = this.currentMonth.replace(/[^0-9]/g, '').substring(4);
  305. const pad = (n) => String(n).padStart(2, '0');
  306. if (this.selectedDateRange.length === 2) {
  307. this.startServiceTime = `${year}-${pad(month)}-${pad(this.selectedDateRange[0])} 00:00:00`;
  308. this.endServiceTime = `${year}-${pad(month)}-${pad(this.selectedDateRange[1])} 23:59:59`;
  309. } else {
  310. this.startServiceTime = `${year}-${pad(month)}-${pad(this.selectedDateRange[0])} 00:00:00`;
  311. this.endServiceTime = `${year}-${pad(month)}-${pad(this.selectedDateRange[0])} 23:59:59`;
  312. }
  313. this.hasTimeFilter = true;
  314. this.closeDropdown();
  315. this.loadOrders();
  316. },
  317. getMainActionText(item) {
  318. return '查看详情';
  319. },
  320. mainAction(item) {
  321. uni.navigateTo({ url: `/pages/orders/detail?id=${item.id}` });
  322. },
  323. openRemarkInput() {
  324. this.remarkText = '';
  325. this.showRemarkInput = true;
  326. },
  327. closeRemarkInput() {
  328. this.showRemarkInput = false;
  329. this.remarkText = '';
  330. },
  331. submitRemark() {
  332. const text = this.remarkText.trim();
  333. if (!text) {
  334. uni.showToast({ title: '备注内容不能为空', icon: 'none' });
  335. return;
  336. }
  337. const now = new Date();
  338. const dateStr = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`;
  339. if (!this.currentPetInfo.petLogs) {
  340. this.$set(this.currentPetInfo, 'petLogs', []);
  341. }
  342. this.currentPetInfo.petLogs.unshift({
  343. date: dateStr,
  344. content: text,
  345. recorder: '我'
  346. });
  347. uni.showToast({ title: '备注已添加', icon: 'success' });
  348. this.closeRemarkInput();
  349. },
  350. /**
  351. * 取消订单处理逻辑
  352. * @param {Object} item - 订单项
  353. */
  354. handleCancelOrder(item) {
  355. uni.showModal({
  356. title: '提示',
  357. content: '确认是否取消这个订单?',
  358. success: async (res) => {
  359. if (res.confirm) {
  360. try {
  361. uni.showLoading({ title: '取消中...', mask: true });
  362. await cancelOrderApi({ orderId: item.id });
  363. uni.showToast({ title: '订单已取消', icon: 'success' });
  364. // 延时刷新列表,防止提示框闪现
  365. setTimeout(() => {
  366. this.loadOrders();
  367. }, 1500);
  368. } catch (err) {
  369. console.error('取消订单失败:', err);
  370. uni.showToast({ title: '取消失败', icon: 'none' });
  371. } finally {
  372. uni.hideLoading();
  373. }
  374. }
  375. }
  376. });
  377. }
  378. }
  379. }