parent
9cfe5e88d7
commit
f60193a9b9
@ -1,5 +1,2 @@ |
||||
wxAppId=wx01898d5209357d68 |
||||
wxAppSecret=5c52d0526fcdb425075f6d51381d5c03 |
||||
|
||||
fileUrl=F:/hurui_probject/filesystem |
||||
cmsPath=F:/hurui_probject/filesystem |
||||
|
@ -1 +1,2 @@ |
||||
fileUrl=/home/filesystem |
||||
fileUrl=F:/hurui_probject/filesystem |
||||
cmsPath=F:/hurui_probject/filesystem |
||||
|
@ -0,0 +1,53 @@ |
||||
package com.cweb.config; |
||||
|
||||
import java.util.Map; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
|
||||
public class SessionKeyCache { |
||||
|
||||
private static Map<String, CacheData> CACHE_DATA = new ConcurrentHashMap<>(); |
||||
|
||||
public static <T> T getData(String key) { |
||||
CacheData<T> data = CACHE_DATA.get(key); |
||||
if (data != null){ |
||||
if(data.getExpire() <= 0 || data.getSaveTime() >= System.currentTimeMillis()) { |
||||
return data.getData(); |
||||
}else{ |
||||
clear(key); |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
public static <T> void setData(String key, T data, int expire) { |
||||
CACHE_DATA.put(key, new CacheData(data, expire)); |
||||
} |
||||
|
||||
public static void clear(String key) { |
||||
CACHE_DATA.remove(key); |
||||
} |
||||
|
||||
private static class CacheData<T> { |
||||
CacheData(T t, int expire) { |
||||
this.data = t; |
||||
this.expire = expire <= 0 ? 0 : expire*1000; |
||||
this.saveTime = System.currentTimeMillis() + this.expire; |
||||
} |
||||
|
||||
private T data; |
||||
private long saveTime; // 存活时间
|
||||
private long expire; // 过期时间 小于等于0标识永久存活
|
||||
|
||||
public T getData() { |
||||
return data; |
||||
} |
||||
|
||||
public long getExpire() { |
||||
return expire; |
||||
} |
||||
|
||||
public long getSaveTime() { |
||||
return saveTime; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,33 @@ |
||||
package com.cweb.config; |
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService; |
||||
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; |
||||
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
import javax.annotation.PostConstruct; |
||||
|
||||
@Configuration |
||||
public class WxMaConfiguration { |
||||
|
||||
private static WxMaService maService; |
||||
|
||||
public static WxMaService getMaService() { |
||||
if (maService == null) { |
||||
throw new IllegalArgumentException(String.format("未找到对应的配置,请核实!")); |
||||
} |
||||
|
||||
return maService; |
||||
} |
||||
|
||||
@PostConstruct |
||||
public void init() { |
||||
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); |
||||
config.setAppid(SysConst.getSysConfig().getWxAppId()); |
||||
config.setSecret(SysConst.getSysConfig().getWxAppSecret()); |
||||
|
||||
maService = new WxMaServiceImpl(); |
||||
maService.setWxMaConfig(config); |
||||
} |
||||
|
||||
} |
@ -1,143 +0,0 @@ |
||||
package com.cweb.controller; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.hai.common.exception.ErrorCode; |
||||
import com.hai.common.exception.ErrorHelp; |
||||
import com.hai.common.exception.SysCode; |
||||
import com.hai.common.security.*; |
||||
import com.hai.common.utils.ResponseMsgUtil; |
||||
import com.hai.model.ResponseData; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.stereotype.Controller; |
||||
import org.springframework.web.bind.annotation.RequestBody; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RequestMethod; |
||||
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
||||
import javax.annotation.Resource; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.util.Objects; |
||||
|
||||
@Controller |
||||
@RequestMapping(value = "/login") |
||||
@Api(value = "登录") |
||||
public class LoginController { |
||||
|
||||
private static Logger log = LoggerFactory.getLogger(LoginController.class); |
||||
|
||||
@Resource |
||||
private UserCenter userCenter; |
||||
|
||||
@RequestMapping(value="/userLogin",method = RequestMethod.POST) |
||||
@ResponseBody |
||||
@ApiOperation(value = "用户登录 {'phone':'','password':'','code':'','type':''}") |
||||
public ResponseData userLogin(@RequestBody String reqBody, HttpServletRequest request, HttpServletResponse response) { |
||||
try { |
||||
|
||||
|
||||
/* UserInfoModel userInfoModel = new UserInfoModel(); |
||||
String aesStr = AESEncodeUtil.aesEncrypt(customer.getId().toString()); |
||||
SessionObject so = new SessionObject(aesStr, 1,userInfoModel); |
||||
userCenter.save(request, response, so);*/ |
||||
return ResponseMsgUtil.success(null); |
||||
} catch (Exception e) { |
||||
return ResponseMsgUtil.exception(e); |
||||
} |
||||
} |
||||
|
||||
@RequestMapping(value="/logout",method = RequestMethod.POST) |
||||
@ResponseBody |
||||
@ApiOperation(value = "登出") |
||||
public ResponseData logout(HttpServletRequest request, HttpServletResponse response){ |
||||
try { |
||||
userCenter.remove(request, response); |
||||
return ResponseMsgUtil.success("退出成功"); |
||||
|
||||
} catch (Exception e) { |
||||
log.error("login error!",e); |
||||
return ResponseMsgUtil.exception(e); |
||||
} |
||||
|
||||
} |
||||
|
||||
@RequestMapping(value="/checkVerifyCode",method = RequestMethod.POST) |
||||
@ResponseBody |
||||
@ApiOperation(value = "校验验证码 ({'phone':'','code':''})") |
||||
public ResponseData checkVerifyCode(@RequestBody String reqBody){ |
||||
try { |
||||
if(StringUtils.isBlank(reqBody)) { |
||||
log.error("login error!","参数错误"); |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); |
||||
} |
||||
JSONObject jsonObject = JSONObject.parseObject(reqBody); |
||||
String phone = jsonObject.getString("phone"); |
||||
String code = jsonObject.getString("code"); |
||||
if(StringUtils.isBlank(phone) || StringUtils.isBlank(code)) { |
||||
log.error("login error!","参数错误"); |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); |
||||
} |
||||
|
||||
VerifyCode verifyCode = VerifyCodeStorage.getDate(phone); |
||||
if (verifyCode == null) { |
||||
return ResponseMsgUtil.success(false); |
||||
} |
||||
|
||||
// 校验验证码
|
||||
if (Objects.equals(String.valueOf(verifyCode.getObject()),code)) { |
||||
// 清除记录,验证码只能验证一次
|
||||
//2020-08-06暂不做清除处理
|
||||
//VerifyCodeStorage.remove(phone);
|
||||
return ResponseMsgUtil.success(true); |
||||
} else { |
||||
return ResponseMsgUtil.success(false); |
||||
} |
||||
} catch (Exception e) { |
||||
log.error("login error!",e); |
||||
return ResponseMsgUtil.exception(e); |
||||
} |
||||
|
||||
} |
||||
|
||||
@RequestMapping(value="/forgetPassword",method = RequestMethod.POST) |
||||
@ResponseBody |
||||
@ApiOperation(value = "忘记密码 ({'phone':'','code':'','password':'','sign':''})") |
||||
public ResponseData forgetPassword(@RequestBody String reqBody) { |
||||
try { |
||||
|
||||
JSONObject jsonObject = JSONObject.parseObject(reqBody); |
||||
|
||||
String phone = jsonObject.getString("phone"); //用户电话
|
||||
String code = jsonObject.getString("code"); // 验证码
|
||||
String password = jsonObject.getString("password");//新密码
|
||||
|
||||
if(StringUtils.isBlank(phone) || StringUtils.isBlank(code) || |
||||
StringUtils.isBlank(password)) { |
||||
log.error("LoginController --> forgetPassword() error!","参数错误"); |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); |
||||
} |
||||
|
||||
VerifyCode verifyCode = VerifyCodeStorage.getDate(phone); |
||||
if (verifyCode == null) { |
||||
log.error("LoginController --> forgetPassword() error!","验证码错误"); |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.SMS_CODE_ERROR, ""); |
||||
} |
||||
|
||||
// 校验验证码
|
||||
if (!Objects.equals(String.valueOf(verifyCode.getObject()),code)) { |
||||
log.error("LoginController --> forgetPassword() error!","验证码错误"); |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.SMS_CODE_ERROR, ""); |
||||
} |
||||
|
||||
return ResponseMsgUtil.success("密码设置成功"); |
||||
} catch (Exception e) { |
||||
log.error("LoginController --> forgetPassword() error!",e); |
||||
return ResponseMsgUtil.exception(e); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,218 @@ |
||||
package com.cweb.controller; |
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService; |
||||
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; |
||||
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; |
||||
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; |
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.cweb.config.SessionKeyCache; |
||||
import com.cweb.config.WxMaConfiguration; |
||||
import com.hai.common.exception.ErrorCode; |
||||
import com.hai.common.exception.ErrorHelp; |
||||
import com.hai.common.exception.SysCode; |
||||
import com.hai.common.security.SessionObject; |
||||
import com.hai.common.security.UserCenter; |
||||
import com.hai.common.utils.ResponseMsgUtil; |
||||
import com.hai.entity.HighUser; |
||||
import com.hai.model.HighUserModel; |
||||
import com.hai.model.ResponseData; |
||||
import com.hai.service.HighUserService; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Controller; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RequestMethod; |
||||
import org.springframework.web.bind.annotation.RequestParam; |
||||
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
||||
import javax.annotation.Resource; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.util.Date; |
||||
|
||||
@Controller |
||||
@RequestMapping(value = "/wechat") |
||||
@Api(value = "微信授权") |
||||
public class WechatController { |
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(WechatController.class); |
||||
|
||||
@Autowired |
||||
private UserCenter userCenter; |
||||
|
||||
@Resource |
||||
private HighUserService highUserService; |
||||
|
||||
@RequestMapping(value = "/handleCode", method = RequestMethod.GET) |
||||
@ResponseBody |
||||
@ApiOperation(value = "微信小程序code解析") |
||||
public ResponseData compairCode(@RequestParam(value = "code", required = true) String code, |
||||
HttpServletRequest request, HttpServletResponse response) { |
||||
try { |
||||
|
||||
//校验code,openId不能同时为空
|
||||
if (StringUtils.isEmpty(code)) { |
||||
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.REQ_PARAMS_ERROR, ""); |
||||
} |
||||
//请求微信api,获取用户session_key以及openId
|
||||
final WxMaService wxService = WxMaConfiguration.getMaService(); |
||||
WxMaJscode2SessionResult session = wxService.jsCode2SessionInfo(code); |
||||
//保存小程序用户登录的openId及sessionKey信息
|
||||
SessionKeyCache.setData(session.getOpenid(), session.getSessionKey(), -1); |
||||
JSONObject jo = new JSONObject(); |
||||
jo.put("openId", session.getOpenid()); |
||||
return ResponseMsgUtil.success(jo); |
||||
} catch (Exception e) { |
||||
return ResponseMsgUtil.exception(e); |
||||
} |
||||
} |
||||
|
||||
@RequestMapping(value = "/login", method = RequestMethod.GET) |
||||
@ResponseBody |
||||
@ApiOperation(value = "微信小程序授权用户登录接口") |
||||
public ResponseData login(@RequestParam(value = "openId", required = true) String openId, |
||||
HttpServletRequest request, HttpServletResponse response) { |
||||
try { |
||||
|
||||
//校验code,openId不能同时为空
|
||||
if (StringUtils.isEmpty(openId)) { |
||||
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.REQ_PARAMS_ERROR, ""); |
||||
} |
||||
HighUser highUser = highUserService.findByOpenId(openId); |
||||
if (highUser == null) { |
||||
log.error("login error!", "未找到用户"); |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.UN_MEMBER_ERROR, ""); |
||||
} |
||||
|
||||
// 定义个人所有数据
|
||||
HighUserModel loginDataModel = new HighUserModel(); |
||||
highUser.setPassword(null); |
||||
loginDataModel.setHighUser(highUser); |
||||
|
||||
SessionObject so = new SessionObject(openId, 99 , loginDataModel); |
||||
userCenter.save(request, response, so); |
||||
|
||||
return ResponseMsgUtil.success(so); |
||||
} catch (Exception e) { |
||||
return ResponseMsgUtil.exception(e); |
||||
} |
||||
} |
||||
|
||||
@RequestMapping(value = "/getPhoneNumber", method = RequestMethod.GET) |
||||
@ResponseBody |
||||
@ApiOperation(value = "小程序获取用户手机号码") |
||||
public ResponseData getPhoneNumber(@RequestParam(value = "encryptedData", required = true) String encryptedData, |
||||
@RequestParam(value = "iv", required = true) String iv, |
||||
@RequestParam(value = "openId", required = true) String openId, |
||||
HttpServletRequest request, HttpServletResponse response) { |
||||
try { |
||||
log.error("origin encryptedData:" + encryptedData + ";iv:" + iv); |
||||
//校验openId不能为空
|
||||
//encryptedData,iv与unionId不能同时为空
|
||||
encryptedData = encryptedData.replace(" ", "+"); |
||||
iv = iv.replace(" ", "+"); |
||||
log.error("dest encryptedData:" + encryptedData + ";iv:" + iv); |
||||
if (StringUtils.isEmpty(openId) |
||||
|| StringUtils.isEmpty(encryptedData) || StringUtils.isEmpty(iv)) { |
||||
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.REQ_PARAMS_ERROR, ""); |
||||
} |
||||
//请求微信api,获取用户session_key以及openId
|
||||
String sessionKey = SessionKeyCache.getData(openId); |
||||
|
||||
final WxMaService wxService = WxMaConfiguration.getMaService(); |
||||
|
||||
WxMaPhoneNumberInfo phoneNoInfo = wxService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv); |
||||
if (phoneNoInfo == null || StringUtils.isEmpty(phoneNoInfo.getPurePhoneNumber())) { |
||||
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "用户phoneNumber解析失败"); |
||||
} |
||||
HighUser user = highUserService.findByPhone(phoneNoInfo.getPurePhoneNumber()); |
||||
if (user == null) { |
||||
user = new HighUser(); |
||||
user.setOpenId(openId); |
||||
user.setRegTime(new Date()); |
||||
user.setStatus(1); |
||||
user.setPhone(phoneNoInfo.getPurePhoneNumber()); |
||||
highUserService.insertUser(user); |
||||
} else { |
||||
user.setOpenId(openId);; |
||||
highUserService.updateUser(user); |
||||
} |
||||
|
||||
// 定义个人所有数据
|
||||
HighUserModel highUserModel = new HighUserModel(); |
||||
user.setPassword(null); |
||||
highUserModel.setHighUser(user); |
||||
|
||||
SessionObject so = new SessionObject(openId, 1 , highUserModel); |
||||
userCenter.save(request, response, so); |
||||
|
||||
return ResponseMsgUtil.success(so); |
||||
|
||||
} catch (Exception e) { |
||||
return ResponseMsgUtil.exception(e); |
||||
} |
||||
} |
||||
|
||||
@RequestMapping(value = "/getUserInfo", method = RequestMethod.GET) |
||||
@ResponseBody |
||||
@ApiOperation(value = "小程序获取用户信息") |
||||
public ResponseData getUserInfo(@RequestParam(value = "encryptedData", required = true) String encryptedData, |
||||
@RequestParam(value = "iv", required = true) String iv, |
||||
@RequestParam(value = "openId", required = true) String openId, |
||||
HttpServletRequest request, HttpServletResponse response) { |
||||
try { |
||||
//校验openId不能为空
|
||||
//encryptedData,iv与unionId不能同时为空
|
||||
encryptedData = encryptedData.replace(" ", "+"); |
||||
iv = iv.replace(" ", "+"); |
||||
if (StringUtils.isEmpty(openId) |
||||
|| StringUtils.isEmpty(encryptedData) || StringUtils.isEmpty(iv)) { |
||||
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.REQ_PARAMS_ERROR, ""); |
||||
} |
||||
//请求微信api,获取用户session_key以及openId
|
||||
String sessionKey = SessionKeyCache.getData(openId); |
||||
|
||||
final WxMaService wxService = WxMaConfiguration.getMaService(); |
||||
|
||||
WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(sessionKey, encryptedData, iv); |
||||
System.out.println(""); |
||||
if (userInfo == null) { |
||||
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "用户phoneNumber解析失败"); |
||||
} |
||||
HighUser user = highUserService.findByOpenId(openId); |
||||
if (user == null) { |
||||
user = new HighUser(); |
||||
user.setOpenId(openId); |
||||
user.setRegTime(new Date()); |
||||
user.setStatus(1); |
||||
user.setHeaderImg(userInfo.getAvatarUrl()); |
||||
user.setName(userInfo.getNickName()); |
||||
highUserService.insertUser(user); |
||||
} else { |
||||
user.setOpenId(openId); |
||||
user.setHeaderImg(userInfo.getAvatarUrl()); |
||||
user.setName(userInfo.getNickName()); |
||||
highUserService.updateUser(user); |
||||
} |
||||
|
||||
// 定义个人所有数据
|
||||
HighUserModel highUserModel = new HighUserModel(); |
||||
user.setPassword(null); |
||||
highUserModel.setHighUser(user); |
||||
|
||||
SessionObject so = new SessionObject(openId, 1 , highUserModel); |
||||
userCenter.save(request, response, so); |
||||
|
||||
return ResponseMsgUtil.success(so); |
||||
|
||||
} catch (Exception e) { |
||||
return ResponseMsgUtil.exception(e); |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
@ -1 +1,5 @@ |
||||
fileUrl=/home/filesystem |
||||
wxAppId=wx01898d5209357d68 |
||||
wxAppSecret=5c52d0526fcdb425075f6d51381d5c03 |
||||
|
||||
fileUrl=F:/hurui_probject/filesystem |
||||
cmsPath=F:/hurui_probject/filesystem |
||||
|
@ -0,0 +1,21 @@ |
||||
package com.hai.model; |
||||
|
||||
import com.hai.entity.SecUser; |
||||
|
||||
/** |
||||
* @Auther: 胡锐 |
||||
* @Description: |
||||
* @Date: 2021/3/10 20:22 |
||||
*/ |
||||
public class HighMerchantModel { |
||||
|
||||
private SecUser secUser; |
||||
|
||||
public SecUser getSecUser() { |
||||
return secUser; |
||||
} |
||||
|
||||
public void setSecUser(SecUser secUser) { |
||||
this.secUser = secUser; |
||||
} |
||||
} |
@ -0,0 +1,22 @@ |
||||
package com.hai.model; |
||||
|
||||
import com.hai.entity.SecUser; |
||||
|
||||
/** |
||||
* @Auther: 胡锐 |
||||
* @Description: |
||||
* @Date: 2021/3/10 22:11 |
||||
*/ |
||||
public class HighMerchantStoreModel { |
||||
|
||||
// 账号
|
||||
private SecUser secUser; |
||||
|
||||
public SecUser getSecUser() { |
||||
return secUser; |
||||
} |
||||
|
||||
public void setSecUser(SecUser secUser) { |
||||
this.secUser = secUser; |
||||
} |
||||
} |
@ -0,0 +1,22 @@ |
||||
package com.hai.model; |
||||
|
||||
import com.hai.entity.HighUser; |
||||
|
||||
/** |
||||
* @Auther: 胡锐 |
||||
* @Description: |
||||
* @Date: 2021/3/10 22:50 |
||||
*/ |
||||
public class HighUserModel { |
||||
|
||||
// 用户
|
||||
private HighUser highUser; |
||||
|
||||
public HighUser getHighUser() { |
||||
return highUser; |
||||
} |
||||
|
||||
public void setHighUser(HighUser highUser) { |
||||
this.highUser = highUser; |
||||
} |
||||
} |
Loading…
Reference in new issue