cloudEditionService.js 16 KB

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