master
parent
b4ba7e5c32
commit
0b94f7c8bd
|
|
@ -1,6 +1,8 @@
|
|||
package com.njzscloud.dispose.cst.driver.pojo.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.njzscloud.common.mp.support.handler.j.JsonTypeHandler;
|
||||
import com.njzscloud.dispose.common.pojo.entity.BaseEntity;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
|
@ -8,6 +10,7 @@ import lombok.ToString;
|
|||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 司机实体(仅包含表字段)
|
||||
|
|
@ -53,7 +56,8 @@ public class DriverEntity extends BaseEntity {
|
|||
/**
|
||||
* 驾驶证图片
|
||||
*/
|
||||
private String drivingLicence;
|
||||
@TableField(typeHandler = JsonTypeHandler.class)
|
||||
private List<String> drivingLicence;
|
||||
|
||||
/**
|
||||
* 驾驶证有效期开始
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package com.njzscloud.dispose.cst.driver.pojo.result;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.njzscloud.common.mp.support.handler.j.JsonTypeHandler;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
|
@ -7,6 +9,7 @@ import lombok.experimental.Accessors;
|
|||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 司机详情/列表返回结果
|
||||
|
|
@ -62,7 +65,8 @@ public class DriverResult {
|
|||
/**
|
||||
* 驾驶证图片
|
||||
*/
|
||||
private String drivingLicence;
|
||||
@TableField(typeHandler = JsonTypeHandler.class)
|
||||
private List<String> drivingLicence;
|
||||
|
||||
/**
|
||||
* 驾驶证有效期开始
|
||||
|
|
|
|||
|
|
@ -1,11 +1,15 @@
|
|||
package com.njzscloud.dispose.cst.driver.service.impl;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.njzscloud.common.core.ex.Exceptions;
|
||||
import com.njzscloud.common.mp.support.PageParam;
|
||||
import com.njzscloud.common.mp.support.PageResult;
|
||||
import com.njzscloud.dispose.cst.customer.mapper.CustomerMapper;
|
||||
import com.njzscloud.dispose.cst.customer.pojo.entity.CustomerEntity;
|
||||
import com.njzscloud.dispose.cst.driver.mapper.DriverMapper;
|
||||
import com.njzscloud.dispose.cst.driver.pojo.entity.DriverEntity;
|
||||
import com.njzscloud.dispose.cst.driver.pojo.param.DriverQueryParam;
|
||||
|
|
@ -28,13 +32,36 @@ import java.util.List;
|
|||
@RequiredArgsConstructor
|
||||
public class DriverServiceImpl extends ServiceImpl<DriverMapper, DriverEntity> implements DriverService {
|
||||
|
||||
private final CustomerMapper customerMapper;
|
||||
|
||||
@Override
|
||||
public void add(DriverEntity driverEntity) {
|
||||
this.save(driverEntity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void modify(DriverEntity driverEntity) {
|
||||
Long driverId = driverEntity.getId();
|
||||
// 查询司机当前信息
|
||||
DriverEntity currentDriver = this.getById(driverId);
|
||||
if (currentDriver == null) {
|
||||
throw Exceptions.clierr("司机信息不存在");
|
||||
}
|
||||
|
||||
Long newOrgId = driverEntity.getOrgId();
|
||||
Long oldOrgId = currentDriver.getOrgId();
|
||||
Long customerId = currentDriver.getCustomerId();
|
||||
|
||||
// 判断是否需要更新客户的orgId
|
||||
if (customerId != null && newOrgId != null && ObjectUtil.notEqual(newOrgId, oldOrgId)) {
|
||||
// 更新客户的orgId
|
||||
CustomerEntity customer = new CustomerEntity();
|
||||
customer.setId(customerId);
|
||||
customer.setOrgId(newOrgId);
|
||||
customerMapper.updateById(customer);
|
||||
}
|
||||
|
||||
this.updateById(driverEntity);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ import com.njzscloud.dispose.dev.WbsHandle;
|
|||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.time.Duration;
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import lombok.RequiredArgsConstructor;
|
|||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public enum OrgCategory implements DictStr {
|
||||
GeTi("GeTi", "个体"),
|
||||
GeRen("GeRen", "个人"),
|
||||
|
||||
QiYe("QiYe", "企业"),
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@ import com.njzscloud.common.mp.support.PageResult;
|
|||
import com.njzscloud.dispose.sys.user.pojo.param.*;
|
||||
import com.njzscloud.dispose.sys.user.pojo.result.UserDetailResult;
|
||||
import com.njzscloud.dispose.sys.user.service.UserService;
|
||||
import com.njzscloud.dispose.wechat.pojo.param.WechatParam;
|
||||
import com.njzscloud.dispose.wechat.service.WechatService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
|
@ -25,6 +27,7 @@ import java.util.List;
|
|||
public class UserController {
|
||||
|
||||
private final UserService userService;
|
||||
private final WechatService wechatService;
|
||||
|
||||
/**
|
||||
* 新增
|
||||
|
|
@ -123,4 +126,20 @@ public class UserController {
|
|||
userService.resetPasswd(id);
|
||||
return R.success(true, "重置成功,新密码:6个6");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信openId
|
||||
*/
|
||||
@PostMapping("/getOpenId")
|
||||
public R<?> getOpenId(@RequestBody WechatParam param) {
|
||||
return R.success(wechatService.getOpenId(param));
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信授权手机号
|
||||
*/
|
||||
@PostMapping("/getMiniPhone")
|
||||
public R<?> getMiniPhone(@RequestBody WechatParam param) {
|
||||
return R.success(wechatService.getMiniPhone(param));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -94,7 +94,7 @@ public class UserRegisterParam implements Constrained {
|
|||
/**
|
||||
* 用户名
|
||||
*/
|
||||
@NotBlank(message = "账号不能为空")
|
||||
// @NotBlank(message = "账号不能为空")
|
||||
private String username;
|
||||
/**
|
||||
* 手机号
|
||||
|
|
@ -103,12 +103,16 @@ public class UserRegisterParam implements Constrained {
|
|||
/**
|
||||
* 密码
|
||||
*/
|
||||
@NotBlank(message = "密码不能为空")
|
||||
// @NotBlank(message = "密码不能为空")
|
||||
private String secret;
|
||||
/**
|
||||
* 微信 openid
|
||||
*/
|
||||
private String wechatCode;
|
||||
/**
|
||||
* 微信 openid
|
||||
*/
|
||||
private String openid;
|
||||
/**
|
||||
* 允许登录的客户端; 字典代码:client_code
|
||||
*/
|
||||
|
|
@ -147,19 +151,19 @@ public class UserRegisterParam implements Constrained {
|
|||
/**
|
||||
* 客户姓名
|
||||
*/
|
||||
@NotBlank(message = "客户姓名不能为空")
|
||||
// @NotBlank(message = "客户姓名不能为空")
|
||||
private String customerName;
|
||||
|
||||
/**
|
||||
* 客户联系电话
|
||||
*/
|
||||
@NotBlank(message = "客户联系电话不能为空")
|
||||
// @NotBlank(message = "客户联系电话不能为空")
|
||||
private String phone;
|
||||
|
||||
/**
|
||||
* 是否管理员;是否为当前的组织管理员,0-->否、1-->是
|
||||
*/
|
||||
@NotNull(message = "是否为当前组织管理员不能为空")
|
||||
// @NotNull(message = "是否为当前组织管理员不能为空")
|
||||
private Boolean manager;
|
||||
}
|
||||
|
||||
|
|
@ -177,19 +181,19 @@ public class UserRegisterParam implements Constrained {
|
|||
/**
|
||||
* 主体类型,GeTiHu-->个体户、QiYe-->企业
|
||||
*/
|
||||
@NotNull(message = "主体类型不能为空")
|
||||
// @NotNull(message = "主体类型不能为空")
|
||||
private OrgCategory orgCategory;
|
||||
|
||||
/**
|
||||
* 统一社会信用代码
|
||||
*/
|
||||
@NotBlank(message = "统一社会信用代码不能为空")
|
||||
// @NotBlank(message = "统一社会信用代码不能为空")
|
||||
private String uscc;
|
||||
|
||||
/**
|
||||
* 组织名称
|
||||
*/
|
||||
@NotBlank(message = "组织名称不能为空")
|
||||
// @NotBlank(message = "组织名称不能为空")
|
||||
private String orgName;
|
||||
|
||||
/**
|
||||
|
|
@ -197,6 +201,11 @@ public class UserRegisterParam implements Constrained {
|
|||
*/
|
||||
private String businessLicense;
|
||||
|
||||
/**
|
||||
* 驾驶证图片
|
||||
*/
|
||||
private List<String> drivingLicence;
|
||||
|
||||
/**
|
||||
* 营业执照有效期
|
||||
*/
|
||||
|
|
@ -220,13 +229,13 @@ public class UserRegisterParam implements Constrained {
|
|||
/**
|
||||
* 法人身份证有效期
|
||||
*/
|
||||
@NotNull(message = "法人身份证有效期不能为空")
|
||||
// @NotNull(message = "法人身份证有效期不能为空")
|
||||
private LocalDate idcardStartTime;
|
||||
|
||||
/**
|
||||
* 法人身份证有效期
|
||||
*/
|
||||
@NotNull(message = "法人身份证有效期不能为空")
|
||||
// @NotNull(message = "法人身份证有效期不能为空")
|
||||
private LocalDate idcardEndTime;
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@ import com.njzscloud.dispose.cst.customer.constant.IdentityCategory;
|
|||
import com.njzscloud.dispose.cst.customer.pojo.param.AddCustomerParam;
|
||||
import com.njzscloud.dispose.cst.customer.pojo.result.AddCustomerResult;
|
||||
import com.njzscloud.dispose.cst.customer.service.CustomerService;
|
||||
import com.njzscloud.dispose.cst.driver.pojo.entity.DriverEntity;
|
||||
import com.njzscloud.dispose.cst.driver.service.DriverService;
|
||||
import com.njzscloud.dispose.cst.org.pojo.param.AddOrgParam;
|
||||
import com.njzscloud.dispose.finance.constant.AccountType;
|
||||
import com.njzscloud.dispose.finance.pojo.entity.MoneyAccountEntity;
|
||||
|
|
@ -61,6 +63,7 @@ public class UserService extends ServiceImpl<UserMapper, UserEntity> implements
|
|||
private final CustomerService customerService;
|
||||
private final AuthService authService;
|
||||
private final MoneyAccountService moneyAccountService;
|
||||
private final DriverService driverService;
|
||||
|
||||
/**
|
||||
* 新增
|
||||
|
|
@ -156,12 +159,14 @@ public class UserService extends ServiceImpl<UserMapper, UserEntity> implements
|
|||
UserRegisterParam.Account account = userRegisterParam.getAccount();
|
||||
AddUserAccountParam addUserAccountParam = BeanUtil.copyProperties(account, AddUserAccountParam.class);
|
||||
String wechatCode = account.getWechatCode();
|
||||
if (StrUtil.isNotBlank(wechatCode)) {
|
||||
String openid = account.getOpenid();
|
||||
if (StrUtil.isNotBlank(wechatCode) && StrUtil.isBlank(openid)) {
|
||||
Code2SessionResult code2SessionResult = WechatUtil.code2Session(new Code2SessionParam().setJs_code(wechatCode));
|
||||
Assert.isTrue(code2SessionResult.isSucc(), () -> Exceptions.exception("微信校验失败"));
|
||||
addUserAccountParam.setWechatOpenid(code2SessionResult.getOpenid())
|
||||
.setWechatUnionid(code2SessionResult.getUnionid())
|
||||
;
|
||||
.setWechatUnionid(code2SessionResult.getUnionid());
|
||||
} else {
|
||||
addUserAccountParam.setWechatOpenid(openid);
|
||||
}
|
||||
|
||||
AddUserParam addUserParam = BeanUtil
|
||||
|
|
@ -184,14 +189,21 @@ public class UserService extends ServiceImpl<UserMapper, UserEntity> implements
|
|||
UserRegisterParam.Org org = userRegisterParam.getOrg();
|
||||
AddCustomerResult customerResult = customerService.add(BeanUtil.copyProperties(customer, AddCustomerParam.class)
|
||||
.setAddOrgParam(BeanUtil.copyProperties(org, AddOrgParam.class)));
|
||||
List<RoleDetailResult> roleDetailResults = userRoleService.listRole(userId);
|
||||
|
||||
Set<String> roles = roleDetailResults.stream().map(RoleDetailResult::getRoleCode).collect(Collectors.toSet());
|
||||
roles.add(ROLE_AUTHENTICATED);
|
||||
roles.add(ROLE_ANONYMOUS);
|
||||
UserDetail userDetail = authService.my(userId, account.getClientCode())
|
||||
.setAuthWay(userRegisterParam.getAuthWay())
|
||||
.setRoles(roles);
|
||||
if (IdentityCategory.SiJi.equals(identityCategory)) {
|
||||
// 添加司机信息到cst_driver
|
||||
DriverEntity driverEntity = new DriverEntity();
|
||||
driverEntity.setUserId(userId);
|
||||
driverEntity.setCustomerId(customerResult.getCustomerId());
|
||||
driverEntity.setOrgId(customerResult.getOrgId());
|
||||
driverEntity.setDrivingLicenceNo(org.getIdcard());
|
||||
driverEntity.setDriverName(account.getUsername());
|
||||
driverEntity.setPhone(account.getPhone());
|
||||
driverEntity.setDrivingLicence(org.getDrivingLicence());
|
||||
driverEntity.setLicenceStartTime(org.getLicenseStartTime());
|
||||
driverEntity.setLicenceEndTime(org.getLicenseEndTime());
|
||||
driverService.add(driverEntity);
|
||||
}
|
||||
// 添加账户信息
|
||||
MoneyAccountEntity moneyAccount = new MoneyAccountEntity();
|
||||
if (Boolean.TRUE.equals(customer.getManager())) {
|
||||
|
|
@ -203,6 +215,14 @@ public class UserService extends ServiceImpl<UserMapper, UserEntity> implements
|
|||
moneyAccount.setUserId(userId);
|
||||
moneyAccount.setCustomerId(customerResult.getCustomerId());
|
||||
moneyAccountService.add(moneyAccount);
|
||||
|
||||
List<RoleDetailResult> roleDetailResults = userRoleService.listRole(userId);
|
||||
Set<String> roles = roleDetailResults.stream().map(RoleDetailResult::getRoleCode).collect(Collectors.toSet());
|
||||
roles.add(ROLE_AUTHENTICATED);
|
||||
roles.add(ROLE_ANONYMOUS);
|
||||
UserDetail userDetail = authService.my(userId, account.getClientCode())
|
||||
.setAuthWay(userRegisterParam.getAuthWay())
|
||||
.setRoles(roles);
|
||||
return SecurityUtil.registrationUser(userDetail);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
package com.njzscloud.dispose.wechat.pojo.param;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ljw
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@Accessors(chain = true)
|
||||
public class WechatParam {
|
||||
|
||||
/**
|
||||
* code参数缺失
|
||||
*/
|
||||
@NotBlank(message = "code参数缺失")
|
||||
private String code;
|
||||
|
||||
/**
|
||||
* 小程序完整用户信息的加密数据
|
||||
*/
|
||||
private String encryptedData;
|
||||
|
||||
/**
|
||||
* 小程序加密算法的初始向量
|
||||
*/
|
||||
private String iv;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,196 @@
|
|||
package com.njzscloud.dispose.wechat.service;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.njzscloud.common.core.ex.Exceptions;
|
||||
import com.njzscloud.common.security.contant.AuthWay;
|
||||
import com.njzscloud.common.security.support.UserDetail;
|
||||
import com.njzscloud.common.security.util.SecurityUtil;
|
||||
import com.njzscloud.dispose.sys.auth.service.AuthService;
|
||||
import com.njzscloud.dispose.sys.role.pojo.result.RoleDetailResult;
|
||||
import com.njzscloud.dispose.sys.user.pojo.entity.UserAccountEntity;
|
||||
import com.njzscloud.dispose.sys.user.service.UserAccountService;
|
||||
import com.njzscloud.dispose.sys.user.service.UserRoleService;
|
||||
import com.njzscloud.dispose.wechat.pojo.param.WechatParam;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.njzscloud.common.security.contant.Constants.ROLE_ANONYMOUS;
|
||||
import static com.njzscloud.common.security.contant.Constants.ROLE_AUTHENTICATED;
|
||||
|
||||
/**
|
||||
* 资金流水
|
||||
*
|
||||
* @author ljw
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class WechatService {
|
||||
|
||||
private final UserAccountService userAccountService;
|
||||
private final AuthService authService;
|
||||
private final UserRoleService userRoleService;
|
||||
|
||||
/**
|
||||
* 获取openId
|
||||
*
|
||||
* @param param 获取openId入参
|
||||
*/
|
||||
public Map<String, Object> getOpenId(WechatParam param) {
|
||||
log.info("登录参数 - code: {}, encryptedData长度: {}, iv: {}",
|
||||
param.getCode(),
|
||||
param.getEncryptedData() != null ? param.getEncryptedData().length() : 0,
|
||||
param.getIv());
|
||||
|
||||
String code = param.getCode();
|
||||
if (StrUtil.isBlank(code)) {
|
||||
throw Exceptions.clierr("授权码code不能为空");
|
||||
}
|
||||
|
||||
String appid = "wx3c06d9dd4e56c58d";
|
||||
String secret = "ff280a71a4c06fc2956178f8c472ef96";
|
||||
|
||||
// 使用code向微信服务器换取session_key和openid
|
||||
String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code="
|
||||
+ param.getCode() + "&grant_type=authorization_code";
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
String response = restTemplate.getForObject(url, String.class);
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
if (null == jsonObject) {
|
||||
throw Exceptions.clierr("获取小程序信息失败");
|
||||
}
|
||||
// 检查微信服务器返回的错误码
|
||||
if (jsonObject.containsKey("errcode")) {
|
||||
throw Exceptions.clierr("获取小程序信息失败,错误码:" + jsonObject.getInteger("errcode")
|
||||
+ ",错误信息:" + jsonObject.getString("errmsg"));
|
||||
}
|
||||
String openid = jsonObject.getString("openid");
|
||||
String sessionKey = jsonObject.getString("session_key");
|
||||
log.info("获取到openid: {}, sessionKey长度: {}", openid, sessionKey.length());
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
UserAccountEntity entity = userAccountService.getOne(Wrappers.<UserAccountEntity>lambdaQuery().eq(UserAccountEntity::getWechatOpenid, openid));
|
||||
if (entity == null) {
|
||||
map.put("type", "register");
|
||||
map.put("openid", openid);
|
||||
} else {
|
||||
map.put("type", "login");
|
||||
List<RoleDetailResult> roleDetailResults = userRoleService.listRole(entity.getUserId());
|
||||
Set<String> roles = roleDetailResults.stream().map(RoleDetailResult::getRoleCode).collect(Collectors.toSet());
|
||||
roles.add(ROLE_AUTHENTICATED);
|
||||
roles.add(ROLE_ANONYMOUS);
|
||||
UserDetail userDetail = authService.my(entity.getUserId(), entity.getClientCode())
|
||||
.setAuthWay(AuthWay.WECHAT_MINI)
|
||||
.setRoles(roles);
|
||||
UserDetail detail = SecurityUtil.registrationUser(userDetail);
|
||||
map.put("userDetail", detail);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 小程序手机号授权
|
||||
*/
|
||||
public Map<String, Object> getMiniPhone(WechatParam param) {
|
||||
// 1. 从前端请求体中获取code
|
||||
String code = param.getCode();
|
||||
if (StrUtil.isBlank(code)) {
|
||||
throw Exceptions.clierr("授权码code不能为空");
|
||||
}
|
||||
|
||||
// 2. 获取access_token
|
||||
String accessToken;
|
||||
try {
|
||||
accessToken = this.getAccessToken();
|
||||
} catch (Exception e) {
|
||||
log.error("获取access_token失败: ", e);
|
||||
throw Exceptions.clierr("服务暂不可用,请稍后重试");
|
||||
}
|
||||
// 3. 构建请求URL和参数
|
||||
String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken;
|
||||
|
||||
// 4. 使用 Hutool 发送 POST 请求(解决 412 错误)
|
||||
String requestBody = String.format("{\"code\":\"%s\"}", code);
|
||||
log.info("发送请求到微信接口,请求体: {}", requestBody);
|
||||
|
||||
String response;
|
||||
try {
|
||||
HttpResponse httpResponse = HttpRequest.post(url)
|
||||
.body(requestBody)
|
||||
.timeout(30000)
|
||||
.execute();
|
||||
response = httpResponse.body();
|
||||
httpResponse.close();
|
||||
} catch (Exception e) {
|
||||
log.error("调用微信授权手机号接口网络异常: ", e);
|
||||
throw Exceptions.clierr("网络请求失败");
|
||||
}
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
log.info("微信接口返回原始数据: {}", response);
|
||||
|
||||
// 5. 处理微信接口响应
|
||||
if (jsonObject == null) {
|
||||
throw Exceptions.clierr("获取手机信息失败,响应为空");
|
||||
}
|
||||
Integer errCode = jsonObject.getInteger("errcode");
|
||||
if (errCode != null && errCode != 0) {
|
||||
String errMsg = jsonObject.getString("errmsg");
|
||||
log.error("微信接口返回错误,errCode: {}, errMsg: {}", errCode, errMsg);
|
||||
throw Exceptions.clierr("授权失败: " + errMsg);
|
||||
}
|
||||
|
||||
// 6. 解析并返回结构化的手机号信息
|
||||
JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
|
||||
if (phoneInfo == null) {
|
||||
throw Exceptions.clierr("响应中缺少手机号信息");
|
||||
}
|
||||
|
||||
// 提取核心信息
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
String phone = phoneInfo.getString("phoneNumber");
|
||||
result.put("phoneNumber", phone);
|
||||
result.put("purePhoneNumber", phoneInfo.getString("purePhoneNumber"));
|
||||
result.put("countryCode", phoneInfo.getString("countryCode"));
|
||||
|
||||
// 记录日志(注意在生产环境需脱敏)
|
||||
log.info("手机号授权成功,国家码: {},手机号: {}",
|
||||
phoneInfo.getString("countryCode"),
|
||||
phoneInfo.getString("purePhoneNumber"));
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取全局唯一的Access Token(注意缓存,避免频繁调用)
|
||||
*/
|
||||
private String getAccessToken() {
|
||||
String appid = "wx3c06d9dd4e56c58d";
|
||||
String secret = "ff280a71a4c06fc2956178f8c472ef96";
|
||||
String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
String response = restTemplate.getForObject(tokenUrl, String.class);
|
||||
JSONObject jsonResponse = JSONObject.parseObject(response);
|
||||
|
||||
if (jsonResponse == null || jsonResponse.containsKey("errcode")) {
|
||||
// 正确判断错误:当存在errcode,或响应为空时,才是失败
|
||||
String errMsg = jsonResponse != null ? jsonResponse.getString("errmsg") : "响应为空";
|
||||
log.error("获取Access Token失败: {}", errMsg);
|
||||
throw Exceptions.clierr("获取服务凭证失败");
|
||||
}
|
||||
String token = jsonResponse.getString("access_token");
|
||||
log.info("成功获取Access Token,有效期: {}秒", jsonResponse.getIntValue("expires_in"));
|
||||
return token;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -7,6 +7,8 @@ spring:
|
|||
auth-allows:
|
||||
- /oss/**
|
||||
- /user/register
|
||||
- /user/getOpenId
|
||||
- /user/getMiniPhone
|
||||
- /endpoint/reload
|
||||
- /permission/refresh_cache
|
||||
- /static/**
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ spring:
|
|||
auth-ignores:
|
||||
- /auth/obtain_code
|
||||
- /user/register
|
||||
- /user/getOpenId
|
||||
- /user/getMiniPhone
|
||||
- /oss/**
|
||||
- /district/tree
|
||||
- /sys_sn_config/add
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@
|
|||
<result column="driving_licence_no" property="drivingLicenceNo"/>
|
||||
<result column="driver_name" property="driverName"/>
|
||||
<result column="phone" property="phone"/>
|
||||
<result column="driving_licence" property="drivingLicence"/>
|
||||
<result column="driving_licence" property="drivingLicence" typeHandler="com.njzscloud.common.mp.support.handler.j.JsonTypeHandler"/>
|
||||
<result column="licence_start_time" property="licenceStartTime"/>
|
||||
<result column="licence_end_time" property="licenceEndTime"/>
|
||||
<result column="busy" property="busy"/>
|
||||
|
|
|
|||
Loading…
Reference in New Issue