cloudEditionService.js 17 KB

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