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