cloudEditionService.js 17 KB

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