cloudEditionService.js 16 KB

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