|
|
|
@ -6,12 +6,14 @@ 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.SysConst; |
|
|
|
|
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.HttpsUtils; |
|
|
|
|
import com.hai.common.utils.ResponseMsgUtil; |
|
|
|
|
import com.hai.entity.HighUser; |
|
|
|
|
import com.hai.model.HighUserModel; |
|
|
|
@ -32,7 +34,11 @@ import org.springframework.web.bind.annotation.ResponseBody; |
|
|
|
|
import javax.annotation.Resource; |
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
import java.net.URLEncoder; |
|
|
|
|
import java.util.Date; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.LinkedHashMap; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
@Controller |
|
|
|
|
@RequestMapping(value = "/wechat") |
|
|
|
@ -49,7 +55,7 @@ public class WechatController { |
|
|
|
|
|
|
|
|
|
@RequestMapping(value = "/handleCode", method = RequestMethod.GET) |
|
|
|
|
@ResponseBody |
|
|
|
|
@ApiOperation(value = "微信小程序code解析") |
|
|
|
|
@ApiOperation(value = "小程序code解析") |
|
|
|
|
public ResponseData compairCode(@RequestParam(value = "code", required = true) String code, |
|
|
|
|
HttpServletRequest request, HttpServletResponse response) { |
|
|
|
|
try { |
|
|
|
@ -73,7 +79,7 @@ public class WechatController { |
|
|
|
|
|
|
|
|
|
@RequestMapping(value = "/login", method = RequestMethod.GET) |
|
|
|
|
@ResponseBody |
|
|
|
|
@ApiOperation(value = "微信小程序授权用户登录接口") |
|
|
|
|
@ApiOperation(value = "小程序授权用户登录接口") |
|
|
|
|
public ResponseData login(@RequestParam(value = "openId", required = true) String openId, |
|
|
|
|
HttpServletRequest request, HttpServletResponse response) { |
|
|
|
|
try { |
|
|
|
@ -218,4 +224,114 @@ public class WechatController { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RequestMapping(value = "/getH5Code", method = RequestMethod.GET) |
|
|
|
|
@ResponseBody |
|
|
|
|
@ApiOperation(value = "【H5】获取Code") |
|
|
|
|
public ResponseData getH5Code() { |
|
|
|
|
try { |
|
|
|
|
Map<String, Object> params = new LinkedHashMap<>(); |
|
|
|
|
params.put("appid", SysConst.getSysConfig().getWxH5AppId()); |
|
|
|
|
params.put("redirect_uri", URLEncoder.encode("https://hsgcs.dctpay.com/H5/index.html#/", "utf-8")); |
|
|
|
|
//params.put("redirect_uri", "https://hsgcs.dctpay.com/H5/index.html#/");
|
|
|
|
|
params.put("response_type", "code"); |
|
|
|
|
params.put("scope", "snsapi_userinfo"); |
|
|
|
|
params.put("state", "STATE"); |
|
|
|
|
|
|
|
|
|
String apiUrl = "https://open.weixin.qq.com/connect/oauth2/authorize"; |
|
|
|
|
StringBuffer param = new StringBuffer(); |
|
|
|
|
int i = 0; |
|
|
|
|
for (String key : params.keySet()) { |
|
|
|
|
if (i == 0) |
|
|
|
|
param.append("?"); |
|
|
|
|
else |
|
|
|
|
param.append("&"); |
|
|
|
|
param.append(key).append("=").append(params.get(key)); |
|
|
|
|
i++; |
|
|
|
|
} |
|
|
|
|
apiUrl += param + "#wechat_redirect"; |
|
|
|
|
return ResponseMsgUtil.success(apiUrl); |
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
return ResponseMsgUtil.exception(e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@RequestMapping(value = "/getH5AccessToken", method = RequestMethod.GET) |
|
|
|
|
@ResponseBody |
|
|
|
|
@ApiOperation(value = "【H5】获取AccessToken") |
|
|
|
|
public ResponseData getH5AccessToken(@RequestParam(value = "code", required = true) String code) { |
|
|
|
|
try { |
|
|
|
|
Map<String, Object> params = new HashMap<>(); |
|
|
|
|
params.put("appid", SysConst.getSysConfig().getWxH5AppId()); |
|
|
|
|
params.put("secret", SysConst.getSysConfig().getWxH5AppSecret()); |
|
|
|
|
params.put("code", code); |
|
|
|
|
params.put("grant_type", "authorization_code"); |
|
|
|
|
return ResponseMsgUtil.success(HttpsUtils.doGet("https://api.weixin.qq.com/sns/oauth2/access_token", params)); |
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
return ResponseMsgUtil.exception(e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@RequestMapping(value = "/getH5UserInfo", method = RequestMethod.GET) |
|
|
|
|
@ResponseBody |
|
|
|
|
@ApiOperation(value = "【H5】获取用户信息") |
|
|
|
|
public ResponseData getH5UserInfo(@RequestParam(value = "code", required = true) String code, |
|
|
|
|
HttpServletRequest request, HttpServletResponse response) { |
|
|
|
|
try { |
|
|
|
|
// 获取access_token
|
|
|
|
|
Map<String, Object> params = new HashMap<>(); |
|
|
|
|
params.put("appid", SysConst.getSysConfig().getWxH5AppId()); |
|
|
|
|
params.put("secret", SysConst.getSysConfig().getWxH5AppSecret()); |
|
|
|
|
params.put("code", code); |
|
|
|
|
params.put("grant_type", "authorization_code"); |
|
|
|
|
JSONObject jsonObject = HttpsUtils.doGet("https://api.weixin.qq.com/sns/oauth2/access_token", params); |
|
|
|
|
String accessToken = jsonObject.getString("access_token"); |
|
|
|
|
String openid = jsonObject.getString("openid"); |
|
|
|
|
if (StringUtils.isBlank(accessToken) || StringUtils.isBlank(openid)) { |
|
|
|
|
log.error("getH5UserInfo error!", "获取微信access_token失败" ); |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "获取微信access_token失败"); |
|
|
|
|
} |
|
|
|
|
params.clear(); |
|
|
|
|
params.put("access_token", accessToken); |
|
|
|
|
params.put("openid", openid); |
|
|
|
|
params.put("lang", "zh_CN"); |
|
|
|
|
JSONObject userInfo = HttpsUtils.doGet("https://api.weixin.qq.com/sns/userinfo", params); |
|
|
|
|
System.out.println(userInfo.toJSONString()); |
|
|
|
|
if (StringUtils.isNotBlank(userInfo.getString("errcode"))) { |
|
|
|
|
log.error("getH5UserInfo error!", "获取微信用户信息失败"); |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "获取微信用户信息失败"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
HighUser user = highUserService.findByOpenId(openid); |
|
|
|
|
if (user == null) { |
|
|
|
|
user = new HighUser(); |
|
|
|
|
user.setOpenId(openid); |
|
|
|
|
user.setRegTime(new Date()); |
|
|
|
|
user.setStatus(1); |
|
|
|
|
user.setGold(0); |
|
|
|
|
user.setHeaderImg(userInfo.getString("headimgurl")); |
|
|
|
|
user.setName(userInfo.getString("nickname")); |
|
|
|
|
highUserService.insertUser(user); |
|
|
|
|
} else { |
|
|
|
|
user.setOpenId(openid); |
|
|
|
|
user.setHeaderImg(userInfo.getString("headimgurl")); |
|
|
|
|
user.setName(userInfo.getString("nickname")); |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|