cloudEditionService.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. //获得客户服务日志
  240. userData['serviceLog']=[];
  241. if(hash.isExistence(userData['clientInfo'])){
  242. userData['serviceLog']=await models.CLD_service_log.findAllByClientid(client_id);
  243. }
  244. return userData;
  245. },
  246. /**
  247. * 用户升级专业版
  248. */
  249. upCuringDo: async function(data){
  250. var url = curingUrl+'/setUserUpgrade';
  251. var ssoid=hash.hashDecode(data.ssoid);
  252. let deadline=data.deadline;
  253. let smssend=0;
  254. if(data.smssend==='true'){
  255. smssend=1;
  256. }
  257. let status=1;
  258. if(data.status==='2'){
  259. status=2;
  260. }else if(data.status==='3'){
  261. status=3;
  262. }
  263. var postData ={'ssoId':ssoid,'cid':data.compilationId,'deadline':deadline,'smssend':smssend,'status':status};
  264. var result= await postRequest(url,postData);
  265. result=JSON.parse(result);
  266. if(result.error==0){ //接口更新成
  267. var curingDetail=await models.cloud_curing.getCuringByMobile(data.mobile);
  268. var id='';
  269. if(hash.isExistence(curingDetail)){ //是否需要同步同步到本地库中
  270. var url= curingUrl+'/getUsersAndCompilation?ssoID='+ ssoid;
  271. var userData= await curlRequest(url);
  272. //获得更新数量updateTotal
  273. var updateTotal=0;
  274. userData['userInfo']['upgrade_list'].forEach(function(value,key){
  275. if(value['isUpgrade']){
  276. updateTotal++;
  277. }
  278. });
  279. //更新数量
  280. var values = {
  281. updateTotal: updateTotal
  282. }
  283. var where={
  284. where: {id: curingDetail['id']}
  285. };
  286. await models.cloud_curing.update(values,where);
  287. id=curingDetail['id'];
  288. }else{
  289. sid=hash.hashDecode(STAFF.sid);
  290. //获得登陆用户相关信息
  291. var obj = {
  292. mobile: data.mobile,
  293. sso_id: data.ssoid,
  294. compilation_id: data.compilationId,
  295. //curingCompany: data.name,
  296. sid: sid,
  297. cid: STAFF.cid,
  298. status: 1,
  299. //addtime: new Date().getTime(),
  300. updateTotal: 1
  301. };
  302. var detail=await models.cloud_curing.create(obj);
  303. id=detail['cloud_curing']['id'];
  304. }
  305. //记录锁日志
  306. let operation=STAFF.username+'升级'+data.name;
  307. if(status==2){
  308. operation=STAFF.username+'降级'+data.name+'(学习版)';
  309. }
  310. if(status!=3){
  311. await models.operate_log.createOperateLog(1,id,operation);
  312. }
  313. if(typeof deadline === 'string' && deadline !== 'undefined' && deadline.trim() !== ""){
  314. operation=data.name+'期限改为'+deadline;
  315. await models.operate_log.createOperateLog(1,id,operation);
  316. }
  317. return true;
  318. }else{
  319. return false;
  320. }
  321. },
  322. /**
  323. * 用户关联CLD客户
  324. */
  325. relevanceClientDo: async function(data){
  326. var cid=hash.hashDecode(data.cidKey);
  327. var ssoId=hash.hashDecode(data.ssoId);
  328. if(!hash.isNotANumber(cid)||!hash.isNotANumber(ssoId)){
  329. return false;
  330. }
  331. var curingDetail=await models.cloud_curing.getCuringBySsoid(ssoId);
  332. if(hash.isExistence(curingDetail)) {
  333. var clientDetail=await models.CLD_client.findById(cid);
  334. if(hash.isExistence(clientDetail)){
  335. var id=hash.hashDecode(curingDetail['id']);
  336. //更新数量
  337. var values = {
  338. client_id: cid
  339. }
  340. var where={
  341. where: {id: id}
  342. };
  343. await models.cloud_curing.update(values,where);
  344. //记录操作日志
  345. var operation=STAFF.username+'关联CLD联系人'+clientDetail.clientname;
  346. await models.operate_log.createOperateLog(1,id,operation);
  347. return true;
  348. }else{
  349. return false;
  350. }
  351. }else{
  352. return false;
  353. }
  354. },
  355. /**
  356. * 用户取消关联CLD客户
  357. */
  358. relieveClientDo: async function(data){
  359. var ssoId=hash.hashDecode(data.ssoId);
  360. if(!hash.isNotANumber(ssoId)){
  361. return false;
  362. }
  363. var curingDetail=await models.cloud_curing.getCuringBySsoid(ssoId);
  364. if(hash.isExistence(curingDetail)) {
  365. var id=hash.hashDecode(curingDetail['id']);
  366. //更新数量
  367. var values = {
  368. client_id: 0
  369. }
  370. var where={
  371. where: {id: id}
  372. };
  373. await models.cloud_curing.update(values,where);
  374. var clientDetail=await models.CLD_client.findById(curingDetail['client_id']);
  375. //记录操作日志
  376. var operation=STAFF.username+'移除CLD联系人'+clientDetail.clientname;
  377. await models.operate_log.createOperateLog(1,id,operation);
  378. return true;
  379. }else{
  380. return false;
  381. }
  382. },
  383. /**
  384. * 获得本地编办列表--废弃
  385. */
  386. getCuringCompilationList:async function (){
  387. var compilationList = await models.cloud_curing.getCuringCompilationList();
  388. return compilationList;
  389. },
  390. //获取本地养护数据--可能废弃
  391. getBindCuringByMobile:async function (mobile){
  392. var curingList = await models.cloud_curing.getCuringByMobile(mobile);
  393. if (curingList!=null){
  394. var cid=[curingList['client_id']];
  395. var attributes= ['cid', 'clientname', 'companyid', 'companyname'];
  396. var clientList = await models.CLD_client.findAllInCid(cid,attributes);
  397. curingList.clientInfo={};
  398. clientList.forEach(function(clientVal,clientI){
  399. if(curingList.client_id==clientVal.cid){
  400. curingList.clientInfo=clientVal;
  401. }
  402. });
  403. curingList.addtime=moment.unix(curingList.addtime).format('YYYY-MM-DD HH:mm');
  404. }
  405. return curingList;
  406. },
  407. //根据手机号码获得养护用户信息--可能废弃
  408. getCuringByMobile: async function(mobile){
  409. var url= curingUrl+'/getUsersAndCompilation?mobile='+ mobile;
  410. var userData= await curlRequest(url);
  411. return userData;
  412. },
  413. }
  414. module.exports = cloudService;
  415. //接口调用
  416. var curlRequest = async function(url){
  417. var got = require('got');
  418. var data=[];
  419. await got(url, { json: true,timeout:300 }).then(response => {
  420. data= response.body.data;
  421. }).catch(error => {
  422. console.log('GET调用接口超时');
  423. });
  424. return data;
  425. }
  426. var postRequest= async function(url,data){
  427. //var fs = require('fs');
  428. var got = require('got');
  429. var qs = require('querystring');
  430. var keysList=Object.keys(data);
  431. var content = qs.stringify(data);
  432. result= await got.post(url, {
  433. body: content,
  434. timeout:300,
  435. headers: {
  436. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  437. }
  438. }).catch(error => {
  439. console.log('POST调用接口超时');
  440. });
  441. return result.body;
  442. //var FormData = require('form-data');
  443. //var form = new FormData();
  444. /* keysList.forEach(function(v,i){
  445. console.log(v);
  446. form.append(v, data[v]);
  447. });*/
  448. //form.append('ssoId', data.ssoId);
  449. //form.append('cid', data.cid);
  450. //console.log(form);
  451. //console.log(keysList);
  452. //form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
  453. //?longle=12
  454. //url='http://cld.com/longle/list/add';
  455. //var content = qs.stringify({'longle':125});
  456. //console.log(data);
  457. }
  458. var modelsQuery= async function(sql){
  459. var data=[];
  460. await models.sequelize.query(sql).spread((results, metadata) => {
  461. // 结果将是一个空数组,元数据将包含受影响的行数。
  462. data=results;
  463. });
  464. return data;
  465. }
  466. /**
  467. * 分页相关数据计算
  468. * modelsTable 数据表对象
  469. * parameter 设置传的参数
  470. */
  471. var getPage= async function(modelsTable,where,currentPage,pageSize,parameter,totalCount){
  472. if(!hash.isExistence(totalCount)){
  473. if(hash.isExistence(where)){
  474. totalCount=await modelsTable.count({where});
  475. }else{
  476. totalCount=await modelsTable.count();
  477. }
  478. }
  479. currentPage=parseInt(currentPage);
  480. var total =0;
  481. if(totalCount!=0){
  482. total = Math.trunc ( totalCount / pageSize );
  483. }
  484. var totalPage = (totalCount % pageSize) == 0 ? total : total + 1;
  485. var pageMax=1;
  486. var pageWidth=15;
  487. //开始页数
  488. var startPage=1;
  489. if (currentPage >= pageWidth) {
  490. pageMax = parseInt ( currentPage / pageWidth ) + 1;
  491. startPage = parseInt ( currentPage / pageWidth ) * pageWidth - 1;
  492. }
  493. //结束页数
  494. var endPage=pageWidth * pageMax;
  495. if(endPage>totalPage){
  496. endPage=totalPage;
  497. }
  498. var page={
  499. 'currentPage':currentPage, //当前页
  500. 'previousPage':currentPage!=0?currentPage-1:currentPage, //上一页
  501. 'nextPage': currentPage==totalPage ? totalPage:currentPage+1,
  502. 'totalPage':totalPage,
  503. 'startPage':startPage,
  504. 'endPage':endPage,
  505. 'pageWidth':pageWidth,
  506. 'parameter':encodeURI(parameter)
  507. };
  508. return page;
  509. }