cloudEditionService.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /**
  2. * Created by MyPC on 2019/11/5.
  3. */
  4. var models=require('../models');
  5. var moment = require('moment');
  6. const hash=require('../class/hash');
  7. const urlObj=require("url");
  8. //http://qa.smartcost.com.cn:2060/cld
  9. //"https://yhuat.smartcost.com.cn/cld";
  10. var curingUrl ="https://yhuat.smartcost.com.cn/cld"; //'https://yhyun.smartcost.com.cn/cld';
  11. //var curingUrl ="http://qa.smartcost.com.cn:2060/cld";
  12. var buildUrl = 'https://yun.smartcost.com.cn/cld';
  13. var cloudService={
  14. /**
  15. * 获得养护列表
  16. * paramsCompilation 传参包含
  17. * -compilation 已升级编办
  18. * -latestCompilation 最近使用编办
  19. */
  20. getCuringList: async function(req, res){
  21. //获得远程养护用户
  22. //分页数据
  23. var page=1;
  24. var compilation='';
  25. var latestCompilation='';
  26. var sortField='create_time';
  27. var sort='-1';
  28. var wd=''; //检索内容
  29. //编办参数-排序参数-当前页
  30. if(hash.isExistence(req.url)){
  31. var params = urlObj.parse(req.url, true).query;
  32. if(hash.isExistence(params.compilation)){
  33. compilation=params.compilation;
  34. }
  35. if(hash.isExistence(params.latestCompilation)){
  36. latestCompilation=params.latestCompilation;
  37. }
  38. //排序
  39. var mAge=1000*60*60*24*28;
  40. if(hash.isExistence(params.sortField)){
  41. res.cookie('sortField','create_time', {maxAge: mAge});
  42. sortField='create_time';
  43. if(params.sortField=='latest_login'){
  44. sortField='latest_login';
  45. res.cookie('sortField','latest_login', {maxAge: mAge});
  46. }
  47. }else if(hash.isExistence(req.cookies.sortField)){
  48. sortField=req.cookies.sortField;
  49. }
  50. if(hash.isExistence(params.sort)){
  51. res.cookie('sort','-1', {maxAge: mAge});
  52. sort=-1;
  53. if(params.sort=='1'){
  54. sort=1;
  55. res.cookie('sort','1', {maxAge: mAge});
  56. }
  57. }else if(hash.isExistence(req.cookies.sort)){
  58. sort=req.cookies.sort;
  59. }
  60. //当前页数
  61. var page=req.params.currentPage;
  62. if(!hash.isNotANumber(page)){
  63. page=1;
  64. }
  65. //检索 内容
  66. if(hash.isExistence(params.wd)){
  67. wd=params.wd;
  68. }
  69. }
  70. //分页参数
  71. var limit=12;
  72. var offset=0;
  73. if(page!=1){
  74. var pg=page-1;
  75. offset=pg*limit;
  76. }
  77. //排序参数
  78. //var sortField=curingInterfaceConfig.sort[0];
  79. //var sort=curingInterfaceConfig.sort[1];
  80. var url= curingUrl+'/getUserList'+
  81. '?page='+page+'&pageSize='+limit+'&latestUsed='+latestCompilation+'&upGrade='+compilation+'&sortField='+sortField+'&sortType='+sort+
  82. '&keyword='+wd;
  83. /* 接口请求获得的信息
  84. * userInfo
  85. * pageData
  86. * compilationlist
  87. */
  88. var curingCloudData=await curlRequest(url);
  89. var curingCloudList=curingCloudData.userInfo;
  90. //组合数据以获得本地扩展数据
  91. if(!hash.isExistence(curingCloudList)){
  92. curingCloudList={curingList:[],pageData:[],sortList:[]};
  93. }
  94. var mobile=[];
  95. curingCloudList.forEach(function(v,i){
  96. mobile.push(v.mobile);
  97. });
  98. if(!hash.isExistence(mobile)){
  99. return {curingList:[],pageData:[]};
  100. }
  101. //获得指定数据
  102. var curingList = await models.cloud_curing.getCuringInMobile(mobile);
  103. //组合同步云版数据
  104. var falg=false;
  105. curingCloudList.forEach(async function(cclValue,cclKey){
  106. curingCloudList[cclKey].client_id=0
  107. falg=true;
  108. for(var i=0;i<curingList.length;i++){
  109. if(cclValue.mobile==curingList[i].mobile){
  110. curingCloudList[cclKey].client_id=curingList[i].client_id;
  111. falg=false;
  112. break;
  113. }
  114. }
  115. if(falg){//新增本地扩展信息
  116. sid=hash.hashDecode(STAFF.sid);
  117. var obj = {
  118. mobile: cclValue.mobile,
  119. sso_id: cclValue.ssoId,
  120. sid: sid,
  121. cid: STAFF.cid,
  122. status: 2,
  123. };
  124. var detail = await models.cloud_curing.create(obj);
  125. }
  126. });
  127. //获得编办数据
  128. var compilationList=await this.getCompilationList(); //编办信息
  129. //console.log(compilationList);
  130. //数据组合--cld客户数据
  131. var cid=[];
  132. curingCloudList.forEach(function(v,i){
  133. if(v.client_id!=0){
  134. cid.push(v.client_id);
  135. }
  136. });
  137. var clientList =[];
  138. if(hash.isExistence(cid)){
  139. var attributes= ['cid', 'clientname', 'companyid', 'companyname'];
  140. clientList = await models.CLD_client.findAllInCid(cid,attributes);
  141. }
  142. //组合页面需要展示的数据
  143. curingCloudList.forEach(function(v,i){
  144. curingCloudList[i].clientInfo={};
  145. clientList.forEach(function(clientVal,clientI){
  146. if(v.client_id==clientVal.cid){
  147. curingCloudList[i].clientInfo=clientVal;
  148. }
  149. });
  150. curingCloudList[i].addtime=moment(v.create_time).format('YYYY-MM-DD HH:mm');
  151. curingCloudList[i].latest_login=moment(v.latest_login).format('YYYY-MM-DD HH:mm');
  152. curingCloudList[i].client_id=hash.hashEncode(v.client_id.toString());
  153. curingCloudList[i].ssoId=hash.hashEncode(v.ssoId.toString());
  154. //最后使用
  155. curingCloudList[i].latest_usedName='';
  156. for(var ci=0;ci<compilationList.length;ci++){
  157. if(compilationList[ci]._id==v.latest_used){
  158. curingCloudList[i].latest_usedName=compilationList[ci].name;
  159. break;
  160. }
  161. }
  162. //登录时长
  163. if(hash.isExistence(v.online_list)){
  164. var onlineTimeLength=v.online_list.length-1;
  165. curingCloudList[i].online_newest=v.online_list[onlineTimeLength].online_times+'('+v.online_list[onlineTimeLength].dateString+')';
  166. }else{
  167. curingCloudList[i].online_newest='';
  168. }
  169. //已升级专业版
  170. curingCloudList[i].upgradeListName='';
  171. for(var gi=0;gi<v.upgrade_list.length;gi++){
  172. for(var ci=0;ci<compilationList.length;ci++){
  173. if( compilationList[ci]._id == v.upgrade_list[gi].compilationID&&v.upgrade_list[gi].isUpgrade) {
  174. curingCloudList[i].upgradeListName+='<span class="badge badge-primary">'+compilationList[ci].name+'</span>';
  175. break;
  176. }
  177. }
  178. }
  179. });
  180. var parameter='?compilation='+compilation+'&latestCompilation='+latestCompilation+'&sortField='+sortField+'&sort='+sort;
  181. //分页计算
  182. var pageData = await getPage('','',page,limit,parameter,curingCloudData.pageData.total);
  183. return {curingList:curingCloudList,pageData:pageData,sortList:[sortField,sort]};
  184. },
  185. /**
  186. * 获得养护编办--接口拉取
  187. */
  188. getCompilationList:async function (){
  189. var url= curingUrl+'/getCompilationList';
  190. var data= await curlRequest(url);
  191. return data;
  192. },
  193. /**
  194. * 用户详情页-数据
  195. */
  196. getCuringBySsoid: async function(ssoid,client_id){
  197. if(!hash.isNotANumber(ssoid)){
  198. ssoid=hash.hashDecode(ssoid);
  199. if(!hash.isNotANumber(ssoid)){
  200. return [];
  201. }
  202. }
  203. var url= curingUrl+'/getUsersAndCompilation?ssoID='+ ssoid;
  204. var userData= await curlRequest(url);
  205. if(!hash.isExistence(userData)){
  206. return [];
  207. }
  208. //最后使用
  209. userData['userInfo']['latest_usedName']='';
  210. //本地养护用户
  211. userData['curingInfo']=await models.cloud_curing.getCuringBySsoid(ssoid);
  212. client_id=userData['curingInfo']['client_id'];
  213. var detail =[];
  214. if(hash.isExistence(client_id)){
  215. detail = await models.CLD_client.findById(client_id);
  216. }
  217. userData['clientInfo']=detail;
  218. //组合数据升级产品数据
  219. userData['compilationList'].forEach(function(value,key){
  220. userData['userInfo']['upgrade_list'].forEach(function(v,k){
  221. if(v['compilationID']==value['_id']){
  222. userData['compilationList'][key]['isUpgrade']=v['isUpgrade'];
  223. }
  224. });
  225. if(value['_id']==userData['userInfo']['latest_used']){
  226. userData['userInfo']['latest_usedName']=value['name'];
  227. }
  228. });
  229. userData['userInfo']['ssoIdKey']=hash.hashEncode(userData['userInfo']['ssoId'].toString());
  230. //获得操作日志
  231. userData['operateLog']=[];
  232. if(hash.isExistence(userData['curingInfo'])){
  233. userData['operateLog']=await models.operate_log.findByCondition(1,hash.hashDecode(userData['curingInfo']['id']));
  234. }
  235. return userData;
  236. },
  237. /**
  238. * 用户升级专业版
  239. */
  240. upCuringDo: async function(data){
  241. var url = curingUrl+'/setUserUpgrade';
  242. var ssoid=hash.hashDecode(data.ssoid);
  243. var postData ={'ssoId':ssoid,'cid':data.compilationId};
  244. var result= await postRequest(url,postData);
  245. result=JSON.parse(result);
  246. if(result.error==0){ //接口更新成
  247. var curingDetail=await models.cloud_curing.getCuringByMobile(data.mobile);
  248. var id='';
  249. if(hash.isExistence(curingDetail)){ //是否需要同步同步到本地库中
  250. var url= curingUrl+'/getUsersAndCompilation?ssoID='+ ssoid;
  251. var userData= await curlRequest(url);
  252. //获得更新数量updateTotal
  253. var updateTotal=0;
  254. userData['userInfo']['upgrade_list'].forEach(function(value,key){
  255. if(value['isUpgrade']){
  256. updateTotal++;
  257. }
  258. });
  259. //更新数量
  260. var values = {
  261. updateTotal: updateTotal
  262. }
  263. var where={
  264. where: {id: curingDetail['id']}
  265. };
  266. await models.cloud_curing.update(values,where);
  267. id=curingDetail['id'];
  268. }else{
  269. sid=hash.hashDecode(STAFF.sid);
  270. //获得登陆用户相关信息
  271. var obj = {
  272. mobile: data.mobile,
  273. sso_id: data.ssoid,
  274. compilation_id: data.compilationId,
  275. //curingCompany: data.name,
  276. sid: sid,
  277. cid: STAFF.cid,
  278. status: 1,
  279. //addtime: new Date().getTime(),
  280. updateTotal: 1
  281. };
  282. var detail=await models.cloud_curing.create(obj);
  283. id=detail['cloud_curing']['id'];
  284. }
  285. var operation=STAFF.username+'升级'+data.name;
  286. //记录锁日志
  287. await models.operate_log.createOperateLog(1,id,operation);
  288. return true;
  289. }else{
  290. return false;
  291. }
  292. },
  293. /**
  294. * 用户关联CLD客户
  295. */
  296. relevanceClientDo: async function(data){
  297. var cid=hash.hashDecode(data.cidKey);
  298. var ssoId=hash.hashDecode(data.ssoId);
  299. if(!hash.isNotANumber(cid)||!hash.isNotANumber(ssoId)){
  300. return false;
  301. }
  302. var curingDetail=await models.cloud_curing.getCuringBySsoid(ssoId);
  303. if(hash.isExistence(curingDetail)) {
  304. var clientDetail=await models.CLD_client.findById(cid);
  305. if(hash.isExistence(clientDetail)){
  306. var id=hash.hashDecode(curingDetail['id']);
  307. //更新数量
  308. var values = {
  309. client_id: cid
  310. }
  311. var where={
  312. where: {id: id}
  313. };
  314. await models.cloud_curing.update(values,where);
  315. //记录操作日志
  316. var operation=STAFF.username+'关联CLD联系人'+clientDetail.clientname;
  317. await models.operate_log.createOperateLog(1,id,operation);
  318. return true;
  319. }else{
  320. return false;
  321. }
  322. }else{
  323. return false;
  324. }
  325. },
  326. /**
  327. * 用户取消关联CLD客户
  328. */
  329. relieveClientDo: async function(data){
  330. var ssoId=hash.hashDecode(data.ssoId);
  331. if(!hash.isNotANumber(ssoId)){
  332. return false;
  333. }
  334. var curingDetail=await models.cloud_curing.getCuringBySsoid(ssoId);
  335. if(hash.isExistence(curingDetail)) {
  336. var id=hash.hashDecode(curingDetail['id']);
  337. //更新数量
  338. var values = {
  339. client_id: ''
  340. }
  341. var where={
  342. where: {id: id}
  343. };
  344. await models.cloud_curing.update(values,where);
  345. var clientDetail=await models.CLD_client.findById(curingDetail['client_id']);
  346. //记录操作日志
  347. var operation=STAFF.username+'移除CLD联系人'+clientDetail.clientname;
  348. await models.operate_log.createOperateLog(1,id,operation);
  349. return true;
  350. }else{
  351. return false;
  352. }
  353. },
  354. /**
  355. * 获得本地编办列表--废弃
  356. */
  357. getCuringCompilationList:async function (){
  358. var compilationList = await models.cloud_curing.getCuringCompilationList();
  359. return compilationList;
  360. },
  361. //获取本地养护数据--可能废弃
  362. getBindCuringByMobile:async function (mobile){
  363. var curingList = await models.cloud_curing.getCuringByMobile(mobile);
  364. if (curingList!=null){
  365. var cid=[curingList['client_id']];
  366. var attributes= ['cid', 'clientname', 'companyid', 'companyname'];
  367. var clientList = await models.CLD_client.findAllInCid(cid,attributes);
  368. curingList.clientInfo={};
  369. clientList.forEach(function(clientVal,clientI){
  370. if(curingList.client_id==clientVal.cid){
  371. curingList.clientInfo=clientVal;
  372. }
  373. });
  374. curingList.addtime=moment.unix(curingList.addtime).format('YYYY-MM-DD HH:mm');
  375. }
  376. return curingList;
  377. },
  378. //根据手机号码获得养护用户信息--可能废弃
  379. getCuringByMobile: async function(mobile){
  380. var url= curingUrl+'/getUsersAndCompilation?mobile='+ mobile;
  381. var userData= await curlRequest(url);
  382. return userData;
  383. },
  384. }
  385. module.exports = cloudService;
  386. //接口调用
  387. var curlRequest = async function(url){
  388. var got = require('got');
  389. var data=[];
  390. await got(url, { json: true,timeout:300 }).then(response => {
  391. data= response.body.data;
  392. }).catch(error => {
  393. console.log('GET调用接口超时');
  394. });
  395. return data;
  396. }
  397. var postRequest= async function(url,data){
  398. //var fs = require('fs');
  399. var got = require('got');
  400. var qs = require('querystring');
  401. var keysList=Object.keys(data);
  402. var content = qs.stringify(data);
  403. result= await got.post(url, {
  404. body: content,
  405. timeout:300,
  406. headers: {
  407. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  408. }
  409. }).catch(error => {
  410. console.log('POST调用接口超时');
  411. });
  412. return result.body;
  413. //var FormData = require('form-data');
  414. //var form = new FormData();
  415. /* keysList.forEach(function(v,i){
  416. console.log(v);
  417. form.append(v, data[v]);
  418. });*/
  419. //form.append('ssoId', data.ssoId);
  420. //form.append('cid', data.cid);
  421. //console.log(form);
  422. //console.log(keysList);
  423. //form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
  424. //?longle=12
  425. //url='http://cld.com/longle/list/add';
  426. //var content = qs.stringify({'longle':125});
  427. //console.log(data);
  428. }
  429. var modelsQuery= async function(sql){
  430. var data=[];
  431. await models.sequelize.query(sql).spread((results, metadata) => {
  432. // 结果将是一个空数组,元数据将包含受影响的行数。
  433. data=results;
  434. });
  435. return data;
  436. }
  437. /**
  438. * 分页相关数据计算
  439. * modelsTable 数据表对象
  440. * parameter 设置传的参数
  441. */
  442. var getPage= async function(modelsTable,where,currentPage,pageSize,parameter,totalCount){
  443. if(!hash.isExistence(totalCount)){
  444. if(hash.isExistence(where)){
  445. totalCount=await modelsTable.count({where});
  446. }else{
  447. totalCount=await modelsTable.count();
  448. }
  449. }
  450. currentPage=parseInt(currentPage);
  451. var total =0;
  452. if(totalCount!=0){
  453. total = Math.trunc ( totalCount / pageSize );
  454. }
  455. var totalPage = (totalCount % pageSize) == 0 ? total : total + 1;
  456. var pageMax=1;
  457. var pageWidth=15;
  458. //开始页数
  459. var startPage=1;
  460. if (currentPage >= pageWidth) {
  461. pageMax = parseInt ( currentPage / pageWidth ) + 1;
  462. startPage = parseInt ( currentPage / pageWidth ) * pageWidth - 1;
  463. }
  464. //结束页数
  465. var endPage=pageWidth * pageMax;
  466. if(endPage>totalPage){
  467. endPage=totalPage;
  468. }
  469. var page={
  470. 'currentPage':currentPage, //当前页
  471. 'previousPage':currentPage!=0?currentPage-1:currentPage, //上一页
  472. 'nextPage': currentPage==totalPage ? totalPage:currentPage+1,
  473. 'totalPage':totalPage,
  474. 'startPage':startPage,
  475. 'endPage':endPage,
  476. 'pageWidth':pageWidth,
  477. 'parameter':encodeURI(parameter)
  478. };
  479. return page;
  480. }