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