祖安之光
2025-08-07 ad5c4cc086a40708e66040574ff1d465b66304b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
 
// 云函数入口函数
exports.main = async (event, context) => {
  try {
    const db = cloud.database()
    const { pageNum = 1, pageSize = 10 } = event
 
    // 获取总数
    const countRes = await db.collection('productFlowList').where({
      create_id: event.userId
    }).count()
    const total = countRes.total
 
    // 获取分页数据
    const listRes = await db.collection('productFlowList').aggregate()
      .lookup({
        from: "userList",
        localField: "create_id",
        foreignField: "id",
        as: "user"
      })
      .unwind({
        path: "$user",
        preserveNullAndEmptyArrays: true
      })
      .lookup({
        from: "departmentList",
        localField: "user.depart_id",
        foreignField: "id",
        as: "user.department"
      })
      .unwind({
        path: "$user.department",
        preserveNullAndEmptyArrays: true
      })
      .lookup({
        from: "companyList",
        localField: "user.company_id",
        foreignField: "id",
        as: "user.company"
      })
      .unwind({
        path: "$user.company",
        preserveNullAndEmptyArrays: true
      })
      .lookup({
        from: "productBasic",
        localField: "basic_id",
        foreignField: "id",
        as: "productBasic"
      })
      .unwind({
        path: "$productBasic",
        preserveNullAndEmptyArrays: true
      })
      .lookup({
        from: "productList",
        localField: "product_id",
        foreignField: "id",
        as: "codeTemp"
      })
      .addFields({
        code: { $arrayElemAt: ["$codeTemp.code", 0] }
      })
      .project({
        codeTemp: 0
      })
      .match({
        create_id: event.userId
      })
      .addFields(
        { sortTime: { $ifNull: [{ $toDate: "$update_time" }, { $toDate: "$create_time" }] } })
      .sort({ sortTime: -1 })
      .skip((pageNum - 1) * pageSize)
      .limit(pageSize)
      .end()
    return {
      list: listRes.list,
      total,
      pageNum: Number(pageNum),
      pageSize: Number(pageSize),
      hasMore: (pageNum * pageSize) < total
    }
  } catch (err) {
    console.error('云函数异常:', err)
    return {
      code: 500,
      message: '服务器异常',
      error: err.message
    }
  }
}