333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import BaseService from "./baseService";
import { SysRoleAuthData } from "../entity/sys/core/sysRoleAuthData";
import { Raw } from "typeorm";
import { SysRoleAuth } from "../entity/sys/core/sysRoleAuth";
 
/**
 * 系统权限处理
 */
export default class Auth extends BaseService {
  //#region 获得权限条件 getAutWhere
  /**
   * 获得权限条件
   * @param name - your name
   */
  public async getAutWhere() {
    const { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let where: any = {};
    switch (body.menu_Id) {
      case 6: // Sys_MvcTableColumn
        return where;
      case 102: // Purchase_Order
      case 112: // Sale_Order 出库单
        if (!userInfo.isAdministrator) {
          // 获得仓库权限
          let _where = await this.getStorageAuth();
          where = Object.assign(where, _where);
          // 获得货主权限
          _where = await this.getConsignorAuth();
          where = Object.assign(where, _where);
          // 只看自己权限
          let authInfo = await this.dbRead.findOne(SysRoleAuth, {
            role_Id: userInfo.role_Id,
            menu_Id: body.menu_Id
          });
          if (authInfo && authInfo.authValue.indexOf("OnlySelf=1") >= 0) {
            where.createID = userInfo.user_Id;
          }
        }
        break;
      case 103: // Purchase_Enter
      case 301: // Purchase_Shelve
      case 104: // Purchase_Return
      case 269: // Purchase_Shelve
      case 1603: // Purchase_DamagedOrder
 
      case 380: // xSale_Order_Batch 生成波次
      case 248: // Sale_OrderPrint 波次查询
      case 268: // Sale_Outer 打包查询
      case 114: // Sale_Return 出库退货单
      case 302: // Sale_OrderPlan 出库计划
 
      case 385: // vBase_ProductPosition 库存明细
      case 155: // vBase_ProductPositionGroup 物料库存查询
      case 145: // xBase_ProductPositionGroup 货位库存查询
      case 271: // Base_ProductPositionHistory 库存监测记录
      case 272: // Base_ProductPlaceHolder 库存占位查询
      case 450: // Storage_PositionTransfer 货位转移查询
      case 147: // Storage_Check 库存盘点查询
      case 141: // Storage_Enter 其他入库
      case 142: // Storage_Outer 其他出库
      case 143: // Storage_PurchasePriceAdjust 库存成本调整
      case 228: // Storage_Adjust 库存调整
      case 1686: // Storage_ConsignorTransfer 货主过户
      case 1688: // Storage_ValidAdjust 库存有效期调整
 
      case 136: // Storage_AllocateApply 调拨申请单
      case 137: // Storage_AllocateEnter 调拨入库单
      case 153: // vStorage_AllocateApplyList 物料调拨统计
 
      case 1570: // vShelfLifeEarlyWarning 保质期预警
      case 1571: // vOldLibraryEarlyWarning 库龄预警
      case 1720: // vBase_ProductPosition_Lower 货位最低库存预警
      case 107: // vPurchase_EnterList 入库明细查询
      case 1602: // vPurchase_ShelveList 上架明细查询
      case 478: // vPurchase_EnterList_Report 收货入库报表
      case 480: // vPurchase_OrderList_Report 预到货单查询
      case 1548: // vPurchase_ReturnList 收货退货查询
 
      case 12: // vPurchase_OrderList 预到货明细查询
      case 203: // vBase_ProductInfo_StorageLower_Replenishment 库存下限转补货
      case 204: // vSale_OrderList_Replenishment 缺货订单转补货
      case 205: // Storage_Replenishment 补货单查询
      case 207: // Storage_ReplenishmentScan 补货扫描记录
 
      case 121: // vSale_OrderList 出库订单明细查询
      case 122: // vSale_OuterList 打包单明细查询
      case 123: // vSale_Outer_Case 打包装箱查询
      case 481: // vSale_SendList_Report 出货报表
      case 1551: // vSale_ReturnList 出库单退货查询
 
      case 216: // xStat_StorageDay 库存统计
      case 213: // Stat_StorageDay 每日物料库存查询
      case 215: // Stat_StorageDayDetails 每日库存明细查询
      case 1535: // vStorage_OuterList 其他出库明细查询
      case 1536: // vStorage_EnterList 其他入库明细查询
 
      case 1606: // vPurchase_Enter_Ranking 物料入库排名报表(年)
      case 1610: // vPurchase_Enter_RankByMonth 物料入库排名报表(月)
      case 1613: // vPurchase_Enter_RankByWeek 物料入库排名报表(周)
      case 1614: // vPurchase_Enter_RankByDay 物料库排名报表(天)
 
      case 1607: // vSale_Ranking 物料出库排名报表(年)
      case 1611: // vSale_RankByMonth 物料出库排名报表(月)
      case 1612: // vSale_RankByWeek 物料出库排名报表(周)
      case 1615: // vSale_RankByDay 物料出库排名报表(天)
 
      case 1718: // vSale_OrderPicking_WorkloadStatistics 拣货人员工作量统计
      case 1719: // vSale_OrderPickingList 拣货人员拣货明细
      case 1528: // cBase_ProductPositionGroup 生成盘点单
        if (!userInfo.isAdministrator) {
          // 获得仓库权限
          let _where = await this.getStorageAuth();
          where = Object.assign(where, _where);
          // 获得货主权限
          _where = await this.getConsignorAuth();
          where = Object.assign(where, _where);
        }
        break;
      case 119: // Sale_OrderMatching 配货记录查询
      case 164: // vPurchase_EnterList_Provider 供应商进货统计
      case 109: // vPurchase_EnterList_Product 物料进货统计
        if (!userInfo.isAdministrator) {
          // 获得货主权限
          let _where = await this.getConsignorAuth();
          where = Object.assign(where, _where);
        }
        break;
      case 258: // Base_Storage
      case 259: // Base_Position
      case 1561: // Base_StorageArea
        if (!userInfo.isAdministrator) {
          // 获得仓库权限
          let _where = await this.getStorageAuth();
          where = Object.assign(where, _where);
        }
        break;
      case 1793: // App_GoodsView 客户巡检
        if (userInfo.roleName === "客户") {
          // 客户只看自己的数据
          let _where = {
            createID: userInfo.user_Id
          };
          where = Object.assign(where, _where);
        }
        break;
      default:
        break;
    }
    //SaaS权限
    // let isSaaS = await ctx.helper.isSaaS();
    if (!body.noUserProduct_Id && body.menu_Id !== -1) {
      where["userProduct_Id"] = userInfo.userProduct_Id;
    }
 
    // 货主用户登录
    if (userInfo.userType === "consignor" && body.menu_Id != -1) {
      where["consignor_Id"] = userInfo.consignor_Id;
    }
 
    return where;
  }
  //#endregion
 
  //#region 获得下拉框权限
  /**
   * 获得下拉框权限
   */
  public async getDropDownWhere(dropDown_Id: number) {
    let where = "";
 
    switch (dropDown_Id) {
      case 797: // 货主ID
      case 897: // 目标货主
        // 获得货主权限
        where = await this.getConsignorAuth("string");
        break;
      case 31: // 仓库ID
        // 获得仓库权限
        where = await this.getStorageAuth("string");
        break;
      case 33: // 供应商
        // 获得供应商权限
        where = await this.getProviderAuth("string");
        break;
      default:
        where = "1=1";
        break;
    }
 
    return where;
  }
  //#endregion
 
  //#region getStorageAuth
  /**
   * 获取仓库权限
   * @param type 可选择值为string/object
   */
  public async getStorageAuth(type: string = "object") {
    let where: any;
    let userInfo = await this.userInfo;
    // 全部权限
    let allAuth = await this.dbRead.findOne(SysRoleAuthData, {
      dataType_Id: 2,
      user_Id: userInfo.user_Id,
      node_Id: 0,
      authValue: "1"
    });
    if (allAuth || userInfo.isAdministrator) {
      if (type === "string") {
        where = `userProduct_Id=${userInfo.userProduct_Id}`;
      } else {
        where = {};
        where["userProduct_Id"] = userInfo.userProduct_Id;
      }
    } else {
      if (type === "string") {
        if (userInfo.userType == "client") {
          // 客户端调用API不控制权限
          where = "";
        } else {
          where = `storage_Id in(SELECT Node_Id FROM dbo.Sys_RoleAuthData WHERE dataType_Id=2 AND AuthValue=1 AND user_Id=${userInfo.user_Id})`;
        }
      } else {
        where = {};
        where["storage_Id"] = Raw(
          () => `storage_Id in(SELECT Node_Id FROM dbo.Sys_RoleAuthData WHERE dataType_Id=2 AND AuthValue=1 AND user_Id=${userInfo.user_Id})`
        );
      }
    }
 
    return where;
  }
  //#endregion
 
  //#region getProviderAuth
  /**
   * 获取供应商权限
   * @param type 可选择值为string/object
   */
  public async getProviderAuth(type: string = "object") {
    let where: any;
    let userInfo = await this.userInfo;
    // 全部权限
    let allAuth = await this.dbRead.findOne(SysRoleAuthData, {
      dataType_Id: 3,
      user_Id: userInfo.user_Id,
      node_Id: 0,
      authValue: "1"
    });
    if (allAuth || userInfo.isAdministrator) {
      if (type === "string") {
        where = `userProduct_Id=${userInfo.userProduct_Id}`;
      } else {
        where = {};
        where["userProduct_Id"] = userInfo.userProduct_Id;
      }
    } else {
      if (type === "string") {
        if (userInfo.userType == "client") {
          // 客户端调用API不控制权限
          where = "";
        } else {
          where = `provider_Id in(SELECT Node_Id FROM dbo.Sys_RoleAuthData WHERE dataType_Id=3 AND AuthValue=1 AND user_Id=${userInfo.user_Id})`;
        }
      } else {
        where = {};
        where["provider_Id"] = Raw(
          () => `provider_Id in(SELECT Node_Id FROM dbo.Sys_RoleAuthData WHERE dataType_Id=3 AND AuthValue=1 AND user_Id=${userInfo.user_Id})`
        );
      }
    }
 
    return where;
  }
  //#endregion
 
  //#region getConsignorAuth
  /**
   * 获取货主权限
   * @param type 可选择值为string/object
   */
  public async getConsignorAuth(type: string = "object") {
    let where: any;
    let userInfo = await this.userInfo;
    // 全部权限
    let allAuth = await this.dbRead.findOne(SysRoleAuthData, {
      dataType_Id: 1,
      user_Id: userInfo.user_Id,
      node_Id: 0,
      authValue: "1"
    });
    if (allAuth || userInfo.isAdministrator) {
      if (type === "string") {
        where = `userProduct_Id=${userInfo.userProduct_Id}`;
      } else {
        where = {};
        where["userProduct_Id"] = userInfo.userProduct_Id;
      }
    } else {
      if (type === "string") {
        if (userInfo.userType == "client") {
          // 客户端调用API不控制权限
          where = "";
        } else {
          where = `consignor_Id in(SELECT Node_Id FROM dbo.Sys_RoleAuthData WHERE dataType_Id=1 AND AuthValue=1 AND user_Id=${userInfo.user_Id})`;
        }
      } else {
        where = {};
        where["consignor_Id"] = Raw(
          () => `consignor_Id in(SELECT Node_Id FROM dbo.Sys_RoleAuthData WHERE dataType_Id=1 AND AuthValue=1 AND user_Id=${userInfo.user_Id})`
        );
      }
    }
 
    return where;
  }
  //#endregion
}