user_model.js 18 KB

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