cloudEditionService.js 18 KB

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