cloudEditionService.js 18 KB

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