user_model.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /**
  2. * 用户业务模型
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/6/9
  6. * @version
  7. */
  8. import mongoose from "mongoose";
  9. import Request from "request";
  10. import BaseModel from "../../common/base/base_model"
  11. import LogModel from "./log_model";
  12. class UserModel extends BaseModel {
  13. /**
  14. * 企业所在地区
  15. *
  16. * @var {object}
  17. */
  18. province = ['北京', '天津', '河北', '山西', '内蒙古', '辽宁', '吉林', '黑龙江', '上海', '江苏', '浙江', '安徽',
  19. '福建', '江西', '山东', '河南', '湖北', '湖南', '广东', '广西', '海南', '重庆', '四川', '贵州', '云南', '西藏',
  20. '陕西', '甘肃', '青海', '宁夏', '新疆', '台湾', '香港', '澳门',];
  21. /**
  22. * 企业类型
  23. *
  24. * @var
  25. */
  26. companyType = ['建设单位', '设计单位', '施工单位', '监理单位', '审核单位', '咨询公司', '招标代理', '住建部', '财政', '审计',
  27. '造价管理站', '学校', '个人', '其他'];
  28. /**
  29. * 企业规模
  30. *
  31. * @var
  32. */
  33. companyScale = ['1-50', '50-100', '100-500', '500+'];
  34. /**
  35. * 最近天数
  36. *
  37. * @var
  38. */
  39. dayMsg = ['所有', '最近24小时', '最近3天', '最近7天', '最近30天'];
  40. /**
  41. * 构造函数
  42. *
  43. * @return {void}
  44. */
  45. constructor() {
  46. let parent = super();
  47. parent.model = mongoose.model('user');
  48. parent.init();
  49. }
  50. /**
  51. * 根据用户名密码调用SSO接口获取信息
  52. *
  53. * @param {string} username
  54. * @param {string} password
  55. * @return {object}
  56. */
  57. async getInfoFromSSO(username, password) {
  58. let postData = {
  59. url: 'http://sso.smartcost.com.cn/api/jzlogin',
  60. form: { username: username, userpasswd: password },
  61. encoding: 'utf8'
  62. };
  63. return new Promise(function (resolve, reject) {
  64. try {
  65. // 请求接口
  66. Request.post(postData, function (err, postResponse, body) {
  67. if (err) {
  68. console.log(err);
  69. reject('请求错误');
  70. }
  71. if(!postResponse) reject('请求错误');
  72. if (postResponse.statusCode !== 200) {
  73. reject('通行证验证失败!');
  74. }
  75. resolve(body);
  76. });
  77. } catch (error) {
  78. reject([]);
  79. }
  80. });
  81. }
  82. /**
  83. * 根据用户手机号码调用SSO接口获取信息
  84. *
  85. * @param {string} mobile
  86. * @param {string} login 1为登录,不存在则是查询
  87. * @return {object}
  88. */
  89. async getInfoFromSSOMobile(mobile, login = '1') {
  90. const fromData = { account: mobile };
  91. if (login === '1') {
  92. fromData.login = 1;
  93. }
  94. let postData = {
  95. url: 'http://sso.smartcost.com.cn/building/api/mobile/login',
  96. form: fromData,
  97. encoding: 'utf8'
  98. };
  99. return new Promise(function (resolve, reject) {
  100. try {
  101. // 请求接口
  102. Request.post(postData, function (err, postResponse, body) {
  103. if (err) {
  104. console.log('111');
  105. reject('请求错误');
  106. }
  107. if (postResponse.statusCode !== 200) {
  108. reject('通行证验证失败!');
  109. }
  110. resolve(body);
  111. });
  112. } catch (error) {
  113. reject([]);
  114. }
  115. });
  116. }
  117. /**
  118. * 根据用户id和token调用SSO接口获取信息
  119. *
  120. * @param {string} username
  121. * @param {string} password
  122. * @return {object}
  123. */
  124. async getInfoFromSSO2(ssoID, token) {
  125. let postData = {
  126. url: 'http://sso.smartcost.com.cn/building/api/login/auth',
  127. form: { ssoID: ssoID, token: token },
  128. encoding: 'utf8'
  129. };
  130. return new Promise(function (resolve, reject) {
  131. try {
  132. // 请求接口
  133. Request.post(postData, function (err, postResponse, body) {
  134. if (err) {
  135. console.log('111');
  136. reject('请求错误');
  137. }
  138. if (postResponse.statusCode !== 200) {
  139. reject('通行证验证失败!');
  140. }
  141. resolve(body);
  142. });
  143. } catch (error) {
  144. reject([]);
  145. }
  146. });
  147. }
  148. /**
  149. * 标记用户
  150. *
  151. * @param {object} userData
  152. * @param {Object} request
  153. * @return {Promise}
  154. */
  155. async markUser(userData, request = null) {
  156. let userDataFromDb = await this.findDataBySsoId(userData.ssoId);
  157. // let userDataFromDb = await this.findDataByName(userData.username); //后面新增的账号可淘汰这方法,当前使用是为了兼容旧的账号
  158. let result = false;
  159. userData.latest_login = new Date().getTime();
  160. if (userDataFromDb === null) {
  161. // 不存在用户则入库
  162. this.setScene();//恢复场景,用户有可能公司real_name等信息为空,不能设置为必填
  163. result = await this.addUser(userData);
  164. userDataFromDb = result;
  165. } else {
  166. // 存在则新增登录信息并更新账号信息
  167. let condition = {ssoId: userData.ssoId};
  168. // let condition = { username: userData.username };
  169. let UpdateData = {
  170. email: userData.email,
  171. mobile: userData.mobile,
  172. // ssoId: userData.ssoId,
  173. qq: userData.qq,
  174. latest_login: userData.latest_login,
  175. isUserActive: userData.isUserActive,
  176. token: userData.token,
  177. };
  178. let updateResult = await this.updateUser(condition, UpdateData);
  179. if (updateResult.ok === 1) {
  180. let logModel = new LogModel();
  181. result = await logModel.addLoginLog(userDataFromDb._id, request);
  182. }
  183. }
  184. request.session.sessionUser.id = userDataFromDb._id;
  185. request.session.sessionUser.real_name = userDataFromDb.real_name;
  186. request.session.sessionUser.latest_used = userDataFromDb.latest_used;//设置最近使用的编办
  187. return result;
  188. }
  189. /**
  190. * 选择场景
  191. *
  192. * @param {string} scene
  193. */
  194. setScene(scene = '') {
  195. /* switch (scene) {
  196. case 'saveInfo':
  197. this.model.schema.path('real_name').required(true);
  198. this.model.schema.path('company').required(true);
  199. this.model.schema.path('province').required(true);
  200. this.model.schema.path('version').required(true);
  201. break;
  202. case '':
  203. this.model.schema.path('real_name').required(false);
  204. this.model.schema.path('company').required(false);
  205. this.model.schema.path('province').required(false);
  206. this.model.schema.path('version').required(false);
  207. }*/
  208. }
  209. /**
  210. * 根据用户名查找数据
  211. *
  212. * @param {string} username
  213. * @return {object}
  214. */
  215. findDataByName(username) {
  216. return this.db.findOne({ username: username });
  217. }
  218. /**
  219. * 根据ssoID查找数据
  220. *
  221. * @param {string} ssoId
  222. * @return {object}
  223. */
  224. findDataBySsoId(ssoId) {
  225. return this.db.findOne({ ssoId: ssoId });
  226. }
  227. /**
  228. * 根据手机号查找数据
  229. *
  230. * @param {string} mobile
  231. * @return {object}
  232. */
  233. findDataByMobile(mobile) {
  234. return this.db.findOne({ mobile: mobile });
  235. }
  236. /**
  237. * 根据userId查找数据
  238. *
  239. * @param {string} ssoId
  240. * @return {object}
  241. */
  242. async findDataById(id, fields) {
  243. const objId = mongoose.Types.ObjectId(id);
  244. return fields ? await this.db.findOne({_id: objId}, fields) : await this.db.findOne({_id: objId});
  245. }
  246. /**
  247. * 验证用户token正确性
  248. * 一个账号不允许多处同时在线,每次登陆都会更新session和数据库的token,每个请求都会比对session和数据库的token
  249. * @param {String} id - 用户ID
  250. * @param {String} token - 登陆生成的token
  251. * @return {Boolean}
  252. */
  253. async checkToken(id, token) {
  254. const user = await this.findDataById(id, '-_id token');
  255. if (!user.token) { // 兼容第一次上线,已登陆的用户还没token,需要返回验证正确
  256. return true;
  257. }
  258. return user.token === token;
  259. }
  260. async findDataByAccount(account) {
  261. let userinfo = await this.db.findOne({ mobile: account });
  262. let userinfo2 = await this.db.findOne({ email: account });
  263. if (userinfo) {
  264. return userinfo;
  265. } else if (userinfo2) {
  266. return userinfo2;
  267. } else {
  268. return false;
  269. }
  270. }
  271. /**
  272. * 新增用户
  273. *
  274. * @param {object} userData
  275. * @return {Promise|boolean}
  276. */
  277. addUser(userData) {
  278. let insertData = {
  279. ssoId: userData.ssoId,
  280. username: userData.username,
  281. email: userData.email,
  282. mobile: userData.mobile,
  283. qq: userData.qq,
  284. create_time: new Date().getTime(),
  285. latest_login: new Date().getTime(),
  286. isUserActive: userData.isUserActive,
  287. token: userData.token
  288. };
  289. return this.db.create(insertData);
  290. }
  291. //更新最近使用编办ID
  292. async updateLatestUsed(userID, compilationID) {
  293. if (userID && compilationID) {
  294. return await this.db.update({ '_id': userID }, { 'latest_used': compilationID });
  295. }
  296. }
  297. /**
  298. * 更新用户数据
  299. *
  300. * @param {object} updateData
  301. * @return {Promise}
  302. */
  303. async updateUser(condition, updateData) {
  304. if (Object.keys(condition).length <= 0 || Object.keys(updateData).length <= 0) {
  305. return null;
  306. }
  307. return await this.db.update(condition, updateData);
  308. }
  309. async addVersion(condition, versionInfo) {
  310. return await this.db.findOneAndUpdate(condition, { $addToSet: { versionInfo: versionInfo } });
  311. }
  312. async removeVersion(userId, compilationId) {
  313. let userObjId = mongoose.Types.ObjectId(userId);
  314. return await this.db.findOneAndUpdate({ _id: userObjId }, { $pull: { versionInfo: { compilationId: compilationId } } });
  315. }
  316. /**
  317. * 判断用户使用免费版还是专业版
  318. *
  319. * @param ssoId
  320. * @param compilationId
  321. * @return {version}
  322. */
  323. async getVersionFromUpgrade(ssoId, compilationId) {
  324. let version = '纵横公路养护云造价(学习版)';//'纵横公路养护造价(免费云版)'; 2019-03-28 需求修改,听说不知道多久的以后还会改回来--勿删!!!!!
  325. let userData = await this.findDataBySsoId(ssoId);
  326. if (userData.upgrade_list !== undefined) {
  327. let compilationInfo = userData.upgrade_list.find(function (item) {
  328. return item.compilationID === compilationId;
  329. });
  330. if (compilationInfo !== undefined && compilationInfo.isUpgrade === true) {
  331. version = '纵横公路养护云造价(专业版)';
  332. }
  333. }
  334. return version;
  335. }
  336. /**
  337. * 判断用户是免费版还是专业版用户
  338. */
  339. async isFree(ssoId, compilationId) {
  340. const userData = await this.findDataBySsoId(ssoId);
  341. if (!userData) {
  342. throw '不存在此用户';
  343. }
  344. const upgrade_list = userData.upgrade_list;
  345. let free = true;
  346. if (upgrade_list && upgrade_list.length > 0) {
  347. const upgrade = upgrade_list.find(function (item) {
  348. return item.compilationID === compilationId && item.isUpgrade === true;
  349. });
  350. if (upgrade) {
  351. free = false;
  352. }
  353. }
  354. return free
  355. }
  356. /**
  357. * 从session中判断用户是否是免费版
  358. * @param {String} sessionVersion
  359. * @return {Boolean}
  360. */
  361. isFreeFromSession(sessionVersion) {
  362. if (!sessionVersion) {
  363. return true;
  364. }
  365. return sessionVersion.indexOf('学习') >= 0;
  366. }
  367. /*
  368. * 添加联系人,互相添加
  369. */
  370. async addContact(userID, contactID) {
  371. const data = [
  372. { user: userID, contact: contactID },
  373. { user: contactID, contact: userID }
  374. ];
  375. const task = [];
  376. for (const { user, contact } of data) {
  377. const hasThisContact = await this.model.count({ _id: mongoose.Types.ObjectId(user), 'contacts.userID': contact });
  378. // 没有则添加
  379. if (!hasThisContact) {
  380. task.push(this.model.update({ _id: mongoose.Types.ObjectId(user) }, { $push: { contacts: { userID: contact } } }));
  381. }
  382. }
  383. if (task.length) {
  384. await Promise.all(task);
  385. }
  386. }
  387. async getContacts(userID) {
  388. const user = await this.model.findOne({ _id: mongoose.Types.ObjectId(userID) }, 'contacts').lean();
  389. if (!user || !user.contacts || !user.contacts.length) {
  390. return [];
  391. }
  392. const userIDList = user.contacts.map(item => mongoose.Types.ObjectId(item.userID));
  393. const users = await this.model.find({ _id: { $in: userIDList } }, 'real_name mobile company');
  394. return users;
  395. }
  396. /**
  397. * 获取列表
  398. *
  399. * @param {object} condition
  400. * @param {number} page
  401. * @param {Number} pageSize
  402. * @return {promise}
  403. */
  404. async getList(condition = null, page = 1, pageSize = 30, sort = {_id:-1}) {
  405. page = parseInt(page);
  406. page = page <= 1 ? 1 : page;
  407. let option = {pageSize: pageSize, offset: parseInt((page - 1) * pageSize), sort: sort};
  408. let userList = await this.db.find(condition, null, option);
  409. userList = userList.length > 0 ? userList : [];
  410. return userList;
  411. }
  412. /**
  413. * 获取过滤条件
  414. *
  415. * @return {Object}
  416. */
  417. getFilterCondition(request) {
  418. let condition = {};
  419. let regtime = request.query.regtime;
  420. regtime = regtime !== '' && regtime !== undefined ? parseInt(regtime) : 0;
  421. if (regtime !== 0) {
  422. condition.create_time = this.getTimestamp(regtime);
  423. }
  424. //最近登录时间
  425. let loginTime = request.query.loginTime;
  426. loginTime = loginTime !== '' && loginTime !== undefined ? parseInt(loginTime) : 0;
  427. if (loginTime !== 0) {
  428. condition.latest_login = this.getTimestamp(loginTime);
  429. }
  430. let version = request.query.version;
  431. if(version !== '' && version !== undefined) {
  432. condition.version = version;
  433. }
  434. // 已升级费用定额
  435. let upGrade = request.query.upGrade;
  436. if(upGrade !== '' && upGrade !== undefined){
  437. condition.upgrade_list = {"$elemMatch":{"compilationID":upGrade,"isUpgrade":true}};
  438. }
  439. // 最近使用费用定额
  440. let latestUsed = request.query.latestUsed;
  441. if(latestUsed !== '' && latestUsed !== undefined){
  442. condition.latest_used = latestUsed;
  443. }
  444. let keyword = request.query.keyword;
  445. if (keyword !== '' && keyword !== undefined) {
  446. condition.$or = [{real_name : {$regex: keyword}},{email : {$regex: keyword}},{mobile : {$regex: keyword}},{qq : {$regex: keyword}},{company : {$regex: keyword}}];
  447. }
  448. return condition;
  449. }
  450. /**
  451. * 获取时间戳区间
  452. *
  453. * @return {Object}
  454. */
  455. getTimestamp(type) {
  456. let startTime = '';
  457. switch (type) {
  458. case 1 :
  459. startTime = Date.parse(new Date())-86400*1000;
  460. break;
  461. case 2 :
  462. startTime = Date.parse(new Date())-86400*1000*3;
  463. break;
  464. case 3 :
  465. startTime = Date.parse(new Date())-86400*1000*7;
  466. break;
  467. case 4 :
  468. startTime = Date.parse(new Date())-86400*1000*30;
  469. break;
  470. default :
  471. break;
  472. }
  473. let endTime = Date.parse(new Date());
  474. return startTime === '' ? '' : {'$gte': startTime, '$lt': endTime};
  475. }
  476. /**
  477. * 获取daymsg
  478. *
  479. */
  480. getDayMsg(index){
  481. return this.dayMsg[index];
  482. }
  483. /**
  484. * 获取usercompilation到期列表
  485. *
  486. * @param {object} condition
  487. * @param {number} page
  488. * @param {Number} pageSize
  489. * @return {promise}
  490. */
  491. async getDeadlineList(condition = null) {
  492. let userList = await this.db.find(condition);
  493. userList = userList.length > 0 ? userList : [];
  494. return userList;
  495. }
  496. }
  497. export default UserModel;