增加微信登录(数据库和mapper文件还未修改)
This commit is contained in:
parent
daef1c9abd
commit
1fcc7988f5
|
|
@ -0,0 +1,100 @@
|
||||||
|
package com.ebts.web.controller.wechat;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.ebts.common.annotation.Log;
|
||||||
|
import com.ebts.common.constant.Constants;
|
||||||
|
import com.ebts.common.core.entity.AjaxResult;
|
||||||
|
import com.ebts.common.core.entity.entity.User;
|
||||||
|
import com.ebts.common.core.entity.model.LoginBody;
|
||||||
|
import com.ebts.common.enums.BusinessType;
|
||||||
|
import com.ebts.common.utils.HttpClient;
|
||||||
|
import com.ebts.common.utils.StringUtils;
|
||||||
|
import com.ebts.framework.config.WeChatConfig;
|
||||||
|
import com.ebts.framework.web.service.WeChatService;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author 18209
|
||||||
|
* @Date 2021/6/5 8:44
|
||||||
|
* @Email clay@hchyun.com
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@Api(value = "微信登录接口", tags = "微信登录接口")
|
||||||
|
@RequestMapping("/elecadmin/applet/login")
|
||||||
|
public class WeChatController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WeChatService weChatService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信授权登录
|
||||||
|
*
|
||||||
|
* @param loginBody
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@ApiOperation("微信登录")
|
||||||
|
@Log(title = "微信登录", businessType = BusinessType.EXPORT)
|
||||||
|
@PostMapping("/authorization")
|
||||||
|
public AjaxResult LoginByWeChat(@RequestBody LoginBody loginBody) {
|
||||||
|
String loginUrl = getUrl(loginBody.getType(), loginBody.getCode());
|
||||||
|
String rul = HttpClient.doGet(loginUrl);
|
||||||
|
JSONObject wechat = JSONObject.parseObject(rul);
|
||||||
|
String openid = wechat.getString("openid");
|
||||||
|
String unionid = wechat.getString("unionid");
|
||||||
|
AjaxResult ajax = AjaxResult.success();
|
||||||
|
ajax.put("sessionKey", wechat.getString("session_key"));
|
||||||
|
if (StringUtils.isEmpty(openid) || StringUtils.isEmpty(unionid)) {
|
||||||
|
return AjaxResult.error("授权失败");
|
||||||
|
}
|
||||||
|
Map<String, Object> mode = weChatService.loginWeCat(new User(openid, unionid, loginBody.getType(), loginBody.getNickName(), loginBody.getAvatar()));
|
||||||
|
User user = (User) mode.get("user");
|
||||||
|
ajax.put(Constants.TOKEN, mode.get("token"));
|
||||||
|
ajax.put("activate", mode.get("activate"));
|
||||||
|
ajax.put("user", user);
|
||||||
|
return ajax;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl(String type, String code) {
|
||||||
|
String loginUrl = "";
|
||||||
|
switch (type) {
|
||||||
|
case "01":
|
||||||
|
loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + WeChatConfig.getUserAppid() + "&secret=" + WeChatConfig.getUserSecret() + "&js_code=" + code + "&grant_type=" + WeChatConfig.getGrant_type();
|
||||||
|
break;
|
||||||
|
case "02":
|
||||||
|
loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + WeChatConfig.getFunAppid() + "&secret=" + WeChatConfig.getFunSecret() + "&js_code=" + code + "&grant_type=" + WeChatConfig.getGrant_type();
|
||||||
|
break;
|
||||||
|
case "03":
|
||||||
|
loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + WeChatConfig.getEleAppid() + "&secret=" + WeChatConfig.getEleSecret() + "&js_code=" + code + "&grant_type=" + WeChatConfig.getGrant_type();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return loginUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation("账号密码登录")
|
||||||
|
@PostMapping
|
||||||
|
public AjaxResult login(@RequestBody LoginBody loginBody) {
|
||||||
|
AjaxResult ajax = AjaxResult.success();
|
||||||
|
Map<String, Object> mode = weChatService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getType());
|
||||||
|
ajax.put(Constants.TOKEN, mode.get("token"));
|
||||||
|
User user = (User) mode.get("user");
|
||||||
|
if (StringUtils.isEmpty(user.getPhonenumber())) {
|
||||||
|
ajax.put("activate", false);
|
||||||
|
} else {
|
||||||
|
ajax.put("activate", true);
|
||||||
|
}
|
||||||
|
ajax.put("user", user);
|
||||||
|
return ajax;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -38,7 +38,26 @@ ftp:
|
||||||
# 保护目录
|
# 保护目录
|
||||||
prifiles: prifiles
|
prifiles: prifiles
|
||||||
|
|
||||||
|
wechat:
|
||||||
|
# 微信用户小程序appid
|
||||||
|
userAppid: wx4c2099a19c69664f
|
||||||
|
# 微信职能小程序appid
|
||||||
|
funAppid: wx17aa476b91c2a391
|
||||||
|
# 微信技术端小程序appid
|
||||||
|
eleAppid: wx17aa476b91c2a391
|
||||||
|
# 微信用户小程序secret
|
||||||
|
userSecret: 3c4d9f5deb39bb10319e0ea0582d2bc0
|
||||||
|
# 微信职能小程序secret
|
||||||
|
funSecret: 456464645
|
||||||
|
# 微信技术小程序secret
|
||||||
|
eleSecret: 456464645
|
||||||
|
# 微信小程序 grant_type
|
||||||
|
grant_type: authorization_code
|
||||||
|
|
||||||
|
sms:
|
||||||
|
accessKeyId: LTAI5tKJQmGHKaUuYXco2Jiu
|
||||||
|
accessKeySecret: VZbBW1Je4zqm028PViqKIYwtDOQcxB
|
||||||
|
signName: 尊古
|
||||||
|
|
||||||
# 开发环境配置
|
# 开发环境配置
|
||||||
server:
|
server:
|
||||||
|
|
|
||||||
|
|
@ -119,6 +119,12 @@
|
||||||
<groupId>eu.bitwalker</groupId>
|
<groupId>eu.bitwalker</groupId>
|
||||||
<artifactId>UserAgentUtils</artifactId>
|
<artifactId>UserAgentUtils</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!--http网络请求-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
<version>4.5.5</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- servlet包 -->
|
<!-- servlet包 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.validation.constraints.Email;
|
import javax.validation.constraints.Email;
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
import javax.validation.constraints.Size;
|
import javax.validation.constraints.Size;
|
||||||
|
|
||||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||||
|
|
@ -39,6 +40,7 @@ public class User extends BaseEntity {
|
||||||
/**
|
/**
|
||||||
* 用户账号
|
* 用户账号
|
||||||
*/
|
*/
|
||||||
|
@NotNull
|
||||||
@Excel(name = "登录名称")
|
@Excel(name = "登录名称")
|
||||||
private String userName;
|
private String userName;
|
||||||
|
|
||||||
|
|
@ -51,6 +53,7 @@ public class User extends BaseEntity {
|
||||||
/**
|
/**
|
||||||
* 用户邮箱
|
* 用户邮箱
|
||||||
*/
|
*/
|
||||||
|
@Email
|
||||||
@Excel(name = "用户邮箱")
|
@Excel(name = "用户邮箱")
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
|
|
@ -91,6 +94,24 @@ public class User extends BaseEntity {
|
||||||
* 删除标志(0代表存在 2代表删除)
|
* 删除标志(0代表存在 2代表删除)
|
||||||
*/
|
*/
|
||||||
private String delFlag;
|
private String delFlag;
|
||||||
|
/**
|
||||||
|
* 用户类型
|
||||||
|
*/
|
||||||
|
private String userType;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 一个微信开放平台帐号下的应用,同一用户的 union
|
||||||
|
*/
|
||||||
|
private String unionId;
|
||||||
|
/**
|
||||||
|
* 用户唯一标识
|
||||||
|
*/
|
||||||
|
private String openId;
|
||||||
|
/**
|
||||||
|
* 城市
|
||||||
|
*/
|
||||||
|
private String city;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 最后登录IP
|
* 最后登录IP
|
||||||
|
|
@ -132,10 +153,62 @@ public class User extends BaseEntity {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public User(Long userId, String phonenumber) {
|
||||||
|
this.userId = userId;
|
||||||
|
this.phonenumber = phonenumber;
|
||||||
|
}
|
||||||
|
|
||||||
public User(Long userId) {
|
public User(Long userId) {
|
||||||
this.userId = userId;
|
this.userId = userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public User(String openid, String unionId, String type, String nickName, String avatar) {
|
||||||
|
this.openId = openid;
|
||||||
|
this.unionId = unionId;
|
||||||
|
this.deptId = 100L;
|
||||||
|
this.nickName = nickName;
|
||||||
|
this.userName = "";
|
||||||
|
this.avatar = avatar;
|
||||||
|
this.password = "$10$NVCwybYSzhzDROCUEHI/E.jV04YhhZ7dBEQUBCFbA3weMY3xzHmce";
|
||||||
|
this.delFlag = "0";
|
||||||
|
this.userType = type;
|
||||||
|
this.roleIds = new Long[1];
|
||||||
|
this.roleIds[0] = 103L;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUnionId() {
|
||||||
|
return unionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUnionId(String unionId) {
|
||||||
|
this.unionId = unionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOpenId() {
|
||||||
|
return openId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOpenId(String openId) {
|
||||||
|
this.openId = openId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCity() {
|
||||||
|
return city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCity(String city) {
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserType() {
|
||||||
|
return userType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserType(String userType) {
|
||||||
|
this.userType = userType;
|
||||||
|
}
|
||||||
|
|
||||||
public Long getUserId() {
|
public Long getUserId() {
|
||||||
return userId;
|
return userId;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,12 +20,42 @@ public class LoginBody {
|
||||||
* 验证码
|
* 验证码
|
||||||
*/
|
*/
|
||||||
private String code;
|
private String code;
|
||||||
|
/**
|
||||||
|
* 微信昵称
|
||||||
|
*/
|
||||||
|
private String nickName;
|
||||||
|
/**
|
||||||
|
* 用户头像
|
||||||
|
*/
|
||||||
|
private String avatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 唯一标识
|
* 唯一标识
|
||||||
*/
|
*/
|
||||||
private String uuid = "";
|
private String uuid = "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户登录位置
|
||||||
|
*/
|
||||||
|
private String type = "01";
|
||||||
|
|
||||||
|
|
||||||
|
public String getNickName() {
|
||||||
|
return nickName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNickName(String nickName) {
|
||||||
|
this.nickName = nickName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAvatar() {
|
||||||
|
return avatar;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAvatar(String avatar) {
|
||||||
|
this.avatar = avatar;
|
||||||
|
}
|
||||||
|
|
||||||
public String getUsername() {
|
public String getUsername() {
|
||||||
return username;
|
return username;
|
||||||
}
|
}
|
||||||
|
|
@ -57,4 +87,12 @@ public class LoginBody {
|
||||||
public void setUuid(String uuid) {
|
public void setUuid(String uuid) {
|
||||||
this.uuid = uuid;
|
this.uuid = uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,127 @@
|
||||||
|
package com.ebts.common.utils;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import org.apache.http.HttpEntity;
|
||||||
|
import org.apache.http.ParseException;
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author 18209
|
||||||
|
* @Date 2021/6/1 22:43
|
||||||
|
* @Email clay@hchyun.com
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
public class HttpClient {
|
||||||
|
|
||||||
|
|
||||||
|
public static String doPost(String url, JSONObject js) {
|
||||||
|
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
||||||
|
HttpPost httpPost = new HttpPost(url);
|
||||||
|
String jsonString = js + "".toString();
|
||||||
|
StringEntity entity = new StringEntity(jsonString, "UTF-8");
|
||||||
|
httpPost.setEntity(entity);
|
||||||
|
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
|
||||||
|
CloseableHttpResponse response = null;
|
||||||
|
|
||||||
|
String var8;
|
||||||
|
try {
|
||||||
|
response = httpClient.execute(httpPost);
|
||||||
|
HttpEntity responseEntity = response.getEntity();
|
||||||
|
System.out.println("响应状态为:" + response.getStatusLine());
|
||||||
|
if (responseEntity == null) {
|
||||||
|
return "erro";
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
|
||||||
|
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
|
||||||
|
var8 = EntityUtils.toString(responseEntity);
|
||||||
|
} catch (IOException | ParseException var19) {
|
||||||
|
var19.printStackTrace();
|
||||||
|
var8 = "erro";
|
||||||
|
return var8;
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (httpClient != null) {
|
||||||
|
httpClient.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response != null) {
|
||||||
|
response.close();
|
||||||
|
}
|
||||||
|
} catch (IOException var18) {
|
||||||
|
var18.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return var8;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String doGet(String url) {
|
||||||
|
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
||||||
|
StringBuilder params = new StringBuilder();
|
||||||
|
|
||||||
|
try {
|
||||||
|
params.append("name=").append(URLEncoder.encode("&", "utf-8")).append("&").append("age=24");
|
||||||
|
} catch (UnsupportedEncodingException var20) {
|
||||||
|
var20.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpGet httpGet = new HttpGet(url);
|
||||||
|
CloseableHttpResponse response = null;
|
||||||
|
|
||||||
|
String var8;
|
||||||
|
try {
|
||||||
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(5000).setRedirectsEnabled(true).build();
|
||||||
|
httpGet.setConfig(requestConfig);
|
||||||
|
response = httpClient.execute(httpGet);
|
||||||
|
HttpEntity responseEntity = response.getEntity();
|
||||||
|
System.out.println("响应状态为:" + response.getStatusLine());
|
||||||
|
if (responseEntity == null) {
|
||||||
|
return "erro";
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
|
||||||
|
String rul = EntityUtils.toString(responseEntity);
|
||||||
|
if (httpClient != null) {
|
||||||
|
httpClient.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response != null) {
|
||||||
|
response.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
var8 = rul;
|
||||||
|
} catch (IOException | ParseException var21) {
|
||||||
|
var21.printStackTrace();
|
||||||
|
return "erro";
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (httpClient != null) {
|
||||||
|
httpClient.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response != null) {
|
||||||
|
response.close();
|
||||||
|
}
|
||||||
|
} catch (IOException var19) {
|
||||||
|
var19.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return var8;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,81 @@
|
||||||
|
package com.ebts.framework.config;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author 18209
|
||||||
|
* @Date 2021/6/2 16:41
|
||||||
|
* @Email clay@hchyun.com
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "wechat")
|
||||||
|
public class WeChatConfig {
|
||||||
|
|
||||||
|
private static String userAppid;
|
||||||
|
private static String funAppid;
|
||||||
|
private static String eleAppid;
|
||||||
|
|
||||||
|
private static String userSecret;
|
||||||
|
private static String funSecret;
|
||||||
|
private static String eleSecret;
|
||||||
|
|
||||||
|
private static String grant_type;
|
||||||
|
|
||||||
|
public static String getUserAppid() {
|
||||||
|
return userAppid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserAppid(String userAppid) {
|
||||||
|
WeChatConfig.userAppid = userAppid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getFunAppid() {
|
||||||
|
return funAppid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFunAppid(String funAppid) {
|
||||||
|
WeChatConfig.funAppid = funAppid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getEleAppid() {
|
||||||
|
return eleAppid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEleAppid(String eleAppid) {
|
||||||
|
WeChatConfig.eleAppid = eleAppid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getUserSecret() {
|
||||||
|
return userSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserSecret(String userSecret) {
|
||||||
|
WeChatConfig.userSecret = userSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getFunSecret() {
|
||||||
|
return funSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFunSecret(String funSecret) {
|
||||||
|
WeChatConfig.funSecret = funSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getEleSecret() {
|
||||||
|
return eleSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEleSecret(String eleSecret) {
|
||||||
|
WeChatConfig.eleSecret = eleSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getGrant_type() {
|
||||||
|
return grant_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGrant_type(String grant_type) {
|
||||||
|
WeChatConfig.grant_type = grant_type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,111 @@
|
||||||
|
package com.ebts.framework.web.service;
|
||||||
|
|
||||||
|
import com.ebts.common.constant.Constants;
|
||||||
|
import com.ebts.common.core.entity.entity.User;
|
||||||
|
import com.ebts.common.core.entity.model.LoginUser;
|
||||||
|
import com.ebts.common.exception.BaseException;
|
||||||
|
import com.ebts.common.exception.CustomException;
|
||||||
|
import com.ebts.common.exception.user.UserPasswordNotMatchException;
|
||||||
|
import com.ebts.common.utils.MessageUtils;
|
||||||
|
import com.ebts.common.utils.StringUtils;
|
||||||
|
import com.ebts.framework.dao.AsyncDao;
|
||||||
|
import com.ebts.framework.dao.factory.AsyncFactory;
|
||||||
|
import com.ebts.system.dao.UserDao;
|
||||||
|
import com.ebts.system.service.UserService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author 18209
|
||||||
|
* @Date 2021/6/5 8:37
|
||||||
|
* @Email clay@hchyun.com
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class WeChatService {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(WeChatService.class);
|
||||||
|
@Autowired
|
||||||
|
private TokenService tokenService;
|
||||||
|
@Autowired
|
||||||
|
private UserDao userDao;
|
||||||
|
@Autowired
|
||||||
|
private SysPermissionService permissionService;
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private AuthenticationManager authenticationManager;
|
||||||
|
|
||||||
|
|
||||||
|
public Map<String, Object> loginWeCat(User user) {
|
||||||
|
User userDB = userDao.selectUserByOpenId(user.getOpenId(), user.getUnionId(), user.getUserType());
|
||||||
|
Map<String, Object> mode = new HashMap<>();
|
||||||
|
mode.put("activate", true);
|
||||||
|
if (null == userDB) {
|
||||||
|
int rows = userService.insertUser(user);
|
||||||
|
if (rows <= 0) {
|
||||||
|
log.info("微信授权失败");
|
||||||
|
throw new BaseException("微信授权失败!");
|
||||||
|
}
|
||||||
|
userDB = userDao.selectUserByOpenId(user.getOpenId(), user.getUnionId(), user.getUserType());
|
||||||
|
mode.put("activate", false);
|
||||||
|
} else if (StringUtils.isEmpty(userDB.getPhonenumber())) {
|
||||||
|
mode.put("activate", false);
|
||||||
|
}
|
||||||
|
userDB.setPassword("");
|
||||||
|
LoginUser loginUser = new LoginUser(userDB, permissionService.getMenuPermission(userDB));
|
||||||
|
mode.put("token", tokenService.createToken(loginUser));
|
||||||
|
mode.put("user", userDB);
|
||||||
|
return mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录验证
|
||||||
|
*
|
||||||
|
* @param username 用户名
|
||||||
|
* @param password 密码
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
|
public Map<String, Object> login(String username, String password, String type) {
|
||||||
|
// 用户验证
|
||||||
|
Authentication authentication = null;
|
||||||
|
try {
|
||||||
|
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
||||||
|
authentication = authenticationManager
|
||||||
|
.authenticate(new UsernamePasswordAuthenticationToken(username, password));
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (e instanceof BadCredentialsException) {
|
||||||
|
AsyncDao.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
||||||
|
throw new UserPasswordNotMatchException();
|
||||||
|
} else {
|
||||||
|
AsyncDao.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
||||||
|
throw new CustomException(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AsyncDao.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
||||||
|
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
||||||
|
User user = loginUser.getUser();
|
||||||
|
Map<String, Object> mode = new HashMap<>();
|
||||||
|
if (!user.getUserType().equals(type)) {
|
||||||
|
log.info("登录入口有误:{} 已被删除.", username);
|
||||||
|
throw new BaseException("对不起,用户:" + username + " 您登录的入口有误");
|
||||||
|
}
|
||||||
|
user.setPassword("");
|
||||||
|
mode.put("user", user);
|
||||||
|
// 生成token
|
||||||
|
mode.put("token", tokenService.createToken(loginUser));
|
||||||
|
return mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -17,7 +17,7 @@ public interface UserDao {
|
||||||
* @param user 用户信息
|
* @param user 用户信息
|
||||||
* @return 用户信息集合信息
|
* @return 用户信息集合信息
|
||||||
*/
|
*/
|
||||||
public List<User> selectUserList(User user);
|
List<User> selectUserList(User user);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过用户名查询用户
|
* 通过用户名查询用户
|
||||||
|
|
@ -25,7 +25,15 @@ public interface UserDao {
|
||||||
* @param userName 用户名
|
* @param userName 用户名
|
||||||
* @return 用户对象信息
|
* @return 用户对象信息
|
||||||
*/
|
*/
|
||||||
public User selectUserByUserName(String userName);
|
User selectUserByUserName(String userName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过openid查询用户
|
||||||
|
*
|
||||||
|
* @param openid 用户名
|
||||||
|
* @return 用户对象信息
|
||||||
|
*/
|
||||||
|
User selectUserByOpenId(@Param("openid") String openid, @Param("unionId") String unionId, @Param("type") String type);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过用户ID查询用户
|
* 通过用户ID查询用户
|
||||||
|
|
@ -33,7 +41,7 @@ public interface UserDao {
|
||||||
* @param userId 用户ID
|
* @param userId 用户ID
|
||||||
* @return 用户对象信息
|
* @return 用户对象信息
|
||||||
*/
|
*/
|
||||||
public User selectUserById(Long userId);
|
User selectUserById(Long userId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增用户信息
|
* 新增用户信息
|
||||||
|
|
@ -41,7 +49,7 @@ public interface UserDao {
|
||||||
* @param user 用户信息
|
* @param user 用户信息
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public int insertUser(User user);
|
int insertUser(User user);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改用户信息
|
* 修改用户信息
|
||||||
|
|
@ -49,7 +57,7 @@ public interface UserDao {
|
||||||
* @param user 用户信息
|
* @param user 用户信息
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public int updateUser(User user);
|
int updateUser(User user);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改用户头像
|
* 修改用户头像
|
||||||
|
|
@ -58,7 +66,7 @@ public interface UserDao {
|
||||||
* @param avatar 头像地址
|
* @param avatar 头像地址
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public int updateUserAvatar(@Param("userName") String userName, @Param("avatar") String avatar);
|
int updateUserAvatar(@Param("userName") String userName, @Param("avatar") String avatar);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 重置用户密码
|
* 重置用户密码
|
||||||
|
|
@ -67,7 +75,7 @@ public interface UserDao {
|
||||||
* @param password 密码
|
* @param password 密码
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public int resetUserPwd(@Param("userName") String userName, @Param("password") String password);
|
int resetUserPwd(@Param("userName") String userName, @Param("password") String password);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过用户ID删除用户
|
* 通过用户ID删除用户
|
||||||
|
|
@ -75,7 +83,7 @@ public interface UserDao {
|
||||||
* @param userId 用户ID
|
* @param userId 用户ID
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public int deleteUserById(Long userId);
|
int deleteUserById(Long userId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量删除用户信息
|
* 批量删除用户信息
|
||||||
|
|
@ -83,7 +91,7 @@ public interface UserDao {
|
||||||
* @param userIds 需要删除的用户ID
|
* @param userIds 需要删除的用户ID
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public int deleteUserByIds(Long[] userIds);
|
int deleteUserByIds(Long[] userIds);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验用户名称是否唯一
|
* 校验用户名称是否唯一
|
||||||
|
|
@ -91,7 +99,7 @@ public interface UserDao {
|
||||||
* @param userName 用户名称
|
* @param userName 用户名称
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public int checkUserNameUnique(String userName);
|
int checkUserNameUnique(String userName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验手机号码是否唯一
|
* 校验手机号码是否唯一
|
||||||
|
|
@ -99,7 +107,7 @@ public interface UserDao {
|
||||||
* @param phonenumber 手机号码
|
* @param phonenumber 手机号码
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public User checkPhoneUnique(String phonenumber);
|
User checkPhoneUnique(String phonenumber);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验email是否唯一
|
* 校验email是否唯一
|
||||||
|
|
@ -107,5 +115,5 @@ public interface UserDao {
|
||||||
* @param email 用户邮箱
|
* @param email 用户邮箱
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public User checkEmailUnique(String email);
|
User checkEmailUnique(String email);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
<result property="updateBy" column="update_by" />
|
<result property="updateBy" column="update_by" />
|
||||||
<result property="updateTime" column="update_time" />
|
<result property="updateTime" column="update_time" />
|
||||||
<result property="remark" column="remark" />
|
<result property="remark" column="remark" />
|
||||||
|
<result property="userType" column="user_type" />
|
||||||
|
<result property="openId" column="open_id" />
|
||||||
|
<result property="unionId" column="union_id" />
|
||||||
|
<result property="city" column="city" />
|
||||||
<association property="dept" column="dept_id" javaType="Dept" resultMap="deptResult" />
|
<association property="dept" column="dept_id" javaType="Dept" resultMap="deptResult" />
|
||||||
<collection property="roles" javaType="java.util.List" resultMap="RoleResult" />
|
<collection property="roles" javaType="java.util.List" resultMap="RoleResult" />
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
@ -46,7 +50,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
<sql id="selectUserVo">
|
<sql id="selectUserVo">
|
||||||
select u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,
|
select u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, u.union_id,u.open_id,u.city,u.user_type,
|
||||||
d.dept_id, d.parent_id, d.dept_name, d.order_num, d.leader, d.status as dept_status,
|
d.dept_id, d.parent_id, d.dept_name, d.order_num, d.leader, d.status as dept_status,
|
||||||
r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status as role_status
|
r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status as role_status
|
||||||
from sys_user u
|
from sys_user u
|
||||||
|
|
@ -59,6 +63,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
select u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, d.dept_name, d.leader from sys_user u
|
select u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, d.dept_name, d.leader from sys_user u
|
||||||
left join sys_dept d on u.dept_id = d.dept_id
|
left join sys_dept d on u.dept_id = d.dept_id
|
||||||
where u.del_flag = '0'
|
where u.del_flag = '0'
|
||||||
|
<if test="userType != '99'">
|
||||||
|
AND u.user_type != '99'
|
||||||
|
</if>
|
||||||
<if test="userName != null and userName != ''">
|
<if test="userName != null and userName != ''">
|
||||||
AND u.user_name like concat('%', #{userName}, '%')
|
AND u.user_name like concat('%', #{userName}, '%')
|
||||||
</if>
|
</if>
|
||||||
|
|
@ -90,6 +97,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
<include refid="selectUserVo"/>
|
<include refid="selectUserVo"/>
|
||||||
where u.user_id = #{userId}
|
where u.user_id = #{userId}
|
||||||
</select>
|
</select>
|
||||||
|
<select id="selectUserByOpenId" parameterType="String" resultMap="UserResult">
|
||||||
|
<include refid="selectUserVo"/>
|
||||||
|
where u.open_id = #{openid} and u.user_type = #{type} and u.union_id = #{unionId}
|
||||||
|
</select>
|
||||||
|
|
||||||
<select id="checkUserNameUnique" parameterType="String" resultType="int">
|
<select id="checkUserNameUnique" parameterType="String" resultType="int">
|
||||||
select count(1) from sys_user where user_name = #{userName} limit 1
|
select count(1) from sys_user where user_name = #{userName} limit 1
|
||||||
|
|
@ -107,6 +118,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
insert into sys_user(
|
insert into sys_user(
|
||||||
<if test="userId != null and userId != 0">user_id,</if>
|
<if test="userId != null and userId != 0">user_id,</if>
|
||||||
<if test="deptId != null and deptId != 0">dept_id,</if>
|
<if test="deptId != null and deptId != 0">dept_id,</if>
|
||||||
|
<if test="openId != null and openId != ''">open_id,</if>
|
||||||
|
<if test="unionId != null and unionId != ''">union_id,</if>
|
||||||
|
<if test="userType != null and userType != 0">user_type,</if>
|
||||||
<if test="userName != null and userName != ''">user_name,</if>
|
<if test="userName != null and userName != ''">user_name,</if>
|
||||||
<if test="nickName != null and nickName != ''">nick_name,</if>
|
<if test="nickName != null and nickName != ''">nick_name,</if>
|
||||||
<if test="email != null and email != ''">email,</if>
|
<if test="email != null and email != ''">email,</if>
|
||||||
|
|
@ -119,8 +133,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
<if test="remark != null and remark != ''">remark,</if>
|
<if test="remark != null and remark != ''">remark,</if>
|
||||||
create_time
|
create_time
|
||||||
)values(
|
)values(
|
||||||
<if test="userId != null and userId != ''">#{userId},</if>
|
<if test="userId != null and userId != 0">#{userId},</if>
|
||||||
<if test="deptId != null and deptId != ''">#{deptId},</if>
|
<if test="deptId != null and deptId != 0">#{deptId},</if>
|
||||||
|
<if test="openId != null and openId != ''">#{openId},</if>
|
||||||
|
<if test="unionId != null and unionId != ''">#{unionId},</if>
|
||||||
|
<if test="userType != null and userType != 0">#{userType},</if>
|
||||||
<if test="userName != null and userName != ''">#{userName},</if>
|
<if test="userName != null and userName != ''">#{userName},</if>
|
||||||
<if test="nickName != null and nickName != ''">#{nickName},</if>
|
<if test="nickName != null and nickName != ''">#{nickName},</if>
|
||||||
<if test="email != null and email != ''">#{email},</if>
|
<if test="email != null and email != ''">#{email},</if>
|
||||||
|
|
@ -168,9 +185,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
update sys_user set password = #{password} where user_name = #{userName}
|
update sys_user set password = #{password} where user_name = #{userName}
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
<delete id="deleteUserById" parameterType="Long">
|
<update id="deleteUserById" parameterType="Long">
|
||||||
delete from sys_user where user_id = #{userId}
|
update sys_user set del_flag = '2' where user_id = #{userId}
|
||||||
</delete>
|
</update>
|
||||||
|
|
||||||
<delete id="deleteUserByIds" parameterType="Long">
|
<delete id="deleteUserByIds" parameterType="Long">
|
||||||
update sys_user set del_flag = '2' where user_id in
|
update sys_user set del_flag = '2' where user_id in
|
||||||
|
|
@ -178,5 +195,15 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
#{userId}
|
#{userId}
|
||||||
</foreach>
|
</foreach>
|
||||||
</delete>
|
</delete>
|
||||||
|
<!-- <delete id="deleteUserById" parameterType="Long">-->
|
||||||
|
<!-- delete from sys_user where user_id = #{userId}-->
|
||||||
|
<!-- </delete>-->
|
||||||
|
<!-- -->
|
||||||
|
<!-- <delete id="deleteUserByIds" parameterType="Long">-->
|
||||||
|
<!-- update sys_user set del_flag = '2' where user_id in-->
|
||||||
|
<!-- <foreach collection="array" item="userId" open="(" separator="," close=")">-->
|
||||||
|
<!-- #{userId}-->
|
||||||
|
<!-- </foreach> -->
|
||||||
|
<!-- </delete>-->
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
Loading…
Reference in New Issue