diff --git a/jeecg-boot-base/jeecg-boot-base-api/jeecg-system-cloud-api/src/main/java/org/jeecg/common/system/api/fallback/SysBaseAPIFallback.java b/jeecg-boot-base/jeecg-boot-base-api/jeecg-system-cloud-api/src/main/java/org/jeecg/common/system/api/fallback/SysBaseAPIFallback.java index 9fc7b87..467bdf4 100644 --- a/jeecg-boot-base/jeecg-boot-base-api/jeecg-system-cloud-api/src/main/java/org/jeecg/common/system/api/fallback/SysBaseAPIFallback.java +++ b/jeecg-boot-base/jeecg-boot-base-api/jeecg-system-cloud-api/src/main/java/org/jeecg/common/system/api/fallback/SysBaseAPIFallback.java @@ -234,6 +234,13 @@ public class SysBaseAPIFallback implements ISysBaseAPI { log.error("服务节点不通,导致获取登录用户信息失败: " + cause.getMessage(), cause); return null; } + + @Override + public HanHaiMember getUserByNameHanHaiXcxOpenId(String username) { + log.error("服务节点不通,导致获取登录用户信息失败: " + cause.getMessage(), cause); + return null; + } + // // @Override // public DbMumber getUserByName3(String username,Integer isOrgan) { diff --git a/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/common/api/CommonAPI.java b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/common/api/CommonAPI.java index c740ea7..20ab574 100644 --- a/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/common/api/CommonAPI.java +++ b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/common/api/CommonAPI.java @@ -64,6 +64,13 @@ public interface CommonAPI { public HanHaiMember getUserByNameHanHaiAccount(String username); + /** + * 小程序验证 - 小程序openid验证 + * @param username + * @return + * */ + public HanHaiMember getUserByNameHanHaiXcxOpenId(String username); + /** * 6字典表的 翻译 diff --git a/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroRealm.java b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroRealm.java index d265473..dc2efd7 100644 --- a/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroRealm.java +++ b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroRealm.java @@ -191,6 +191,36 @@ public class ShiroRealm extends AuthorizingRealm { } + /** + * 校验token的有效性 + * + * @param token + */ + public HanHaiMember checkUserTokenIsEffectHanHaiOpenId(String token) throws AuthenticationException { + Result result = new Result(); + // 解密获得username,用于和数据库进行对比 + String openid = JwtUtil.getUsername(token); + if (openid == null) { + throw new AuthenticationException("token非法无效!"); + } + + // 查询用户信息 + log.debug("———校验token是否有效————checkUserTokenIsEffect——————— "+ token); + HanHaiMember user = commonApi.getUserByNameHanHaiXcxOpenId(openid); + if (user == null || user.getAppletOpenid() == null) { + throw new AuthenticationException("用户不存在!"); + } +// // 判断用户状态 +// if (user.getDeleteFlag().equals("Y")) { +// throw new AuthenticationException("账号已被注销,请联系管理员!"); +// } + // 校验token是否超时失效 & 或者账号密码是否错误 + if (!jwtTokenRefresh(token, openid, openid)) { + throw new JeecgBoot401Exception(CommonConstant.TOKEN_IS_INVALID_MSG); + } + return user; + } + /** * JWTToken刷新生命周期 (实现: 用户在线操作不掉线功能) diff --git a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/teambuyController/ApiLoginController.java b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/teambuyController/ApiLoginController.java new file mode 100644 index 0000000..719ae9c --- /dev/null +++ b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/teambuyController/ApiLoginController.java @@ -0,0 +1,40 @@ +package org.jeecg.modules.api.teambuyController; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.jeecg.common.api.vo.Result; +import org.jeecg.modules.apiService.LoginApiService; +import org.jeecg.modules.bean.LoginReq; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +@Api(tags="登录-登录相关接口") +@RestController +@RequestMapping("/teambuy/login") +@Slf4j +public class ApiLoginController { + /******************************************************************************************************************/ + //工作信息 + @Resource + private LoginApiService loginApiService; + /******************************************************************************************************************/ + + //小程序-微信授权登录接口 + @ApiOperation(value="小程序-微信授权登录接口", notes="小程序-微信授权登录接口") + @GetMapping("/login") + public Result login(LoginReq loginReq){ + return loginApiService.login(loginReq); + } + + //绑定手机号码 + @ApiOperation(value="小程序-绑定手机号码", notes="小程序-绑定手机号码") + @GetMapping(value = "/bindPhone") + public Result bindPhone(String phoneCode){ + return loginApiService.bindPhone(phoneCode); + } + +} diff --git a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/teambuyController/RecommendController.java b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/teambuyController/RecommendController.java new file mode 100644 index 0000000..2f3d31e --- /dev/null +++ b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/teambuyController/RecommendController.java @@ -0,0 +1,44 @@ +package org.jeecg.modules.api.teambuyController; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.jeecg.common.api.vo.Result; +import org.jeecg.modules.apiBean.PageBean; +import org.jeecg.modules.apiService.RecommendService; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +@Api(tags="我的-邀请好友&我的团队相关接口") +@RestController +@RequestMapping("/teambuy/share") +@Slf4j +public class RecommendController { + + /******************************************************************************************************************/ + //邀请好友 + @Resource + private RecommendService recommendService; + /******************************************************************************************************************/ + + //邀请好友-获取推荐二维码 + @ApiOperation(value="我的-获取推荐二维码", notes="我的-获取推荐二维码") + @RequestMapping(value = "/getInviteCode", method = {RequestMethod.POST}) + public Result getInviteCode(@RequestHeader("X-Access-Token") String token){ + return recommendService.getInviteCode(token); + } + + //我的团队-查询直推用户列表 + @ApiOperation(value="我的团队-查询直推用户列表", notes="我的-获取直推用户列表") + @RequestMapping(value = "/queryRecommendUserList", method = {RequestMethod.POST}) + public Result queryRecommendUserList(@RequestHeader("X-Access-Token") String token, PageBean pageBean){ + return recommendService.queryRecommendUserList(token, pageBean); + } + + + +} diff --git a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiBean/HttpClientUtil.java b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiBean/HttpClientUtil.java new file mode 100644 index 0000000..bcb5511 --- /dev/null +++ b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiBean/HttpClientUtil.java @@ -0,0 +1,1119 @@ +package org.jeecg.modules.apiBean; + +import com.alibaba.fastjson.JSON; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.NameValuePair; +import org.apache.http.client.CookieStore; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.*; +import org.apache.http.client.utils.HttpClientUtils; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.conn.ssl.SSLContextBuilder; +import org.apache.http.conn.ssl.TrustStrategy; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.BasicCookieStore; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; + +import javax.net.ssl.SSLContext; +import java.io.IOException; +import java.net.URI; +import java.nio.charset.Charset; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 通过url获取数据 + * + * @author zc + * + */ +public class HttpClientUtil { + + /** + * 设置可访问https + * @return + */ + public static CloseableHttpClient createSSLClientDefault() { + try { + SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { + //信任所有 + public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { + return true; + } + }).build(); + SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + + return HttpClients.custom().setSSLSocketFactory(sslsf).build(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (KeyStoreException e) { + e.printStackTrace(); + } + return HttpClients.createDefault(); + } +/*************************************Get**********************************************/ + /** + * 一般查询用doget + * @param url + * @param param + * @param + * @return + */ + public static String doGet(String url, Map param) { + // 创建Httpclient对象 + CloseableHttpClient httpclient = createSSLClientDefault();//调用createSSLClientDefault + String resultString = ""; + CloseableHttpResponse response = null; + try { + + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + CookieStore cookieStore = new BasicCookieStore(); + httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); + // 创建http GET请求 + HttpGet httpGet = new HttpGet(uri); + //设置请求头 +// if (parameter != null) { +// //签名 +// httpGet.addHeader("Sign",parameter.getSign()); +// //用户ID +// httpGet.addHeader("User_ID",parameter.getUserId()+""); +// //用户角色ID +// httpGet.addHeader("User_RID",parameter.getRId()); +// //用户单位ID +// httpGet.addHeader("Dept_ID",parameter.getDeptId()+""); +// //用户科室ID +// httpGet.addHeader("Unit_ID",parameter.getUnitid()+""); +// httpGet.addHeader("CacheKey",parameter.getCacheKey()); +// httpGet.addHeader("Org_id", parameter.getOrg_id() + ""); +// httpGet.addHeader("Product_id", parameter.getProduct_id() + ""); +// httpGet.addHeader("Timestamp", parameter.getTimestamp()); +// } + + // 执行请求 + response = httpclient.execute(httpGet); + // 判断返回状态是否为200 + if (response.getStatusLine().getStatusCode() == 200) { + resultString = EntityUtils.toString(response.getEntity(), + "UTF-8"); + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (response != null) { + response.close(); + } + httpclient.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + public static String doGet(String url) { + return doGet(url, null); + } + + public static String doGet3(String url, Map param) { + // 创建Httpclient对象 + //调用createSSLClientDefault + CloseableHttpClient httpclient = createSSLClientDefault(); + String resultString = ""; + CloseableHttpResponse response = null; + try { + + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + + // 创建http GET请求 + HttpGet httpGet = new HttpGet(uri); + //设置请求头 +// if (parameter != null) { +// //签名 +// httpGet.addHeader("Sign",parameter.getSign()); +// //用户ID +// httpGet.addHeader("User_ID",parameter.getUserId()+""); +// //用户角色ID +// httpGet.addHeader("User_RID",parameter.getRId()); +// //用户单位ID +// httpGet.addHeader("Dept_ID",parameter.getDeptId()+""); +// //用户科室ID +// httpGet.addHeader("Unit_ID",parameter.getUnitid()+""); +// httpGet.addHeader("CacheKey",parameter.getCacheKey()); +// httpGet.addHeader("Org_id", parameter.getOrg_id() + ""); +// httpGet.addHeader("Product_id", parameter.getProduct_id() + ""); +// httpGet.addHeader("Timestamp", parameter.getTimestamp()); +// } + + // 执行请求 + response = httpclient.execute(httpGet); + // 判断返回状态是否为200 + if (response.getStatusLine().getStatusCode() == 200) { + resultString = EntityUtils.toString(response.getEntity(), + "UTF-8"); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (response != null) { + response.close(); + } + httpclient.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + /** + * 上传文件的doGet + * @param url + * @param param + * @param + * @return + */ + public static String doGet2(String url, Map param) { + + // 创建Httpclient对象 + CloseableHttpClient httpclient = createSSLClientDefault(); + String resultString = ""; + CloseableHttpResponse response = null; + try { + + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建http GET请求 + HttpGet httpGet = new HttpGet(uri); +// if (parameter != null) { +// //用户ID +// httpGet.addHeader("User_ID",parameter.getUserId()+""); +// //用户角色ID +// httpGet.addHeader("User_RID",parameter.getRId()); +// //用户单位ID +// httpGet.addHeader("Dept_ID",parameter.getDeptId()+""); +// //用户科室ID +// httpGet.addHeader("Unit_ID",parameter.getUnitid()+""); +// httpGet.addHeader("CacheKey",parameter.getCacheKey()); +// httpGet.addHeader("Org_id", parameter.getOrg_id()); +// httpGet.addHeader("Product_id", parameter.getProduct_id()); +// httpGet.addHeader("Sign", parameter.getSign()); +// httpGet.addHeader("requestCode", parameter.getRequestCode()); +// httpGet.addHeader("Content-Type", "application/json;charset=UTF-8"); +// httpGet.addHeader("Timestamp", parameter.getTimestamp()); +// } + // 执行请求 + response = httpclient.execute(httpGet); + if (response.getStatusLine().getStatusCode() == 200) { + resultString = EntityUtils.toString(response.getEntity(), + "UTF-8"); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (response != null) { + response.close(); + } + httpclient.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + public static String doGet2(String url) { + return doGet2(url, null); + + } + + public static String doGet4(String url, Map param) { + // 创建Httpclient对象 + //调用createSSLClientDefault + CloseableHttpClient httpclient = createSSLClientDefault(); + String resultString = ""; + CloseableHttpResponse response = null; + try { + + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + + // 创建http GET请求 + HttpGet httpGet = new HttpGet(uri); + //设置请求头 +// httpGet.addHeader(""); +// if (parameter != null) { +// //签名 +// httpGet.addHeader("Sign",parameter.getSign()); +// //用户ID +// httpGet.addHeader("User_ID",parameter.getUserId()+""); +// //用户角色ID +// httpGet.addHeader("User_RID",parameter.getRId()); +// //用户单位ID +// httpGet.addHeader("Dept_ID",parameter.getDeptId()+""); +// //用户科室ID +// httpGet.addHeader("Unit_ID",parameter.getUnitid()+""); +// httpGet.addHeader("CacheKey",parameter.getCacheKey()); +// httpGet.addHeader("Org_id", parameter.getOrg_id() + ""); +// httpGet.addHeader("Product_id", parameter.getProduct_id() + ""); +// httpGet.addHeader("Timestamp", parameter.getTimestamp()); +// } + + // 执行请求 + response = httpclient.execute(httpGet); + // 判断返回状态是否为200 + if (response.getStatusLine().getStatusCode() == 200) { + resultString = EntityUtils.toString(response.getEntity(), + "UTF-8"); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (response != null) { + response.close(); + } + httpclient.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + public static String doGet5(String url, Map param) { + // 创建Httpclient对象 + //调用createSSLClientDefault + CloseableHttpClient httpclient = createSSLClientDefault(); + String resultString = ""; + CloseableHttpResponse response = null; + try { + + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + + // 创建http GET请求 + HttpGet httpGet = new HttpGet(uri); + //设置请求头 +// httpGet.addHeader(""); +// if (parameter != null) { +// httpGet.addHeader("Accept","application/json, text/javascript, */*; q=0.01"); +// httpGet.addHeader("Accept-Encoding","gzip, deflate, br"); +// httpGet.addHeader("Accept-Language","zh-CN,zh;q=0.9"); +// httpGet.addHeader("Connection","keep-alive"); + httpGet.addHeader("Cookie","PHPSESSID=b1epvn4gilmvlqd63gb71sgb4m"); +// httpGet.addHeader("Host","www.5577yc.com"); +// httpGet.addHeader("Referer","https://www.5577yc.com/pc/member/index.html"); +// httpGet.addHeader("Sec-Fetch-Mode","cors"); +// httpGet.addHeader("Sec-Fetch-Site","same-origin"); +// httpGet.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"); + httpGet.addHeader("X-Requested-With","XMLHttpRequest"); +// httpGet.addHeader("Org_id", parameter.getOrg_id() + ""); +// httpGet.addHeader("Product_id", parameter.getProduct_id() + ""); +// httpGet.addHeader("Timestamp", parameter.getTimestamp()); +// } + + // 执行请求 + response = httpclient.execute(httpGet); + // 判断返回状态是否为200 + if (response.getStatusLine().getStatusCode() == 200) { + resultString = EntityUtils.toString(response.getEntity(), + "UTF-8"); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (response != null) { + response.close(); + } + httpclient.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + public static String doGet5(String url) { + return doGet5(url, null); + + } + /*************************************Get**********************************************/ + + /*************************************Post**********************************************/ + /** + * 原始doPost 基本不用 + * @param url + * @param param + * @param + * @return + */ + public static String doPost(String url, Map param) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + + try { + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(url); + + // 创建参数列表 + if (param != null) { + List paramList = new ArrayList<>(); + for (String key : param.keySet()) { + paramList.add(new BasicNameValuePair(key, param.get(key))); + } + // 模拟表单 + UrlEncodedFormEntity entity = new UrlEncodedFormEntity( + paramList); + httpPost.setEntity(entity); + } + // 执行http请求 + response = httpClient.execute(httpPost); + + + + + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return resultString; + } + + /** + * 项目经常用的doPost2 + * @param url + * @param param + * @param + * @return + */ + public static String doPost2(String url, Map param){ + + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + + try { + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(uri); + httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); +// httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + // 创建参数列表 + if (param != null) { + List paramList = new ArrayList<>(); + for (String key : param.keySet()) { + paramList.add(new BasicNameValuePair(key, param.get(key))); + } + + // 模拟表单 + UrlEncodedFormEntity entity = new UrlEncodedFormEntity( + paramList,"UTF-8"); + entity.setContentEncoding("UTF-8"); + entity.setContentType("application/x-www-form-urlencoded"); + httpPost.setEntity(entity); + + } + // 执行http请求 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return resultString; + } + + /** + * .net登录post + * @param url + * @param param + * @param + * @return + */ + public static String doPost3(String url, Map param){ + + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + + try { + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(uri); + // 创建参数列表 + if (param != null) { + List paramList = new ArrayList<>(); + for (String key : param.keySet()) { + paramList.add(new BasicNameValuePair(key, param.get(key))); + } + // 模拟表单 + UrlEncodedFormEntity entity = new UrlEncodedFormEntity( + paramList, Charset.forName("UTF-8")); + entity.setContentEncoding("UTF-8"); + httpPost.setEntity(entity); + } + // 执行http请求 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return resultString; + } + + + + /** + * 微信文件上传 + * @param url + * @param content + * @param fileName + * @return + */ + public static String doPost6(String url, byte[] content,String fileName){ + + String resultString = null; + //创建HttpClient + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(url); + org.apache.http.entity.mime.MultipartEntityBuilder builder = org.apache.http.entity.mime.MultipartEntityBuilder.create(); + /*绑定文件参数,传入文件流和contenttype,此处也可以继续添加其他formdata参数*/ + builder.addBinaryBody("file",content, ContentType.MULTIPART_FORM_DATA,fileName); + HttpEntity entity = builder.build(); + httpPost.setEntity(entity); + HttpResponse response = null; + //执行提交 + try{ + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + HttpClientUtils.closeQuietly(httpClient); + HttpClientUtils.closeQuietly(response); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return resultString; + } + + + /** + * 微信公众号文件上传 + * @param url + * @param content + * @param fileName + * @return + */ + public static String doPost7(String url, byte[] content,String fileName){ + + String resultString = null; + //创建HttpClient + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(url); + org.apache.http.entity.mime.MultipartEntityBuilder builder = org.apache.http.entity.mime.MultipartEntityBuilder.create(); + /*绑定文件参数,传入文件流和contenttype,此处也可以继续添加其他formdata参数*/ + builder.addBinaryBody("media",content, ContentType.MULTIPART_FORM_DATA,fileName); + HttpEntity entity = builder.build(); + httpPost.setEntity(entity); + HttpResponse response = null; + //执行提交 + try{ + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + HttpClientUtils.closeQuietly(httpClient); + HttpClientUtils.closeQuietly(response); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return resultString; + } + + /** + * 项目经常用的doPost4 + * @param url + * @param param + * @return + */ + public static String doPost4(String url, Map param){ + + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + StringBuffer cookie = new StringBuffer(); + Map map = new HashMap<>(); + try { + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(uri); + // 创建参数列表 + if (param != null) { + List paramList = new ArrayList<>(); + for (String key : param.keySet()) { + paramList.add(new BasicNameValuePair(key, param.get(key))); + } + httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); + // 模拟表单 + UrlEncodedFormEntity entity = new UrlEncodedFormEntity( + paramList, Charset.forName("UTF-8")); + httpPost.setEntity(entity); + } + // 执行http请求 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return resultString; + } + + + /** + * 项目经常用的doPost4 + * @param url + * @param param + * @param parameter + * @return + */ + public static String doPost5(String url, Map param,String parameter){ + + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + StringBuffer cookie = new StringBuffer(); + Map map = new HashMap<>(); + try { + // 创建uri + CookieStore cookieStore = new BasicCookieStore(); + httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(uri); + // 创建参数列表 + if (param != null) { + List paramList = new ArrayList<>(); + for (String key : param.keySet()) { + paramList.add(new BasicNameValuePair(key, param.get(key))); + } + httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); + httpPost.addHeader("Host","www.www7945.com"); + httpPost.addHeader("Origin","https://www.www7945.com"); + httpPost.addHeader("Referer","https://www.www7945.com/mobile/"); + httpPost.addHeader("User-Agent:","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"); + httpPost.addHeader("Connection","keep-alive"); + // 模拟表单 + UrlEncodedFormEntity entity = new UrlEncodedFormEntity( + paramList, Charset.forName("UTF-8")); + entity.setContentEncoding("UTF-8"); + httpPost.setEntity(entity); + } + if(parameter!=null){ + httpPost.addHeader("Cookie",parameter); + } + // 执行http请求 + response = httpClient.execute(httpPost); + String tokenStr = null; + + if (response != null) { + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode == HttpStatus.SC_OK) { + // 获得Cookies + List cookies = cookieStore.getCookies(); + for (org.apache.http.cookie.Cookie c : cookies) { + cookie.append(c.getName()).append("=").append(c.getValue()).append(";"); + if (c.getName().contains("token")) { + tokenStr = c.getValue(); + } + } + } + } + map.put("token",tokenStr); + map.put("cookie",cookie.toString()); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + map.put("resultString",JSON.parse(resultString)); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + if(parameter!=null){ + return resultString; + } + return JSON.toJSONString(map); + } + + + + public static String doPost5(String url) { + return doPost5(url, null,null); + } + + public static String doPost4(String url) { + return doPost4(url, null); + } + + public static String doPost2(String url) { + return doPost2(url); + } + + + public static String doPost(String url) { + return doPost(url, null); + } + + /** + * 当数据需要以JSON格式传输 + * @param url + * @param json + * @param + * @return + */ + public static String doPostJson(String url, String json) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + try { + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(url); + // 创建请求内容 + StringEntity entity = new StringEntity(json, + ContentType.APPLICATION_JSON); + httpPost.setEntity(entity); + // 执行http请求 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return resultString; + } + + + public static String doPostJson3(String url, String json) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + try { + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(url); + // 创建请求内容 + StringEntity entity = new StringEntity(json, + ContentType.APPLICATION_FORM_URLENCODED); + httpPost.setEntity(entity); + // 执行http请求 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return resultString; + } + + /** + * 文件上传 + * @param url + * @param json + * @param + * @return + */ + public static String doPostJson2(String url, String json) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + try { + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(url); + // 创建请求内容 + StringEntity entity = new StringEntity(json, + ContentType.APPLICATION_JSON); + httpPost.setEntity(entity); + // 执行http请求 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return resultString; + } + + /*************************************Post**********************************************/ + + + + + + + + + /*************************************Delete**********************************************/ + /** + * 原始删除 基本不怎么用 + * @param url + * @return + */ + public static String doDelete(String url) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + + try { + // 创建Http Post请求 + HttpDelete httpPost = new HttpDelete(url); + // 执行http请求 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + /** + * 常用删除 + * @param url + * @param param + * @param + * @return + */ + public static String doDelete2(String url,Map param) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + + try { + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建Http Delete请求 + HttpDelete httpDelete = new HttpDelete(uri); + // 执行http请求 + response = httpClient.execute(httpDelete); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + /** + * 上传文件删除 + * @param url + * @param param + * @param + * @return + */ + public static String doDelete3(String url,Map param) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + + try { + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建Http Delete请求 + HttpDelete httpDelete = new HttpDelete(uri); + // 执行http请求 + response = httpClient.execute(httpDelete); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + /*************************************Delete**********************************************/ + + + + + + + + + + /*************************************Put**********************************************/ + public static String doPut(String url) { + return doPut(url, null); + } + + /** + * 修改 + * @param url + * @param param + * @param + * @return + */ + public static String doPut(String url, Map param) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + + try { + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建Http Put请求 + HttpPut httpPut = new HttpPut(uri); + // 创建参数列表 + if (param != null) { + List paramList = new ArrayList<>(); + for (String key : param.keySet()) { + paramList.add(new BasicNameValuePair(key, param.get(key))); + } + // 模拟表单 + UrlEncodedFormEntity entity = new UrlEncodedFormEntity( + paramList, Charset.forName("UTF-8"));//Charset.forName("UTF-8")解决乱码 + entity.setContentEncoding("UTF-8"); + httpPut.setEntity(entity); + } + // 执行http请求 + response = httpClient.execute(httpPut); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return resultString; + } + + /** + * 以JSON格式修改 + * @param url + * @param json + * @param parameter + * @return + */ + public static String doPutJson(String url, String json) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + try { + // 创建Http Post请求 + HttpPut httpPut = new HttpPut(url); + // 创建请求内容 + StringEntity entity = new StringEntity(json, + ContentType.APPLICATION_JSON); + httpPut.setEntity(entity); + // 执行http请求 + response = httpClient.execute(httpPut); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return resultString; + } + + + +} + diff --git a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiBean/PageBean.java b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiBean/PageBean.java new file mode 100644 index 0000000..c4513d2 --- /dev/null +++ b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiBean/PageBean.java @@ -0,0 +1,19 @@ +package org.jeecg.modules.apiBean; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@Data +public class PageBean { + /**显示条数*/ + @ApiModelProperty(value = "显示条数" ) + private Integer pageSize; + /**当前页*/ + @ApiModelProperty(value = "当前页" ) + private Integer pageNo; + + public PageBean() { + this.pageNo = 1; + this.pageSize = 1000; + } +} diff --git a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiBean/WxQrCodeVo.java b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiBean/WxQrCodeVo.java new file mode 100644 index 0000000..74c3965 --- /dev/null +++ b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiBean/WxQrCodeVo.java @@ -0,0 +1,14 @@ +package org.jeecg.modules.apiBean; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@Data +public class WxQrCodeVo { + /**图片地址*/ + @ApiModelProperty(value = "图片地址") + private String url; + + @ApiModelProperty(value = "名称") + private String name; +} diff --git a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiService/LoginApiService.java b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiService/LoginApiService.java new file mode 100644 index 0000000..3a52b1f --- /dev/null +++ b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiService/LoginApiService.java @@ -0,0 +1,12 @@ +package org.jeecg.modules.apiService; + +import org.jeecg.common.api.vo.Result; +import org.jeecg.modules.bean.LoginReq; + +public interface LoginApiService { + //小程序-微信授权登录接口 + public Result login(LoginReq loginReq); + + //绑定手机号码 + public Result bindPhone(String phoneCode); +} diff --git a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiService/RecommendService.java b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiService/RecommendService.java new file mode 100644 index 0000000..bd35d81 --- /dev/null +++ b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiService/RecommendService.java @@ -0,0 +1,12 @@ +package org.jeecg.modules.apiService; + +import org.jeecg.common.api.vo.Result; +import org.jeecg.modules.apiBean.PageBean; + +public interface RecommendService { + //邀请好友-获取推荐二维码 + public Result getInviteCode(String token); + + //我的团队-查询直推用户列表 + public Result queryRecommendUserList(String token, PageBean pageBean); +} diff --git a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiService/impl/LoginApiServiceImpl.java b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiService/impl/LoginApiServiceImpl.java new file mode 100644 index 0000000..a95b66e --- /dev/null +++ b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiService/impl/LoginApiServiceImpl.java @@ -0,0 +1,168 @@ +package org.jeecg.modules.apiService.impl; + +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.lang3.StringUtils; +import org.jeecg.common.api.vo.Result; +import org.jeecg.common.constant.CommonConstant; +import org.jeecg.common.exception.JeecgBootException; +import org.jeecg.common.system.util.JwtUtil; +import org.jeecg.common.util.RedisUtil; +import org.jeecg.modules.apiService.LoginApiService; +import org.jeecg.modules.bean.HttpConf; +import org.jeecg.modules.bean.LoginReq; +import org.jeecg.modules.hanHaiMember.entity.HanHaiMember; +import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +@Service +public class LoginApiServiceImpl implements LoginApiService { + /******************************************************************************************************************/ + //微信小程序appid(敢为人鲜) + private static final String mpAppId = "wx94a640f07969d6c9"; + //微信小程序appSecret(敢为人鲜) + private static final String mpAppSecret = ""; + + @Resource + private RedisUtil redisUtil; + + @Resource + private IHanHaiMemberService hanHaiMemberService; + + @Resource + private HttpConf httpConf; + /******************************************************************************************************************/ + + @Override + public Result login(LoginReq loginReq) { + Result result = new Result<>(); + Map map = new HashMap<>(); + if (StringUtils.isBlank(loginReq.getCode())) { + throw new JeecgBootException("小程序code为空"); + } + String loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + mpAppId + "&secret=" + mpAppSecret+ "&js_code=" + loginReq.getCode() + "&grant_type=authorization_code"; + //使用工具类 + JSONObject json_test = httpConf.getJSONObject(loginUrl); + String wxOpenid = json_test.getString("openid"); + String sessionKey = json_test.getString("session_key"); + if (StringUtils.isBlank(wxOpenid)) { + throw new JeecgBootException("未获取到openid"); + } + + HanHaiMember member = hanHaiMemberService.lambdaQuery().eq(HanHaiMember::getAppletOpenid,wxOpenid).one(); + if (member == null) { + //如果user等于null说明该用户第一次登录,数据库没有该用户信息。 + loginReq.setOpenid(wxOpenid); + loginReq.setSession_key(sessionKey); + member = new HanHaiMember(); +// member.setSesssionKey(sessionKey); + member.setAppletOpenid(wxOpenid); + member.setNickName(loginReq.getNickName()); + member.setHeadImage(loginReq.getHeadimgurl()); + // 生成token返回给小程序端 + String token = JwtUtil.sign(member.getAppletOpenid(), wxOpenid); + hanHaiMemberService.save(member); + redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token); + // 设置超时时间 + redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 100); + map.put("userInfo", member); + map.put("token", token); + + result.setCode(200); + result.setResult(map); + return result; + } else { +// member.setHeadImage(loginReq.getHeadimgurl()); +// member.setSessionKey(sessionKey); +// memberService.saveOrUpdate(member); + // 生成token返回给小程序端 + String token = JwtUtil.sign(member.getAppletOpenid(), wxOpenid); + redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token); + // 设置超时时间 + redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 100); + map.put("userInfo", member); + map.put("token", token); + //用户id做im账号 + result.setResult(map); + result.setCode(200); +// result.setMessage(member.getNickName()+"已于"+member.getCreateTime()+"注册成功!请勿重复注册!"); +// // 未绑定手机号,跳转授权绑定手机号 +// if (StringUtils.isBlank(member.getPhone())) { +// result.setMessage("该微信用户尚未绑定手机号,请授权手机进行绑定"); +// result.setCode(905); +// result.setResult(map); +// return result; +// } + } + return result; + } + + @Override + public Result bindPhone(String phoneCode) { + try { + String phoneNumber = this.getPhoneNumber(phoneCode); + return Result.OK(phoneNumber); + }catch (Exception e){ + return Result.error(e.getMessage()); + } + } + + private static final String API_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"; + public String getPhoneNumber(String code) throws Exception { + URL url = new URL(API_URL + "?access_token=" + this.getAccessToken()); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("POST"); + conn.setRequestProperty("Content-Type", "application/json; utf-8"); + conn.setRequestProperty("Accept", "application/json"); + conn.setDoOutput(true); + + JSONObject jsonInput = new JSONObject(); + jsonInput.put("code", code); + + try (DataOutputStream os = new DataOutputStream(conn.getOutputStream())) { + byte[] input = jsonInput.toString().getBytes(StandardCharsets.UTF_8); + os.write(input, 0, input.length); + } + + try (BufferedReader br = new BufferedReader( + new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { + StringBuilder response = new StringBuilder(); + String responseLine; + while ((responseLine = br.readLine()) != null) { + response.append(responseLine.trim()); + } + //获取手机号码 + return response.toString(); + } + } + + private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"; + public String getAccessToken() throws Exception { + String requestUrl = String.format(TOKEN_URL, mpAppId, mpAppSecret); + URL url = new URL(requestUrl); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + + try (BufferedReader br = new BufferedReader( + new InputStreamReader(conn.getInputStream(), "UTF-8"))) { + StringBuilder response = new StringBuilder(); + String responseLine; + while ((responseLine = br.readLine()) != null) { + response.append(responseLine.trim()); + } + + org.json.JSONObject jsonResponse = new org.json.JSONObject(response.toString()); + return jsonResponse.getString("access_token"); + } + } + +} diff --git a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiService/impl/RecommendServiceImpl.java b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiService/impl/RecommendServiceImpl.java new file mode 100644 index 0000000..7471e4d --- /dev/null +++ b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/apiService/impl/RecommendServiceImpl.java @@ -0,0 +1,263 @@ +package org.jeecg.modules.apiService.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.TypeReference; +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.jeecg.common.api.vo.Result; +import org.jeecg.config.shiro.ShiroRealm; +import org.jeecg.modules.apiBean.HttpClientUtil; +import org.jeecg.modules.apiBean.PageBean; +import org.jeecg.modules.apiBean.WxQrCodeVo; +import org.jeecg.modules.apiService.RecommendService; +import org.jeecg.modules.hanHaiMember.entity.HanHaiMember; +import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService; +import org.jeecg.modules.teambuyConfig.entity.TeambuyConfig; +import org.jeecg.modules.teambuyConfig.service.ITeambuyConfigService; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.commons.CommonsMultipartFile; + +import javax.annotation.Resource; +import java.io.*; +import java.util.HashMap; +import java.util.Map; + +@Service +public class RecommendServiceImpl implements RecommendService { + /*************************************************************************************/ + //权限验证 + @Resource + private ShiroRealm shiroRealm; + + //用户信息 + @Resource + private IHanHaiMemberService hanHaiMemberService; + + //配置信息 + @Resource + private ITeambuyConfigService teambuyConfigService; + + private String appid = "wx94a640f07969d6c9";//小程序appid + private String secret = "";//小程序密钥 + @Value("${jeecg.oss.endpoint}") + private String endpoint; + @Value("${jeecg.oss.accessKey}") + private String accessKey; + @Value("${jeecg.oss.secretKey}") + private String secretKey; + @Value("${jeecg.oss.bucketName}") + private String bucketName; + @Value("${jeecg.oss.staticDomain}") + private String staticDomain; + /*************************************************************************************/ + + //获取个人推荐二维码 + @Override + public Result getInviteCode(String token) { + //权限验证 + HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token); + + //获取版本信息 + TeambuyConfig vsion = teambuyConfigService.lambdaQuery() + .eq(TeambuyConfig::getParamCode, "v_sion") + .one(); + Integer vsionStr = Integer.parseInt(vsion.getParamValueText()); + String trial = "release"; + if(vsionStr == 0){ + trial= "release"; + }else if(vsionStr == 1){ + trial= "trial"; + }else{ + trial= "develop"; + } + + Map param = new HashMap<>(); + //获取跳转路径信息 + TeambuyConfig xcxSharePage = teambuyConfigService.lambdaQuery() + .eq(TeambuyConfig::getParamCode, "xcxSharePage") + .one(); + + String key = "shareId=" + hanHaiMember.getId(); + + param.put("path", xcxSharePage.getParamValueText() + "?" + key); //跳转页面 + String accessToken = this.getAccessToken(); + RestTemplate rest = new RestTemplate(); + InputStream inputStream = null; + OutputStream outputStream = null; + File file = null; + + //获取存储地址 + TeambuyConfig oneImage = teambuyConfigService.lambdaQuery() + .eq(TeambuyConfig::getParamCode, "codeImg") + .one(); + + String codeImg = oneImage.getParamValueText(); + try{ + String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken; + param.put("scene", hanHaiMember.getId()); + param.put("width", 150); + param.put("auto_color", false); + param.put("env_version", trial); + Map line_color = new HashMap<>(); + line_color.put("r", 0); + line_color.put("g", 0); + line_color.put("b", 0); + param.put("line_color", line_color); + MultiValueMap headers = new LinkedMultiValueMap<>(); + org.springframework.http.HttpEntity requestEntity = new org.springframework.http.HttpEntity(JSON.toJSONString(param), headers); + ResponseEntity entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]); + byte[] result = entity.getBody(); + + inputStream = new ByteArrayInputStream(result); + String tempSave = codeImg; + String filePath = tempSave; + + file = new File(filePath); + if (!file.exists()) { + file.createNewFile(); + } + outputStream = new FileOutputStream(file); + int len = 0; + byte[] buf = new byte[1024]; + while ((len = inputStream.read(buf, 0, 1024)) != -1) { + outputStream.write(buf, 0, len); + } + outputStream.flush(); + + //将文件上传至阿里云 + DiskFileItemFactory factory = new DiskFileItemFactory(16, null); + FileItem item = factory.createItem("File", "text/plain", true, file.getName()); + int bytesRead = 0; + byte[] buffer = new byte[8192]; + try { + FileInputStream fis = new FileInputStream(file); + OutputStream os = item.getOutputStream(); + while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) { + os.write(buffer, 0, bytesRead); + } + os.close(); + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + MultipartFile mf = new CommonsMultipartFile(item); + //返回图片下载地址 + WxQrCodeVo wxCodeVo = new WxQrCodeVo(); + wxCodeVo.setUrl(staticDomain+this.uploadAliYunOss(mf)); + + wxCodeVo.setName("敢为人鲜欢迎您"); + + return Result.OK(wxCodeVo); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (outputStream != null) { + try { + outputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + //删除文件 + if (file.exists()) { + file.delete(); + } + } + return null; + } + + //我的团队-查询直推用户列表 + @Override + public Result queryRecommendUserList(String token, PageBean pageBean) { + //权限验证 + HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token); + //返回信息 + String massege = ""; + //分页信息 + Page page = null; + //查询信息 + LambdaQueryChainWrapper query = null; + //返回信息 + Page pageList = null; + + try{ + //分页 + page = new Page(pageBean.getPageNo(), pageBean.getPageSize()); + query = hanHaiMemberService + .lambdaQuery(); + + //组装查询条件 + query.eq(HanHaiMember::getVid, hanHaiMember.getId()); + + //按照创建时间降序排列 + query.orderByAsc(HanHaiMember::getCreateTime); + + //获取直推用户信息 + pageList = query.page(page); + + return Result.OK("直推用户列表", pageList); + }catch (Exception e){ + e.printStackTrace(); + return Result.error("直推用户列表查询失败"); + } + } + + /** + * 获取令牌 + * + * @return + */ + private String getAccessToken() { + String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret; + String doGet2 = HttpClientUtil.doGet2(requestUrl); + Map map = JSON.parseObject(doGet2, new TypeReference>() { + }); + return map.get("access_token"); + } + + /** + * 上传文件至阿里云oss + * + * @return + */ + private String uploadAliYunOss(MultipartFile mf) throws Exception { + String uploadFile = "massage"; + String fileName = new StringBuffer(mf.getOriginalFilename()).append(".jpg").toString();// 获取文件名 + String dbpath = uploadFile + File.separator + fileName; + if (dbpath.contains("\\")) { + dbpath = dbpath.replace("\\", "/"); + } + String endpoint = this.endpoint; + String accessKey = this.accessKey; + String secretKey = this.secretKey; + // 创建OSSClient实例。 + OSS ossClient = new OSSClientBuilder().build(endpoint, accessKey, secretKey); + // 上传Byte数组。 + byte[] content = mf.getBytes(); + String bucketName = this.bucketName; + ossClient.putObject(bucketName, dbpath, new ByteArrayInputStream(content)); + // 关闭OSSClient。 + ossClient.shutdown(); + return dbpath; + } +} diff --git a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/service/impl/SysBaseApiImpl.java b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/service/impl/SysBaseApiImpl.java index 7dbb082..fac4cf2 100644 --- a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/service/impl/SysBaseApiImpl.java +++ b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/service/impl/SysBaseApiImpl.java @@ -144,6 +144,23 @@ public class SysBaseApiImpl implements ISysBaseAPI { return user; } + /** + * 公共验证--根据账号查询验证用户信息-客户端 + * @param username + * @return + * */ + @Override + public HanHaiMember getUserByNameHanHaiXcxOpenId(String username){ + if(oConvertUtils.isEmpty(username)) { + return null; + } + HanHaiMember user = hanHaiMemberService.lambdaQuery().eq(HanHaiMember::getAppletOpenid,username).one(); + if(user==null) { + return null; + } + return user; + } + @Override public String translateDictFromTable(String table, String text, String code, String key) {