import jsonrpcFunc from '../../../ulti/jsonrpc' Page({ data: { loginInfo: '', //权限验证信息 combinedOrders: [], //销售数据汇总 currentPage: 1, //当前第几页 pageSize: 10, //一页多少数据 totalRecords: 0, //整个模型目前有多少条记录 orderlineIDs: [], //销售订单行数据 isloading: 50 //加载进度条百分比 }, onLoad(query) { // 页面加载 const loginInfo = dd.getStorageSync({ key: 'loginInfo' }).data; if (loginInfo && loginInfo.apiurl && loginInfo.databaseName && loginInfo.username && loginInfo.password && loginInfo.uid > 0) { this.setData({ loginInfo }); // console.log('销售页得到的本地信息', loginInfo); //执行获取销售订单第一页数据分页第一页默认 this.fetchData(1); } else { // console.log('本地缓存中未获取到登录信息'); } }, onReady() { // 页面加载完成 }, onShow() { // 页面显示 }, onHide() { // 页面隐藏 }, onUnload() { // 页面被关闭 }, onTitleClick() { // 标题被点击 }, onPullDownRefresh() { // 页面被下拉 }, onReachBottom() { // 页面被拉到底部 }, onShareAppMessage() { // 返回自定义分享信息 return { title: 'My App', desc: 'My App description', path: 'pages/index/index', }; }, fetchData(page) { const { loginInfo, pageSize } = this.data; const offset = (page - 1) * pageSize; // 调用 search_count 方法获取总记录数 const saleOrderCountData = { "jsonrpc": "2.0", "method": "call", "id": 2, "params": { "service": "object", "method": "execute_kw", "args": [ loginInfo.databaseName, loginInfo.uid, loginInfo.password, 'sale.order', "search_count", [ [] ] ] } }; jsonrpcFunc(loginInfo, saleOrderCountData) .then((count) => { this.setData({ totalRecords: count }); // console.log('销售订单总记录数:', count); const saleOrderData = { "jsonrpc": "2.0", "method": "call", "id": 2, "params": { "service": "object", "method": "execute_kw", "args": [ loginInfo.databaseName, loginInfo.uid, loginInfo.password, 'sale.order', "search_read", [], { fields: [], "limit": pageSize, "offset": offset, "order": "id DESC" } ] } }; return jsonrpcFunc(loginInfo, saleOrderData); }) .then((salesOrderData) => { this.setData({ combinedOrders: salesOrderData.map(order => ({ ...order, orderLines: [] })) }); // console.log('销售订单数据:', salesOrderData); const orderlineIDs = []; salesOrderData.forEach(order => { if (order.order_line && Array.isArray(order.order_line)) { order.order_line.forEach(id => { if (!orderlineIDs.includes(id)) { orderlineIDs.push(id); } }); } }); this.setData({ orderlineIDs }); // console.log('获取到的销售订单行 ID:', orderlineIDs); if (orderlineIDs.length > 0) { const domain = orderlineIDs; const saleOrderLineData = { "jsonrpc": "2.0", "method": "call", "id": 2, "params": { "service": "object", "method": "execute_kw", "args": [ loginInfo.databaseName, loginInfo.uid, loginInfo.password, 'sale.order.line', "read", [domain], { fields: [], } ] } }; // console.log('saleOrderLineData ', saleOrderLineData); return jsonrpcFunc(loginInfo, saleOrderLineData); } return null; }) .then((salesOrderLineData) => { if (salesOrderLineData) { const { combinedOrders } = this.data; salesOrderLineData.forEach(orderLine => { const orderId = orderLine.order_id && orderLine.order_id[0]; const targetOrder = combinedOrders.find(order => order.id === orderId); if (targetOrder) { targetOrder.orderLines.push(orderLine); } }); this.setData({ combinedOrders }); // console.log('销售订单行数据:', salesOrderLineData); // console.log('合并后的订单数据:', combinedOrders); // 提取所有 fineCode_3 的 ID const fineCode3Ids = []; combinedOrders.forEach(order => { order.orderLines.forEach(orderLine => { if (orderLine.fineCode_3 && Array.isArray(orderLine.fineCode_3)) { orderLine.fineCode_3.forEach(id => { if (!fineCode3Ids.includes(id)) { fineCode3Ids.push(id); } }); } }); }); if (fineCode3Ids.length > 0) { const fineCode3Data = { "jsonrpc": "2.0", "method": "call", "id": 2, "params": { "service": "object", "method": "execute_kw", "args": [ loginInfo.databaseName, loginInfo.uid, loginInfo.password, // 这里需要替换为 fineCode_3 关联的模型名称 'finecode_xima_l', "read", [fineCode3Ids], { fields: ['name'] } ] } }; return jsonrpcFunc(loginInfo, fineCode3Data); } } return null; }) .then((fineCode3Data) => { if (fineCode3Data) { const { combinedOrders } = this.data; const fineCode3NameMap = {}; fineCode3Data.forEach(item => { fineCode3NameMap[item.id] = item.name; }); combinedOrders.forEach(order => { order.orderLines.forEach(orderLine => { if (orderLine.fineCode_3 && Array.isArray(orderLine.fineCode_3)) { orderLine.fineCode3Names = orderLine.fineCode_3.map(id => fineCode3NameMap[id]); } }); }); this.setData({ combinedOrders, isloading: 100 }); // console.log('合并后的订单数据(包含 fineCode_3 名称):', combinedOrders); } }) .catch((error) => { console.error('请求数据时出错:', error); }); }, //下一页逻辑 handleNextPage() { const { currentPage, totalRecords, pageSize } = this.data; const totalPages = Math.ceil(totalRecords / pageSize); if (currentPage < totalPages) { this.setData({ currentPage: currentPage + 1 }); this.fetchData(currentPage + 1); } }, // //上一页逻辑 handlePreviousPage() { const { currentPage } = this.data; if (currentPage > 1) { this.setData({ currentPage: currentPage - 1 }); this.fetchData(currentPage - 1); } }, // //点击卡片触发这条记录保存 handleTap(e) { const orderId = e.currentTarget.dataset.info; const { combinedOrders } = this.data; const selectedOrder = combinedOrders.find(order => order.id === orderId); if (selectedOrder) { const objectKey = `saleOrderlines`; // const objectKey = `order_${orderId}`; dd.setStorage({ key: objectKey, data: selectedOrder, success: function () { dd.navigateTo({ url: `/pages/deal/saleOrder/kanban/from/saleOrderFrom?objectKey=${objectKey}` }); }, fail: function (err) { console.error('存储数据到本地存储失败:', err); } }); } }, // });