2024-06-19 朱宝民 迁移4个微信小程序接口
3 文件已重命名
5个文件已添加
10个文件已修改
New file |
| | |
| | | package com.dy.common.webFilter; |
| | | |
| | | import com.dy.common.multiDataSource.DataSourceContext; |
| | | import jakarta.servlet.*; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.io.IOException; |
| | | |
| | | /** |
| | | * @author ZhuBaoMin |
| | | * @date 2024-06-19 11:02 |
| | | * @LastEditTime 2024-06-19 11:02 |
| | | * @Description |
| | | */ |
| | | |
| | | @Slf4j |
| | | public class WXDataSourceNameSetFilter implements Filter { |
| | | @Override |
| | | public void init(FilterConfig filterConfig) throws ServletException { |
| | | } |
| | | |
| | | @Override |
| | | public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { |
| | | HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; |
| | | String wxDataSourceName = httpRequest.getHeader("tag"); |
| | | if(wxDataSourceName != null && wxDataSourceName.trim().length() > 0){ |
| | | log.info("微信开发,设置数据源名称为:" + wxDataSourceName); |
| | | //把组织单位标签作为数据源名称 |
| | | DataSourceContext.set(wxDataSourceName); |
| | | } else { |
| | | log.info("用户未选择数据源"); |
| | | } |
| | | |
| | | filterChain.doFilter(servletRequest, servletResponse); |
| | | } |
| | | |
| | | @Override |
| | | public void destroy() { |
| | | } |
| | | } |
| | |
| | | #name: ym |
| | | type: com.alibaba.druid.pool.DruidDataSource |
| | | driverClassName: com.mysql.cj.jdbc.Driver |
| | | # url: jdbc:mysql://192.168.40.166:3306/pipIrr_ym?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull |
| | | url: jdbc:mysql://127.0.0.1:3306/pipIrr_ym?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull |
| | | url: jdbc:mysql://192.168.40.166:3306/pipIrr_ym?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull |
| | | # url: jdbc:mysql://127.0.0.1:3306/pipIrr_ym?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull |
| | | username: root |
| | | password: dysql,;.abc!@# |
| | | druid: |
| | |
| | | |
| | | pipIrr: |
| | | global: |
| | | dev: true #是否开发阶段,true或false |
| | | dev: false #是否开发阶段,true或false |
| | | dsName: ym #开发阶段,设置临时的数据库名称 |
| | | mw: |
| | | webPort: 8070 |
| | |
| | | datasource: #配置数据源 |
| | | type: com.alibaba.druid.pool.DruidDataSource |
| | | driver-class-name: com.mysql.cj.jdbc.Driver |
| | | # url: jdbc:mysql://192.168.40.166:3306/pipIrr_ym?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull |
| | | url: jdbc:mysql://127.0.0.1:3306/pipIrr_ym?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull |
| | | url: jdbc:mysql://192.168.40.166:3306/pipIrr_ym?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull |
| | | # url: jdbc:mysql://127.0.0.1:3306/pipIrr_ym?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull |
| | | username: root |
| | | password: dysql,;.abc!@# |
| | | druid: |
| | |
| | | package com.dy.pipIrrSell.client; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; |
| | | import com.aliyuncs.exceptions.ClientException; |
| | | import com.dy.common.aop.SsoAop; |
| | | import com.dy.common.webUtil.BaseResponse; |
| | | import com.dy.common.webUtil.BaseResponseUtils; |
| | |
| | | import com.dy.common.webUtil.ResultCodeMsg; |
| | | import com.dy.pipIrrGlobal.pojoBa.BaClient; |
| | | import com.dy.pipIrrGlobal.pojoSe.SeClient; |
| | | import com.dy.pipIrrGlobal.pojoSe.SeCodeVerify; |
| | | import com.dy.pipIrrGlobal.pojoSe.SeOpenId; |
| | | import com.dy.pipIrrGlobal.voSe.VoClient; |
| | | import com.dy.pipIrrGlobal.voSe.VoClientWechat; |
| | | import com.dy.pipIrrSell.client.dto.CodeVerifyDTO; |
| | | import com.dy.pipIrrSell.result.SellResultCode; |
| | | import com.dy.pipIrrSell.sms.AliyunSmsSv; |
| | | import com.dy.pipIrrSell.sms.RandomCode; |
| | | import com.dy.pipIrrSell.util.RestTemplateUtil; |
| | | import com.dy.pipIrrSell.wechatpay.PayInfo; |
| | | import io.swagger.v3.oas.annotations.Operation; |
| | |
| | | import org.springframework.validation.BindingResult; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.*; |
| | | |
| | | /** |
| | |
| | | @RequiredArgsConstructor |
| | | public class ClientCtrl { |
| | | private final ClientSv clientSv; |
| | | private final AliyunSmsSv aliyunSmsSv; |
| | | //private final RedisUtils redisUtils; |
| | | private final RestTemplateUtil restTemplateUtil; |
| | | |
| | |
| | | map.put("projectNo",projectNo3); |
| | | return BaseResponseUtils.buildSuccess(map); |
| | | } |
| | | |
| | | /** |
| | | * 发送验证码 |
| | | * @param phoneNumber |
| | | * @return |
| | | */ |
| | | @Operation(summary = "发送验证码", description = "发送验证码") |
| | | @ApiResponses(value = { |
| | | @ApiResponse( |
| | | responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, |
| | | description = "操作结果:true:成功,false:失败(BaseResponse.content)", |
| | | content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, |
| | | schema = @Schema(implementation = Boolean.class))} |
| | | ) |
| | | }) |
| | | @PostMapping(path = "send_sms") |
| | | @SsoAop() |
| | | public BaseResponse<Boolean> sendSms(@RequestParam(name = "phoneNumber", required = true) String phoneNumber) throws ClientException { |
| | | if(phoneNumber == null || phoneNumber.length() <= 0) { |
| | | return BaseResponseUtils.buildFail(SellResultCode.PHONE_NUMBER_CANNOT_BE_NULL.getMessage()); |
| | | } |
| | | |
| | | // 生成6位验证码并转为Json格式 |
| | | String securityCode = String.valueOf(RandomCode.genCode()); |
| | | JSONObject param = new JSONObject(); |
| | | param.put("code", securityCode); |
| | | String templateParam = param.toJSONString(); |
| | | |
| | | //redisUtils.set(phoneNumber, securityCode, 60); |
| | | |
| | | // 获取当前时间戳并延后3分钟 |
| | | Long timestamp = System.currentTimeMillis(); |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTimeInMillis(timestamp); |
| | | calendar.add(Calendar.SECOND, 180); |
| | | Long expires = calendar.getTimeInMillis(); |
| | | |
| | | SeCodeVerify codeVerify = new SeCodeVerify(); |
| | | codeVerify.setPhoneNumber(phoneNumber); |
| | | codeVerify.setSecurityCode(securityCode); |
| | | codeVerify.setExpires(expires); |
| | | clientSv.addCodeVerify(codeVerify); |
| | | |
| | | SendSmsResponse response = aliyunSmsSv.sendSms(phoneNumber, templateParam); |
| | | if (response.getCode().equals("OK")) { |
| | | // 发送成功处理逻辑 |
| | | return BaseResponseUtils.buildSuccess(true) ; |
| | | } else { |
| | | // 发送失败处理逻辑 |
| | | return BaseResponseUtils.buildFail(SellResultCode.SECURITY_CODE_SEND_FAIL.getMessage()) ; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 校验验证码 |
| | | * @param po |
| | | * @param bindingResult |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | @Operation(summary = "校验验证码", description = "校验验证码") |
| | | @ApiResponses(value = { |
| | | @ApiResponse( |
| | | responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, |
| | | description = "操作结果:true:成功,false:失败(BaseResponse.content)", |
| | | content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, |
| | | schema = @Schema(implementation = Boolean.class))} |
| | | ) |
| | | }) |
| | | @PostMapping(path = "verify", consumes = MediaType.APPLICATION_JSON_VALUE) |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @SsoAop() |
| | | public BaseResponse<Boolean> verify(@RequestBody @Valid CodeVerifyDTO po, BindingResult bindingResult) throws IOException { |
| | | if(bindingResult != null && bindingResult.hasErrors()){ |
| | | return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); |
| | | } |
| | | |
| | | String phoneNumber = po.getPhoneNumber(); |
| | | String securityCode = po.getSecurityCode(); |
| | | String code = po.getCode(); |
| | | |
| | | // 进行手机号、验证码、过期时间校验 |
| | | SeCodeVerify codeVerify = clientSv.getCodeVerify(phoneNumber); |
| | | if(codeVerify == null) { |
| | | return BaseResponseUtils.buildFail(SellResultCode.NO_SECURITY_CODE_FOR_PHONE.getMessage()); |
| | | } |
| | | |
| | | if(!codeVerify.getSecurityCode().equals(securityCode)) { |
| | | return BaseResponseUtils.buildFail(SellResultCode.SECURITY_CODE_ERROR.getMessage()); |
| | | } |
| | | |
| | | Long currentTimestamp = System.currentTimeMillis(); |
| | | if(currentTimestamp > codeVerify.getExpires() ) { |
| | | return BaseResponseUtils.buildFail(SellResultCode.VALIDATION_TIMEOUT.getMessage()); |
| | | } |
| | | |
| | | // 校验通过,进行登录凭证校验 |
| | | Map<String, Object> queryParams = new HashMap<>(); |
| | | queryParams.put("appid", appid); |
| | | queryParams.put("secret", secret); |
| | | queryParams.put("js_code", code); |
| | | queryParams.put("grant_type", grantType); |
| | | Map<String, String> headerParams = new HashMap<>(); |
| | | JSONObject job = restTemplateUtil.get(loginUrl, queryParams, headerParams); |
| | | |
| | | if(job.getLong("errcode") != null && job.getLong("errcode") >= -1) { |
| | | return BaseResponseUtils.buildFail("登录凭证校验失败"); |
| | | } |
| | | String openid = job.getString("openid"); |
| | | String sessionKey = job.getString("session_key"); |
| | | |
| | | Long clientId = clientSv.getClientIdByPhone(phoneNumber); |
| | | String SessionId = ""; |
| | | if(clientId != null) { |
| | | // 添加微信用户账户记录 |
| | | SeOpenId seOpenId = new SeOpenId(); |
| | | seOpenId.setClientId(clientId); |
| | | seOpenId.setOpenId(openid); |
| | | seOpenId.setSessionKey(sessionKey); |
| | | seOpenId.setCreateTime(new Date()); |
| | | //Long SessionId = clientSv.addOpenId(seOpenId); |
| | | Long rec = clientSv.addOpenId(seOpenId); |
| | | if(rec != null) { |
| | | SessionId = String.valueOf(rec); |
| | | } |
| | | return BaseResponseUtils.buildSuccess(SessionId); |
| | | |
| | | } else { |
| | | return BaseResponseUtils.buildError(SellResultCode.PHONE_NUMBER_IS_ERROR.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取农户基本信息,小程序首页使用 |
| | | * @param sessionId |
| | | * @return |
| | | */ |
| | | @GetMapping(path = "/simple_info") |
| | | @SsoAop() |
| | | public BaseResponse<VoClientWechat> getSimpleClientInfo(@RequestParam Long sessionId){ |
| | | try { |
| | | VoClientWechat res = clientSv.getSimpleClientInfo(sessionId, null); |
| | | return BaseResponseUtils.buildSuccess(res); |
| | | } catch (Exception e) { |
| | | log.error("查询农户异常", e); |
| | | return BaseResponseUtils.buildException(e.getMessage()) ; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 登录凭证登录 |
| | | * @param code 临时登录凭证 |
| | | * @return |
| | | */ |
| | | @GetMapping(path = "/code_login") |
| | | @SsoAop() |
| | | public BaseResponse<VoClientWechat> codeLogin(@RequestParam String code) throws IOException { |
| | | |
| | | // 登录凭证校验 |
| | | Map<String, Object> queryParams = new HashMap<>(); |
| | | queryParams.put("appid", appid); |
| | | queryParams.put("secret", secret); |
| | | queryParams.put("js_code", code); |
| | | queryParams.put("grant_type", grantType); |
| | | Map<String, String> headerParams = new HashMap<>(); |
| | | JSONObject job = restTemplateUtil.get(loginUrl, queryParams, headerParams); |
| | | |
| | | String openId = job.getString("openid"); |
| | | if(openId == null) { |
| | | return BaseResponseUtils.buildFail(SellResultCode.LOGIN_FAIL.getMessage()); |
| | | } |
| | | |
| | | try { |
| | | VoClientWechat res = clientSv.getSimpleClientInfo(null, openId); |
| | | return BaseResponseUtils.buildSuccess(res); |
| | | } catch (Exception e) { |
| | | log.error("查询农户异常", e); |
| | | return BaseResponseUtils.buildException(e.getMessage()) ; |
| | | } |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | import com.dy.pipIrrGlobal.daoBa.BaDistrictMapper; |
| | | import com.dy.pipIrrGlobal.daoBa.BaSettingsMapper; |
| | | import com.dy.pipIrrGlobal.daoSe.SeClientMapper; |
| | | import com.dy.pipIrrGlobal.daoSe.SeCodeVerifyMapper; |
| | | import com.dy.pipIrrGlobal.daoSe.SeOpenIdMapper; |
| | | import com.dy.pipIrrGlobal.pojoSe.SeClient; |
| | | import com.dy.pipIrrGlobal.pojoSe.SeCodeVerify; |
| | | import com.dy.pipIrrGlobal.pojoSe.SeOpenId; |
| | | import com.dy.pipIrrGlobal.voSe.VoClient; |
| | | import com.dy.pipIrrGlobal.voSe.VoClientWechat; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.dubbo.common.utils.PojoUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | private BaDistrictMapper baDistrictMapper; |
| | | |
| | | @Autowired |
| | | private SeCodeVerifyMapper seCodeVerifyMapper; |
| | | private BaSettingsMapper baSettingsMapper; |
| | | |
| | | @Autowired |
| | | private SeOpenIdMapper seOpenIdMapper; |
| | | |
| | | @Autowired |
| | | private BaSettingsMapper baSettingsMapper; |
| | | |
| | | /** |
| | | * 根据指定条件获取农户数据 |
| | |
| | | return seClientMapper.insertSelective(po); |
| | | } |
| | | |
| | | /** |
| | | * 添加验证码记录 |
| | | * @param po |
| | | * @return |
| | | */ |
| | | public Integer addCodeVerify(SeCodeVerify po) { |
| | | return seCodeVerifyMapper.insert(po); |
| | | } |
| | | |
| | | /** |
| | | * 根据手机号获取验证码验证对象 |
| | | * @param phoneNumber |
| | | * @return |
| | | */ |
| | | public SeCodeVerify getCodeVerify(String phoneNumber) { |
| | | return seCodeVerifyMapper.getCodeVerify(phoneNumber); |
| | | } |
| | | /** |
| | | * 根据6位区划串模糊查询农户编号 |
| | | * @param district6 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 根据配置项获取配置项值 |
| | | * @param itemName |
| | | * @return |
| | | */ |
| | | public String getItemValue(String itemName) { |
| | | return baSettingsMapper.getItemValue(itemName); |
| | | } |
| | | |
| | | /** |
| | | * 根据电话号码获取农户ID |
| | | * @param phoneNumber |
| | | * @return |
| | |
| | | seOpenIdMapper.insert(po); |
| | | //return po.getClientId(); |
| | | return po.getId(); |
| | | } |
| | | |
| | | /** |
| | | * 根据配置项获取配置项值 |
| | | * @param itemName |
| | | * @return |
| | | */ |
| | | public String getItemValue(String itemName) { |
| | | return baSettingsMapper.getItemValue(itemName); |
| | | } |
| | | |
| | | /** |
| | | * 获取农户基本信息,小程序首页使用 |
| | | * @param sessionId |
| | | * @return |
| | | */ |
| | | public VoClientWechat getSimpleClientInfo(Long sessionId, String openId) { |
| | | return seClientMapper.getSimpleClientInfo(sessionId, openId); |
| | | } |
| | | } |
| | |
| | | No_TRADE_SUMMARIES(100001, "没有符合条件的交易汇总记录"), |
| | | No_TRADE_DETAILS(100001, "没有符合条件的交易明细"), |
| | | |
| | | /** |
| | | * 微信用户 |
| | | */ |
| | | PHONE_NUMBER_CANNOT_BE_NULL(20001, "手机号不能为空"), |
| | | SECURITY_CODE_SEND_FAIL(20001, "验证码发送失败"), |
| | | VERIFY_PARAMS_INCOMPLETE(20001, "验证参数不完整"), |
| | | CODE_VERIFY_FAIL(20001, "验证码校验失败"), |
| | | |
| | | NO_SECURITY_CODE_FOR_PHONE(20002, "该手机号未发送验证码"), |
| | | SECURITY_CODE_ERROR(20003, "验证码错误"), |
| | | VALIDATION_TIMEOUT(20004, "验证超时"), |
| | | PHONE_NUMBER_IS_ERROR(20004, "手机号错误,非注册农户"), |
| | | |
| | | /** |
| | | * 小程序 |
| | | */ |
| | | LOGIN_FAIL(20004, "登录失败"); |
| | | PHONE_NUMBER_IS_ERROR(20004, "手机号错误,非注册农户"); |
| | | |
| | | private final Integer code; |
| | | private final String message; |
New file |
| | |
| | | package com.dy.pipIrrWechat.client; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; |
| | | import com.aliyuncs.exceptions.ClientException; |
| | | import com.dy.common.webUtil.BaseResponse; |
| | | import com.dy.common.webUtil.BaseResponseUtils; |
| | | import com.dy.pipIrrGlobal.pojoSe.SeCodeVerify; |
| | | import com.dy.pipIrrGlobal.pojoSe.SeOpenId; |
| | | import com.dy.pipIrrGlobal.voSe.VoClientWechat; |
| | | import com.dy.pipIrrSell.wechatpay.PayInfo; |
| | | import com.dy.pipIrrWechat.client.dto.CodeLoginDTO; |
| | | import com.dy.pipIrrWechat.client.dto.CodeVerifyDTO; |
| | | import com.dy.pipIrrWechat.result.WechatResultCode; |
| | | import com.dy.pipIrrWechat.sms.AliyunSmsSv; |
| | | import com.dy.pipIrrWechat.sms.RandomCode; |
| | | import com.dy.pipIrrWechat.util.RestTemplateUtil; |
| | | import io.swagger.v3.oas.annotations.tags.Tag; |
| | | import jakarta.validation.Valid; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.BindingResult; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * @author ZhuBaoMin |
| | | * @date 2024-06-19 14:21 |
| | | * @LastEditTime 2024-06-19 14:21 |
| | | * @Description |
| | | */ |
| | | |
| | | @Slf4j |
| | | @Tag(name = "农户数据操作", description = "农户数据操作") |
| | | @RestController |
| | | @RequestMapping(path="client") |
| | | @RequiredArgsConstructor |
| | | public class ClientCtrl { |
| | | private final ClientSv clientSv; |
| | | private final AliyunSmsSv aliyunSmsSv; |
| | | private final RestTemplateUtil restTemplateUtil; |
| | | private final String privateCertFileName = PayInfo.privateCertFileName; |
| | | private final String appid = PayInfo.appid; |
| | | private final String secret = PayInfo.secret; |
| | | private final String mchid = PayInfo.mchid; |
| | | private final String schema = PayInfo.schema; |
| | | private final String signType = PayInfo.signType; |
| | | private final String description = PayInfo.description; |
| | | private final String loginUrl = PayInfo.loginUrl; |
| | | private final String notifyUrl = PayInfo.notifyUrl; |
| | | private final String grantType = PayInfo.grantType; |
| | | |
| | | /** |
| | | * 登录凭证登录 |
| | | * @param po |
| | | * @param bindingResult |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | @PostMapping(path = "code_login", consumes = MediaType.APPLICATION_JSON_VALUE) |
| | | public BaseResponse<VoClientWechat> codeLogin(@RequestBody @Valid CodeLoginDTO po, BindingResult bindingResult) throws IOException { |
| | | if(bindingResult != null && bindingResult.hasErrors()){ |
| | | return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); |
| | | } |
| | | String code = po.getCode(); |
| | | |
| | | // 登录凭证校验 |
| | | Map<String, Object> queryParams = new HashMap<>(); |
| | | queryParams.put("appid", appid); |
| | | queryParams.put("secret", secret); |
| | | queryParams.put("js_code", code); |
| | | queryParams.put("grant_type", grantType); |
| | | Map<String, String> headerParams = new HashMap<>(); |
| | | JSONObject job = restTemplateUtil.get(loginUrl, queryParams, headerParams); |
| | | |
| | | String openId = job.getString("openid"); |
| | | if(openId == null) { |
| | | return BaseResponseUtils.buildErrorMsg(WechatResultCode.LOGIN_FAIL.getMessage()); |
| | | } |
| | | |
| | | try { |
| | | VoClientWechat res = clientSv.getSimpleClientInfo(null, openId); |
| | | return BaseResponseUtils.buildSuccess(res); |
| | | } catch (Exception e) { |
| | | log.error("查询农户异常", e); |
| | | return BaseResponseUtils.buildException(e.getMessage()) ; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 发送验证码 |
| | | * @param phoneNumber |
| | | * @return |
| | | */ |
| | | @PostMapping(path = "send_sms") |
| | | public BaseResponse<Boolean> sendSms(@RequestParam(name = "phoneNumber", required = true) String phoneNumber) throws ClientException { |
| | | if(phoneNumber == null || phoneNumber.length() <= 0) { |
| | | return BaseResponseUtils.buildErrorMsg(WechatResultCode.PHONE_NUMBER_CANNOT_BE_NULL.getMessage()); |
| | | } |
| | | |
| | | // 生成6位验证码并转为Json格式 |
| | | String securityCode = String.valueOf(RandomCode.genCode()); |
| | | JSONObject param = new JSONObject(); |
| | | param.put("code", securityCode); |
| | | String templateParam = param.toJSONString(); |
| | | |
| | | //redisUtils.set(phoneNumber, securityCode, 60); |
| | | |
| | | // 获取当前时间戳并延后3分钟 |
| | | Long timestamp = System.currentTimeMillis(); |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTimeInMillis(timestamp); |
| | | calendar.add(Calendar.SECOND, 180); |
| | | Long expires = calendar.getTimeInMillis(); |
| | | |
| | | SeCodeVerify codeVerify = new SeCodeVerify(); |
| | | codeVerify.setPhoneNumber(phoneNumber); |
| | | codeVerify.setSecurityCode(securityCode); |
| | | codeVerify.setExpires(expires); |
| | | clientSv.addCodeVerify(codeVerify); |
| | | |
| | | SendSmsResponse response = aliyunSmsSv.sendSms(phoneNumber, templateParam); |
| | | if (response.getCode().equals("OK")) { |
| | | // 发送成功处理逻辑 |
| | | return BaseResponseUtils.buildSuccess(true) ; |
| | | } else { |
| | | // 发送失败处理逻辑 |
| | | return BaseResponseUtils.buildErrorMsg(WechatResultCode.SECURITY_CODE_SEND_FAIL.getMessage()) ; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 校验验证码 |
| | | * @param po |
| | | * @param bindingResult |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | @PostMapping(path = "verify", consumes = MediaType.APPLICATION_JSON_VALUE) |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public BaseResponse<Boolean> verify(@RequestBody @Valid CodeVerifyDTO po, BindingResult bindingResult) throws IOException { |
| | | if(bindingResult != null && bindingResult.hasErrors()){ |
| | | return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); |
| | | } |
| | | |
| | | String phoneNumber = po.getPhoneNumber(); |
| | | String securityCode = po.getSecurityCode(); |
| | | String code = po.getCode(); |
| | | |
| | | // 进行手机号、验证码、过期时间校验 |
| | | SeCodeVerify codeVerify = clientSv.getCodeVerify(phoneNumber); |
| | | if(codeVerify == null) { |
| | | return BaseResponseUtils.buildErrorMsg(WechatResultCode.NO_SECURITY_CODE_FOR_PHONE.getMessage()); |
| | | } |
| | | |
| | | if(!codeVerify.getSecurityCode().equals(securityCode)) { |
| | | return BaseResponseUtils.buildErrorMsg(WechatResultCode.SECURITY_CODE_ERROR.getMessage()); |
| | | } |
| | | |
| | | Long currentTimestamp = System.currentTimeMillis(); |
| | | if(currentTimestamp > codeVerify.getExpires() ) { |
| | | return BaseResponseUtils.buildErrorMsg(WechatResultCode.VALIDATION_TIMEOUT.getMessage()); |
| | | } |
| | | |
| | | // 校验通过,进行登录凭证校验 |
| | | Map<String, Object> queryParams = new HashMap<>(); |
| | | queryParams.put("appid", appid); |
| | | queryParams.put("secret", secret); |
| | | queryParams.put("js_code", code); |
| | | queryParams.put("grant_type", grantType); |
| | | Map<String, String> headerParams = new HashMap<>(); |
| | | JSONObject job = restTemplateUtil.get(loginUrl, queryParams, headerParams); |
| | | |
| | | if(job.getLong("errcode") != null && job.getLong("errcode") >= -1) { |
| | | return BaseResponseUtils.buildErrorMsg("登录凭证校验失败"); |
| | | } |
| | | String openid = job.getString("openid"); |
| | | String sessionKey = job.getString("session_key"); |
| | | |
| | | Long clientId = clientSv.getClientIdByPhone(phoneNumber); |
| | | String SessionId = ""; |
| | | if(clientId != null) { |
| | | // 添加微信用户账户记录 |
| | | SeOpenId seOpenId = new SeOpenId(); |
| | | seOpenId.setClientId(clientId); |
| | | seOpenId.setOpenId(openid); |
| | | seOpenId.setSessionKey(sessionKey); |
| | | seOpenId.setCreateTime(new Date()); |
| | | //Long SessionId = clientSv.addOpenId(seOpenId); |
| | | Long rec = clientSv.addOpenId(seOpenId); |
| | | if(rec != null) { |
| | | SessionId = String.valueOf(rec); |
| | | } |
| | | return BaseResponseUtils.buildSuccess(SessionId); |
| | | |
| | | } else { |
| | | return BaseResponseUtils.buildErrorMsg(WechatResultCode.PHONE_NUMBER_IS_ERROR.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取农户基本信息,小程序首页使用 |
| | | * @param sessionId |
| | | * @return |
| | | */ |
| | | @GetMapping(path = "/simple_info") |
| | | public BaseResponse<VoClientWechat> getSimpleClientInfo(@RequestParam Long sessionId){ |
| | | try { |
| | | VoClientWechat res = clientSv.getSimpleClientInfo(sessionId, null); |
| | | return BaseResponseUtils.buildSuccess(res); |
| | | } catch (Exception e) { |
| | | log.error("查询农户异常", e); |
| | | return BaseResponseUtils.buildException(e.getMessage()) ; |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.pipIrrWechat.client; |
| | | |
| | | import com.dy.pipIrrGlobal.daoSe.SeClientMapper; |
| | | import com.dy.pipIrrGlobal.daoSe.SeCodeVerifyMapper; |
| | | import com.dy.pipIrrGlobal.daoSe.SeOpenIdMapper; |
| | | import com.dy.pipIrrGlobal.pojoSe.SeCodeVerify; |
| | | import com.dy.pipIrrGlobal.pojoSe.SeOpenId; |
| | | import com.dy.pipIrrGlobal.voSe.VoClientWechat; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | /** |
| | | * @author ZhuBaoMin |
| | | * @date 2024-06-19 14:25 |
| | | * @LastEditTime 2024-06-19 14:25 |
| | | * @Description |
| | | */ |
| | | |
| | | @Slf4j |
| | | @Service |
| | | public class ClientSv { |
| | | @Autowired |
| | | private SeClientMapper seClientMapper; |
| | | @Autowired |
| | | private SeCodeVerifyMapper seCodeVerifyMapper; |
| | | @Autowired |
| | | private SeOpenIdMapper seOpenIdMapper; |
| | | |
| | | /** |
| | | * 根据电话号码获取农户ID |
| | | * @param phoneNumber |
| | | * @return |
| | | */ |
| | | public Long getClientIdByPhone(String phoneNumber) { |
| | | return seClientMapper.getClientIdByPhone(phoneNumber); |
| | | } |
| | | |
| | | /** |
| | | * 添加微信用户账户记录 |
| | | * @param po |
| | | * @return |
| | | */ |
| | | public Long addOpenId(SeOpenId po) { |
| | | seOpenIdMapper.insert(po); |
| | | //return po.getClientId(); |
| | | return po.getId(); |
| | | } |
| | | |
| | | /** |
| | | * 添加验证码记录 |
| | | * @param po |
| | | * @return |
| | | */ |
| | | public Integer addCodeVerify(SeCodeVerify po) { |
| | | return seCodeVerifyMapper.insert(po); |
| | | } |
| | | |
| | | /** |
| | | * 根据手机号获取验证码验证对象 |
| | | * @param phoneNumber |
| | | * @return |
| | | */ |
| | | public SeCodeVerify getCodeVerify(String phoneNumber) { |
| | | return seCodeVerifyMapper.getCodeVerify(phoneNumber); |
| | | } |
| | | |
| | | /** |
| | | * 获取农户基本信息,小程序首页使用 |
| | | * @param sessionId |
| | | * @return |
| | | */ |
| | | public VoClientWechat getSimpleClientInfo(Long sessionId, String openId) { |
| | | return seClientMapper.getSimpleClientInfo(sessionId, openId); |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.pipIrrWechat.client.dto; |
| | | |
| | | import jakarta.validation.constraints.NotBlank; |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * @author ZhuBaoMin |
| | | * @date 2024-06-19 15:57 |
| | | * @LastEditTime 2024-06-19 15:57 |
| | | * @Description |
| | | */ |
| | | |
| | | @Data |
| | | public class CodeLoginDTO { |
| | | /** |
| | | * 临时登录凭证 |
| | | */ |
| | | @NotBlank(message = "临时登录凭证不能为空") |
| | | private String code; |
| | | } |
File was renamed from pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/client/dto/CodeVerifyDTO.java |
| | |
| | | package com.dy.pipIrrSell.client.dto; |
| | | package com.dy.pipIrrWechat.client.dto; |
| | | |
| | | import jakarta.validation.constraints.NotBlank; |
| | | import lombok.Data; |
| | |
| | | * @return |
| | | */ |
| | | @GetMapping(path = "/get") |
| | | @SsoAop() |
| | | //@SsoAop() |
| | | public BaseResponse<List<VoUnclosedValve>> getUnclosedValves(@RequestParam Long operator){ |
| | | try { |
| | | List<VoUnclosedValve> res = commandSv.getUnclosedValves(operator); |
| | |
| | | package com.dy.pipIrrWechat.config; |
| | | |
| | | import com.dy.common.webFilter.DevOfDataSourceNameSetFilter; |
| | | import com.dy.common.webFilter.UserTokenFilter; |
| | | import com.dy.common.webFilter.WXDataSourceNameSetFilter; |
| | | import jakarta.servlet.Filter; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.boot.web.servlet.FilterRegistrationBean; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | |
| | | */ |
| | | @Configuration |
| | | public class WebFilterConfiguration { |
| | | |
| | | @Value("${pipIrr.global.dev}") |
| | | public String isDevStage ;//是否为开发阶段 |
| | | @Value("${pipIrr.global.dsName}") |
| | | public String dsName ;//开发阶段的数据源名称 |
| | | |
| | | /** |
| | | * DevOfDataSourceNameSetFilter与UserTokenFilter只能一个被配置上, |
| | | * 所以他们的order都是1 |
| | | */ |
| | | private static final int order_UserTokenFilter = 1 ;//与下面 |
| | | private static final int order_DevOfDataSourceNameSetFilter = 1 ; |
| | | |
| | | |
| | | @Bean |
| | | public FilterRegistrationBean<? extends Filter> RegFilter() { |
| | | FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>(); |
| | | if(this.isDevStage != null && !this.isDevStage.trim().equals("") && this.isDevStage.trim().equalsIgnoreCase("true")){ |
| | | filterRegistrationBean.setFilter(new DevOfDataSourceNameSetFilter()); |
| | | filterRegistrationBean.addUrlPatterns("/*");//配置过滤规则 |
| | | filterRegistrationBean.addInitParameter("dataSourceName",dsName);//设置init参数 |
| | | filterRegistrationBean.setName("DevOfDataSourceNameSetFilter");//设置过滤器名称 |
| | | filterRegistrationBean.setOrder(order_DevOfDataSourceNameSetFilter);//执行次序 |
| | | }else{ |
| | | filterRegistrationBean.setFilter(new UserTokenFilter()); |
| | | filterRegistrationBean.addUrlPatterns("/*");//配置过滤规则 |
| | | filterRegistrationBean.setName("UserTokenFilter");//设置过滤器名称 |
| | | filterRegistrationBean.setOrder(order_UserTokenFilter);//执行次序 |
| | | } |
| | | filterRegistrationBean.setFilter(new WXDataSourceNameSetFilter()); |
| | | filterRegistrationBean.addUrlPatterns("/*");//配置过滤规则 |
| | | filterRegistrationBean.setName("WXDataSourceNameSetFilter");//设置过滤器名称 |
| | | filterRegistrationBean.setOrder(1);//执行次序 |
| | | |
| | | return filterRegistrationBean; |
| | | } |
| | | |
| | |
| | | /** |
| | | * RTU |
| | | */ |
| | | RTU_NOT_EXIST(20001, "阀控器不存在"); |
| | | RTU_NOT_EXIST(20001, "阀控器不存在"), |
| | | //RTU_ADDR_CANNOT_BE_NULL(20002, "阀控器地址不能为空"); |
| | | |
| | | /** |
| | | * 微信用户 |
| | | */ |
| | | PHONE_NUMBER_CANNOT_BE_NULL(20001, "手机号不能为空"), |
| | | SECURITY_CODE_SEND_FAIL(20001, "验证码发送失败"), |
| | | VERIFY_PARAMS_INCOMPLETE(20001, "验证参数不完整"), |
| | | CODE_VERIFY_FAIL(20001, "验证码校验失败"), |
| | | |
| | | NO_SECURITY_CODE_FOR_PHONE(20002, "该手机号未发送验证码"), |
| | | SECURITY_CODE_ERROR(20003, "验证码错误"), |
| | | VALIDATION_TIMEOUT(20004, "验证超时"), |
| | | PHONE_NUMBER_IS_ERROR(20004, "手机号错误,非注册农户"), |
| | | LOGIN_FAIL(20004, "登录失败"); |
| | | |
| | | private final Integer code; |
| | | private final String message; |
| | | } |
File was renamed from pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/sms/AliyunSmsSv.java |
| | |
| | | package com.dy.pipIrrSell.sms; |
| | | package com.dy.pipIrrWechat.sms; |
| | | |
| | | import com.aliyuncs.DefaultAcsClient; |
| | | import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; |
File was renamed from pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/sms/RandomCode.java |
| | |
| | | package com.dy.pipIrrSell.sms; |
| | | package com.dy.pipIrrWechat.sms; |
| | | |
| | | /** |
| | | * @author ZhuBaoMin |
New file |
| | |
| | | package com.dy.pipIrrWechat.util; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.*; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.MultiValueMap; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @author ZhuBaoMin |
| | | * @date 2024-03-06 13:47 |
| | | * @LastEditTime 2024-03-06 13:47 |
| | | * @Description |
| | | */ |
| | | |
| | | @Component |
| | | public class RestTemplateUtil { |
| | | |
| | | @Autowired |
| | | private RestTemplate restTemplate; |
| | | |
| | | //@Qualifier("RestTemplateWithCert") |
| | | //@Resource |
| | | //private RestTemplate restTemplate; |
| | | |
| | | public JSONObject get(String url, Map<String, Object> queryParams) throws IOException { |
| | | return get(url, queryParams, new HashMap<>(1)); |
| | | } |
| | | |
| | | public JSONObject get(String url, Map<String, Object> queryParams, Map<String, String> headerParams) throws IOException { |
| | | String tempUrl = setParamsByAppendUrl(queryParams, url); |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headerParams.forEach(headers::add); |
| | | HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(null, headers); |
| | | ResponseEntity<String> response = restTemplate.exchange(tempUrl, HttpMethod.GET, httpEntity, String.class); |
| | | return JSONObject.parseObject(response.getBody()); |
| | | } |
| | | |
| | | public JSONObject getHeaders(String url, Map<String, Object> queryParams, Map<String, String> headerParams) throws IOException { |
| | | String tempUrl = setParamsByAppendUrl(queryParams, url); |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headerParams.forEach(headers::add); |
| | | HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(null, headers); |
| | | ResponseEntity<String> response = restTemplate.exchange(tempUrl, HttpMethod.GET, httpEntity, String.class); |
| | | |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("headers", response.getHeaders()); |
| | | jsonObject.put("body", response.getBody()); |
| | | return jsonObject; |
| | | } |
| | | |
| | | public JSONObject get2(String url, Map<String, Object> queryParams, Map<String, String> headerParams) throws IOException { |
| | | String tempUrl = setParamsByPath(queryParams, url); |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headerParams.forEach(headers::add); |
| | | HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(null, headers); |
| | | ResponseEntity<String> response = restTemplate.exchange(tempUrl, HttpMethod.GET, httpEntity, String.class, queryParams); |
| | | return JSONObject.parseObject(response.getBody()); |
| | | } |
| | | |
| | | public JSONObject post(String url, String json, Map<String, String> headerParams) { |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headerParams.forEach(headers::add); |
| | | headers.setContentType(MediaType.APPLICATION_JSON); |
| | | headers.add("Accept", MediaType.APPLICATION_JSON.toString()); |
| | | HttpEntity<String> httpEntity = new HttpEntity<>(json, headers); |
| | | ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class); |
| | | return JSONObject.parseObject(response.getBody()); |
| | | } |
| | | |
| | | private String setParamsByPath(Map<String, Object> queryParams, String url) { |
| | | // url?id={id}&name={name} |
| | | if (queryParams == null || queryParams.isEmpty()) { |
| | | return url; |
| | | } |
| | | StringBuilder sb = new StringBuilder(); |
| | | try { |
| | | for (Map.Entry<String, Object> entry : queryParams.entrySet()) { |
| | | sb.append("&").append(entry.getKey()).append("=").append("{").append(entry.getKey()).append("}"); |
| | | } |
| | | if (!url.contains("?")) { |
| | | sb.deleteCharAt(0).insert(0, "?"); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return url + sb; |
| | | } |
| | | |
| | | private String setParamsByAppendUrl(Map<String, Object> queryParams, String url) { |
| | | // url?id=1&name=zzc |
| | | if (queryParams == null || queryParams.isEmpty()) { |
| | | return url; |
| | | } |
| | | StringBuilder sb = new StringBuilder(); |
| | | try { |
| | | for (Map.Entry<String, Object> entry : queryParams.entrySet()) { |
| | | sb.append("&").append(entry.getKey()).append("="); |
| | | sb.append(entry.getValue()); |
| | | } |
| | | if (!url.contains("?")) { |
| | | sb.deleteCharAt(0).insert(0, "?"); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return url + sb; |
| | | } |
| | | |
| | | } |
| | |
| | | idSuffix: ${pipIrr.wechat.idSuffix} |
| | | #ConfigListener中应用 |
| | | #configFileNames: config-global.xml,config-demo.xml |
| | | #阿里短信服务 |
| | | aliyun: |
| | | sms: |
| | | sms-access-key-id: LTAI5tPCmHqfyJ9YnoPorEwt |
| | | sms-access-key-secret: sU2CoLdNgcjnf5uPPU2dY7NNGNvOIX |
| | | sms-template-code: SMS_460776024 |
| | | sms-sign-name: 大禹节水 |