zhubaomin
2025-04-11 9f3c4a33279f10ed420d604765487558ab0744f0
pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/client/ClientCtrl.java
New file
@@ -0,0 +1,260 @@
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.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 com.dy.pipIrrWechat.wechatpay.PayInfo;
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 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<JSONObject> 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", PayInfo.appid);
        queryParams.put("secret", PayInfo.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.containsKey("errmsg ")) {
            return BaseResponseUtils.buildErrorMsg(WechatResultCode.INVALID_CODE.getMessage());
        }
        String openId = job.getString("openid");
        if(openId == null) {
            return BaseResponseUtils.buildErrorMsg(WechatResultCode.LOGIN_FAIL.getMessage());
        }
        try {
            JSONObject job_client = new JSONObject();
            VoClientWechat res = clientSv.getSimpleClientInfo(null, openId);
            if(res != null) {
                job_client.put("client", res);
            } else {
                VoClientWechat voClientWechat = new VoClientWechat();
                job_client.put("client", voClientWechat);
            }
            return BaseResponseUtils.buildSuccess(job_client);
        } 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());
        }
        Long clientId = clientSv.getClientIdByPhone(phoneNumber);
        if(clientId == null) {
            return BaseResponseUtils.buildErrorMsg("该手机号对应的农户不存在");
        }
        // 生成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", PayInfo.appid);
        queryParams.put("secret", PayInfo.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);
            }
            JSONObject job_result = new JSONObject();
            job_result.put("clientId", String.valueOf(clientId));
            job_result.put("sessionId", sessionId);
            return BaseResponseUtils.buildSuccess(job_result);
        } else {
            return BaseResponseUtils.buildErrorMsg(WechatResultCode.PHONE_NUMBER_IS_ERROR.getMessage());
        }
    }
    /**
     * 解绑农户与微信的绑定,根据sessionId删除绑定记录
     * @param sessionId
     * @return
     */
    @PostMapping(path = "unbind")
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<Boolean> unbindWechat(@RequestParam("sessionId")Long sessionId) {
        if(sessionId == null || sessionId <=0) {
            return BaseResponseUtils.buildErrorMsg("sessionId无效");
        }
        Integer rec = clientSv.unbindWechat(sessionId);
        if(rec == null || rec == 0) {
            return BaseResponseUtils.buildErrorMsg("sessionId错误");
        }
        return BaseResponseUtils.buildSuccess();
    }
    /**
     * 获取农户基本信息,小程序首页使用
     * @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()) ;
        }
    }
}