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. if(params.sortField=='latest_login'){
  42. res.cookie('sortField','latest_login', {maxAge: mAge});
  43. }
  44. }
  45. if(hash.isExistence(params.sort)){
  46. res.cookie('sort','-1', {maxAge: mAge});
  47. if(params.sortField=='1'){
  48. res.cookie('sort','1', {maxAge: mAge});
  49. }
  50. }
  51. //当前页数
  52. var page=req.params.currentPage;
  53. if(!hash.isNotANumber(page)){
  54. page=1;
  55. }
  56. }
  57. if(hash.isExistence(req.cookies.sortField)){
  58. sortField=req.cookies.sortField;
  59. }
  60. if(hash.isExistence(req.cookies.sort)){
  61. sort=req.cookies.sort;
  62. }
  63. //分页参数
  64. var limit=12;
  65. var offset=0;
  66. if(page!=1){
  67. var pg=page-1;
  68. offset=pg*limit;
  69. }
  70. //排序参数
  71. //var sortField=curingInterfaceConfig.sort[0];
  72. //var sort=curingInterfaceConfig.sort[1];
  73. var url= curingUrl+'/getUserList'+
  74. '?page='+page+'&pageSize='+limit+'&latestUsed='+latestCompilation+'&upGrade='+compilation+'&sortField='+sortField+'&sort='+sort;
  75. console.log(url);
  76. /* 接口请求获得的信息
  77. * userInfo
  78. * pageData
  79. * compilationlist
  80. */
  81. var curingCloudData=await curlRequest(url);
  82. var curingCloudList=curingCloudData.userInfo;
  83. //组合数据以获得本地扩展数据
  84. var mobile=[];
  85. curingCloudList.forEach(function(v,i){
  86. mobile.push(v.mobile);
  87. });
  88. if(!hash.isExistence(mobile)){
  89. return {curingList:[],pageData:[]};
  90. }
  91. //获得指定数据
  92. var curingList = await models.cloud_curing.getCuringInMobile(mobile);
  93. //组合同步云版数据
  94. var falg=false;
  95. curingCloudList.forEach(async function(cclValue,cclKey){
  96. curingCloudList[cclKey].client_id=0
  97. falg=true;
  98. for(var i=0;i<curingList.length;i++){
  99. if(cclValue.mobile==curingList[i].mobile){
  100. curingCloudList[cclKey].client_id=curingList[i].client_id;
  101. falg=false;
  102. break;
  103. }
  104. }
  105. if(falg){//新增本地扩展信息
  106. sid=hash.hashDecode(STAFF.sid);
  107. var obj = {
  108. mobile: cclValue.mobile,
  109. sso_id: cclValue.ssoId,
  110. // curingCompany: cclValue.company,
  111. sid: sid,
  112. cid: STAFF.cid,
  113. status: 2,
  114. //addtime: new Date().getTime(),
  115. //updateTotal: cclValue.isUserActive
  116. };
  117. var detail = await models.cloud_curing.create(obj);
  118. }
  119. });
  120. //获得编办数据
  121. var compilationList=await this.getCompilationList(); //编办信息
  122. //console.log(compilationList);
  123. //数据组合--cld客户数据
  124. var cid=[];
  125. curingCloudList.forEach(function(v,i){
  126. if(v.client_id!=0){
  127. cid.push(v.client_id);
  128. }
  129. });
  130. var clientList =[];
  131. if(hash.isExistence(cid)){
  132. var attributes= ['cid', 'clientname', 'companyid', 'companyname'];
  133. clientList = await models.CLD_client.findAllInCid(cid,attributes);
  134. }
  135. //组合页面需要展示的数据
  136. curingCloudList.forEach(function(v,i){
  137. curingCloudList[i].clientInfo={};
  138. clientList.forEach(function(clientVal,clientI){
  139. if(v.client_id==clientVal.cid){
  140. curingCloudList[i].clientInfo=clientVal;
  141. }
  142. });
  143. curingCloudList[i].addtime=moment(v.create_time).format('YYYY-MM-DD HH:mm');
  144. curingCloudList[i].latest_login=moment(v.latest_login).format('YYYY-MM-DD HH:mm');
  145. curingCloudList[i].client_id=hash.hashEncode(v.client_id.toString());
  146. //curingCloudList[i].sso_id=v.sso_id;
  147. //最后使用
  148. curingCloudList[i].latest_usedName='';
  149. for(var ci=0;ci<compilationList.length;ci++){
  150. if(compilationList[ci]._id==v.latest_used){
  151. curingCloudList[i].latest_usedName=compilationList[ci].name;
  152. break;
  153. }
  154. }
  155. //登录时长
  156. if(hash.isExistence(v.online_list)){
  157. var onlineTimeLength=v.online_list.length-1;
  158. curingCloudList[i].online_newest=v.online_list[onlineTimeLength].online_times+'('+v.online_list[onlineTimeLength].dateString+')';
  159. }else{
  160. curingCloudList[i].online_newest='';
  161. }
  162. //已升级专业版
  163. curingCloudList[i].upgradeListName='';
  164. for(var gi=1;gi<v.upgrade_list.length;gi++){
  165. for(var ci=0;ci<compilationList.length;ci++){
  166. if( compilationList[ci]._id == v.upgrade_list[gi].compilationID) {
  167. curingCloudList[i].upgradeListName+='<span class="badge badge-primary"><%= compilation.name %></span>';
  168. break;
  169. }
  170. }
  171. }
  172. });
  173. var parameter='?compilation='+compilation+'&latestCompilation='+latestCompilation;
  174. //分页计算
  175. var pageData = await getPage('','',page,limit,parameter,curingCloudData.pageData.total);
  176. //console.log(curingCloudList);
  177. return {curingList:curingCloudList,pageData:pageData};
  178. },
  179. //获得本地编办列表
  180. getCuringCompilationList:async function (){
  181. var compilationList = await models.cloud_curing.getCuringCompilationList();
  182. return compilationList;
  183. /*var curingList = await models.cloud_curing.getCuringByMobile(mobile);
  184. if (curingList!=null){
  185. var cid=[curingList['client_id']];
  186. var attributes= ['cid', 'clientname', 'companyid', 'companyname'];
  187. var clientList = await models.CLD_client.findAllInCid(cid,attributes);
  188. curingList.clientname='';
  189. curingList.companyname='';
  190. clientList.forEach(function(clientVal,clientI){
  191. if(curingList.client_id==clientVal.cid){
  192. curingList.clientname=clientVal.clientname;
  193. curingList.companyname=clientVal.companyname;
  194. }
  195. });
  196. curingList.addtime=moment.unix(curingList.addtime).format('YYYY-MM-DD HH:mm');
  197. }
  198. */
  199. },
  200. //获得养护编办
  201. getCompilationList:async function (){
  202. var url= curingUrl+'/getCompilationList';
  203. var data= await curlRequest(url);
  204. return data;
  205. },
  206. //获取本地养护数据
  207. getBindCuringByMobile:async function (mobile){
  208. var curingList = await models.cloud_curing.getCuringByMobile(mobile);
  209. if (curingList!=null){
  210. var cid=[curingList['client_id']];
  211. var attributes= ['cid', 'clientname', 'companyid', 'companyname'];
  212. var clientList = await models.CLD_client.findAllInCid(cid,attributes);
  213. curingList.clientInfo={};
  214. clientList.forEach(function(clientVal,clientI){
  215. if(curingList.client_id==clientVal.cid){
  216. curingList.clientInfo=clientVal;
  217. }
  218. });
  219. curingList.addtime=moment.unix(curingList.addtime).format('YYYY-MM-DD HH:mm');
  220. }
  221. return curingList;
  222. },
  223. //根据手机号码获得养护用户信息
  224. getCuringByMobile: async function(mobile){
  225. var url= curingUrl+'/getUsersAndCompilation?mobile='+ mobile;
  226. var userData= await curlRequest(url);
  227. return userData;
  228. },
  229. /**
  230. * 用户详情页-数据
  231. */
  232. getCuringBySsoid: async function(ssoid,client_id){
  233. var curingSsoid=ssoid;
  234. if(!hash.isNotANumber(ssoid)){
  235. ssoid=hash.hashDecode(ssoid);
  236. if(!hash.isNotANumber(ssoid)){
  237. return [];
  238. }
  239. }
  240. var url= curingUrl+'/getUsersAndCompilation?ssoID='+ ssoid;
  241. var userData= await curlRequest(url);
  242. if(!hash.isExistence(userData)){
  243. return [];
  244. }
  245. //本地养护用户
  246. userData['curingInfo']=await models.cloud_curing.getCuringBySsoid(curingSsoid);
  247. client_id=userData['curingInfo']['client_id'];
  248. /*userData['clientInfo']='';
  249. if(!hash.isNotANumber(client_id)){
  250. client_id=hash.hashDecode(client_id);
  251. if(!hash.isNotANumber(client_id)){
  252. userData['clientInfo']=[];
  253. }
  254. }*/
  255. var detail =[];
  256. if(hash.isExistence(client_id)){
  257. detail = await models.CLD_client.findById(client_id);
  258. }
  259. userData['clientInfo']=detail;
  260. //组合数据升级产品数据
  261. userData['compilationList'].forEach(function(value,key){
  262. userData['userInfo']['upgrade_list'].forEach(function(v,k){
  263. if(v['compilationID']==value['_id']){
  264. userData['compilationList'][key]['isUpgrade']=v['isUpgrade'];
  265. }
  266. });
  267. });
  268. userData['userInfo']['ssoIdKey']=hash.hashEncode(userData['userInfo']['ssoId'].toString());
  269. //获得操作日志
  270. userData['operateLog']=[];
  271. if(hash.isExistence(userData['curingInfo'])){
  272. userData['operateLog']=await models.operate_log.findByCondition(1,hash.hashDecode(userData['curingInfo']['id']));
  273. }
  274. return userData;
  275. },
  276. upCuringDo: async function(data){
  277. var url = curingUrl+'/setUserUpgrade';
  278. var ssoid=hash.hashDecode(data.ssoid);
  279. var postData ={'ssoId':ssoid,'cid':data.compilationId};
  280. var result= await postRequest(url,postData);
  281. result=JSON.parse(result);
  282. if(result.error==0){ //接口更新成
  283. var curingDetail=await models.cloud_curing.getCuringByMobile(data.mobile);
  284. var id='';
  285. if(hash.isExistence(curingDetail)){ //是否需要同步同步到本地库中
  286. var url= curingUrl+'/getUsersAndCompilation?ssoID='+ ssoid;
  287. var userData= await curlRequest(url);
  288. //获得更新数量updateTotal
  289. var updateTotal=0;
  290. userData['userInfo']['upgrade_list'].forEach(function(value,key){
  291. if(value['isUpgrade']){
  292. updateTotal++;
  293. }
  294. });
  295. //更新数量
  296. var values = {
  297. updateTotal: updateTotal
  298. }
  299. var where={
  300. where: {id: curingDetail['id']}
  301. };
  302. await models.cloud_curing.update(values,where);
  303. id=curingDetail['id'];
  304. }else{
  305. sid=hash.hashDecode(STAFF.sid);
  306. //获得登陆用户相关信息
  307. var obj = {
  308. mobile: data.mobile,
  309. sso_id: data.ssoid,
  310. compilation_id: data.compilationId,
  311. curingCompany: data.name,
  312. sid: sid,
  313. cid: STAFF.cid,
  314. status: 1,
  315. addtime: new Date().getTime(),
  316. updateTotal: 1
  317. };
  318. var detail=await models.cloud_curing.create(obj);
  319. id=detail['cloud_curing']['id'];
  320. }
  321. var operation=STAFF.username+'升级'+data.name;
  322. //记录锁日志
  323. await models.operate_log.createOperateLog(1,id,operation);
  324. return true;
  325. }else{
  326. return false;
  327. }
  328. },
  329. //养护用户绑定CLD客户
  330. relevanceClientDo: async function(data){
  331. var cid=hash.hashDecode(data.cidKey);
  332. var id=hash.hashDecode(data.id);
  333. if(!hash.isNotANumber(cid)||!hash.isNotANumber(id)){
  334. return false;
  335. }
  336. var curingDetail=await models.cloud_curing.getCuringById(id);
  337. if(hash.isExistence(curingDetail)) {
  338. var clientDetail=await models.CLD_client.findById(cid);
  339. if(hash.isExistence(clientDetail)){
  340. //更新数量
  341. var values = {
  342. client_id: cid
  343. }
  344. var where={
  345. where: {id: curingDetail['id']}
  346. };
  347. await models.cloud_curing.update(values,where);
  348. //记录操作日志
  349. var operation=STAFF.username+'关联CLD联系人'+clientDetail.clientname;
  350. await models.operate_log.createOperateLog(1,id,operation);
  351. return true;
  352. }else{
  353. return false;
  354. }
  355. }else{
  356. return false;
  357. }
  358. },
  359. //移除关联CLD客户
  360. relieveClientDo: async function(data){
  361. var id=hash.hashDecode(data.id);
  362. if(!hash.isNotANumber(id)){
  363. return false;
  364. }
  365. var curingDetail=await models.cloud_curing.getCuringById(id);
  366. if(hash.isExistence(curingDetail)) {
  367. //更新数量
  368. var values = {
  369. client_id: ''
  370. }
  371. var where={
  372. where: {id: curingDetail['id']}
  373. };
  374. await models.cloud_curing.update(values,where);
  375. var clientDetail=await models.CLD_client.findById(curingDetail['client_id']);
  376. //记录操作日志
  377. var operation=STAFF.username+'移除CLD联系人'+clientDetail.clientname;
  378. await models.operate_log.createOperateLog(1,id,operation);
  379. return true;
  380. }else{
  381. return false;
  382. }
  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. }