cloudEditionService.js 18 KB

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