logic.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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: '/static/dog.png',
  134. petName: order.petName || '',
  135. petBreed: order.breed || '',
  136. startLocation: order.fromAddress || '',
  137. startAddress: order.fromAddress || '',
  138. startDistance: '0km',
  139. endLocation: (order.customerName || '') + ' ' + (order.customerPhone || ''),
  140. endAddress: order.toAddress || '',
  141. customerPhone: order.customerPhone || '',
  142. endDistance: '0km',
  143. serviceContent: order.remark || '',
  144. remark: order.remark || ''
  145. }
  146. },
  147. getDisplayStatus(item) {
  148. if (item.statusText === '已完成') return '已完成';
  149. if (item.statusText === '已拒绝') return '已拒绝';
  150. if (item.statusText === '接单') {
  151. return item.type === 1 ? '待接送' : '待服务';
  152. }
  153. return item.type === 1 ? '配送中' : '服务中';
  154. },
  155. getStatusClass(item) {
  156. let display = this.getDisplayStatus(item);
  157. if (display === '已完成') return 'finish';
  158. if (display === '已拒绝') return 'reject';
  159. if (display === '配送中' || display === '服务中') return 'processing';
  160. return 'highlight';
  161. },
  162. goToDetail(item) {
  163. uni.navigateTo({ url: `/pages/orders/detail?id=${item.id}` });
  164. },
  165. showPetProfile(item) {
  166. this.currentPetInfo = {
  167. ...item,
  168. petGender: 'M',
  169. petAge: '2岁',
  170. petWeight: '15kg',
  171. petPersonality: '活泼亲人,精力旺盛',
  172. petHobby: '喜欢追飞盘,爱吃肉干',
  173. petRemark: '肠胃较弱,不能乱喂零食;出门易爆冲,请拉紧牵引绳。',
  174. petTags: ['拉响警报', '不能吃鸡肉', '精力旺盛'],
  175. petLogs: [
  176. { date: '2026-02-09 14:00', content: '今天遛弯拉了两次粑粑,精神状态很好。', recorder: '王阿姨' },
  177. { date: '2026-02-08 10:30', content: '有些挑食,剩了小半碗狗粮。', recorder: '李师傅' },
  178. { date: '2026-02-05 09:00', content: '建档。', recorder: '系统记录' }
  179. ]
  180. };
  181. this.showPetModal = true;
  182. },
  183. closePetProfile() {
  184. this.showPetModal = false;
  185. },
  186. openNavigation(item, pointType) {
  187. this.navTargetItem = item;
  188. this.navTargetPointType = pointType;
  189. this.showNavModal = true;
  190. },
  191. closeNavModal() {
  192. this.showNavModal = false;
  193. },
  194. chooseMap(mapType) {
  195. let item = this.navTargetItem;
  196. let pointType = this.navTargetPointType;
  197. let name = pointType === 'start' ? item.startLocation : item.endLocation;
  198. let address = pointType === 'start' ? item.startAddress : item.endAddress;
  199. this.showNavModal = false;
  200. uni.openLocation({
  201. latitude: 30.52,
  202. longitude: 114.31,
  203. name: name || '目的地',
  204. address: address || '默认地址',
  205. success: function () {
  206. console.log('打开导航成功: ' + mapType);
  207. }
  208. });
  209. },
  210. toggleCallMenu(item) {
  211. if (this.activeCallItem === item) {
  212. this.activeCallItem = null;
  213. } else {
  214. this.activeCallItem = item;
  215. }
  216. },
  217. closeCallMenu() {
  218. this.activeCallItem = null;
  219. },
  220. doCall(type, item) {
  221. let phoneNum = ''
  222. const targetItem = item || this.activeCallItem
  223. if (type === 'merchant') {
  224. phoneNum = '18900008451'
  225. } else if (type === 'customer') {
  226. phoneNum = targetItem?.customerPhone || '13800000001'
  227. }
  228. if (phoneNum) {
  229. uni.makePhoneCall({ phoneNumber: phoneNum })
  230. }
  231. this.activeCallItem = null;
  232. },
  233. reportAbnormal(item) {
  234. uni.navigateTo({ url: '/pages/orders/anomaly?orderId=' + (item.id || '') });
  235. },
  236. toggleDropdown(idx) {
  237. if (this.activeDropdown === idx) {
  238. this.activeDropdown = 0;
  239. } else {
  240. this.activeDropdown = idx;
  241. }
  242. },
  243. closeDropdown() {
  244. this.activeDropdown = 0;
  245. },
  246. selectType(index) {
  247. this.currentTypeFilterIdx = index;
  248. this.closeDropdown();
  249. },
  250. initCalendar() {
  251. let days = [];
  252. for (let i = 1; i <= 28; i++) {
  253. days.push(i);
  254. }
  255. this.calendarDays = days;
  256. this.selectedDateRange = [2, 4];
  257. },
  258. prevMonth() { uni.showToast({ title: '上个月', icon: 'none' }); },
  259. nextMonth() { uni.showToast({ title: '下个月', icon: 'none' }); },
  260. selectDateItem(day) {
  261. if (this.selectedDateRange.length === 2) {
  262. this.selectedDateRange = [day];
  263. } else if (this.selectedDateRange.length === 1) {
  264. let start = this.selectedDateRange[0];
  265. if (day > start) {
  266. this.selectedDateRange = [start, day];
  267. } else if (day < start) {
  268. this.selectedDateRange = [day, start];
  269. } else {
  270. this.selectedDateRange = [];
  271. }
  272. } else {
  273. this.selectedDateRange = [day];
  274. }
  275. },
  276. getDateClass(day) {
  277. if (this.selectedDateRange.length === 0) return '';
  278. if (this.selectedDateRange.length === 1) {
  279. return day === this.selectedDateRange[0] ? 'is-start' : '';
  280. }
  281. let start = this.selectedDateRange[0];
  282. let end = this.selectedDateRange[1];
  283. if (day === start) return 'is-start';
  284. if (day === end) return 'is-end';
  285. if (day > start && day < end) return 'is-between';
  286. return '';
  287. },
  288. resetTimeFilter() {
  289. this.hasTimeFilter = false;
  290. this.selectedDateRange = [];
  291. this.startServiceTime = '';
  292. this.endServiceTime = '';
  293. this.closeDropdown();
  294. this.loadOrders();
  295. },
  296. confirmTimeFilter() {
  297. if (this.selectedDateRange.length === 0) {
  298. uni.showToast({ title: '请先选择日期', icon: 'none' });
  299. return;
  300. }
  301. // 构建时间范围参数
  302. const year = this.currentMonth.replace(/[^0-9]/g, '').substring(0, 4);
  303. const month = this.currentMonth.replace(/[^0-9]/g, '').substring(4);
  304. const pad = (n) => String(n).padStart(2, '0');
  305. if (this.selectedDateRange.length === 2) {
  306. this.startServiceTime = `${year}-${pad(month)}-${pad(this.selectedDateRange[0])} 00:00:00`;
  307. this.endServiceTime = `${year}-${pad(month)}-${pad(this.selectedDateRange[1])} 23:59:59`;
  308. } else {
  309. this.startServiceTime = `${year}-${pad(month)}-${pad(this.selectedDateRange[0])} 00:00:00`;
  310. this.endServiceTime = `${year}-${pad(month)}-${pad(this.selectedDateRange[0])} 23:59:59`;
  311. }
  312. this.hasTimeFilter = true;
  313. this.closeDropdown();
  314. this.loadOrders();
  315. },
  316. getMainActionText(item) {
  317. return '查看详情';
  318. },
  319. mainAction(item) {
  320. uni.navigateTo({ url: `/pages/orders/detail?id=${item.id}` });
  321. },
  322. openRemarkInput() {
  323. this.remarkText = '';
  324. this.showRemarkInput = true;
  325. },
  326. closeRemarkInput() {
  327. this.showRemarkInput = false;
  328. this.remarkText = '';
  329. },
  330. submitRemark() {
  331. const text = this.remarkText.trim();
  332. if (!text) {
  333. uni.showToast({ title: '备注内容不能为空', icon: 'none' });
  334. return;
  335. }
  336. const now = new Date();
  337. 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')}`;
  338. if (!this.currentPetInfo.petLogs) {
  339. this.$set(this.currentPetInfo, 'petLogs', []);
  340. }
  341. this.currentPetInfo.petLogs.unshift({
  342. date: dateStr,
  343. content: text,
  344. recorder: '我'
  345. });
  346. uni.showToast({ title: '备注已添加', icon: 'success' });
  347. this.closeRemarkInput();
  348. },
  349. /**
  350. * 取消订单处理逻辑
  351. * @param {Object} item - 订单项
  352. */
  353. handleCancelOrder(item) {
  354. uni.showModal({
  355. title: '提示',
  356. content: '确认是否取消这个订单?',
  357. success: async (res) => {
  358. if (res.confirm) {
  359. try {
  360. uni.showLoading({ title: '取消中...', mask: true });
  361. await cancelOrderApi({ orderId: item.id });
  362. uni.showToast({ title: '订单已取消', icon: 'success' });
  363. // 延时刷新列表,防止提示框闪现
  364. setTimeout(() => {
  365. this.loadOrders();
  366. }, 1500);
  367. } catch (err) {
  368. console.error('取消订单失败:', err);
  369. uni.showToast({ title: '取消失败', icon: 'none' });
  370. } finally {
  371. uni.hideLoading();
  372. }
  373. }
  374. }
  375. });
  376. }
  377. }
  378. }