cloudEditionService.js 17 KB

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