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