AccessTokenController.java 4.47 KB
package com.subsidy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.subsidy.common.ResponseData;
import com.subsidy.common.ResponseVO;
import com.subsidy.common.configure.RestTemplateConfig;
import com.subsidy.common.configure.WechatConfig;
import com.subsidy.mapper.AccessTokenMapper;
import com.subsidy.model.AccessTokenDO;
import com.subsidy.util.ConstantUtils;
import com.subsidy.util.SecretUtils;
import com.subsidy.vo.wechat.AccessTokenVO;
import com.subsidy.vo.wechat.AuthenRequestVO;
import com.subsidy.vo.wechat.AuthenTokenVO;
import com.subsidy.vo.wechat.JsSdkVO;
import com.subsidy.vo.wechat.SignVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DengMin
 * @since 2022-02-18
 */
@RestController
@Api(tags = "微信")
@RequestMapping("/wechat")
public class AccessTokenController {

    @Autowired
    private RestTemplateConfig restTemplateConfig;

    @Autowired
    private WechatConfig wechatConfig;

    @Autowired
    private AccessTokenMapper accessTokenMapper;

    @PostMapping("/getJsapiTicket")
    @ApiOperation("通过token请求获得jsapi_ticket    {url}")
    public ResponseVO getJsapiTicket(@RequestBody SignVO signVO) {

        List<AccessTokenDO> accessTokens = accessTokenMapper.selectList(null);

        String accessToken = "";
        if (accessTokens.size()==0){
            String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wechatConfig.getAppId() + "&secret=" + wechatConfig.getAppSecret();
            ResponseEntity<AccessTokenVO> response = restTemplateConfig.restTemplate()
                    .exchange(url, HttpMethod.GET, null, AccessTokenVO.class);
            accessToken = response.getBody().getAccess_token();
            AccessTokenDO accessToken1 = new AccessTokenDO();
            accessToken1.setAccessToken(accessToken);
            accessToken1.setUpdateDate(LocalDateTime.now());
            accessTokenMapper.insert(accessToken1);
        }else {
            AccessTokenDO accessToken1 = accessTokens.get(0);
            long diff = Duration.between(accessToken1.getUpdateDate(),LocalDateTime.now()).toMillis();
            if (diff>=2*60*60*1000-5*1000){
                String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wechatConfig.getAppId() + "&secret=" + wechatConfig.getAppSecret();
                ResponseEntity<AccessTokenVO> response = restTemplateConfig.restTemplate()
                        .exchange(url, HttpMethod.GET, null, AccessTokenVO.class);
                accessToken = response.getBody().getAccess_token();
//                AccessToken accessToken2= new AccessToken();
                accessToken1.setAccessToken(accessToken);
                accessToken1.setUpdateDate(LocalDateTime.now());
                accessTokenMapper.updateById(accessToken1);
            }else {
                accessToken = accessToken1.getAccessToken();
            }
        }
        signVO.setAccess_token(accessToken);

        String url1 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accessToken+ "&type=jsapi";
        ResponseEntity<JsSdkVO> response1 = restTemplateConfig.restTemplate()
                .exchange(url1, HttpMethod.GET, null, JsSdkVO.class);
        String ticket = response1.getBody().getTicket();
        signVO.setJsapi_ticket(ticket);
        signVO.setNoncestr("1rtgtafNdHhxADMD");
        signVO.setTimestamp(String.valueOf(System.currentTimeMillis()));
        String signStr = "jsapi_ticket=" + signVO.getJsapi_ticket() + "&noncestr=" + signVO.getNoncestr() + "&timestamp=" + signVO.getTimestamp()
                + "&url=" + signVO.getUrl();

        signVO.setSign(SecretUtils.getSHAString(signStr));
        return ResponseData.generateCreatedResponse(0,signVO);
    }


}