cloudEditionService.js 17 KB

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