Files

359 lines
8.2 KiB
TypeScript
Raw Normal View History

2023-08-31 16:24:37 +08:00
import { getInfo } from "../../api/login";
2023-08-30 17:27:21 +08:00
import { stockLogList, stockStatistics, storageList } from "../../api/stock";
import httpClient from "../../utils/request";
// pages/stock/stock.ts
Page({
/**
*
*/
data: {
2023-08-31 16:24:37 +08:00
permissions: [],
2023-08-30 17:27:21 +08:00
authToken: undefined,
stockType: 0, // 库存类型
// 统计数据
statisticsData: {
total: {
label: "产品总数",
value: 0,
},
in: {
label: "当月入库",
value: 0,
},
out: {
label: "当月出库",
value: 0,
},
},
inStockQuery: {
pageNum: 1,
pageSize: 10,
},
outStockQuery: {
pageNum: 1,
pageSize: 10,
},
storageQuery: {
pageNum: 1,
pageSize: 10,
},
storageList: [], // 库存列表
inStockList: [], // 入库列表
outStockList: [], // 出库列表
storageLoading: false,
inStockLoading: false,
outStockLoading: false,
storageCompleted: false,
inStockCompleted: false,
outStockCompleted: false,
},
onTabsChange(e: any) {
const { value: stockType } = e.detail;
this.setData({
stockType,
});
if (stockType > 0) {
if (
(stockType == 1 && this.data.inStockList.length == 0) ||
(stockType == 2 && this.data.outStockList.length == 0)
) {
this.getStockIOList();
}
} else {
if (this.data.storageList.length === 0) {
this.getStorageList();
}
}
},
async loadData() {
const authToken = getApp().globalData.authToken;
this.setData({
authToken,
});
if (!authToken) return;
const { stockType } = this.data;
if (stockType == 0) {
await this.getStorageList();
} else {
await this.getStockIOList();
}
await this.loadStatisticData();
},
/**
*
*/
async getStockIOList() {
const { inStockQuery, outStockQuery, stockType } = this.data;
let query;
if (stockType == 1) {
this.setData({
inStockLoading: true,
});
query = inStockQuery;
} else if (stockType == 2) {
this.setData({
outStockLoading: true,
});
query = outStockQuery;
} else {
return;
}
try {
const resp: any = await stockLogList({
type: stockType,
pageNum: query.pageNum,
pageSize: query.pageSize,
});
let completed = false;
if (stockType == 1) {
if (
Math.ceil(resp.total / inStockQuery.pageSize) == inStockQuery.pageNum
) {
completed = true;
}
this.setData({
inStockCompleted: completed,
// @ts-ignore
inStockList: [
...this.data.inStockList,
...resp.rows.map((el: any) => ({
...el,
provePic:
el.provePic?.split(",").map((img: string) => {
if (img.startsWith("http://") || img.startsWith("https://")) {
return img;
} else {
return `${httpClient.baseUrl}${img}`;
}
}) ?? [],
})),
],
});
if (!completed) {
this.setData({
["inStockQuery.pageNum"]: inStockQuery.pageNum + 1,
});
}
} else if (stockType == 2) {
if (
Math.ceil(resp.total / outStockQuery.pageSize) ==
outStockQuery.pageNum
) {
completed = true;
}
this.setData({
outStockCompleted: completed,
// @ts-ignore
outStockList: [
...this.data.outStockList,
...resp.rows.map((el: any) => ({
...el,
provePic:
el.provePic?.split(",").map((img: string) => {
if (img.startsWith("http://") || img.startsWith("https://")) {
return img;
} else {
return `${httpClient.baseUrl}${img}`;
}
}) ?? [],
})),
],
});
if (!completed) {
this.setData({
["outStockQuery.pageNum"]: outStockQuery.pageNum + 1,
});
}
}
} catch (error) {
console.log(error);
}
if (this.data.stockType == 1) {
this.setData({
inStockLoading: false,
});
} else if (this.data.stockType == 2) {
this.setData({
outStockLoading: false,
});
}
},
/**
*
*/
async getStorageList() {
this.setData({
storageLoading: false,
});
const { storageQuery } = this.data;
try {
const resp: any = await storageList(storageQuery);
let completed = false;
if (
Math.ceil(resp.total / storageQuery.pageSize) == storageQuery.pageNum
) {
completed = true;
}
this.setData({
// @ts-ignore
storageList: [...this.data.storageList, ...resp.rows],
storageCompleted: completed,
});
if (!completed) {
this.setData({
[`storageQuery.pageNum`]: storageQuery.pageNum + 1,
});
}
} catch (error) {}
this.setData({
storageLoading: false,
});
},
/**
*
*/
async loadStatisticData() {
const resp: any = await stockStatistics();
this.setData({
["statisticsData.in.value"]: resp.data.in,
["statisticsData.out.value"]: resp.data.out,
["statisticsData.total.value"]: resp.data.total,
});
},
/**
*
*/
loadMoreStorage() {
console.log("库存到底了");
const { storageLoading, storageCompleted } = this.data;
if (storageLoading || storageCompleted) {
return;
}
this.getStorageList();
},
/**
*
*/
loadMoreInStock() {
const { inStockLoading, inStockCompleted } = this.data;
console.log(inStockLoading, inStockCompleted);
if (inStockLoading || inStockCompleted) {
return;
}
this.getStockIOList();
},
/**
*
*/
loadMoreOutStock() {
const { outStockLoading, outStockCompleted } = this.data;
if (outStockLoading || outStockCompleted) {
return;
}
this.getStockIOList();
},
handleAddStock() {
const { stockType } = this.data;
wx.navigateTo({
url: `/pages/add-stock/add-stock?type=${stockType}`,
});
},
2023-08-31 16:24:37 +08:00
loadUserInfo() {
const permissions = getApp().globalData.permissions;
if (permissions) {
this.setData({
permissions,
});
} else {
const token = getApp().globalData.authToken;
getInfo(token).then((resp: any) => {
this.setData({
permissions: resp.permissions,
});
getApp().globalData.permissions = resp.permissions;
});
}
},
2023-08-30 17:27:21 +08:00
/**
* --
*/
onLoad() {
this.loadData();
2023-08-31 16:24:37 +08:00
this.loadUserInfo();
2023-08-30 17:27:21 +08:00
},
/**
*
*/
async handleRefresh() {
this.setData({
["inStockQuery.pageNum"]: 1,
["outStockQuery.pageNum"]: 1,
["storageQuery.pageNum"]: 1,
storageLoading: false,
inStockLoading: false,
outStockLoading: false,
storageCompleted: false,
inStockCompleted: false,
outStockCompleted: false,
inStockList: [],
outStockList: [],
storageList: [],
});
await this.loadData();
wx.stopPullDownRefresh();
},
/**
* --
*/
onReady() {},
/**
* --
*/
onShow() {},
/**
* --
*/
onHide() {},
/**
* --
*/
onUnload() {},
/**
* --
*/
onPullDownRefresh() {
this.handleRefresh();
},
/**
*
*/
onReachBottom() {
const { stockType } = this.data;
if (stockType == 0) {
this.loadMoreStorage();
} else if (stockType == 1) {
this.loadMoreInStock();
} else if (stockType == 2) {
this.loadMoreOutStock();
}
},
/**
*
*/
onShareAppMessage() {},
});