cloudEditionService.js 17 KB

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