cloudEditionService.js 16 KB

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