cloudEditionService.js 12 KB

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