cloudBuildEditionService.js 19 KB

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