cloudEditionService.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. * compilation_id 编办ID
  16. */
  17. getCuringList: async function(compilation_id,page=1){
  18. //获得远程养护用户
  19. //分页数据
  20. var limit=12;
  21. var offset=0;
  22. if(page!=1){
  23. var pg=page-1;
  24. offset=pg*limit;
  25. }
  26. if(!hash.isExistence(compilation_id)){
  27. compilation_id='';
  28. }
  29. var url= curingUrl+'/getUserList?page='+page+'&pageSize='+limit+'&latestUsed='+compilation_id;
  30. /* 接口请求获得的信息
  31. * userInfo
  32. * pageData
  33. * compilationlist
  34. */
  35. var curingCloudData=await curlRequest(url);
  36. var curingCloudList=curingCloudData.userInfo;
  37. //组合数据以获得本地扩展数据
  38. var mobile=[];
  39. curingCloudList.forEach(function(v,i){
  40. mobile.push(v.mobile);
  41. });
  42. if(!hash.isExistence(mobile)){
  43. return {curingList:[],pageData:[]};
  44. }
  45. //获得指定数据
  46. var curingList = await models.cloud_curing.getCuringInMobile(mobile,compilation_id);
  47. //组合同步云版数据
  48. var falg=false;
  49. curingCloudList.forEach(async function(cclValue,cclKey){
  50. curingCloudList[cclKey].client_id=0
  51. falg=true;
  52. for(var i=0;i<=curingList.length;i++){
  53. if(cclValue.mobile==curingList[i].mobile){
  54. curingCloudList[cclKey].client_id=curingList[i].client_id;
  55. falg=false;
  56. break;
  57. }
  58. }
  59. if(falg){//新增本地扩展信息
  60. sid=hash.hashDecode(STAFF.sid);
  61. var obj = {
  62. mobile: cclValue.mobile,
  63. sso_id: cclValue.ssoId,
  64. //compilation_id: cclValue.filter.compilationID,
  65. // curingCompany: cclValue.company,
  66. sid: sid,
  67. cid: STAFF.cid,
  68. status: 2,
  69. //addtime: new Date().getTime(),
  70. //updateTotal: cclValue.isUserActive
  71. };
  72. var detail = await models.cloud_curing.create(obj);
  73. }
  74. });
  75. //获得编办数据
  76. var compilationList=await this.getCompilationList(); //编办信息
  77. console.log(compilationList);
  78. //数据组合--cld客户数据
  79. var cid=[];
  80. curingCloudList.forEach(function(v,i){
  81. if(v.client_id!=0){
  82. cid.push(v.client_id);
  83. }
  84. });
  85. var clientList =[];
  86. if(hash.isExistence(cid)){
  87. var attributes= ['cid', 'clientname', 'companyid', 'companyname'];
  88. clientList = await models.CLD_client.findAllInCid(cid,attributes);
  89. }
  90. //组合页面需要展示的数据
  91. curingCloudList.forEach(function(v,i){
  92. curingCloudList[i].clientInfo={};
  93. clientList.forEach(function(clientVal,clientI){
  94. if(v.client_id==clientVal.cid){
  95. curingCloudList[i].clientInfo=clientVal;
  96. }
  97. });
  98. curingCloudList[i].addtime=moment(v.create_time).format('YYYY-MM-DD HH:mm');
  99. curingCloudList[i].latest_login=moment(v.latest_login).format('YYYY-MM-DD HH:mm');
  100. curingCloudList[i].client_id=hash.hashEncode(v.client_id.toString());
  101. //curingCloudList[i].sso_id=v.sso_id;
  102. //最后使用
  103. curingCloudList[i].latest_usedName='';
  104. for(var ci=1;ci<=compilationList.lenght;ci++){
  105. console.log(v.latest_used);
  106. console.log(compilationList[ci]._id);
  107. if(compilationList[ci]._id==v.latest_used){
  108. curingCloudList[i].latest_usedName=compilationList[ci].name;
  109. break;
  110. }
  111. }
  112. //登录时长
  113. if(!hash.isExistence(v.online_times)){
  114. curingCloudList[i].online_times='';
  115. }
  116. });
  117. var parameter='?compilation_id='+compilation_id;
  118. //分页计算
  119. var pageData = await getPage('','',page,limit,parameter,curingCloudData.pageData.total);
  120. //console.log(curingCloudList);
  121. return {curingList:curingCloudList,pageData:pageData};
  122. //获得本地用户
  123. /*var limit=12;
  124. var offset=0;
  125. if(page!=1){
  126. var pg=page-1;
  127. offset=pg*limit;
  128. }
  129. var option={offset: offset,
  130. limit: limit,
  131. raw:true,
  132. order:[['id','desc']]};
  133. if(hash.isExistence(compilation_id)){
  134. option.where={compilation_id: compilation_id};
  135. }
  136. var curingList = await models.cloud_curing.findAll(option);
  137. var parameter='?compilation_id='+compilation_id;
  138. //分页计算
  139. var pageData = await getPage(models.cloud_curing,option.where,page,limit,parameter);
  140. //数据组合--cld客户数据
  141. var cid=[];
  142. curingList.forEach(function(v,i){
  143. cid.push(v.client_id);
  144. });
  145. var attributes= ['cid', 'clientname', 'companyid', 'companyname'];
  146. var clientList = await models.CLD_client.findAllInCid(cid,attributes);
  147. //组合页面需要展示的数据
  148. curingList.forEach(function(v,i){
  149. curingList[i].clientInfo={};
  150. clientList.forEach(function(clientVal,clientI){
  151. if(v.client_id==clientVal.cid){
  152. curingList[i].clientInfo=clientVal;
  153. }
  154. });
  155. curingList[i].addtime=moment.unix(v.addtime).format('YYYY-MM-DD HH:mm');
  156. curingList[i].id=hash.hashEncode(v.id.toString());
  157. curingList[i].client_id=hash.hashEncode(v.client_id.toString());
  158. curingList[i].sso_id=v.sso_id;
  159. });
  160. //curingList['pageData']=pageData;
  161. return {curingList:curingList,pageData:pageData};*/
  162. },
  163. //获得本地编办列表
  164. getCuringCompilationList:async function (){
  165. var compilationList = await models.cloud_curing.getCuringCompilationList();
  166. return compilationList;
  167. /*var curingList = await models.cloud_curing.getCuringByMobile(mobile);
  168. if (curingList!=null){
  169. var cid=[curingList['client_id']];
  170. var attributes= ['cid', 'clientname', 'companyid', 'companyname'];
  171. var clientList = await models.CLD_client.findAllInCid(cid,attributes);
  172. curingList.clientname='';
  173. curingList.companyname='';
  174. clientList.forEach(function(clientVal,clientI){
  175. if(curingList.client_id==clientVal.cid){
  176. curingList.clientname=clientVal.clientname;
  177. curingList.companyname=clientVal.companyname;
  178. }
  179. });
  180. curingList.addtime=moment.unix(curingList.addtime).format('YYYY-MM-DD HH:mm');
  181. }
  182. */
  183. },
  184. //获得养护编办
  185. getCompilationList:async function (){
  186. var url= curingUrl+'/getCompilationList';
  187. var data= await curlRequest(url);
  188. return data;
  189. },
  190. //获取本地养护数据
  191. getBindCuringByMobile:async function (mobile){
  192. var curingList = await models.cloud_curing.getCuringByMobile(mobile);
  193. if (curingList!=null){
  194. var cid=[curingList['client_id']];
  195. var attributes= ['cid', 'clientname', 'companyid', 'companyname'];
  196. var clientList = await models.CLD_client.findAllInCid(cid,attributes);
  197. curingList.clientInfo={};
  198. clientList.forEach(function(clientVal,clientI){
  199. if(curingList.client_id==clientVal.cid){
  200. curingList.clientInfo=clientVal;
  201. }
  202. });
  203. curingList.addtime=moment.unix(curingList.addtime).format('YYYY-MM-DD HH:mm');
  204. }
  205. return curingList;
  206. },
  207. //根据手机号码获得养护用户信息
  208. getCuringByMobile: async function(mobile){
  209. var url= curingUrl+'/getUsersAndCompilation?mobile='+ mobile;
  210. var userData= await curlRequest(url);
  211. return userData;
  212. },
  213. //根据通行证ID获得养护相关的所有信息
  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 page={
  439. 'currentPage':currentPage, //当前页
  440. 'previousPage':currentPage!=0?currentPage-1:currentPage, //上一页
  441. 'nextPage': currentPage==totalPage ? totalPage:currentPage+1,
  442. 'totalPage':totalPage,
  443. 'parameter':encodeURI(parameter)
  444. };
  445. return page;
  446. }