cloudEditionService.js 16 KB

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