cloudEditionService.js 16 KB

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