cloudEditionService.js 17 KB

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