1小程序登录完

This commit is contained in:
ly19960718 2021-02-08 14:44:23 +08:00
parent 1ce0cca488
commit b9be77fd2c
11 changed files with 153 additions and 39 deletions

View File

@ -4,12 +4,14 @@ import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2 @EnableSwagger2
@SpringBootApplication @SpringBootApplication
@ComponentScan("cn.com") @ComponentScan("cn.com")
@MapperScan({"cn.com.**.dao"}) @MapperScan({"cn.com.**.dao"})
@EnableScheduling
public class BusinessCardApplication { public class BusinessCardApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -37,6 +37,10 @@ public class AccessTokenManager {
cardUserVOMap.remove(key); cardUserVOMap.remove(key);
} }
public Map<String, CardUserDTO> getKeyList(){
return cardUserVOMap;
}
public void updateLastActivityTime(String key){ public void updateLastActivityTime(String key){
CardUserDTO dto = this.getKey(key); CardUserDTO dto = this.getKey(key);

View File

@ -7,6 +7,8 @@ import cn.com.tenlion.service.carduser.ICardUserService;
import cn.com.tenlion.util.AesUtil; import cn.com.tenlion.util.AesUtil;
import cn.com.tenlion.util.annotation.AccessRightsCheck; import cn.com.tenlion.util.annotation.AccessRightsCheck;
import cn.com.tenlion.util.enumeration.Auth; import cn.com.tenlion.util.enumeration.Auth;
import cn.com.tenlion.util.enumeration.ErrorResultCodeEnum;
import cn.com.tenlion.util.result.ErrorResult;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.util.StringUtil; import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -20,6 +22,8 @@ import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import static cn.com.tenlion.util.enumeration.ErrorResultCodeEnum.TOKEN_ERROR;
/** /**
* TODO * TODO
* @version 1.0 * @version 1.0
@ -74,12 +78,12 @@ public class CheckAuthInterceptor implements HandlerInterceptor {
response.setStatus(response.SC_UNAUTHORIZED); response.setStatus(response.SC_UNAUTHORIZED);
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8"); response.setContentType("application/json; charset=utf-8");
JSONObject res = new JSONObject(); ErrorResult error= new ErrorResult();
res.put("status","401"); error.setCode(TOKEN_ERROR.getValue());
res.put("msg","token不合法"); error.setMsg("token不合法");
PrintWriter out = null ; PrintWriter out = null ;
out = response.getWriter(); out = response.getWriter();
out.write(res.toString()); out.write(error.toString());
out.flush(); out.flush();
out.close(); out.close();
return false; return false;

View File

@ -32,6 +32,19 @@ import java.util.Map;
public class CardUserController { public class CardUserController {
@Autowired @Autowired
private ICardUserService cardUserService; private ICardUserService cardUserService;
@ApiOperation(value = "用户登录", notes = "用户登录接口")
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@PostMapping("logincarduser")
@CheckRequestBodyAnnotation
public SuccessResult loginCardUser(@RequestBody CardUserVO cardUserVO) throws Exception {
return cardUserService.saveCardUser(cardUserVO);
}
@ApiOperation(value = "新增用户表", notes = "新增用户表接口") @ApiOperation(value = "新增用户表", notes = "新增用户表接口")
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@PostMapping("savecarduser") @PostMapping("savecarduser")

View File

@ -6,8 +6,10 @@ import cn.com.tenlion.pojo.dtos.weixinresult.WeiXinAuthResult;
import cn.com.tenlion.pojo.vos.carduser.CardUserVO; import cn.com.tenlion.pojo.vos.carduser.CardUserVO;
import cn.com.tenlion.service.carduser.ICardUserService; import cn.com.tenlion.service.carduser.ICardUserService;
import cn.com.tenlion.util.AesUtil; import cn.com.tenlion.util.AesUtil;
import cn.com.tenlion.util.annotation.AccessRightsCheck;
import cn.com.tenlion.util.check.CheckRequestBodyAnnotation; import cn.com.tenlion.util.check.CheckRequestBodyAnnotation;
import cn.com.tenlion.util.cons.ISystemConstant; import cn.com.tenlion.util.cons.ISystemConstant;
import cn.com.tenlion.util.enumeration.Auth;
import cn.com.tenlion.util.exception.ParamsException; import cn.com.tenlion.util.exception.ParamsException;
import cn.com.tenlion.util.exception.RemoveException; import cn.com.tenlion.util.exception.RemoveException;
import cn.com.tenlion.util.exception.SearchException; import cn.com.tenlion.util.exception.SearchException;
@ -16,6 +18,7 @@ import cn.com.tenlion.util.result.SuccessResult;
import cn.com.tenlion.util.result.SuccessResultData; import cn.com.tenlion.util.result.SuccessResultData;
import cn.com.tenlion.util.vx.WxApiUtil; import cn.com.tenlion.util.vx.WxApiUtil;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import cn.com.tenlion.controller.AbstractController; import cn.com.tenlion.controller.AbstractController;
@ -50,42 +53,64 @@ public class CardUserAppController extends AbstractController {
* @return * @return
* @throws Exception * @throws Exception
*/ */
@GetMapping("weiXinLoginBySmallRoutine"+ISystemConstant.RELEASE_SUFFIX+"/{vxCode}/{vxPhone}") @GetMapping("weiXinLoginBySmallRoutine/{vxCode}")
public SuccessResult weiXinLoginBySmallRoutine( @AccessRightsCheck(auth= Auth.NOCHECK,authUrl="",operation="小程序登录")
@PathVariable("vxCode") String vxCode, @PathVariable("vxPhone") String vxPhone public SuccessResult weiXinLoginBySmallRoutine(@PathVariable("vxCode") String vxCode) throws Exception {
) throws Exception {
WxApiUtil wxapi = new WxApiUtil(); WxApiUtil wxapi = new WxApiUtil();
WeiXinAuthResult austerest = wxapi.weiXinLoginGetAuth(vxCode, ISystemConstant.VX_APP_ID, ISystemConstant.VX_APP_SECRET, ISystemConstant.VX_GRANT_TYPE); WeiXinAuthResult austerest = wxapi.weiXinLoginGetAuth(vxCode, ISystemConstant.VX_APP_ID, ISystemConstant.VX_APP_SECRET, ISystemConstant.VX_GRANT_TYPE);
if (austerest == null) { if (austerest == null) {
throw new ParamsException("系统异常-1"); throw new ParamsException("系统异常-1");
} }
if (!austerest.getErrcode().equals("0")) { if (austerest.getOpenid() == null || StringUtils.isEmpty(austerest.getOpenid())) {
throw new ParamsException("系统异常-2"); throw new ParamsException("系统异常-2");
} }
CardUserDTO carduserdto = cardUserService.getCardUserByVxOpenId(austerest.getOpenid()); CardUserDTO carduserdto = cardUserService.getCardUserByVxOpenId(austerest.getOpenid());
String userId; String userId;
CardUserDTO userdto = null; CardUserDTO userdto = null;
String checkPhone = "_0"; //未绑定手机号
if (carduserdto != null) { if (carduserdto != null) {
userId = carduserdto.getCardUserId(); userId = carduserdto.getCardUserId();
userdto = carduserdto; userdto = carduserdto;
checkPhone = "_1";
} else { } else {
CardUserVO carduservo = new CardUserVO(); CardUserVO carduservo = new CardUserVO();
carduservo.setUserName("user1");
carduservo.setUserPassword("123456");
carduservo.setVxOpenId(austerest.getOpenid()); carduservo.setVxOpenId(austerest.getOpenid());
carduservo.setVxUnionId(austerest.getUnionid()); carduservo.setVxUnionId(austerest.getUnionid());
carduservo.setUserPhone(vxPhone);
carduservo.setUserName(vxPhone);
userId = cardUserService.saveCardUserReturnId(carduservo); userId = cardUserService.saveCardUserReturnId(carduservo);
userdto = cardUserService.getCardUserByVxOpenId(userId); userdto = cardUserService.getCardUserById(userId);
} }
String token = AesUtil.aesCommonEncoder(ISystemConstant.APP_TOKEN_AES_KEY, userId); String token = AesUtil.aesCommonEncoder(ISystemConstant.APP_TOKEN_AES_KEY, userId);
token = token+checkPhone;
AccessTokenManager.getInstance().setKey(token, userdto); AccessTokenManager.getInstance().setKey(token, userdto);
AccessTokenManager.getInstance().updateLastActivityTime(token); AccessTokenManager.getInstance().updateLastActivityTime(token);
return new SuccessResultData<>(token); return new SuccessResultData<>(token);
} }
/**
* 小程序登录绑定手机号
* @return
* @throws Exception
*/
@GetMapping("weiXinLoginBindPhone/{phone}")
@AccessRightsCheck(auth= Auth.NOCHECK,authUrl="",operation="小程序登录")
public SuccessResult weiXinLoginBySmallRoutine(@PathVariable("phone") String phone,@RequestHeader("token") String token) throws Exception {
CardUserDTO user = AccessTokenManager.getInstance().getKey(token);
user.setUserPhone(phone);
user.setUserName(phone);
CardUserVO vo = new CardUserVO();
vo.setUserName(phone);
vo.setUserPhone(phone);
cardUserService.updateCardUser(user.getCardUserId(),vo);
String token2 = AesUtil.aesCommonEncoder(ISystemConstant.APP_TOKEN_AES_KEY, user.getCardUserId());
token2 = token2+"_1";
AccessTokenManager.getInstance().setKey(token2, user);
AccessTokenManager.getInstance().updateLastActivityTime(token2);
AccessTokenManager.getInstance().removeKey(token);
return new SuccessResultData<>(token2);
}

View File

@ -19,6 +19,11 @@ public class CardUserDTO {
* 用户姓名 * 用户姓名
*/ */
private String userName; private String userName;
/**
* 用户密码
*/
private String userPassword;
/** /**
* 用户手机 * 用户手机
*/ */
@ -26,11 +31,11 @@ public class CardUserDTO {
/** /**
* 用户开放平台唯一标识 * 用户开放平台唯一标识
*/ */
private String VxUnionId; private String vxUnionId;
/** /**
* 微信用户唯一标识 * 微信用户唯一标识
*/ */
private String VxOpenId; private String vxOpenId;
/** /**
* 账号最后活动时间用于token过期判断 * 账号最后活动时间用于token过期判断
@ -53,6 +58,14 @@ public class CardUserDTO {
this.userName = userName; this.userName = userName;
} }
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserPhone() { public String getUserPhone() {
return userPhone == null ? "" : userPhone.trim(); return userPhone == null ? "" : userPhone.trim();
} }
@ -62,19 +75,19 @@ public class CardUserDTO {
} }
public String getVxUnionId() { public String getVxUnionId() {
return VxUnionId == null ? "" : VxUnionId.trim(); return vxUnionId == null ? "" : vxUnionId.trim();
} }
public void setVxUnionId(String VxUnionId) { public void setVxUnionId(String vxUnionId) {
this.VxUnionId = VxUnionId; this.vxUnionId = vxUnionId;
} }
public String getVxOpenId() { public String getVxOpenId() {
return VxOpenId == null ? "" : VxOpenId.trim(); return vxOpenId == null ? "" : vxOpenId.trim();
} }
public void setVxOpenId(String VxOpenId) { public void setVxOpenId(String vxOpenId) {
this.VxOpenId = VxOpenId; this.vxOpenId = vxOpenId;
} }
public long getLastActivityTime() { public long getLastActivityTime() {

View File

@ -14,6 +14,11 @@ public class CardUserVO {
* 用户姓名 * 用户姓名
*/ */
private String userName; private String userName;
/**
* 用户密码
*/
private String userPassword;
/** /**
* 用户手机 * 用户手机
*/ */
@ -21,11 +26,11 @@ public class CardUserVO {
/** /**
* 用户开放平台唯一标识 * 用户开放平台唯一标识
*/ */
private String VxUnionId; private String vxUnionId;
/** /**
* 微信用户唯一标识 * 微信用户唯一标识
*/ */
private String VxOpenId; private String vxOpenId;
public String getUserName() { public String getUserName() {
@ -36,6 +41,14 @@ public class CardUserVO {
this.userName = userName; this.userName = userName;
} }
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserPhone() { public String getUserPhone() {
return userPhone == null ? "" : userPhone.trim(); return userPhone == null ? "" : userPhone.trim();
} }
@ -45,19 +58,19 @@ public class CardUserVO {
} }
public String getVxUnionId() { public String getVxUnionId() {
return VxUnionId == null ? "" : VxUnionId.trim(); return vxUnionId == null ? "" : vxUnionId.trim();
} }
public void setVxUnionId(String VxUnionId) { public void setVxUnionId(String vxUnionId) {
this.VxUnionId = VxUnionId; this.vxUnionId = vxUnionId;
} }
public String getVxOpenId() { public String getVxOpenId() {
return VxOpenId == null ? "" : VxOpenId.trim(); return vxOpenId == null ? "" : vxOpenId.trim();
} }
public void setVxOpenId(String VxOpenId) { public void setVxOpenId(String vxOpenId) {
this.VxOpenId = VxOpenId; this.vxOpenId = vxOpenId;
} }

View File

@ -35,6 +35,15 @@ public class CardUserServiceImpl extends AbstractService implements ICardUserSer
@Autowired @Autowired
private ICardUserDao cardUserDao; private ICardUserDao cardUserDao;
/* public SuccessResult loginCardUser(String userName,String userPassword)throws Exception{
this.getCardUserById()
}*/
/** /**
* 根据微信open_id获取用户详情 * 根据微信open_id获取用户详情
* @param vxOpenId * @param vxOpenId
@ -94,6 +103,7 @@ public class CardUserServiceImpl extends AbstractService implements ICardUserSer
Map<String, Object> params = HashMapUtil.beanToMap(cardUserVO); Map<String, Object> params = HashMapUtil.beanToMap(cardUserVO);
params.put("cardUserId", cardUserId); params.put("cardUserId", cardUserId);
params.put("gmtCreate", DateUtil.getTime()); params.put("gmtCreate", DateUtil.getTime());
params.put("isDelete", "0");
/* if (token != null) { /* if (token != null) {
setSaveInfo(token, params); setSaveInfo(token, params);
} else { } else {

View File

@ -199,7 +199,7 @@ public interface ISystemConstant {
/** /**
* 小程序 appId* * 小程序 appId*
*/ */
String VX_APP_ID = "wxa49a60262e53d305 "; String VX_APP_ID = "wxa49a60262e53d305";
/** /**
* 小程序 appSecret * 小程序 appSecret
*/ */

View File

@ -55,6 +55,8 @@ public class WxApiUtil {
param.put("secret", secret); param.put("secret", secret);
param.put("js_code", code); param.put("js_code", code);
param.put("grant_type", grantType); param.put("grant_type", grantType);
//String url = VX_SMALL_ROUTINE_SESSION_KEY +"?appid="+appId+"&secret="+secret+"&js_code="+code+"&grant_type="+grantType;
String str = WxUtil.doGet(VX_SMALL_ROUTINE_SESSION_KEY, param); String str = WxUtil.doGet(VX_SMALL_ROUTINE_SESSION_KEY, param);
JSONObject json = JSONObject.parseObject(str); JSONObject json = JSONObject.parseObject(str);
return JSON.toJavaObject(json, WeiXinAuthResult.class); return JSON.toJavaObject(json, WeiXinAuthResult.class);

View File

@ -5,16 +5,37 @@
<resultMap id="cardUserDTO" type="cn.com.tenlion.pojo.dtos.carduser.CardUserDTO"> <resultMap id="cardUserDTO" type="cn.com.tenlion.pojo.dtos.carduser.CardUserDTO">
<id column="card_user_id" property="cardUserId"/> <id column="card_user_id" property="cardUserId"/>
<result column="user_name" property="userName"/> <result column="user_name" property="userName"/>
<result column="user_password" property="userPassword"/>
<result column="user_phone" property="userPhone"/> <result column="user_phone" property="userPhone"/>
<result column="vx_union_id" property="VxUnionId"/> <result column="vx_union_id" property="vxUnionId"/>
<result column="vx_open_id" property="VxOpenId"/> <result column="vx_open_id" property="vxOpenId"/>
</resultMap> </resultMap>
<!-- 根据用户名获取用户表详情 -->
<select id="getCardUserByUserName" parameterType="map" resultMap="cardUserDTO">
SELECT
t1.user_name,
t1.user_password,
t1.user_phone,
t1.vx_union_id,
t1.vx_open_id,
t1.card_user_id
FROM
card_user t1
WHERE
t1.is_delete = 0 AND t1.user_name = #{userName}
</select>
<!-- 根据微信openId获取用户表详情 --> <!-- 根据微信openId获取用户表详情 -->
<select id="getCardUserByVxOpenId" parameterType="map" resultMap="cardUserDTO"> <select id="getCardUserByVxOpenId" parameterType="map" resultMap="cardUserDTO">
SELECT SELECT
t1.user_name, t1.user_name,
t1.user_password,
t1.user_phone, t1.user_phone,
t1.vx_union_id, t1.vx_union_id,
t1.vx_open_id, t1.vx_open_id,
@ -35,6 +56,7 @@
INSERT INTO card_user( INSERT INTO card_user(
card_user_id, card_user_id,
user_name, user_name,
user_password,
user_phone, user_phone,
vx_union_id, vx_union_id,
vx_open_id, vx_open_id,
@ -46,9 +68,10 @@
) VALUES( ) VALUES(
#{cardUserId}, #{cardUserId},
#{userName}, #{userName},
#{userPassword},
#{userPhone}, #{userPhone},
#{VxUnionId}, #{vxUnionId},
#{VxOpenId}, #{vxOpenId},
#{creator}, #{creator},
#{gmtCreate}, #{gmtCreate},
#{modifier}, #{modifier},
@ -91,14 +114,17 @@
<if test="userName != null and userName != ''"> <if test="userName != null and userName != ''">
user_name = #{userName}, user_name = #{userName},
</if> </if>
<if test="userPassword != null and userPassword != ''">
user_password = #{userPassword},
</if>
<if test="userPhone != null and userPhone != ''"> <if test="userPhone != null and userPhone != ''">
user_phone = #{userPhone}, user_phone = #{userPhone},
</if> </if>
<if test="VxUnionId != null and VxUnionId != ''"> <if test="vxUnionId != null and vxUnionId != ''">
vx_union_id = #{VxUnionId}, vx_union_id = #{vxUnionId},
</if> </if>
<if test="VxOpenId != null and VxOpenId != ''"> <if test="vxOpenId != null and vxOpenId != ''">
vx_open_id = #{VxOpenId}, vx_open_id = #{vxOpenId},
</if> </if>
modifier = #{modifier}, modifier = #{modifier},
gmt_modified = #{gmtModified} gmt_modified = #{gmtModified}
@ -110,6 +136,7 @@
<select id="getCardUser" parameterType="map" resultMap="cardUserDTO"> <select id="getCardUser" parameterType="map" resultMap="cardUserDTO">
SELECT SELECT
t1.user_name, t1.user_name,
t1.user_password,
t1.user_phone, t1.user_phone,
t1.vx_union_id, t1.vx_union_id,
t1.vx_open_id, t1.vx_open_id,
@ -128,6 +155,7 @@
<select id="listCardUser" parameterType="map" resultMap="cardUserDTO"> <select id="listCardUser" parameterType="map" resultMap="cardUserDTO">
SELECT SELECT
t1.user_name, t1.user_name,
t1.user_password,
t1.user_phone, t1.user_phone,
t1.vx_union_id, t1.vx_union_id,
t1.vx_open_id, t1.vx_open_id,