@@ -53,6 +53,10 @@ public class AccountVO{ | |||
* 对应角色Id | |||
*/ | |||
private String roleId; | |||
/** | |||
* 微信opeanid | |||
*/ | |||
private String wxOpeanId; | |||
public AccountVO(){} | |||
@@ -149,4 +153,14 @@ public class AccountVO{ | |||
public void setRoleId(String roleId) { | |||
this.roleId = roleId; | |||
} | |||
public String getWxOpeanId() { | |||
return wxOpeanId; | |||
} | |||
public void setWxOpeanId(String wxOpeanId) { | |||
this.wxOpeanId = wxOpeanId; | |||
} | |||
} |
@@ -0,0 +1,21 @@ | |||
package com.hp.user.client.entity; | |||
public class DataResult<T> extends Result{ | |||
/** | |||
* | |||
*/ | |||
private static final long serialVersionUID = 1L; | |||
private T data; | |||
public T getData() { | |||
return data; | |||
} | |||
public void setData(T data) { | |||
this.data = data; | |||
} | |||
} |
@@ -0,0 +1,21 @@ | |||
package com.hp.user.client.entity; | |||
import java.util.List; | |||
public class ListResult<T> extends Result{ | |||
/** | |||
* | |||
*/ | |||
private static final long serialVersionUID = 1L; | |||
private List<T> dataList; | |||
public List<T> getDataList() { | |||
return dataList; | |||
} | |||
public void setDataList(List<T> dataList) { | |||
this.dataList = dataList; | |||
} | |||
} |
@@ -0,0 +1,9 @@ | |||
package com.hp.user.client.entity; | |||
import java.util.List; | |||
public class PageResult<T> extends Result{ | |||
private List<T> dataList; | |||
} |
@@ -0,0 +1,36 @@ | |||
package com.hp.user.client.entity; | |||
import java.io.Serializable; | |||
public class Result implements Serializable{ | |||
/** | |||
* | |||
*/ | |||
private static final long serialVersionUID = 1L; | |||
/** | |||
* 错误码,如果没有错误为null | |||
*/ | |||
private String code; | |||
/** | |||
* 错误信息,如果没有错误为null | |||
* | |||
*/ | |||
private String message; | |||
public String getCode() { | |||
return code; | |||
} | |||
public void setCode(String code) { | |||
this.code = code; | |||
} | |||
public String getMessage() { | |||
return message; | |||
} | |||
public void setMessage(String message) { | |||
this.message = message; | |||
} | |||
} |
@@ -1,6 +1,7 @@ | |||
package com.hp.user.client.service; | |||
import com.hp.user.client.entity.AccountVO; | |||
import com.hp.user.client.entity.DataResult; | |||
/** | |||
* <p> | |||
@@ -43,6 +44,14 @@ public interface AccountService { | |||
/** | |||
* 微信授权登陆 | |||
*/ | |||
public void wxAuthorization(); | |||
public DataResult<AccountVO> wxAuthorization(AccountVO account,String wxCode); | |||
/** | |||
* 通过微信注册 | |||
* @param account | |||
* @param verificationCode 验证码 | |||
* @return | |||
*/ | |||
public DataResult<AccountVO> wxRegister(AccountVO account,String verificationCode); | |||
} |
@@ -10,5 +10,27 @@ public class UserConstants { | |||
* rediskey失效时间 | |||
*/ | |||
public static int EXPIRE_SECONDS = 900; | |||
/** | |||
* 微信开放平台二维码连接 | |||
*/ | |||
public final static String OPEN_QRCODE_URL= "https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_login&state=%s#wechat_redirect"; | |||
/** | |||
* 开放平台获取access_token地址 | |||
*/ | |||
public final static String OPEN_ACCESS_TOKEN_URL="https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code"; | |||
/** | |||
* 获取用户信息 | |||
*/ | |||
public final static String OPEN_USER_INFO_URL ="https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s"; | |||
public final static String WX_APPID = "wx405cd1f8a4a7c949"; | |||
public final static String WX_SECRET = "1b5807ce618a43d2e0a02a4dc14dd952"; | |||
public final static String WX_GRANT_TYPE = "authorization_code"; | |||
} |
@@ -6,14 +6,16 @@ import org.springframework.web.bind.annotation.RequestMapping; | |||
import org.springframework.web.bind.annotation.RestController; | |||
import com.hp.user.client.entity.AccountVO; | |||
import com.hp.user.client.entity.DataResult; | |||
import com.hp.user.client.service.AccountService; | |||
import com.hp.user.service.entity.Account; | |||
import com.hp.user.service.utils.HttpResult; | |||
import io.swagger.annotations.Api; | |||
import io.swagger.annotations.ApiOperation; | |||
@RestController | |||
@RequestMapping("/authentication") | |||
@Api(value="身份认证") | |||
@Api(tags="身份认证") | |||
public class AuthenticationController { | |||
@Autowired | |||
@@ -26,6 +28,7 @@ public class AuthenticationController { | |||
* @param verificationCode 验证码 | |||
*/ | |||
@RequestMapping("/login") | |||
@ApiOperation(value = "验证码登录", notes = "验证码登录", httpMethod = "POST") | |||
public void login(String phone,String verificationCode) { | |||
accountService.login(phone, verificationCode); | |||
} | |||
@@ -43,32 +46,39 @@ public class AuthenticationController { | |||
* 需要用户信息 | |||
*/ | |||
@RequestMapping("/register") | |||
@ApiOperation(value = "注册", notes = "注册", httpMethod = "POST") | |||
public void register(AccountVO account) { | |||
accountService.register(account); | |||
} | |||
/** | |||
* 微信注册 | |||
* 需要用户信息 | |||
*/ | |||
@RequestMapping("/wxRegister") | |||
public void wxRegister() { | |||
} | |||
/** | |||
* 绑定微信号 | |||
* @param userId | |||
* @param wxNumber | |||
*/ | |||
@RequestMapping("/bingWx") | |||
@ApiOperation(value = "绑定微信号", notes = "绑定微信号", httpMethod = "POST") | |||
public void bindWx(String userId,String wxNumber) { | |||
accountService.bindWx(userId, wxNumber); | |||
} | |||
@RequestMapping("/wxAuthorization") | |||
public void wxAuthorization() { | |||
@ApiOperation(value = "微信认证登陆", notes = "微信认证登陆", httpMethod = "POST") | |||
public DataResult<AccountVO> wxAuthorization(AccountVO account,String code) { | |||
DataResult<AccountVO> result = accountService.wxAuthorization(account,code); | |||
return result; | |||
} | |||
/** | |||
* 通过微信注册 | |||
* @param account | |||
* @param verificationCode | |||
*/ | |||
@RequestMapping("/wxRegister") | |||
@ApiOperation(value = "通过微信注册", notes = "通过微信注册", httpMethod = "POST") | |||
public DataResult<AccountVO> wxRegister(AccountVO account,String verificationCode) { | |||
DataResult<AccountVO> result = accountService.wxRegister(account, verificationCode); | |||
return result; | |||
} | |||
@@ -4,17 +4,18 @@ import com.alibaba.fastjson.JSONObject; | |||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |||
import com.baomidou.mybatisplus.core.toolkit.IdWorker; | |||
import com.hp.user.client.entity.AccountVO; | |||
import com.hp.user.client.entity.DataResult; | |||
import com.hp.user.client.service.AccountService; | |||
import com.hp.user.client.service.MessageService; | |||
import com.hp.user.service.constants.UserConstants; | |||
import com.hp.user.service.dao.AccountMapper; | |||
import com.hp.user.service.entity.Account; | |||
import com.hp.user.service.utils.WeixinSignUtil; | |||
import java.util.HashMap; | |||
import java.util.Map; | |||
import javax.servlet.http.HttpUtils; | |||
import org.apache.commons.codec.binary.StringUtils; | |||
import org.apache.commons.lang3.StringUtils; | |||
import org.springframework.beans.BeanUtils; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.stereotype.Service; | |||
@@ -82,7 +83,8 @@ public class AccountServiceImpl implements AccountService { | |||
if(checkPhone(accountVo.getPhone())) { | |||
Account account = new Account(); | |||
BeanUtils.copyProperties(accountVo, account); | |||
account.setUserId(IdWorker.getId()); | |||
Long id = IdWorker.getId(); | |||
account.setUserId(id.toString()); | |||
accountMapper.insert(account); | |||
} | |||
}catch(Exception e) { | |||
@@ -94,9 +96,9 @@ public class AccountServiceImpl implements AccountService { | |||
@Override | |||
public void bindWx(String userIdStr, String wxNumber) { | |||
try { | |||
Long userId = Long.parseLong(userIdStr); | |||
// Long userId = Long.parseLong(userIdStr); | |||
Account account = new Account(); | |||
account.setUserId(userId); | |||
account.setUserId(userIdStr); | |||
account.setWxNumber(wxNumber); | |||
account.setWxNumber(wxNumber); | |||
accountMapper.updateById(account); | |||
@@ -107,28 +109,69 @@ public class AccountServiceImpl implements AccountService { | |||
} | |||
@Override | |||
public void wxAuthorization() { | |||
public DataResult<AccountVO> wxAuthorization(AccountVO account,String wxCode) { | |||
DataResult<AccountVO> result = new DataResult<AccountVO>(); | |||
JSONObject data = null; | |||
try { | |||
String wxLoginAppid = ""; | |||
String wxLoginSecret = ""; | |||
String jsCode = ""; | |||
String wxLoginGrantType = ""; | |||
String wxLoginUrl = ""; | |||
// userVO.getCode() | |||
Map<String, String> param = new HashMap<>(); | |||
param.put("appid", wxLoginAppid); | |||
param.put("secret", wxLoginSecret); | |||
param.put("js_code", jsCode); | |||
param.put("grant_type", wxLoginGrantType); | |||
// 发送请求 | |||
// String wxResult = HttpUtils.doGet(wxLoginUrl, param); | |||
String wxResult = ""; | |||
JSONObject jsonObject = JSONObject.parseObject(wxResult); | |||
data = WeixinSignUtil.getCode2Session(wxCode); | |||
String errmsg = data.getString("errmsg"); | |||
if(null != errmsg) { | |||
result.setCode("-1"); | |||
result.setCode(errmsg); | |||
return result; | |||
} | |||
String openid = data.getString("openid"); | |||
QueryWrapper<Account> queryWrapper = new QueryWrapper<>(); | |||
queryWrapper.eq("wx_opean_id", openid); | |||
Account findAccount = accountMapper.selectOne(queryWrapper); | |||
BeanUtils.copyProperties(findAccount, account); | |||
if(null != findAccount) { | |||
result.setCode("-1"); | |||
result.setCode("微信需要绑定"); | |||
}else { | |||
result.setData(account); | |||
} | |||
}catch(Exception e) { | |||
result.setCode("-1"); | |||
result.setCode("系统出现问题"); | |||
} | |||
return result; | |||
} | |||
@Override | |||
public DataResult<AccountVO> wxRegister(AccountVO accountVO, String verificationCode) { | |||
DataResult<AccountVO> result = new DataResult<AccountVO>(); | |||
try { | |||
if(StringUtils.isBlank(accountVO.getPhone())) { | |||
if(checkPhone(accountVO.getPhone())) { | |||
String code = messageService.getVerificationCode(accountVO.getPhone()); | |||
if(StringUtils.equals(verificationCode, code)) { | |||
} | |||
Account account = new Account(); | |||
BeanUtils.copyProperties(accountVO, account); | |||
Long id = IdWorker.getId(); | |||
account.setUserId(id.toString()); | |||
accountMapper.insert(account); | |||
BeanUtils.copyProperties(account, accountVO); | |||
result.setData(accountVO); | |||
}else { | |||
result.setCode("-1"); | |||
result.setCode("手机号码已经存在"); | |||
return result; | |||
} | |||
}else { | |||
result.setCode("-1"); | |||
result.setCode("手机号不能为空"); | |||
return result; | |||
} | |||
}catch(Exception e) { | |||
} | |||
return result; | |||
} | |||
} |
@@ -0,0 +1,74 @@ | |||
package com.hp.user.service.utils; | |||
import java.net.URLDecoder; | |||
import org.apache.http.HttpResponse; | |||
import org.apache.http.client.methods.HttpGet; | |||
import org.apache.http.client.methods.HttpPost; | |||
import org.apache.http.impl.client.DefaultHttpClient; | |||
import org.apache.http.util.EntityUtils; | |||
/** | |||
* http请求工具类 | |||
* @author yeqid | |||
* @since 2020/12/01 | |||
* | |||
*/ | |||
public class HttpRequestUtil { | |||
/** | |||
* post请求 | |||
* @param url url地址 | |||
* @return | |||
*/ | |||
public static String httpPost(String url){ | |||
//post请求返回结果 | |||
DefaultHttpClient httpClient = new DefaultHttpClient(); | |||
HttpPost method = new HttpPost(url); | |||
String str = ""; | |||
try { | |||
HttpResponse result = httpClient.execute(method); | |||
url = URLDecoder.decode(url, "UTF-8"); | |||
/**请求发送成功,并得到响应**/ | |||
if (result.getStatusLine().getStatusCode() == 200) { | |||
try { | |||
/**读取服务器返回过来的json字符串数据**/ | |||
str = EntityUtils.toString(result.getEntity(),"UTF-8"); | |||
} catch (Exception e) { | |||
// logger.error("post请求提交失败:" + url, e); | |||
} | |||
} | |||
} catch (Exception e) { | |||
// logger.error("post请求提交失败:" + url, e); | |||
} | |||
return str; | |||
} | |||
/** | |||
* 发送get请求 | |||
* @param url 路径 | |||
* @return | |||
*/ | |||
public static String httpGet(String url){ | |||
//get请求返回结果 | |||
String strResult = null; | |||
try { | |||
DefaultHttpClient client = new DefaultHttpClient(); | |||
//发送get请求 | |||
HttpGet request = new HttpGet(url); | |||
HttpResponse response = client.execute(request); | |||
/**请求发送成功,并得到响应**/ | |||
if (response.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK) { | |||
/**读取服务器返回过来的json字符串数据**/ | |||
strResult = EntityUtils.toString(response.getEntity(),"UTF-8"); | |||
} else { | |||
// logger.error("get请求提交失败:" + url); | |||
} | |||
} catch (Exception e) { | |||
// logger.error("get请求提交失败:" + url, e); | |||
} | |||
return strResult; | |||
} | |||
} |
@@ -0,0 +1,62 @@ | |||
package com.hp.user.service.utils; | |||
import com.alibaba.fastjson.JSON; | |||
import com.alibaba.fastjson.JSONObject; | |||
import com.hp.user.service.constants.UserConstants; | |||
/** | |||
* 微信工具类 | |||
* @author yeqid | |||
*@sincs 2020/12/01 | |||
*/ | |||
public class WeixinSignUtil { | |||
/** | |||
* 网页 | |||
*/ | |||
public static JSONObject getAccessToken(String code){ | |||
// String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"; | |||
// GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET | |||
String url = "https://api.weixin.qq.com/cgi-bin/token?"; | |||
String params = "grant_type=client_credential&&appid="+UserConstants.WX_APPID+"&secret="+UserConstants.WX_SECRET; | |||
// String params = "appid="+UserConstants.WX_APPID+"&secret="+UserConstants.WX_SECRET+"&code="+code+"&grant_type=authorization_code"; | |||
String result = HttpRequestUtil.httpGet(url + params); | |||
JSONObject data = JSON.parseObject(result); | |||
return data; | |||
} | |||
public static JSONObject getValidateData(String access_token,String openid){ | |||
String url = "https://api.weixin.qq.com/sns/auth?access_token=" + access_token + "&openid=" + openid; | |||
String result = HttpRequestUtil.httpGet(url); | |||
JSONObject data = JSON.parseObject(result); | |||
return data; | |||
} | |||
public static JSONObject getRefreshToken(String refresh_token){ | |||
String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + UserConstants.WX_APPID + "&grant_type=refresh_token&refresh_token=" + refresh_token; | |||
String result = HttpRequestUtil.httpGet(url); | |||
JSONObject data = JSON.parseObject(result); | |||
return data; | |||
} | |||
public static JSONObject getUserInfo(String access_token,String openid){ | |||
String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN"; | |||
String result = HttpRequestUtil.httpGet(url); | |||
JSONObject data = JSON.parseObject(result); | |||
return data; | |||
} | |||
public static JSONObject getCode2Session(String code) { | |||
String url = "https://api.weixin.qq.com/sns/jscode2session?"; | |||
String params = "appid="+UserConstants.WX_APPID+"&secret="+UserConstants.WX_SECRET+"&js_code="+code+"&grant_type=authorization_code"; | |||
String result = HttpRequestUtil.httpGet(url + params); | |||
JSONObject data = JSON.parseObject(result); | |||
return data; | |||
} | |||
} |