提交代码

dev_xd
姜玉琦 2024-10-15 00:32:25 +08:00
parent a0a14975af
commit 48dda158ad
13 changed files with 209 additions and 74 deletions

View File

@ -1,6 +1,7 @@
package com.yanzhu.system.api;
import com.yanzhu.system.api.domain.SysUserUniopen;
import com.yanzhu.system.api.domain.vo.WxMaLoginBody;
import com.yanzhu.system.api.model.LoginUser;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
@ -30,6 +31,15 @@ public interface RemoteUserService
@GetMapping("/sysUserUniopen/getMaOpenId/{code}")
public R<Map<String,Object>> getMaOpenId(@PathVariable("code") String code, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/**
*
* @param body
* @param source
* @return
*/
@PostMapping("/sysUserUniopen/getMaPhoneNumber")
public R<Map<String,Object>> getMaPhoneNumber(@RequestBody WxMaLoginBody body, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/**
*
* @param maOpenId id
@ -39,6 +49,26 @@ public interface RemoteUserService
@GetMapping("/sysUserUniopen/findByMaOpenId/{maOpenId}")
public R<SysUserUniopen> findByMaOpenId(@PathVariable("maOpenId") String maOpenId, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/**
*
*
* @param sysUserUniopen
* @param source
* @return
*/
@PostMapping("/sysUserUniopen/add")
public R<Integer> addSysUserUniopen(@RequestBody SysUserUniopen sysUserUniopen, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/**
*
*
* @param sysUserUniopen
* @param source
* @return
*/
@PutMapping("/sysUserUniopen/edit")
public R<Integer> editSysUserUniopen(@RequestBody SysUserUniopen sysUserUniopen, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/**
*
*

View File

@ -1,4 +1,4 @@
package com.yanzhu.auth.form;
package com.yanzhu.system.api.domain.vo;
import io.swagger.annotations.ApiModel;
import lombok.Data;
@ -25,6 +25,12 @@ public class WxMaLoginBody {
@NotBlank(message = "授权编号不能为空")
private String openId;
/**
*
*/
@NotBlank(message = "授权密钥不能为空")
private String sessionKey;
/**
*
*/

View File

@ -2,6 +2,7 @@ package com.yanzhu.system.api.factory;
import com.yanzhu.system.api.RemoteUserService;
import com.yanzhu.system.api.domain.SysUserUniopen;
import com.yanzhu.system.api.domain.vo.WxMaLoginBody;
import com.yanzhu.system.api.model.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -35,12 +36,30 @@ public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserServ
return R.fail("获取小程序授权失败:" + throwable.getMessage());
}
@Override
public R<Map<String,Object>> getMaPhoneNumber(WxMaLoginBody body, String source)
{
return R.fail("获取小程序手机号失败:" + throwable.getMessage());
}
@Override
public R<SysUserUniopen> findByMaOpenId(String maOpenId, String source)
{
return R.fail("查询小程序授权失败:" + throwable.getMessage());
}
@Override
public R<Integer> addSysUserUniopen(SysUserUniopen sysUserUniopen, String source)
{
return R.fail("获取小程序手机号失败:" + throwable.getMessage());
}
@Override
public R<Integer> editSysUserUniopen(SysUserUniopen sysUserUniopen, String source)
{
return R.fail("获取小程序手机号失败:" + throwable.getMessage());
}
@Override
public R<LoginUser> getUserInfo(String username, String source)
{

View File

@ -2,7 +2,7 @@ package com.yanzhu.auth.controller;
import com.yanzhu.auth.form.LoginBody;
import com.yanzhu.auth.form.RegisterBody;
import com.yanzhu.auth.form.WxMaLoginBody;
import com.yanzhu.system.api.domain.vo.WxMaLoginBody;
import com.yanzhu.auth.form.WxMaOpenIdBody;
import com.yanzhu.auth.service.SysLoginService;
import com.yanzhu.common.core.constant.SecurityConstants;
@ -67,7 +67,20 @@ public class TokenController
@PostMapping("getMaOpenId")
public R<?> getMaOpenId(@RequestBody @Valid WxMaOpenIdBody body)
{
String maOpenId = sysLoginService.getMaOpenId(body.getCode());
Map<String, Object> data = sysLoginService.getMaOpenId(body.getCode());
// 获取授权openId
return R.ok(data);
}
/**
*
* @param body
* @return
*/
@PostMapping("getMaPhoneNumber")
public R<?> getMaPhoneNumber(@RequestBody @Valid WxMaLoginBody body)
{
String maOpenId = sysLoginService.getMaPhoneNumber(body);
// 获取授权openId
return R.ok(maOpenId);
}
@ -80,10 +93,10 @@ public class TokenController
@PostMapping("maLogin")
public R<?> maLogin(@RequestBody @Valid WxMaLoginBody body)
{
// 用户登录
Map<String,Object> result = sysLoginService.maLogin(body);
// 微信用户登录
LoginUser userInfo = sysLoginService.maLogin(body);
// 获取登录token
return R.ok(result);
return R.ok(tokenService.createToken(userInfo));
}
/**

View File

@ -1,6 +1,6 @@
package com.yanzhu.auth.service;
import com.yanzhu.auth.form.WxMaLoginBody;
import com.yanzhu.system.api.domain.vo.WxMaLoginBody;
import com.yanzhu.system.api.RemoteProService;
import com.yanzhu.system.api.domain.SysUserUniopen;
import org.springframework.beans.factory.annotation.Autowired;
@ -21,7 +21,6 @@ import com.yanzhu.system.api.RemoteUserService;
import com.yanzhu.system.api.domain.SysUser;
import com.yanzhu.system.api.model.LoginUser;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -128,24 +127,81 @@ public class SysLoginService
/**
*
*/
public String getMaOpenId(String code)
public Map<String, Object> getMaOpenId(String code)
{
Map<String,Object> loginResult = remoteUserService.getMaOpenId(code,SecurityConstants.INNER).getData();
return Convert.toStr(loginResult.get("openid"));
return loginResult;
}
/**
*
*/
public String getMaPhoneNumber(WxMaLoginBody loginBody)
{
Map<String,Object> phoneResult = remoteUserService.getMaPhoneNumber(loginBody,SecurityConstants.INNER).getData();
return Convert.toStr(phoneResult.get("phoneNumber"));
}
/**
*
*/
public Map<String,Object> maLogin(WxMaLoginBody loginBody)
public LoginUser maLogin(WxMaLoginBody loginBody)
{
LoginUser userInfo;
// 查询小程序授权情况
R<SysUserUniopen> uniResult = remoteUserService.findByMaOpenId(loginBody.getOpenId(),SecurityConstants.INNER);
if(Objects.isNull(uniResult) || Objects.isNull(uniResult.getData())){
throw new ServiceException("当前微信还未绑定账号!!!请使用账号登录一次");
Map<String,Object> phoneResult = remoteUserService.getMaPhoneNumber(loginBody,SecurityConstants.INNER).getData();
if(Objects.nonNull(phoneResult) && Objects.nonNull(phoneResult.get("phoneNumber"))){
// 查询用户信息
R<LoginUser> userResult = remoteUserService.getUserInfo(Convert.toStr(phoneResult.get("phoneNumber")), SecurityConstants.INNER);
if(Objects.isNull(userResult.getData())){
throw new ServiceException("登录失败,当前手机号未获取项目授权...");
}
return null;
userInfo = userResult.getData();
SysUserUniopen uniopen = new SysUserUniopen();
uniopen.setUserId(userInfo.getUserid());
uniopen.setUserName(userInfo.getUsername());
uniopen.setUserNick(userInfo.getSysUser().getNickName());
uniopen.setMaOpenId(loginBody.getOpenId());
remoteUserService.addSysUserUniopen(uniopen,SecurityConstants.INNER);
}else{
throw new ServiceException("获取授权手机号失败...");
}
}else{
R<LoginUser> userResult = remoteUserService.getUserInfo(Convert.toStr(uniResult.getData().getUserName()), SecurityConstants.INNER);
if(Objects.isNull(userResult.getData())){
throw new ServiceException("登录失败,当前手机号未获取项目授权...");
}
userInfo = userResult.getData();
}
SysUser user = userInfo.getSysUser();
if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
{
recordLogService.recordLogininfor(user.getUserName(), Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
throw new ServiceException("对不起,您的账号:" + user.getUserName() + " 已被删除");
}
if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
{
recordLogService.recordLogininfor(user.getUserName(), Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
throw new ServiceException("对不起,您的账号:" + user.getUserName() + " 已停用");
}
recordLogService.recordLogininfor(user.getUserName(), Constants.LOGIN_SUCCESS, "登录成功");
// 补充用户项目信息
if(Objects.nonNull(user.getActiveComId())){
userInfo.setProjectDeptId(user.getActiveComId());
}
if(Objects.nonNull(user.getActiveComName())){
userInfo.setProjectDeptName(user.getActiveComName());
}
if(Objects.nonNull(user.getActiveProjectId())){
userInfo.setProjectId(user.getActiveProjectId());
}
if(Objects.nonNull(user.getActiveProjectName())){
userInfo.setProjectName(user.getActiveProjectName());
}
return userInfo;
}
/**

View File

@ -1,46 +0,0 @@
package com.yanzhu.manage.controller;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.yanzhu.common.core.domain.R;
import com.yanzhu.common.core.utils.bean.BeanUtils;
import com.yanzhu.common.security.annotation.InnerAuth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
*
*/
@RestController
@RequestMapping("/wxCallBack")
public class WxCallBackController {
@Autowired
private WxMaService wxMaService;
private static final Logger log = LoggerFactory.getLogger(WxCallBackController.class);
/**
*
*/
@InnerAuth
@GetMapping("/getMaOpenId/{code}")
public R<Map<String, Object>> getMaOpenId(@PathVariable("code") String code) {
try {
WxMaJscode2SessionResult sessionInfo =
wxMaService.getUserService().getSessionInfo(code);
log.info("getMaOpenId==>{}...{}",code,sessionInfo.getOpenid());
return R.ok(BeanUtils.beanToMap(sessionInfo));
}catch (Exception e){
return R.fail("小程序登录异常!!!");
}
}
}

View File

@ -2,8 +2,8 @@ package com.yanzhu.system.controller;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.yanzhu.common.core.domain.R;
import com.yanzhu.common.core.utils.bean.BeanUtils;
import com.yanzhu.common.core.utils.poi.ExcelUtil;
import com.yanzhu.common.core.web.controller.BaseController;
import com.yanzhu.common.core.web.domain.AjaxResult;
@ -13,6 +13,7 @@ import com.yanzhu.common.log.enums.BusinessType;
import com.yanzhu.common.security.annotation.InnerAuth;
import com.yanzhu.common.security.annotation.RequiresPermissions;
import com.yanzhu.system.api.domain.SysUserUniopen;
import com.yanzhu.system.api.domain.vo.WxMaLoginBody;
import com.yanzhu.system.service.ISysUserUniopenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -20,6 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -119,7 +121,28 @@ public class SysUserUniopenController extends BaseController
WxMaJscode2SessionResult sessionInfo =
wxMaService.getUserService().getSessionInfo(code);
log.info("getMaOpenId==>{}...{}",code,sessionInfo.getOpenid());
return R.ok(BeanUtils.beanToMap(sessionInfo));
Map<String, Object> data = new HashMap<>();
data.put("openid",sessionInfo.getOpenid());
data.put("sessionKey",sessionInfo.getSessionKey());
return R.ok(data);
}catch (Exception e){
return R.fail("小程序授权异常!!!");
}
}
/**
*
*/
@InnerAuth
@GetMapping("/getMaPhoneNumber")
public R<Map<String, Object>> getMaPhoneNumber(@RequestBody WxMaLoginBody body) {
try {
// 调用解密方法获取手机号
WxMaPhoneNumberInfo phoneNumberInfo = wxMaService.getUserService().getPhoneNoInfo(body.getSessionKey(), body.getEncryptedData(), body.getIv());
log.info("getMaPhoneNumber==>{}...{}",body.getOpenId(),phoneNumberInfo.getPhoneNumber());
Map<String, Object> data = new HashMap<>();
data.put("phoneNumber",phoneNumberInfo.getPhoneNumber());
return R.ok(data);
}catch (Exception e){
return R.fail("小程序授权异常!!!");
}
@ -154,4 +177,16 @@ public class SysUserUniopenController extends BaseController
public R<SysUserUniopen> findByMpOpenId(@PathVariable("mpOpenId") String mpOpenId) {
return R.ok(sysUserUniopenService.selectSysUserUniopenByMpOpenId(mpOpenId));
}
@InnerAuth
@PostMapping("/add")
public R<Integer> addSysUserUniopen(@RequestBody SysUserUniopen sysUserUniopen) {
return R.ok(sysUserUniopenService.insertSysUserUniopen(sysUserUniopen));
}
@InnerAuth
@PutMapping("/edit")
public R<Integer> editSysUserUniopen(@RequestBody SysUserUniopen sysUserUniopen) {
return R.ok(sysUserUniopenService.updateSysUserUniopen(sysUserUniopen));
}
}

View File

@ -160,13 +160,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<select id="selectUserByUserName" parameterType="String" resultMap="SysUserResult">
<include refid="selectUserVo"/>
where u.user_name = #{userName} and u.del_flag = '0'
where u.del_flag = '0' and (u.user_name = #{userName} or u.phonenumber = #{userName})
</select>
<select id="selectUserByUserNameAndDept" parameterType="String" resultMap="SysUserResult">
<include refid="selectUserVo"/>
left join sys_role_dept rd on rd.role_id = r.role_id
where u.user_name = #{userName} and rd.dept_id = ${deptId} and u.del_flag = '0'
where u.del_flag = '0' and (u.user_name = #{userName} or u.phonenumber = #{userName}) and rd.dept_id = ${deptId}
</select>
<select id="selectUserById" parameterType="Long" resultMap="SysUserResult">

View File

@ -3,7 +3,7 @@ import {request} from '../utils/request'
// 获取验证码
export function getCodeImg() {
return request({
url: '/captchaImage',
url: '/code',
method: 'get',
})
}
@ -18,9 +18,9 @@ export function login(data) {
}
// 获取授权
export function getMaOpenId(code) {
export function getMaOpenId(data) {
return request({
url: '/getMaOpenId',
url: '/auth/getMaOpenId',
method: 'post',
data: data,
})

View File

@ -3,5 +3,5 @@ module.exports = {
timeout: 60000,
appId: "wx2350a5efb3f28e66",
baseUrl: 'http://127.0.0.1:8080',
noSecuritys:['/captchaImage','/login','/']
noSecuritys:['/code','/auth/login','/auth/getMaOpenId','/auth/getMaPhoneNumber','/auth/maLogin']
};

View File

@ -2,7 +2,9 @@ import {
getToken,
setToken,
getOpenId,
setOpenId
setOpenId,
getSessionKey,
setSessionKey,
} from '../../utils/auth'
import {
login,
@ -72,8 +74,9 @@ Page({
getMaOpenId: function () {
wx.login({
success: res => {
getMaOpenId(res.code).then(response => {
//
getMaOpenId({'code':res.code}).then(response => {
setOpenId(response.data.openid);
setSessionKey(response.data.sessionKey);
});
}
})
@ -118,7 +121,7 @@ Page({
"uuid": that.data.uuid,
}).then(res => {
if (res.code == '200') {
setToken(res.token)
setToken(res.access_token)
//跳转页面
wx.redirectTo({
url: '../index/index',
@ -132,7 +135,8 @@ Page({
getPhoneNumber(e) {
if(e.detail.code){
let data = {
code:e.detail.code,
openId:getOpenId(),
sessionKey:getSessionKey(),
iv: e.detail.iv,
encryptedData: e.detail.encryptedData
}
@ -141,7 +145,13 @@ Page({
//用户决绝授权
app.toast("请允许微信手机号一键登录");
}
console.log("getPhoneNumber", e)
maLogin(data).then(res =>{
setToken(res.access_token)
//跳转页面
wx.redirectTo({
url: '../index/index',
})
})
},
})

View File

@ -2,6 +2,7 @@
"description": "项目私有配置文件。此文件中的内容将覆盖 project.config.json 中的相同字段。项目的改动优先同步到此文件中。详见文档https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html",
"projectname": "miniprogram",
"setting": {
"compileHotReLoad": true
"compileHotReLoad": true,
"urlCheck": false
}
}

View File

@ -1,5 +1,6 @@
const TokenKey = 'YanZhu-XD-App-Token'
const OpenIdKey = 'YanZhu-XD-App-OpenId'
const SessionKey = 'YanZhu-XD-App-SessionKey';
export function getToken() {
return wx.getStorageSync(TokenKey)
@ -20,3 +21,13 @@ export function setOpenId(openId) {
export function removeOpenId() {
return wx.removeStorageSync(OpenIdKey)
}
export function getSessionKey() {
return wx.getStorageSync(SessionKey)
}
export function setSessionKey(sessionKey) {
return wx.setStorageSync(SessionKey, sessionKey)
}
export function removeSessionKey() {
return wx.removeStorageSync(SessionKey)
}