cloudEditionService.js 16 KB

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