| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- const api = require('../../api/index.js');
-
- Page({
- data: {
- // 筛选状态
- activeType: 0, // all/car/station/user
- activeStatus: 1, // pending/processing/completed/rejected
- // 工单数量统计(待处理工单)
- orderCount: {
- all: 0,
- car: 0,
- station: 0,
- user: 0
- },
- // 原始工单数据(实际项目从接口获取)
- orderData: [],
- // 筛选后的工单列表
- filteredOrders: [],
- // 分页相关配置
- pageNum: 1, // 当前页码
- pageSize: 10, // 每页条数
- hasMore: true, // 是否有更多数据
- isLoading: false // 是否正在加载数据
- },
-
- onLoad(options) {
- wx.$on('wsMessage', this.handleWsMessage);
- // 初始化工单数据
- this.initOrderData();
-
- },
- onHide(){
- // 页面卸载时,取消订阅,这是至关重要的一步!
- wx.$off('wsMessage', this.handleWsMessage);
- },
- /**
- * 处理接收到的 WebSocket 消息
- * @param {Object} message 从 app.js 广播过来的消息对象
- */
- handleWsMessage(message) {
- console.log('Other Page 收到 WebSocket 消息:', message);
-
- // 根据消息类型进行不同的处理
- switch (message.api) {
- case '/sysworkorder/selectpartnerworkorder':
- // 在这里可以更新地图、刷新UI等
- this.initOrderData()
-
- break;
-
- }
- },
- /**
- * 下拉刷新:重新加载工单数据(重置分页)
- */
- onPullDownRefresh() {
- // 重置分页参数,重新加载第一页
- this.setData({
- pageNum: 1,
- hasMore: true,
- filteredOrders: []
- }, () => {
- this.initOrderData(() => {
- wx.stopPullDownRefresh();
- });
- });
- },
-
- /**
- * 触底加载更多
- */
- onReachBottom() {
- // 有更多数据且不在加载中时,加载下一页
- if (this.data.hasMore && !this.data.isLoading) {
- this.setData({ pageNum: this.data.pageNum + 1 }, () => {
- this.filterOrders(); // 筛选模式下加载更多用filterOrders,否则用initOrderData
- });
- }
- },
-
- /**
- * 初始化工单数据(模拟储能车运维场景数据)
- * @param {Function} callback 加载完成后的回调
- */
- initOrderData(callback) {
- // 防止重复加载
- if (this.data.isLoading) return;
-
- this.setData({ isLoading: true });
-
- const { pageNum, pageSize } = this.data;
- const data = {
- workorderType: "",
- chargedischargeType: "",
- settlementType: 0,
- pageNum,
- pageSize
- };
-
- api.request(`/sysworkorder/selectpartnerworkorder`, 'post', data, { isPublic: false })
- .then((res) => {
- console.log('工单列表数据:', res.data);
- if (res.code === 200) {
- const newData = res.data.data;
- // 下拉刷新重置数据,加载更多拼接数据
- const orderData = pageNum === 1 ? newData : [...this.data.orderData, ...newData];
- // 判断是否还有更多数据(返回数据条数小于每页条数则无更多)
- const hasMore = newData.length >= pageSize;
-
- this.setData({
- orderData,
- filteredOrders: orderData, // 初始加载时筛选列表等于原始数据
- hasMore,
- isLoading: false
- }, () => {
- // 统计各类型待处理工单数量(待派单+处理中)
- callback && callback();
- });
- } else {
- this.setData({ isLoading: false });
- wx.showToast({ title: '数据加载失败', icon: 'none' });
- callback && callback();
- }
- })
- .catch((err) => {
- console.error('请求失败:', err);
- this.setData({ isLoading: false });
- wx.showToast({ title: '网络异常', icon: 'none' });
- callback && callback();
- });
- },
-
- /**
- * 筛选工单列表(根据类型+状态,支持分页)
- */
- filterOrders() {
- // 防止重复加载
- if (this.data.isLoading) return;
-
- this.setData({ isLoading: true });
-
- const { activeType, activeStatus, pageNum, pageSize, filteredOrders } = this.data;
- const data = {
- workorderType: activeStatus,
- chargedischargeType: activeType,
- settlementType: 0,
- pageNum,
- pageSize
- };
-
- api.request(`/sysworkorder/selectpartnerworkorder`, 'post', data, { isPublic: false })
- .then((res) => {
- console.log('筛选后工单数据:', res.data);
- if (res.code === 200) {
- const newData = res.data.data;
- // 第一页重置数据,后续页拼接数据
- const newFilteredOrders = pageNum === 1 ? newData : [...filteredOrders, ...newData];
- const hasMore = newData.length >= pageSize;
-
- this.setData({
- filteredOrders: newFilteredOrders,
- hasMore,
- isLoading: false
- });
- } else {
- this.setData({ isLoading: false });
- wx.showToast({ title: '筛选失败', icon: 'none' });
- }
- })
- .catch((err) => {
- console.error('筛选请求失败:', err);
- this.setData({ isLoading: false });
- wx.showToast({ title: '网络异常', icon: 'none' });
- });
- },
-
- /**
- * 切换工单类型筛选(重置分页)
- */
- switchType(e) {
- const type = e.currentTarget.dataset.type;
- this.setData({
- activeType: type,
- pageNum: 1, // 切换筛选条件时重置页码
- hasMore: true, // 重置是否有更多数据
- filteredOrders: [] // 清空现有筛选数据
- }, () => {
- this.filterOrders();
- });
- },
-
- /**
- * 切换工单状态筛选(重置分页)
- */
- switchStatus(e) {
- const status = e.currentTarget.dataset.status;
- this.setData({
- activeStatus: status,
- pageNum: 1, // 切换筛选条件时重置页码
- hasMore: true, // 重置是否有更多数据
- filteredOrders: [] // 清空现有筛选数据
- }, () => {
- this.filterOrders();
- });
- },
-
- /**
- * 派单操作
- */
- dispatchOrder(e) {
- const orderId = e.currentTarget.dataset.id;
- wx.showModal({
- title: '派单确认',
- content: '请输入派单人员姓名',
- editable: true,
- placeholderText: '例如:李师傅',
- success: (res) => {
- if (res.confirm && res.content) {
- this.updateOrderStatus(orderId, 'processing', res.content);
- }
- }
- });
- },
-
- /**
- * 完成工单
- */
- completeOrder(e) {
- const orderId = e.currentTarget.dataset.id;
- wx.showModal({
- title: '完成确认',
- content: '是否确认该工单已完成?',
- success: (res) => {
- if (res.confirm) {
- this.updateOrderStatus(orderId, 'completed', wx.getStorageSync('userName') || '管理员');
- }
- }
- });
- },
-
- /**
- * 驳回工单
- */
- rejectOrder(e) {
- const orderId = e.currentTarget.dataset.id;
- wx.showModal({
- title: '驳回工单',
- content: '请输入驳回原因',
- editable: true,
- placeholderText: '例如:配件缺货、现场条件不满足等',
- success: (res) => {
- if (res.confirm && res.content) {
- // 更新工单状态为驳回,并记录驳回原因
- const { orderData } = this.data;
- const newOrders = orderData.map(item => {
- if (item.id === orderId) {
- return {
- ...item,
- status: 'rejected',
- operatorName: wx.getStorageSync('userName') || '管理员',
- rejectReason: res.content
- };
- }
- return item;
- });
-
- this.setData({ orderData: newOrders }, () => {
- this.filterOrders();
- wx.showToast({
- title: '工单已驳回',
- icon: 'success',
- duration: 1000
- });
- });
-
- // 实际项目中调用接口更新工单状态
- // this.updateOrderApi(orderId, 'rejected', res.content);
- }
- }
- });
- },
-
- /**
- * 更新工单状态(派单/完成)
- */
- updateOrderStatus(orderId, status, operatorName) {
- const { orderData } = this.data;
- const newOrders = orderData.map(item => {
- if (item.id === orderId) {
- return {
- ...item,
- status: status,
- operatorName: operatorName
- };
- }
- return item;
- });
-
- this.setData({ orderData: newOrders }, () => {
- this.filterOrders();
- wx.showToast({
- title: status === 'processing' ? '派单成功' : '工单已完成',
- icon: 'success',
- duration: 1000
- });
- });
-
- // 实际项目中调用接口更新工单状态
- // this.updateOrderApi(orderId, status, operatorName);
- },
-
- /**
- * 跳转工单详情页
- */
- toOrderDetail(e) {
- console.log(e);
- const order = e.currentTarget.dataset.order;
- if (!order.workorderId) return;
-
- // 跳转工单详情页(替换为你的详情页路径)
- wx.navigateTo({
- url: `/pages/orderdetails/index?id=${order.workorderId}`
- });
- },
-
- /**
- * 新建工单
- */
- createOrder() {
- // 跳转到新建工单页面
- wx.navigateTo({
- url: '/pages/create-order/create-order'
- });
- },
-
- /**
- * 实际项目中:调用接口更新工单状态
- */
- updateOrderApi(orderId, status, operatorName, rejectReason = '') {
- wx.showLoading({ title: '处理中...' });
- wx.request({
- url: `https://你的接口地址/work-order/${orderId}/status`,
- method: 'POST',
- data: {
- status,
- operatorName,
- rejectReason
- },
- success: (res) => {
- if (res.data.code === 200) {
- wx.showToast({ title: '操作成功', icon: 'success' });
- } else {
- wx.showToast({ title: res.data.msg || '操作失败', icon: 'none' });
- }
- },
- fail: () => {
- wx.showToast({ title: '网络错误', icon: 'none' });
- },
- complete: () => {
- wx.hideLoading();
- }
- });
- }
- });
|