cloudEditionService.js 16 KB

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