cloudEditionService.js 16 KB

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