cloudEditionService.js 19 KB

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