cloudEditionService.js 16 KB

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