cloudEditionService.js 18 KB

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