cloudEditionService.js 16 KB

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