cloudEditionService.js 18 KB

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