user_model.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. 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('111');
  69. reject('请求错误');
  70. }
  71. if (postResponse.statusCode !== 200) {
  72. reject('通行证验证失败!');
  73. }
  74. resolve(body);
  75. });
  76. } catch (error) {
  77. reject([]);
  78. }
  79. });
  80. }
  81. /**
  82. * 根据用户手机调用SSO接口获取个人信息
  83. *
  84. * @param {string} account
  85. * @return {object}
  86. */
  87. async getInfoFromSSOAccount(account) {
  88. let postData = {
  89. url: 'http://sso.smartcost.com.cn/building/api/userinfo',
  90. form: {account: account},
  91. encoding: 'utf8'
  92. };
  93. return new Promise(function (resolve, reject) {
  94. try {
  95. // 请求接口
  96. Request.post(postData, function (err, postResponse, body) {
  97. if (err) {
  98. console.log('111');
  99. reject('请求错误');
  100. }
  101. if (postResponse.statusCode !== 200) {
  102. reject('通行证验证失败!');
  103. }
  104. resolve(body);
  105. });
  106. } catch (error) {
  107. reject([]);
  108. }
  109. });
  110. }
  111. /**
  112. * 根据用户手机号码调用SSO接口获取信息
  113. *
  114. * @param {string} mobile
  115. * @param {string} login 1为登录,不存在则是查询
  116. * @param {string} comefrom 1为来自大司空
  117. * @return {object}
  118. */
  119. async getInfoFromSSOMobile(mobile, login = '1') {
  120. const fromData = {account: mobile, comefrom: 1};
  121. if (login === '1') {
  122. fromData.login = 1;
  123. }
  124. let postData = {
  125. url: 'http://sso.smartcost.com.cn/building/api/mobile/login',
  126. form: fromData,
  127. encoding: 'utf8'
  128. };
  129. return new Promise(function (resolve, reject) {
  130. try {
  131. // 请求接口
  132. Request.post(postData, function (err, postResponse, body) {
  133. if (err) {
  134. console.log('111');
  135. reject('请求错误');
  136. }
  137. if(!postResponse) reject('请求错误');
  138. if (postResponse.statusCode !== 200) {
  139. reject('通行证验证失败!');
  140. }
  141. resolve(body);
  142. });
  143. } catch (error) {
  144. reject([]);
  145. }
  146. });
  147. }
  148. /**
  149. * 根据用户id和token调用SSO接口获取信息
  150. *
  151. * @param {string} username
  152. * @param {string} password
  153. * @return {object}
  154. */
  155. async getInfoFromSSO2(ssoID, token) {
  156. let postData = {
  157. url: 'http://sso.smartcost.com.cn/building/api/login/auth',
  158. form: {ssoID: ssoID, token: token},
  159. encoding: 'utf8'
  160. };
  161. return new Promise(function (resolve, reject) {
  162. try {
  163. // 请求接口
  164. Request.post(postData, function (err, postResponse, body) {
  165. if (err) {
  166. console.log('111');
  167. reject('请求错误');
  168. }
  169. if (postResponse.statusCode !== 200) {
  170. reject('通行证验证失败!');
  171. }
  172. resolve(body);
  173. });
  174. } catch (error) {
  175. reject([]);
  176. }
  177. });
  178. }
  179. /**
  180. * 注册sso账号或修改sso的qq信息
  181. *
  182. * @param {string} username
  183. * @param {string} password
  184. * @return {object}
  185. */
  186. async regForSSO(mobile, qq, pwd = '', name = '') {
  187. let postData = {
  188. url: 'http://sso.smartcost.com.cn/building/api/reg',
  189. form: {mobile: mobile, qq: qq, password: pwd, name: name},
  190. encoding: 'utf8'
  191. };
  192. return new Promise(function (resolve, reject) {
  193. try {
  194. // 请求接口
  195. Request.post(postData, function (err, postResponse, body) {
  196. if (err) {
  197. console.log('111');
  198. reject('请求错误');
  199. }
  200. if (postResponse.statusCode !== 200) {
  201. reject('通行证验证失败!');
  202. }
  203. resolve(body);
  204. });
  205. } catch (error) {
  206. reject([]);
  207. }
  208. });
  209. }
  210. /**
  211. * 标记用户
  212. *
  213. * @param {object} userData
  214. * @param {Object} request
  215. * @return {Promise}
  216. */
  217. async markUser(userData, request = null) {
  218. let userDataFromDb = await this.findDataBySsoId(userData.ssoId);
  219. // let userDataFromDb = await this.findDataByName(userData.username); //后面新增的账号可淘汰这方法,当前使用是为了兼容旧的账号
  220. let result = false;
  221. userData.latest_login = new Date().getTime();
  222. if (userDataFromDb === null) {
  223. // 不存在用户则入库
  224. this.setScene();//恢复场景,用户有可能公司real_name等信息为空,不能设置为必填
  225. result = await this.addUser(userData);
  226. userDataFromDb = result;
  227. } else {
  228. // 存在则新增登录信息并更新账号信息
  229. let condition = {ssoId: userData.ssoId};
  230. // let condition = {username: userData.username};
  231. let UpdateData = {
  232. email : userData.email,
  233. mobile : userData.mobile,
  234. // ssoId : userData.ssoId,
  235. qq: userData.qq,
  236. latest_login:userData.latest_login,
  237. isUserActive: userData.isUserActive,
  238. token: userData.token
  239. };
  240. console.log("updateUser 开始 -------------------------------");
  241. let updateResult = await this.updateUser(condition,UpdateData);
  242. console.log("updateUser 完成 -------------------------------");
  243. if (updateResult.ok === 1) {
  244. let logModel = new LogModel();
  245. result = await logModel.addLoginLog(userDataFromDb._id, request);
  246. console.log("addLoginLog 完成 -------------------------------");
  247. }
  248. }
  249. request.session.sessionUser.id = userDataFromDb._id;
  250. request.session.sessionUser.real_name = userDataFromDb.real_name;
  251. request.session.sessionUser.latest_used = userDataFromDb.latest_used;//设置最近使用的编办
  252. return result;
  253. }
  254. /**
  255. * 选择场景
  256. *
  257. * @param {string} scene
  258. */
  259. setScene(scene = '') {
  260. /* switch (scene) {
  261. case 'saveInfo':
  262. this.model.schema.path('real_name').required(true);
  263. this.model.schema.path('company').required(true);
  264. this.model.schema.path('province').required(true);
  265. this.model.schema.path('version').required(true);
  266. break;
  267. case '':
  268. this.model.schema.path('real_name').required(false);
  269. this.model.schema.path('company').required(false);
  270. this.model.schema.path('province').required(false);
  271. this.model.schema.path('version').required(false);
  272. }*/
  273. }
  274. /**
  275. * 根据用户名查找数据
  276. *
  277. * @param {string} username
  278. * @return {object}
  279. */
  280. findDataByName(username) {
  281. return this.db.findOne({username: username});
  282. }
  283. /**
  284. * 根据ssoID查找数据
  285. *
  286. * @param {string} ssoId
  287. * @return {object}
  288. */
  289. findDataBySsoId(ssoId) {
  290. return this.db.findOne({ssoId: ssoId});
  291. }
  292. /**
  293. * 根据手机号查找数据
  294. *
  295. * @param {string} mobile
  296. * @return {object}
  297. */
  298. findDataByMobile(mobile) {
  299. return this.db.findOne({mobile: mobile});
  300. }
  301. /**
  302. * 根据userId查找数据
  303. *
  304. * @param {string} ssoId
  305. * @return {object}
  306. */
  307. async findDataById(id, fields) {
  308. const objId = mongoose.Types.ObjectId(id);
  309. return fields ? await this.db.findOne({_id: objId}, fields) : await this.db.findOne({_id: objId});
  310. }
  311. /**
  312. * 验证用户token正确性
  313. * 一个账号不允许多处同时在线,每次登陆都会更新session和数据库的token,每个请求都会比对session和数据库的token
  314. * @param {String} id - 用户ID
  315. * @param {String} token - 登陆生成的token
  316. * @return {Boolean}
  317. */
  318. async checkToken(id, token) {
  319. const user = await this.findDataById(id, '-_id token');
  320. if (!user.token) { // 兼容第一次上线,已登陆的用户还没token,需要返回验证正确
  321. return true;
  322. }
  323. return user.token === token;
  324. }
  325. async findDataByAccount(account) {
  326. let userinfo = await this.db.findOne({mobile: account});
  327. let userinfo2 = await this.db.findOne({email: account});
  328. if (userinfo) {
  329. return userinfo;
  330. } else if (userinfo2) {
  331. return userinfo2;
  332. } else {
  333. return false;
  334. }
  335. }
  336. /**
  337. * 新增用户
  338. *
  339. * @param {object} userData
  340. * @return {Promise|boolean}
  341. */
  342. addUser(userData) {
  343. let insertData = {
  344. ssoId: userData.ssoId,
  345. username: userData.username,
  346. email: userData.email,
  347. mobile: userData.mobile,
  348. qq: userData.qq,
  349. create_time: new Date().getTime(),
  350. latest_login: new Date().getTime(),
  351. isUserActive: userData.isUserActive,
  352. token: userData.token,
  353. real_name: userData.real_name ? userData.real_name : '',
  354. province: userData.province ? userData.province : -1,
  355. company: userData.company ? userData.company : '',
  356. };
  357. return this.db.create(insertData);
  358. }
  359. //更新最近使用编办ID
  360. async updateLatestUsed(userID,compilationID){
  361. if(userID && compilationID){
  362. return await this.db.update({'_id':userID},{'latest_used':compilationID});
  363. }
  364. }
  365. /**
  366. * 更新用户数据
  367. *
  368. * @param {object} updateData
  369. * @return {Promise}
  370. */
  371. async updateUser(condition, updateData) {
  372. if (Object.keys(condition).length <= 0 || Object.keys(updateData).length <= 0) {
  373. return null;
  374. }
  375. return await this.db.update(condition, updateData);
  376. }
  377. async addVersion(condition, versionInfo){
  378. return await this.db.findOneAndUpdate(condition, {$addToSet: {versionInfo: versionInfo}});
  379. }
  380. async removeVersion(userId, compilationId){
  381. let userObjId = mongoose.Types.ObjectId(userId);
  382. return await this.db.findOneAndUpdate({_id: userObjId}, {$pull: {versionInfo: {compilationId: compilationId}}});
  383. }
  384. /**
  385. * 判断用户使用免费版还是专业版
  386. *
  387. * @param ssoId
  388. * @param compilationId
  389. * @return {version}
  390. */
  391. async getVersionFromUpgrade(ssoId, compilationId){
  392. let version = '大司空云计价(免费版)';
  393. let lock = 0;
  394. let userData = await this.findDataBySsoId(ssoId);
  395. if (userData.upgrade_list !== undefined) {
  396. let compilationInfo = userData.upgrade_list.find(function (item) {
  397. return item.compilationID === compilationId;
  398. });
  399. if (compilationInfo !== undefined && compilationInfo.isUpgrade === true) {
  400. version = '大司空云计价(专业版)';
  401. if (compilationInfo.lock !== undefined && compilationInfo.lock !== '') {
  402. lock = compilationInfo.lock;
  403. }
  404. }
  405. }
  406. return {version: version, lock: lock};
  407. }
  408. /**
  409. * 判断用户是免费版还是专业版用户
  410. */
  411. async isFree(ssoId, compilationId) {
  412. const userData = await this.findDataBySsoId(ssoId);
  413. if (!userData) {
  414. throw '不存在此用户';
  415. }
  416. const upgrade_list = userData.upgrade_list;
  417. let free = true;
  418. if (upgrade_list && upgrade_list.length > 0) {
  419. const upgrade = upgrade_list.find(function (item) {
  420. return item.compilationID === compilationId && item.isUpgrade === true;
  421. });
  422. if (upgrade) {
  423. free = false;
  424. }
  425. }
  426. return free
  427. }
  428. /**
  429. * 从session中判断用户是否是免费版
  430. * @param {String} sessionVersion
  431. * @return {Boolean}
  432. */
  433. isFreeFromSession(sessionVersion) {
  434. if (!sessionVersion) {
  435. return true;
  436. }
  437. return sessionVersion.indexOf('免费') >= 0;
  438. }
  439. /*
  440. * 添加联系人,互相添加
  441. */
  442. async addContact(userID, contactID) {
  443. const data = [
  444. { user: userID, contact: contactID },
  445. { user: contactID, contact: userID }
  446. ];
  447. const task = [];
  448. for (const { user, contact } of data) {
  449. const hasThisContact = await this.model.count({_id: mongoose.Types.ObjectId(user), 'contacts.userID': contact});
  450. // 没有则添加
  451. if (!hasThisContact) {
  452. task.push(this.model.update({_id: mongoose.Types.ObjectId(user)}, {$push: {contacts: {userID: contact}}}));
  453. }
  454. }
  455. if (task.length) {
  456. await Promise.all(task);
  457. }
  458. }
  459. async getContacts(userID) {
  460. const user = await this.model.findOne({_id: mongoose.Types.ObjectId(userID)}, 'contacts').lean();
  461. if (!user || !user.contacts || !user.contacts.length) {
  462. return [];
  463. }
  464. const userIDList = user.contacts.map(item => mongoose.Types.ObjectId(item.userID));
  465. const users = await this.model.find({_id: {$in: userIDList}}, 'real_name mobile company');
  466. return users;
  467. }
  468. /**
  469. * 获取列表
  470. *
  471. * @param {object} condition
  472. * @param {number} page
  473. * @param {Number} pageSize
  474. * @return {promise}
  475. */
  476. async getList(condition = null, page = 1, pageSize = 30, sort = {_id:-1}) {
  477. page = parseInt(page);
  478. page = page <= 1 ? 1 : page;
  479. let option = {pageSize: pageSize, offset: parseInt((page - 1) * pageSize), sort: sort};
  480. console.log(condition, option);
  481. let userList = await this.db.find(condition, null, option);
  482. userList = userList.length > 0 ? userList : [];
  483. return userList;
  484. }
  485. /**
  486. * 获取过滤条件
  487. *
  488. * @return {Object}
  489. */
  490. getFilterCondition(request) {
  491. let condition = {};
  492. let regtime = request.query.regtime;
  493. regtime = regtime !== '' && regtime !== undefined ? parseInt(regtime) : 0;
  494. if (regtime !== 0) {
  495. condition.create_time = this.getTimestamp(regtime);
  496. }
  497. //最近登录时间
  498. let loginTime = request.query.loginTime;
  499. loginTime = loginTime !== '' && loginTime !== undefined ? parseInt(loginTime) : 0;
  500. if (loginTime !== 0) {
  501. condition.latest_login = this.getTimestamp(loginTime);
  502. }
  503. let version = request.query.version;
  504. if(version !== '' && version !== undefined) {
  505. condition.version = version;
  506. }
  507. // 已升级费用定额
  508. let upGrade = request.query.upGrade;
  509. if(upGrade !== '' && upGrade !== undefined){
  510. condition.upgrade_list = {"$elemMatch":{"compilationID":upGrade,"isUpgrade":true}};
  511. }
  512. // 最近使用费用定额
  513. let latestUsed = request.query.latestUsed;
  514. if(latestUsed !== '' && latestUsed !== undefined){
  515. condition.latest_used = latestUsed;
  516. }
  517. // 最近登录时长
  518. let onlineTimes = request.query.onlineTimes;
  519. onlineTimes = onlineTimes !== '' && onlineTimes !== undefined ? parseInt(onlineTimes) : 0;
  520. if (onlineTimes !== 0) {
  521. condition.online_times = this.getOnlineTimestamp(onlineTimes);
  522. }
  523. // 企业所在地区
  524. let province = request.query.province;
  525. province = province !== '' && province !== undefined ? parseInt(province) - 1 : -1;
  526. if (province !== -1) {
  527. condition.province = province;
  528. }
  529. let keyword = request.query.keyword;
  530. if (keyword !== '' && keyword !== undefined) {
  531. condition.$or = [{real_name : {$regex: keyword}},{email : {$regex: keyword}},{mobile : {$regex: keyword}},{qq : {$regex: keyword}},{company : {$regex: keyword}}];
  532. }
  533. return condition;
  534. }
  535. /**
  536. * 获取时间戳区间
  537. *
  538. * @return {Object}
  539. */
  540. getTimestamp(type) {
  541. let startTime = '';
  542. switch (type) {
  543. case 1 :
  544. startTime = Date.parse(new Date())-86400*1000;
  545. break;
  546. case 2 :
  547. startTime = Date.parse(new Date())-86400*1000*3;
  548. break;
  549. case 3 :
  550. startTime = Date.parse(new Date())-86400*1000*7;
  551. break;
  552. case 4 :
  553. startTime = Date.parse(new Date())-86400*1000*30;
  554. break;
  555. default :
  556. break;
  557. }
  558. let endTime = Date.parse(new Date());
  559. return startTime === '' ? '' : {'$gte': startTime, '$lt': endTime};
  560. }
  561. /**
  562. * 获取时间戳区间
  563. *
  564. * @return {Object}
  565. */
  566. getOnlineTimestamp(type) {
  567. let startTime = 0;
  568. switch (type) {
  569. case 1 :
  570. startTime = 1800*1000;
  571. break;
  572. case 2 :
  573. startTime = 3600*1000;
  574. break;
  575. case 3 :
  576. startTime = 3600*1000*2;
  577. break;
  578. case 4 :
  579. startTime = 3600*1000*3;
  580. break;
  581. default :
  582. break;
  583. }
  584. return startTime === 0 ? '' : {'$gte': startTime};
  585. }
  586. /**
  587. * 获取daymsg
  588. *
  589. */
  590. getDayMsg(index){
  591. return this.dayMsg[index];
  592. }
  593. }
  594. module.exports = UserModel;