user.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  14. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  15. return new (P || (P = Promise))(function (resolve, reject) {
  16. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  17. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  18. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  19. step((generator = generator.apply(thisArg, _arguments || [])).next());
  20. });
  21. };
  22. var __generator = (this && this.__generator) || function (thisArg, body) {
  23. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  24. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  25. function verb(n) { return function (v) { return step([n, v]); }; }
  26. function step(op) {
  27. if (f) throw new TypeError("Generator is already executing.");
  28. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  29. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  30. if (y = 0, t) op = [op[0] & 2, t.value];
  31. switch (op[0]) {
  32. case 0: case 1: t = op; break;
  33. case 4: _.label++; return { value: op[1], done: false };
  34. case 5: _.label++; y = op[1]; op = [0]; continue;
  35. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  36. default:
  37. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  38. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  39. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  40. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  41. if (t[2]) _.ops.pop();
  42. _.trys.pop(); continue;
  43. }
  44. op = body.call(thisArg, _);
  45. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  46. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  47. }
  48. };
  49. var __values = (this && this.__values) || function(o) {
  50. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  51. if (m) return m.call(o);
  52. if (o && typeof o.length === "number") return {
  53. next: function () {
  54. if (o && i >= o.length) o = void 0;
  55. return { value: o && o[i++], done: !o };
  56. }
  57. };
  58. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  59. };
  60. var __read = (this && this.__read) || function (o, n) {
  61. var m = typeof Symbol === "function" && o[Symbol.iterator];
  62. if (!m) return o;
  63. var i = m.call(o), r, ar = [], e;
  64. try {
  65. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  66. }
  67. catch (error) { e = { error: error }; }
  68. finally {
  69. try {
  70. if (r && !r.done && (m = i["return"])) m.call(i);
  71. }
  72. finally { if (e) throw e.error; }
  73. }
  74. return ar;
  75. };
  76. var __importDefault = (this && this.__importDefault) || function (mod) {
  77. return (mod && mod.__esModule) ? mod : { "default": mod };
  78. };
  79. Object.defineProperty(exports, "__esModule", { value: true });
  80. exports.isAuth = exports.auth = exports.isLogin = exports.logout = exports.login = void 0;
  81. var coolkit_api_1 = __importDefault(require("coolkit-api"));
  82. var dataUtil_1 = require("../utils/dataUtil");
  83. var getThings_1 = __importDefault(require("../utils/getThings"));
  84. var Controller_1 = __importDefault(require("../controller/Controller"));
  85. var coolkit_ws_1 = __importDefault(require("coolkit-ws"));
  86. var initCkWs_1 = __importDefault(require("../utils/initCkWs"));
  87. var lodash_1 = __importDefault(require("lodash"));
  88. var eventBus_1 = __importDefault(require("../utils/eventBus"));
  89. var LanDeviceController_1 = __importDefault(require("../controller/LanDeviceController"));
  90. var CloudDeviceController_1 = __importDefault(require("../controller/CloudDeviceController"));
  91. var restApi_1 = require("../apis/restApi");
  92. var AuthClass_1 = __importDefault(require("../class/AuthClass"));
  93. var removeEntityByDevice_1 = __importDefault(require("../utils/removeEntityByDevice"));
  94. var config_1 = require("../config/config");
  95. var init_1 = require("../lib-ha/init");
  96. var logger_1 = require("../utils/logger");
  97. var login = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  98. var _a, countryCode, phoneNumber, lang, password, email, result, at, apikey, region, err_1;
  99. return __generator(this, function (_b) {
  100. switch (_b.label) {
  101. case 0:
  102. _b.trys.push([0, 7, , 8]);
  103. _a = req.body, countryCode = _a.countryCode, phoneNumber = _a.phoneNumber, lang = _a.lang, password = _a.password, email = _a.email;
  104. return [4, coolkit_api_1.default.user.login({
  105. countryCode: countryCode,
  106. phoneNumber: phoneNumber,
  107. lang: lang,
  108. password: password,
  109. email: email,
  110. })];
  111. case 1:
  112. result = _b.sent();
  113. logger_1.logger.verbose("user login result: ".concat(JSON.stringify(result)));
  114. if (!(result.error === 0)) return [3, 6];
  115. return [4, (0, dataUtil_1.saveData)('user.json', JSON.stringify(__assign(__assign({}, result.data), { login: __assign({}, req.body) })))];
  116. case 2:
  117. _b.sent();
  118. at = lodash_1.default.get(result, ['data', 'at']);
  119. apikey = lodash_1.default.get(result, ['data', 'user', 'apikey']);
  120. region = lodash_1.default.get(result, ['data', 'region']);
  121. return [4, (0, initCkWs_1.default)()];
  122. case 3:
  123. _b.sent();
  124. return [4, (0, getThings_1.default)()];
  125. case 4:
  126. _b.sent();
  127. eventBus_1.default.emit('sse');
  128. return [4, (0, init_1.init)()];
  129. case 5:
  130. _b.sent();
  131. _b.label = 6;
  132. case 6:
  133. res.json(result);
  134. return [3, 8];
  135. case 7:
  136. err_1 = _b.sent();
  137. logger_1.logger.error("user login error: ".concat(err_1));
  138. return [3, 8];
  139. case 8: return [2];
  140. }
  141. });
  142. }); };
  143. exports.login = login;
  144. var logout = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  145. var result, _a, _b, _c, id, device, ckRes, err_2;
  146. var e_1, _d;
  147. return __generator(this, function (_e) {
  148. switch (_e.label) {
  149. case 0:
  150. _e.trys.push([0, 3, , 4]);
  151. return [4, (0, dataUtil_1.clearData)('user.json')];
  152. case 1:
  153. result = _e.sent();
  154. logger_1.logger.verbose("user logout result: ".concat(JSON.stringify(result)));
  155. (0, dataUtil_1.clearData)('disabled.json');
  156. try {
  157. for (_a = __values(Controller_1.default.deviceMap.entries()), _b = _a.next(); !_b.done; _b = _a.next()) {
  158. _c = __read(_b.value, 2), id = _c[0], device = _c[1];
  159. if (req.body.removeEntity) {
  160. (0, removeEntityByDevice_1.default)(device);
  161. }
  162. if (device instanceof LanDeviceController_1.default) {
  163. device.selfApikey = undefined;
  164. device.devicekey = undefined;
  165. device.deviceName = undefined;
  166. device.extra = undefined;
  167. device.params = undefined;
  168. }
  169. if (device instanceof CloudDeviceController_1.default) {
  170. Controller_1.default.deviceMap.delete(id);
  171. }
  172. }
  173. }
  174. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  175. finally {
  176. try {
  177. if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
  178. }
  179. finally { if (e_1) throw e_1.error; }
  180. }
  181. Controller_1.default.unsupportDeviceMap.clear();
  182. return [4, coolkit_api_1.default.user.logout()];
  183. case 2:
  184. ckRes = _e.sent();
  185. logger_1.logger.verbose("user logout ckRes: ".concat(JSON.stringify(ckRes)));
  186. eventBus_1.default.emit('sse');
  187. coolkit_ws_1.default.close();
  188. (0, init_1.closeWs2Ck)();
  189. res.json({
  190. error: 0,
  191. data: null,
  192. });
  193. return [3, 4];
  194. case 3:
  195. err_2 = _e.sent();
  196. logger_1.logger.error("user logout error: ".concat(err_2));
  197. res.json({
  198. error: 500,
  199. data: err_2,
  200. });
  201. return [3, 4];
  202. case 4: return [2];
  203. }
  204. });
  205. }); };
  206. exports.logout = logout;
  207. var isLogin = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  208. var result, username, err_3;
  209. return __generator(this, function (_a) {
  210. switch (_a.label) {
  211. case 0:
  212. _a.trys.push([0, 2, , 3]);
  213. return [4, (0, dataUtil_1.getDataSync)('user.json')];
  214. case 1:
  215. result = _a.sent();
  216. if (result && result.at) {
  217. username = result.user.phoneNumber || result.user.email;
  218. res.json({
  219. error: 0,
  220. data: {
  221. isLogin: true,
  222. username: username
  223. },
  224. });
  225. return [2];
  226. }
  227. res.json({
  228. error: 0,
  229. data: {
  230. isLogin: false,
  231. username: ''
  232. },
  233. });
  234. return [3, 3];
  235. case 2:
  236. err_3 = _a.sent();
  237. logger_1.logger.error("user isLogin error: ".concat(err_3));
  238. res.json({
  239. error: 500,
  240. data: err_3,
  241. });
  242. return [3, 3];
  243. case 3: return [2];
  244. }
  245. });
  246. }); };
  247. exports.isLogin = isLogin;
  248. var auth = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  249. var ip, headers, _a, code, clientId, result, err_4;
  250. return __generator(this, function (_b) {
  251. switch (_b.label) {
  252. case 0:
  253. ip = req.ip, headers = req.headers;
  254. if (lodash_1.default.get(headers, 'cookie') && config_1.isSupervisor) {
  255. res.json({
  256. error: 0,
  257. data: {
  258. isAuth: true,
  259. },
  260. });
  261. return [2];
  262. }
  263. _b.label = 1;
  264. case 1:
  265. _b.trys.push([1, 3, , 4]);
  266. if (AuthClass_1.default.isValid(req.ip)) {
  267. res.json({
  268. error: 0,
  269. data: null,
  270. });
  271. return [2];
  272. }
  273. _a = req.body, code = _a.code, clientId = _a.clientId;
  274. return [4, (0, restApi_1.getAuth)(clientId, code)];
  275. case 2:
  276. result = _b.sent();
  277. if (result && result.status === 200) {
  278. AuthClass_1.default.setAuth(req.ip, clientId, result.data);
  279. eventBus_1.default.emit('init-ha-socket');
  280. logger_1.logger.info("redirectToAuth result data: ".concat(JSON.stringify(result.data)));
  281. res.json({
  282. error: 0,
  283. data: null,
  284. });
  285. }
  286. else {
  287. res.json({
  288. error: result.status,
  289. data: null,
  290. });
  291. }
  292. return [3, 4];
  293. case 3:
  294. err_4 = _b.sent();
  295. res.json({
  296. error: 500,
  297. data: err_4,
  298. });
  299. return [3, 4];
  300. case 4: return [2];
  301. }
  302. });
  303. }); };
  304. exports.auth = auth;
  305. var isAuth = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  306. var ip, headers, status_1;
  307. return __generator(this, function (_a) {
  308. ip = req.ip, headers = req.headers;
  309. if (lodash_1.default.get(headers, 'cookie') && config_1.isSupervisor) {
  310. res.json({
  311. error: 0,
  312. data: {
  313. isAuth: true,
  314. },
  315. });
  316. return [2];
  317. }
  318. try {
  319. status_1 = AuthClass_1.default.isValid(ip);
  320. res.json({
  321. error: 0,
  322. data: {
  323. isAuth: status_1,
  324. },
  325. });
  326. }
  327. catch (err) {
  328. logger_1.logger.error("user isAuth error: ".concat(err));
  329. res.json({
  330. error: 500,
  331. data: err,
  332. });
  333. }
  334. return [2];
  335. });
  336. }); };
  337. exports.isAuth = isAuth;