cloudEditionService.js 17 KB

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