cgserver 6.0.6 → 6.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/dist/lib/AI/AiObject.js +10 -0
  2. package/dist/lib/AI/Astar.js +133 -0
  3. package/dist/lib/AI/BehaviorAI.js +307 -0
  4. package/dist/lib/AI/Entity.js +33 -0
  5. package/dist/lib/AI/Point.js +68 -0
  6. package/dist/lib/AI/TriggerMgr.js +201 -0
  7. package/dist/lib/Config/Config.js +37 -0
  8. package/dist/lib/Config/FrameworkConfig.js +234 -0
  9. package/dist/lib/Config/ServerConfig.js +18 -0
  10. package/dist/lib/Config/_error_.js +35 -0
  11. package/dist/lib/Core/Core.js +561 -0
  12. package/dist/lib/Core/Timer.js +116 -0
  13. package/dist/lib/Database/BaseMongoService.js +79 -0
  14. package/dist/lib/Database/BaseService.js +281 -0
  15. package/dist/lib/Database/Decorator/AutoIncrement.js +11 -0
  16. package/dist/lib/Database/Decorator/DBCache.js +64 -0
  17. package/dist/lib/Database/Decorator/NotNull.js +11 -0
  18. package/dist/lib/Database/Decorator/PrimaryKey.js +11 -0
  19. package/dist/lib/Database/Decorator/Property.js +61 -0
  20. package/dist/lib/Database/Decorator/Table.js +15 -0
  21. package/dist/lib/Database/Decorator/Type.js +20 -0
  22. package/dist/lib/Database/MSSqlManager.js +43 -0
  23. package/dist/lib/Database/MongoManager.js +401 -0
  24. package/dist/lib/Database/MysqlManager.js +180 -0
  25. package/dist/lib/Database/RedisManager.js +408 -0
  26. package/dist/lib/Logic/CacheTool.js +73 -0
  27. package/dist/lib/Logic/HttpTool.js +58 -0
  28. package/dist/lib/Logic/Log.js +77 -0
  29. package/dist/lib/Service/AccountService.js +473 -0
  30. package/dist/lib/Service/MongoAccountService.js +396 -0
  31. package/dist/lib/Service/MongoCacheService.js +27 -0
  32. package/dist/lib/Service/MongoUserService.js +93 -0
  33. package/dist/lib/Service/UserService.js +184 -0
  34. package/dist/lib/Service/ini.js +35 -0
  35. package/dist/lib/SocketServer/IClientWebSocket.js +71 -0
  36. package/dist/lib/SocketServer/IServerWebSocket.js +41 -0
  37. package/dist/lib/SocketServer/ISocketServer.js +190 -0
  38. package/dist/lib/SocketServer/IWebSocket.js +223 -0
  39. package/dist/lib/SocketServer/ProtoFilter/GoogleProtoFilter.js +57 -0
  40. package/dist/lib/SocketServer/ProtoFilter/IProtoFilter.js +8 -0
  41. package/dist/lib/SocketServer/ProtoFilter/JsonProtoFilter.js +29 -0
  42. package/dist/lib/SocketServer/ProtoFilter/ProtoFactory.js +31 -0
  43. package/dist/lib/ThirdParty/AlipayTool.js +122 -0
  44. package/dist/lib/ThirdParty/Alisms.js +46 -0
  45. package/dist/lib/ThirdParty/AppleTool.js +234 -0
  46. package/dist/lib/ThirdParty/EmailTool.js +33 -0
  47. package/dist/lib/ThirdParty/OpenSocial.js +32 -0
  48. package/dist/lib/ThirdParty/QQTool.js +115 -0
  49. package/dist/lib/ThirdParty/QiniuTool.js +21 -0
  50. package/dist/lib/ThirdParty/WechatOATool.js +61 -0
  51. package/dist/lib/ThirdParty/WechatTool.js +74 -0
  52. package/dist/lib/WebServer/Controller/BaseController.js +112 -0
  53. package/dist/lib/WebServer/Controller/BaseUserController.js +168 -0
  54. package/dist/lib/WebServer/Controller/MongoBaseUserController.js +168 -0
  55. package/dist/lib/WebServer/Decorator/AdminValidate.js +19 -0
  56. package/dist/lib/WebServer/Decorator/AuthorityValidate.js +22 -0
  57. package/dist/lib/WebServer/Decorator/CreatorValidate.js +19 -0
  58. package/dist/lib/WebServer/Decorator/JsonAdminValidate.js +15 -0
  59. package/dist/lib/WebServer/Decorator/JsonAuthorityValidate.js +20 -0
  60. package/dist/lib/WebServer/Decorator/JsonCreatorValidate.js +19 -0
  61. package/dist/lib/WebServer/Engine/ControllerManager.js +112 -0
  62. package/dist/lib/WebServer/Engine/Engine.js +135 -0
  63. package/dist/lib/WebServer/Engine/RazorJs.js +544 -0
  64. package/dist/lib/WebServer/Engine/Request.js +214 -0
  65. package/dist/lib/WebServer/Engine/Response.js +94 -0
  66. package/dist/lib/WebServer/IWebServer.js +49 -0
  67. package/dist/lib/cgserver.js +153 -0
  68. package/dist/{AI → types/AI}/AiObject.d.ts +0 -0
  69. package/dist/{AI → types/AI}/Astar.d.ts +0 -0
  70. package/dist/{AI → types/AI}/BehaviorAI.d.ts +0 -0
  71. package/dist/{AI → types/AI}/Entity.d.ts +0 -0
  72. package/dist/{AI → types/AI}/Point.d.ts +0 -0
  73. package/dist/{AI → types/AI}/TriggerMgr.d.ts +0 -0
  74. package/dist/{Config → types/Config}/Config.d.ts +0 -0
  75. package/dist/{Config → types/Config}/FrameworkConfig.d.ts +0 -0
  76. package/dist/{Config → types/Config}/ServerConfig.d.ts +0 -0
  77. package/dist/{Config → types/Config}/_error_.d.ts +0 -0
  78. package/dist/{Core → types/Core}/Core.d.ts +0 -0
  79. package/dist/{Core → types/Core}/Timer.d.ts +0 -0
  80. package/dist/{Database → types/Database}/BaseMongoService.d.ts +0 -0
  81. package/dist/{Database → types/Database}/BaseService.d.ts +0 -0
  82. package/dist/{Database → types/Database}/Decorator/AutoIncrement.d.ts +0 -0
  83. package/dist/{Database → types/Database}/Decorator/DBCache.d.ts +0 -0
  84. package/dist/{Database → types/Database}/Decorator/NotNull.d.ts +0 -0
  85. package/dist/{Database → types/Database}/Decorator/PrimaryKey.d.ts +0 -0
  86. package/dist/{Database → types/Database}/Decorator/Property.d.ts +0 -0
  87. package/dist/{Database → types/Database}/Decorator/Table.d.ts +0 -0
  88. package/dist/{Database → types/Database}/Decorator/Type.d.ts +0 -0
  89. package/dist/{Database → types/Database}/MSSqlManager.d.ts +0 -0
  90. package/dist/{Database → types/Database}/MongoManager.d.ts +0 -0
  91. package/dist/{Database → types/Database}/MysqlManager.d.ts +0 -0
  92. package/dist/{Database → types/Database}/RedisManager.d.ts +0 -0
  93. package/dist/{Logic → types/Logic}/CacheTool.d.ts +0 -0
  94. package/dist/{Logic → types/Logic}/HttpTool.d.ts +0 -0
  95. package/dist/{Logic → types/Logic}/Log.d.ts +0 -0
  96. package/dist/{Service → types/Service}/AccountService.d.ts +0 -0
  97. package/dist/{Service → types/Service}/MongoAccountService.d.ts +0 -0
  98. package/dist/{Service → types/Service}/MongoCacheService.d.ts +0 -0
  99. package/dist/{Service → types/Service}/MongoUserService.d.ts +0 -0
  100. package/dist/{Service → types/Service}/UserService.d.ts +0 -0
  101. package/dist/{Service → types/Service}/ini.d.ts +0 -0
  102. package/dist/{SocketServer → types/SocketServer}/IClientWebSocket.d.ts +0 -0
  103. package/dist/{SocketServer → types/SocketServer}/IServerWebSocket.d.ts +0 -0
  104. package/dist/{SocketServer → types/SocketServer}/ISocketServer.d.ts +0 -0
  105. package/dist/{SocketServer → types/SocketServer}/IWebSocket.d.ts +0 -0
  106. package/dist/{SocketServer → types/SocketServer}/ProtoFilter/GoogleProtoFilter.d.ts +0 -0
  107. package/dist/{SocketServer → types/SocketServer}/ProtoFilter/IProtoFilter.d.ts +0 -0
  108. package/dist/{SocketServer → types/SocketServer}/ProtoFilter/JsonProtoFilter.d.ts +0 -0
  109. package/dist/{SocketServer → types/SocketServer}/ProtoFilter/ProtoFactory.d.ts +0 -0
  110. package/dist/{ThirdParty → types/ThirdParty}/AlipayTool.d.ts +0 -0
  111. package/dist/{ThirdParty → types/ThirdParty}/Alisms.d.ts +0 -0
  112. package/dist/{ThirdParty → types/ThirdParty}/AppleTool.d.ts +0 -0
  113. package/dist/{ThirdParty → types/ThirdParty}/EmailTool.d.ts +0 -0
  114. package/dist/{ThirdParty → types/ThirdParty}/OpenSocial.d.ts +0 -0
  115. package/dist/{ThirdParty → types/ThirdParty}/QQTool.d.ts +0 -0
  116. package/dist/{ThirdParty → types/ThirdParty}/QiniuTool.d.ts +0 -0
  117. package/dist/{ThirdParty → types/ThirdParty}/WechatOATool.d.ts +0 -0
  118. package/dist/{ThirdParty → types/ThirdParty}/WechatTool.d.ts +0 -0
  119. package/dist/{WebServer → types/WebServer}/Controller/BaseController.d.ts +0 -0
  120. package/dist/{WebServer → types/WebServer}/Controller/BaseUserController.d.ts +0 -0
  121. package/dist/{WebServer → types/WebServer}/Controller/MongoBaseUserController.d.ts +0 -0
  122. package/dist/{WebServer → types/WebServer}/Decorator/AdminValidate.d.ts +0 -0
  123. package/dist/{WebServer → types/WebServer}/Decorator/AuthorityValidate.d.ts +0 -0
  124. package/dist/{WebServer → types/WebServer}/Decorator/CreatorValidate.d.ts +0 -0
  125. package/dist/{WebServer → types/WebServer}/Decorator/JsonAdminValidate.d.ts +0 -0
  126. package/dist/{WebServer → types/WebServer}/Decorator/JsonAuthorityValidate.d.ts +0 -0
  127. package/dist/{WebServer → types/WebServer}/Decorator/JsonCreatorValidate.d.ts +0 -0
  128. package/dist/{WebServer → types/WebServer}/Engine/ControllerManager.d.ts +0 -0
  129. package/dist/{WebServer → types/WebServer}/Engine/Engine.d.ts +0 -0
  130. package/dist/{WebServer → types/WebServer}/Engine/RazorJs.d.ts +0 -0
  131. package/dist/{WebServer → types/WebServer}/Engine/Request.d.ts +0 -0
  132. package/dist/{WebServer → types/WebServer}/Engine/Response.d.ts +0 -0
  133. package/dist/{WebServer → types/WebServer}/IWebServer.d.ts +0 -0
  134. package/dist/{cgserver.d.ts → types/cgserver.d.ts} +0 -0
  135. package/package.json +3 -2
  136. package/dist/cgserver.js +0 -1037
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WechatTool = exports.GWechatTool = exports.WechatUserInfo = void 0;
4
+ const _ = require("underscore");
5
+ const URLEncode = require("urlencode");
6
+ const HttpTool_1 = require("../Logic/HttpTool");
7
+ const FrameworkConfig_1 = require("../Config/FrameworkConfig");
8
+ const Log_1 = require("../Logic/Log");
9
+ class WechatUserInfo {
10
+ openid = "OPENID";
11
+ nickname = "NICKNAME";
12
+ sex = 1;
13
+ province = "PROVINCE";
14
+ city = "CITY";
15
+ country = "COUNTRY";
16
+ headimgurl = "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0";
17
+ privilege = [
18
+ "PRIVILEGE1",
19
+ "PRIVILEGE2"
20
+ ];
21
+ unionid = "o6_bmasdasdsad6_2sgVt7hMZOPfL";
22
+ }
23
+ exports.WechatUserInfo = WechatUserInfo;
24
+ exports.GWechatTool = null;
25
+ class WechatTool {
26
+ /**
27
+ * 获取获取code的url
28
+ */
29
+ getAuthCodeUrl() {
30
+ let appid = "wx80f0f10fe1304e9d";
31
+ let url = "https://open.weixin.qq.com/connect/qrconnect?appid=" + FrameworkConfig_1.GFCfg.third_cfg.wechat.app_id;
32
+ url += "&redirect_uri=" + URLEncode.encode(FrameworkConfig_1.GFCfg.third_cfg.wechat.redirect_uri);
33
+ url += "&response_type=code&scope=snsapi_login";
34
+ //必须 client端的状态值。用于第三方应用防止CSRF攻击,成功授权后回调时会原样带回。请务必严格按照流程检查用户与state参数状态的绑定。
35
+ let state = _.random(1000000, 9999999);
36
+ url += "&state=" + state;
37
+ return url;
38
+ }
39
+ async getAccessInfo(auth_code) {
40
+ if (!auth_code) {
41
+ return null;
42
+ }
43
+ let url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + FrameworkConfig_1.GFCfg.third_cfg.wechat.app_id + "&secret=" + FrameworkConfig_1.GFCfg.third_cfg.wechat.app_key + "&code=" + auth_code + "&grant_type=authorization_code";
44
+ let rs = await HttpTool_1.GHttpTool.httpRequest(url);
45
+ /*
46
+ {
47
+ "access_token":"ACCESS_TOKEN",
48
+ "expires_in":7200,
49
+ "refresh_token":"REFRESH_TOKEN",
50
+ "openid":"OPENID",
51
+ "scope":"SCOPE",
52
+ "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
53
+ }
54
+ {"errcode":40029,"errmsg":"invalid code"}
55
+ */
56
+ if (rs.body && rs.body.access_token) {
57
+ return rs.body;
58
+ }
59
+ else {
60
+ Log_1.GLog.error(rs.body);
61
+ }
62
+ return null;
63
+ }
64
+ async getUserInfo(access_token, openid) {
65
+ let url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid;
66
+ let rs = await HttpTool_1.GHttpTool.httpRequest(url);
67
+ if (rs.body) {
68
+ return rs.body;
69
+ }
70
+ return null;
71
+ }
72
+ }
73
+ exports.WechatTool = WechatTool;
74
+ exports.GWechatTool = new WechatTool();
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseController = void 0;
4
+ class BaseController {
5
+ _request = null;
6
+ get request() {
7
+ return this._request;
8
+ }
9
+ _response = null;
10
+ get response() {
11
+ return this._response;
12
+ }
13
+ //模块名称
14
+ _module = "";
15
+ _ctr_name = "";
16
+ get ctrName() {
17
+ return this._ctr_name;
18
+ }
19
+ _engine = null;
20
+ get postData() {
21
+ if (!this._request) {
22
+ return {};
23
+ }
24
+ return this._request.postData || {};
25
+ }
26
+ get paramData() {
27
+ if (!this._request) {
28
+ return {};
29
+ }
30
+ return this._request.params || {};
31
+ }
32
+ constructor(req, res, engine) {
33
+ this._engine = engine;
34
+ this._request = req;
35
+ this._response = res;
36
+ //静态控制器,传过来全是空
37
+ if (this._request) {
38
+ this._request.debugInfo();
39
+ }
40
+ }
41
+ async init() {
42
+ }
43
+ /**
44
+ * 方便static ctr使用
45
+ * @param req
46
+ * @param res
47
+ * @param engine
48
+ */
49
+ initStatic(req, res, engine) {
50
+ this._engine = engine;
51
+ this._request = req;
52
+ this._response = res;
53
+ this._request.debugInfo();
54
+ }
55
+ //填充每个页面需要的通用数据
56
+ _init_data(model) {
57
+ model = model || {};
58
+ model.webName = this._engine.cfg.web_name;
59
+ return { model };
60
+ }
61
+ showJson(model) {
62
+ this._response.renderJson(model, this._request.headers.origin);
63
+ }
64
+ show(model) {
65
+ let html = this._engine.getRenderHtml(this._request, this._response, this._init_data(model));
66
+ this._response.renderHtml(html);
67
+ }
68
+ showText(text, noMeta) {
69
+ if (!noMeta) {
70
+ text = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />" + text;
71
+ }
72
+ this._response.renderHtml(text);
73
+ }
74
+ redirect(module, controller, action, params) {
75
+ module = module || this._module;
76
+ controller = controller || this._ctr_name;
77
+ let url = this.parseFullUrl(module, controller, action);
78
+ params = params || {};
79
+ let str_p = "";
80
+ for (let key in params) {
81
+ str_p += "&" + key + "=" + params[key];
82
+ }
83
+ if (str_p.length > 0) {
84
+ str_p = "?" + str_p.substring(1);
85
+ }
86
+ url += str_p;
87
+ this._response.redirect(url);
88
+ }
89
+ parseFullUrl(module, controller, action) {
90
+ if (typeof action === "undefined" || !action) {
91
+ action = "index";
92
+ if (this._engine.cfg.routs.defaults.action == action) {
93
+ action = null;
94
+ }
95
+ }
96
+ let url = "";
97
+ if (this._engine.cfg.routs.onlyModule) {
98
+ url = this._request.root + "/" + controller;
99
+ }
100
+ else {
101
+ url = this._request.root + "/" + module + "/" + controller;
102
+ }
103
+ if (action) {
104
+ url += "/" + action;
105
+ }
106
+ return url;
107
+ }
108
+ get remoteHost() {
109
+ return this._request.remoteHost;
110
+ }
111
+ }
112
+ exports.BaseController = BaseController;
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseUserController = void 0;
4
+ const BaseController_1 = require("./BaseController");
5
+ const UserService_1 = require("../../Service/UserService");
6
+ const CacheTool_1 = require("../../Logic/CacheTool");
7
+ const FrameworkConfig_1 = require("../../Config/FrameworkConfig");
8
+ const RedisManager_1 = require("../../Database/RedisManager");
9
+ const MongoCacheService_1 = require("../../Service/MongoCacheService");
10
+ const ini_1 = require("../../Service/ini");
11
+ class BaseUserController extends BaseController_1.BaseController {
12
+ _user_session_id = "user_session_id";
13
+ _self_user = null;
14
+ _user = null; //网页内容的所属,比如,查看别人的博客,那这个信息就是作者的
15
+ _session_id = null;
16
+ get selfUser() {
17
+ return this._self_user;
18
+ }
19
+ get isSelf() {
20
+ return this._self_user && (this._self_user.id == this._user.id);
21
+ }
22
+ get isLogin() {
23
+ return this._self_user && true;
24
+ }
25
+ get isCreator() {
26
+ return this._self_user && (this._self_user.role_group == ini_1.ERoleGroup.Creator);
27
+ }
28
+ get isAdmin() {
29
+ return this._self_user && (this._self_user.role_group == ini_1.ERoleGroup.Admin || this._self_user.role_group == ini_1.ERoleGroup.Creator);
30
+ }
31
+ get isProxy() {
32
+ return this._self_user && (this._self_user.role_group == ini_1.ERoleGroup.Proxy);
33
+ }
34
+ get isCommon() {
35
+ return this._self_user && (this._self_user.role_group == ini_1.ERoleGroup.Common);
36
+ }
37
+ async init() {
38
+ this._engine.cfg.session_type = this._engine.cfg.session_type || FrameworkConfig_1.ESessionType.Cache;
39
+ this._session_id = this._request.getCookie(this._user_session_id) || this._request.params.session_id || this._request.postData.session_id;
40
+ let userId = -1;
41
+ if (this._session_id) {
42
+ let user = null;
43
+ if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Cache) {
44
+ user = CacheTool_1.GCacheTool.get(this._session_id);
45
+ }
46
+ else if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Redis) {
47
+ let user_id = parseInt((await RedisManager_1.GRedisMgr.get(this._session_id)) || "-1");
48
+ if (user_id > 0) {
49
+ user = (await UserService_1.GUserSer.getById(user_id));
50
+ }
51
+ }
52
+ else if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Mongo) {
53
+ let user_id = (await MongoCacheService_1.GMongoCacheSer.getData(this._session_id)) || -1;
54
+ if (user_id > 0) {
55
+ user = (await UserService_1.GUserSer.getById(user_id));
56
+ }
57
+ }
58
+ if (!user) {
59
+ this._update_session();
60
+ //Session不存在清除客户端cookie
61
+ this._response.clearCookie(this._user_session_id);
62
+ }
63
+ else {
64
+ await this._login(user);
65
+ userId = user.id;
66
+ }
67
+ }
68
+ let params = this._request.params;
69
+ if (params.userId && params.userId != userId + "") {
70
+ this._user = (await UserService_1.GUserSer.getById(params.userId));
71
+ }
72
+ else {
73
+ //不是别人的信息就查看自己的
74
+ this._user = this._self_user;
75
+ }
76
+ }
77
+ _logout() {
78
+ if (this._session_id) {
79
+ if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Cache) {
80
+ CacheTool_1.GCacheTool.remove(this._session_id);
81
+ }
82
+ else if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Redis) {
83
+ RedisManager_1.GRedisMgr.del(this._session_id);
84
+ }
85
+ else if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Mongo) {
86
+ MongoCacheService_1.GMongoCacheSer.deleteOne({ key: this._session_id });
87
+ }
88
+ this._session_id = null;
89
+ }
90
+ this._response.clearCookie(this._user_session_id);
91
+ }
92
+ _login(user) {
93
+ if (!user) {
94
+ return;
95
+ }
96
+ if (!this._session_id) {
97
+ this._session_id = Math.random().toString(36).substr(2) + user.id;
98
+ }
99
+ let time = 0;
100
+ if (this._request.postData.remember == "on") {
101
+ time = this._engine.cfg.cookie.expires.account_remember;
102
+ }
103
+ else {
104
+ time = this._engine.cfg.cookie.expires.account;
105
+ }
106
+ this._response.setCookie(this._user_session_id, this._session_id, time);
107
+ if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Cache) {
108
+ CacheTool_1.GCacheTool.add(this._session_id, user, time * 1000);
109
+ }
110
+ else if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Redis) {
111
+ RedisManager_1.GRedisMgr.set(this._session_id, user.id).then(() => {
112
+ RedisManager_1.GRedisMgr.expire(this._session_id, time);
113
+ });
114
+ }
115
+ else if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Mongo) {
116
+ let cm = new MongoCacheService_1.MongoCacheModel();
117
+ cm.key = this._session_id;
118
+ cm.data = user.id;
119
+ cm.expireAt = Date.now() + time * 1000;
120
+ MongoCacheService_1.GMongoCacheSer.updateOne(cm, { key: cm.key }, true);
121
+ }
122
+ this._self_user = user;
123
+ }
124
+ /**
125
+ * 用户信息发生更改,同步更新session里面的用户信息
126
+ */
127
+ async _update_session() {
128
+ if (!this._session_id) {
129
+ return;
130
+ }
131
+ let user_id = -1;
132
+ let um = null;
133
+ if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Cache) {
134
+ um = CacheTool_1.GCacheTool.get(this._session_id);
135
+ if (um) {
136
+ user_id = um.id;
137
+ }
138
+ }
139
+ else if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Redis) {
140
+ user_id = parseInt((await RedisManager_1.GRedisMgr.get(this._session_id)) || "-1");
141
+ }
142
+ if (user_id < 0) {
143
+ this._session_id = null;
144
+ return;
145
+ }
146
+ um = (await UserService_1.GUserSer.getById(user_id));
147
+ this._login(um);
148
+ }
149
+ async update_user(user_id) {
150
+ let user = (await UserService_1.GUserSer.getById(user_id));
151
+ if (this._user && this._user.id == user.id) {
152
+ this._user = user;
153
+ }
154
+ if (this._self_user && this._self_user.id == user.id) {
155
+ this._login(user);
156
+ }
157
+ }
158
+ //填充每个页面需要的通用数据
159
+ _init_data(datas) {
160
+ let data = super._init_data(datas);
161
+ data.model.isLogin = this.isLogin;
162
+ data.model.user = this._user;
163
+ data.model.selfUser = this._self_user;
164
+ data.model.isSelf = this.isSelf;
165
+ return data;
166
+ }
167
+ }
168
+ exports.BaseUserController = BaseUserController;
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MongoBaseUserController = void 0;
4
+ const BaseController_1 = require("./BaseController");
5
+ const CacheTool_1 = require("../../Logic/CacheTool");
6
+ const FrameworkConfig_1 = require("../../Config/FrameworkConfig");
7
+ const RedisManager_1 = require("../../Database/RedisManager");
8
+ const MongoCacheService_1 = require("../../Service/MongoCacheService");
9
+ const MongoUserService_1 = require("../../Service/MongoUserService");
10
+ const ini_1 = require("../../Service/ini");
11
+ class MongoBaseUserController extends BaseController_1.BaseController {
12
+ _user_session_id = "user_session_id";
13
+ _self_user = null;
14
+ _user = null; //网页内容的所属,比如,查看别人的博客,那这个信息就是作者的
15
+ _session_id = null;
16
+ get selfUser() {
17
+ return this._self_user;
18
+ }
19
+ get isSelf() {
20
+ return this._self_user && (this._self_user.id == this._user.id);
21
+ }
22
+ get isLogin() {
23
+ return this._self_user && true;
24
+ }
25
+ get isCreator() {
26
+ return this._self_user && (this._self_user.role_group == ini_1.ERoleGroup.Creator);
27
+ }
28
+ get isAdmin() {
29
+ return this._self_user && (this._self_user.role_group == ini_1.ERoleGroup.Admin || this._self_user.role_group == ini_1.ERoleGroup.Creator);
30
+ }
31
+ get isProxy() {
32
+ return this._self_user && (this._self_user.role_group == ini_1.ERoleGroup.Proxy);
33
+ }
34
+ get isCommon() {
35
+ return this._self_user && (this._self_user.role_group == ini_1.ERoleGroup.Common);
36
+ }
37
+ async init() {
38
+ this._engine.cfg.session_type = this._engine.cfg.session_type || FrameworkConfig_1.ESessionType.Cache;
39
+ this._session_id = this._request.getCookie(this._user_session_id) || this._request.params.session_id || this._request.postData.session_id;
40
+ let userId = -1;
41
+ if (this._session_id) {
42
+ let user = null;
43
+ if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Cache) {
44
+ user = CacheTool_1.GCacheTool.get(this._session_id);
45
+ }
46
+ else if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Redis) {
47
+ let user_id = parseInt((await RedisManager_1.GRedisMgr.get(this._session_id)) || "-1");
48
+ if (user_id > 0) {
49
+ user = (await MongoUserService_1.GUserSer.getById(user_id));
50
+ }
51
+ }
52
+ else if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Mongo) {
53
+ let user_id = (await MongoCacheService_1.GMongoCacheSer.getData(this._session_id)) || -1;
54
+ if (user_id > 0) {
55
+ user = (await MongoUserService_1.GUserSer.getById(user_id));
56
+ }
57
+ }
58
+ if (!user) {
59
+ this._update_session();
60
+ //Session不存在清除客户端cookie
61
+ this._response.clearCookie(this._user_session_id);
62
+ }
63
+ else {
64
+ await this._login(user);
65
+ userId = user.id;
66
+ }
67
+ }
68
+ let params = this._request.params;
69
+ if (params.userId && params.userId != userId + "") {
70
+ this._user = (await MongoUserService_1.GUserSer.getById(params.userId));
71
+ }
72
+ else {
73
+ //不是别人的信息就查看自己的
74
+ this._user = this._self_user;
75
+ }
76
+ }
77
+ _logout() {
78
+ if (this._session_id) {
79
+ if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Cache) {
80
+ CacheTool_1.GCacheTool.remove(this._session_id);
81
+ }
82
+ else if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Redis) {
83
+ RedisManager_1.GRedisMgr.del(this._session_id);
84
+ }
85
+ else if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Mongo) {
86
+ MongoCacheService_1.GMongoCacheSer.deleteOne({ key: this._session_id });
87
+ }
88
+ this._session_id = null;
89
+ }
90
+ this._response.clearCookie(this._user_session_id);
91
+ }
92
+ _login(user) {
93
+ if (!user) {
94
+ return;
95
+ }
96
+ if (!this._session_id) {
97
+ this._session_id = Math.random().toString(36).substr(2) + user.id;
98
+ }
99
+ let time = 0;
100
+ if (this._request.postData.remember == "on") {
101
+ time = this._engine.cfg.cookie.expires.account_remember;
102
+ }
103
+ else {
104
+ time = this._engine.cfg.cookie.expires.account;
105
+ }
106
+ this._response.setCookie(this._user_session_id, this._session_id, time);
107
+ if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Cache) {
108
+ CacheTool_1.GCacheTool.add(this._session_id, user, time * 1000);
109
+ }
110
+ else if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Redis) {
111
+ RedisManager_1.GRedisMgr.set(this._session_id, user.id).then(() => {
112
+ RedisManager_1.GRedisMgr.expire(this._session_id, time);
113
+ });
114
+ }
115
+ else if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Mongo) {
116
+ let cm = new MongoCacheService_1.MongoCacheModel();
117
+ cm.key = this._session_id;
118
+ cm.data = user.id;
119
+ cm.expireAt = Date.now() + time * 1000;
120
+ MongoCacheService_1.GMongoCacheSer.updateOne(cm, { key: cm.key }, true);
121
+ }
122
+ this._self_user = user;
123
+ }
124
+ /**
125
+ * 用户信息发生更改,同步更新session里面的用户信息
126
+ */
127
+ async _update_session() {
128
+ if (!this._session_id) {
129
+ return;
130
+ }
131
+ let user_id = -1;
132
+ let um = null;
133
+ if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Cache) {
134
+ um = CacheTool_1.GCacheTool.get(this._session_id);
135
+ if (um) {
136
+ user_id = um.id;
137
+ }
138
+ }
139
+ else if (this._engine.cfg.session_type == FrameworkConfig_1.ESessionType.Redis) {
140
+ user_id = parseInt((await RedisManager_1.GRedisMgr.get(this._session_id)) || "-1");
141
+ }
142
+ if (user_id < 0) {
143
+ this._session_id = null;
144
+ return;
145
+ }
146
+ um = (await MongoUserService_1.GUserSer.getById(user_id));
147
+ this._login(um);
148
+ }
149
+ async update_user(user_id) {
150
+ let user = (await MongoUserService_1.GUserSer.getById(user_id));
151
+ if (this._user && this._user.id == user.id) {
152
+ this._user = user;
153
+ }
154
+ if (this._self_user && this._self_user.id == user.id) {
155
+ this._login(user);
156
+ }
157
+ }
158
+ //填充每个页面需要的通用数据
159
+ _init_data(datas) {
160
+ let data = super._init_data(datas);
161
+ data.model.isLogin = this.isLogin;
162
+ data.model.user = this._user;
163
+ data.model.selfUser = this._self_user;
164
+ data.model.isSelf = this.isSelf;
165
+ return data;
166
+ }
167
+ }
168
+ exports.MongoBaseUserController = MongoBaseUserController;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AdminValidate = void 0;
4
+ function AdminValidate(target, propertyName, descriptor) {
5
+ let method = descriptor.value;
6
+ descriptor.value = function () {
7
+ let self = this;
8
+ if (!self.isLogin) {
9
+ self.redirect(null, "Login");
10
+ return;
11
+ }
12
+ if (!self.isAdmin) {
13
+ self.showText("需要管理员权限");
14
+ return;
15
+ }
16
+ return method.apply(this, arguments);
17
+ };
18
+ }
19
+ exports.AdminValidate = AdminValidate;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthorityValidate = void 0;
4
+ let AuthorityValidate = function (rg, ctr_name) {
5
+ ctr_name = ctr_name || "Wechat";
6
+ return function (target, propertyName, descriptor) {
7
+ let method = descriptor.value;
8
+ descriptor.value = function () {
9
+ let self = this;
10
+ if (!self.isLogin) {
11
+ self.redirect(null, ctr_name);
12
+ return;
13
+ }
14
+ if (rg && self.selfUser.role_group != rg) {
15
+ self.redirect(null, ctr_name); //权限不足
16
+ return;
17
+ }
18
+ return method.apply(this, arguments);
19
+ };
20
+ };
21
+ };
22
+ exports.AuthorityValidate = AuthorityValidate;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CreatorValidate = void 0;
4
+ function CreatorValidate(target, propertyName, descriptor) {
5
+ let method = descriptor.value;
6
+ descriptor.value = function () {
7
+ let self = this;
8
+ if (!self.isLogin) {
9
+ self.redirect(null, "Login");
10
+ return;
11
+ }
12
+ if (!self.isCreator) {
13
+ self.showText("需要创始人权限");
14
+ return;
15
+ }
16
+ return method.apply(this, arguments);
17
+ };
18
+ }
19
+ exports.CreatorValidate = CreatorValidate;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JsonAdminValidate = void 0;
4
+ function JsonAdminValidate(target, propertyName, descriptor) {
5
+ let method = descriptor.value;
6
+ descriptor.value = function () {
7
+ let self = this;
8
+ if (!self.isAdmin) {
9
+ self.showJson({ errcode: { id: 1, des: "需要管理员或创始人权限" }, err: "需要管理员或创始人权限" });
10
+ return;
11
+ }
12
+ return method.apply(this, arguments);
13
+ };
14
+ }
15
+ exports.JsonAdminValidate = JsonAdminValidate;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JsonAuthorityValidate = void 0;
4
+ let JsonAuthorityValidate = function (rg) {
5
+ return function (target, propertyName, descriptor) {
6
+ let method = descriptor.value;
7
+ descriptor.value = function () {
8
+ let self = this;
9
+ if (!self.isLogin) {
10
+ self.showJson({ errcode: { id: 1, des: "未登陆" }, err: "未登陆" });
11
+ }
12
+ if (rg && self.selfUser.role_group != rg) {
13
+ self.showJson({ errcode: { id: 2, des: "权限不足" }, err: "权限不足" });
14
+ return;
15
+ }
16
+ return method.apply(this, arguments);
17
+ };
18
+ };
19
+ };
20
+ exports.JsonAuthorityValidate = JsonAuthorityValidate;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JsonCreatorValidate = void 0;
4
+ function JsonCreatorValidate(target, propertyName, descriptor) {
5
+ let method = descriptor.value;
6
+ descriptor.value = function () {
7
+ let self = this;
8
+ if (!self.isLogin) {
9
+ self.showJson({ errcode: { id: 1, des: "请登陆后尝试" }, err: "请登陆后尝试" });
10
+ return;
11
+ }
12
+ if (!self.isCreator) {
13
+ self.showJson({ errcode: { id: 1, des: "需要创始人权限" }, err: "需要创始人权限" });
14
+ return;
15
+ }
16
+ return method.apply(this, arguments);
17
+ };
18
+ }
19
+ exports.JsonCreatorValidate = JsonCreatorValidate;