新增微信授权登录
This commit is contained in:
parent
e21c7241f0
commit
01eca3dcf9
|
|
@ -72,7 +72,7 @@ public class UsersAction {
|
|||
}
|
||||
return userService.login(loginParam);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 登陆验证
|
||||
* @param loginParam
|
||||
|
|
@ -87,6 +87,17 @@ public class UsersAction {
|
|||
}
|
||||
return userService.loginValidate(loginParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信登录
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(value="/wechatLogin",method=RequestMethod.POST)
|
||||
public JsonResult<?> wechatLogin(String code) {
|
||||
|
||||
return userService.wechatLogin(code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更多用户数据
|
||||
|
|
|
|||
|
|
@ -134,6 +134,9 @@ public class User implements Serializable{
|
|||
@Column(name="latitude")
|
||||
private Double latitude;
|
||||
|
||||
@Column(name = "wechat_unionid")
|
||||
private String wechatUnionid;
|
||||
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
|
|
@ -292,4 +295,11 @@ public class User implements Serializable{
|
|||
this.loginTime = loginTime;
|
||||
}
|
||||
|
||||
public String getWechatUnionid() {
|
||||
return wechatUnionid;
|
||||
}
|
||||
|
||||
public void setWechatUnionid(String wechatUnionid) {
|
||||
this.wechatUnionid = wechatUnionid;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,8 @@ public enum ResultEnum {
|
|||
warn223("223", "请上传正确的商品介绍视频"),
|
||||
error400("400", "安全校验失败"),
|
||||
error401("401", "参数校验失败"),
|
||||
error402("402", "参数不存在");
|
||||
error402("402", "参数不存在"),
|
||||
error403("403", "微信授权失败");
|
||||
|
||||
private ResultEnum(String key, String value) {
|
||||
this.key = key;
|
||||
|
|
|
|||
|
|
@ -223,7 +223,7 @@ public class GwellApi {
|
|||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Map<String, String> register = Login("18501773036");
|
||||
Map<String, String> register = Register("16621000775");
|
||||
System.out.println(register);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,6 +37,13 @@ public interface UserService {
|
|||
* @return
|
||||
*/
|
||||
public JsonResult<?> loginValidate(LoginParam loginParam);
|
||||
|
||||
/**
|
||||
* 微信登录
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
public JsonResult<?> wechatLogin(String code);
|
||||
|
||||
/**
|
||||
* 更多用户数据信息
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import java.util.Random;
|
|||
import com.alibaba.fastjson.JSON;
|
||||
import com.ifish.entity.event.QueueEventBody;
|
||||
import com.ifish.entity.event.QueueEventEntity;
|
||||
import com.ifish.util.WeChatUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -323,6 +324,79 @@ public class UserServiceImpl implements UserService {
|
|||
json.put("userAsset", userAssetDto);
|
||||
return new JsonResult<JSONObject>(ResultEnum.success.getKey(), json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonResult<?> wechatLogin(String code) {
|
||||
JSONObject wechatUserInfo = WeChatUtils.getWechatUserInfo(code);
|
||||
//微信授权失败
|
||||
if (wechatUserInfo == null || wechatUserInfo.size() == 0) {
|
||||
throw new IfishException(ResultEnum.error403);
|
||||
}
|
||||
//用户信息
|
||||
User user = userDao.findUniqueByProperty(Restrictions.eq("wechatUnionid",wechatUserInfo.getString("unionid")));
|
||||
if (user == null) {
|
||||
user = new User();
|
||||
user.setPhoneType("wechat");
|
||||
user.setLoginType("wechat");
|
||||
user.setIsRegisterGwell(BooleanEnum.NO.getKey());
|
||||
user.setIsRegisterNetease(BooleanEnum.NO.getKey());
|
||||
user.setNeteaseToken("");
|
||||
user.setUserType(UserTypeEnum.userType0.getKey());
|
||||
user.setAddress(wechatUserInfo.getString("province") + wechatUserInfo.getString("city"));
|
||||
//随机给一个默认头像
|
||||
String userImg = "default/"+(new Random().nextInt(7)+1)+".png";
|
||||
user.setUserImg(userImg);
|
||||
user.setNickName(wechatUserInfo.getString("nickname"));
|
||||
user.setCreateTime(new Date());
|
||||
userDao.save(user);
|
||||
//首次注册赠送10金币
|
||||
BigDecimal gold = new BigDecimal(10);
|
||||
UserAsset userAsset = new UserAsset(user.getUserId(),gold);
|
||||
userAssetDao.save(userAsset);
|
||||
//获取记录
|
||||
GoldGetRecord getRecord = new GoldGetRecord(user.getUserId(), GoldGetTypeEnum.registerGive.getKey(), gold, GoldGetTypeEnum.registerGive.getValue());
|
||||
goldGetRecordDao.save(getRecord);
|
||||
}
|
||||
//技威参数
|
||||
GwellParamDto gwellParam = getGwellInfo(user.getIsRegisterGwell(), user.getWechatUnionid());
|
||||
if(gwellParam!=null){
|
||||
user.setIsRegisterGwell(gwellParam.getIsRegisterGwell());
|
||||
user.setP2PVerifyCode1(gwellParam.getP2PVerifyCode1());
|
||||
user.setP2PVerifyCode2(gwellParam.getP2PVerifyCode2());
|
||||
user.setGwellUserID(gwellParam.getUserID());
|
||||
}
|
||||
Integer userId = user.getUserId();
|
||||
StringBuffer msg = new StringBuffer();
|
||||
msg.append("感谢您使用爱鱼奇,连接智能设备请查看下方说明书,内含操作视频:http://u.eqxiu.com/s/KmmVl87l\n");
|
||||
msg.append("摄像头售后:18667812003\n睿芯插排售后:15757401229\n绚多插排售后:18857689069\n松诺插排售后:13392205468");
|
||||
//如果当前设备用户未注册
|
||||
if (user.getIsRegisterNetease().equals(BooleanEnum.NO.getKey())) {
|
||||
PushList pushList = new PushList();
|
||||
pushList.setUserId(userId);
|
||||
pushList.setDeviceId(0);
|
||||
pushList.setPhoneType("ALL");
|
||||
pushList.setShowName("");
|
||||
pushList.setPushType(PushTypeEnum.qu_reply.getKey());
|
||||
pushList.setPushTitle("系统通知");
|
||||
pushList.setPushContext(msg.toString());
|
||||
sendPushQueueMessage(pushList);
|
||||
//更新用户注册信息
|
||||
user.setIsRegisterNetease(BooleanEnum.YES.getKey());
|
||||
userDao.update(user);
|
||||
}
|
||||
//更新登陆参数
|
||||
userDao.executeLoginUpdate(userId,user.getLoginType());
|
||||
//用户基本信息
|
||||
UserInfoDto userInfoDto =getUserInfoDto(user, gwellParam);
|
||||
//用户资产信息
|
||||
UserAssetDto userAssetDto = getUserAssetInfo(userId);
|
||||
//返回json信息
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("userInfo", userInfoDto);
|
||||
json.put("userAsset", userAssetDto);
|
||||
return new JsonResult<JSONObject>(ResultEnum.success.getKey(), json);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更多用户数据信息
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -0,0 +1,75 @@
|
|||
package com.ifish.util;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
/**
|
||||
* @author: yan.y
|
||||
* @Description:
|
||||
* @Date: Created in 11:19 2019-12-04
|
||||
* @Modified by:
|
||||
*/
|
||||
public class WeChatUtils {
|
||||
|
||||
/**
|
||||
* 微信应用appid
|
||||
*/
|
||||
private static final String WECHAT_APPID = "wxb3b27d653ec3e3cb";
|
||||
|
||||
/**
|
||||
* 微信应用 appsecret
|
||||
*/
|
||||
private static final String WECHAT_SECRET = "79d35215d7ef369b927f7cb21a2b8ff6";
|
||||
|
||||
/**
|
||||
* 微信获取token url
|
||||
*/
|
||||
private static final String WECHAT_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?";
|
||||
|
||||
/**
|
||||
* 微信获取用户信息url
|
||||
*/
|
||||
private static final String WECHAT_USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo?";
|
||||
|
||||
/**
|
||||
* 获取微信用户信息
|
||||
* @param code 授权码
|
||||
* @return
|
||||
*/
|
||||
public static final JSONObject getWechatUserInfo(String code){
|
||||
//授权url
|
||||
String accessTokenUrl = String.format("%sappid=%s&secret=%s&code=%s&grant_type=authorization_code", WECHAT_ACCESS_TOKEN_URL, WECHAT_APPID, WECHAT_SECRET, code);
|
||||
// 调用微信接口
|
||||
HttpClient httpClient = HttpClientBuilder.create().build();
|
||||
HttpGet accessTokenGet = new HttpGet(accessTokenUrl);
|
||||
JSONObject json = new JSONObject();
|
||||
try {
|
||||
HttpResponse accessTokenResponse = httpClient.execute(accessTokenGet);
|
||||
HttpEntity accessTokenEntity = accessTokenResponse.getEntity();
|
||||
String accessTokenResult = EntityUtils.toString(accessTokenEntity, "UTF-8");
|
||||
JSONObject wechatToken = JSON.parseObject(accessTokenResult);
|
||||
String openId = wechatToken.getString("openid");
|
||||
String accessToken = wechatToken.getString("access_token");
|
||||
|
||||
// 查询用户信息
|
||||
String userInfoUrl = String.format("%saccess_token=%s&openid=%s&lang-zh_CN", WECHAT_USERINFO_URL, accessToken, openId);
|
||||
HttpGet userInfoGet = new HttpGet(userInfoUrl);
|
||||
HttpResponse userInfoResponse = httpClient.execute(userInfoGet);
|
||||
HttpEntity userInfoEntity = userInfoResponse.getEntity();
|
||||
String userInfoResult = EntityUtils.toString(userInfoEntity, "UTF-8");
|
||||
json = JSON.parseObject(userInfoResult);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
return json;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -42,7 +42,12 @@ public class RegisterParam{
|
|||
* 维度
|
||||
*/
|
||||
private Double latitude;
|
||||
|
||||
|
||||
/**
|
||||
* 微信授权码
|
||||
*/
|
||||
private String code;
|
||||
|
||||
public RegisterParam() {}
|
||||
|
||||
public RegisterParam(String phoneNumber, String userPassword, String phoneType,
|
||||
|
|
@ -102,5 +107,12 @@ public class RegisterParam{
|
|||
public void setLatitude(Double latitude) {
|
||||
this.latitude = latitude;
|
||||
}
|
||||
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue