NutzCN Logo
问答 请问一下,框架里面在哪里封装获取微信的access_token
发布于 1836天前 作者 qq_91afbf68 1470 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

没有进行过期时间的判断吗?再次进行获取吗?

@IocBean
@At("/open/api/token")
public class TokenController {
    private static final Log log = Logs.get();
    @Inject
    private SysApiService apiService;

    /**
     * @api {post} /open/api/token/get 获取Token
     * @apiGroup Token
     * @apiVersion 1.0.0
     * @apiPermission anyone
     * @apiParam {String}	appId 					appId
     * @apiParam {String}	appSecret 				appSecret
     * @apiParamExample {json} 示例
     * POST /open/api/token
     * {
     *      "appId": "appId",
     *      "appSecret": "appSecret"
     * }
     * @apiSuccess {number} code 			         code
     * @apiSuccess {String} msg 			         msg
     * @apiSuccess {Object[]} data 				数据对象
     * @apiSuccess {number} data.expires 			有效期
     * @apiSuccess {String} data.token 			Token
     * @apiSuccessExample {json} 示例
     * HTTP/1.1 200 OK
     * {
     *      "code": 0,
     *      "msg": "ok",
     *      "data": {
     *          "token": ""eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0IiwiZXhwIjoxNDcwOTA5OTc4fQ._T7egDYhCL27jCvEv4J0cyjRj8s_YLj2gZjjTA8mzk81mTdeM-JXnH7VmtfaenW33BpJJzs2Hs2sXiiNHdzU6Q",
     *          "expires": 7200,
     *      }
     * }
     * @apiError (失败) {number} code 不等于0
     * @apiError (失败) {string} msg 错误文字描述
     * @apiErrorExample {json} 示例
     * HTTP/1.1 200 OK
     * {
     *      "code": 1
     *      "msg": "token invalid"
     * }
     */
    @At("/get")
    @Ok("json")
    public Object get(@Param("appId") String appId, @Param("appSecret") String appSecret) {
        try {
            Sys_api api = apiService.fetch(Cnd.where("appId", "=", appId).and("appSecret", "=", appSecret));
            if (api == null)
                return Result.error("appId or apiSecret error");
            NutMap map = new NutMap();
            Date date = new Date();
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.HOUR, +2);
            date = c.getTime();
            map.addv("expires", 7200);
            map.addv("token", apiService.generateToken(date, appId));
            return Result.success("ok", map);
        } catch (Exception e) {
            log.debug(e.getMessage());
            return Result.error("fail");
        }
    }
}
8 回复

看微信模块, 不是apiService

我就想,在进行群发和上传图片到微信的时候,他能有判断过期了在获取

是这里吗?

@IocBean(args = {"refer:dao"})
public class WxConfigService extends Service<Wx_config> {
    public WxConfigService(Dao dao) {
        super(dao);
    }

    public WxApi2 getWxApi2(String wxid) {
        Wx_config appInfo = this.fetch(Cnd.where("id", "=", wxid));
        DaoAccessTokenStore myDaoAccessTokenStore = new DaoAccessTokenStore(dao());
        Map<String, Object> params = new HashMap<>();
        params.put("id", appInfo.getId());
        myDaoAccessTokenStore.setTableAccessToken("access_token");
        myDaoAccessTokenStore.setTableAccessTokenExpires("access_token_expires");
        myDaoAccessTokenStore.setTableAccessTokenLastat("access_token_lastat");
        myDaoAccessTokenStore.setFetch("select access_token,access_token_expires,access_token_lastat from wx_config where id=@id");
        myDaoAccessTokenStore.setUpdate("update wx_config set access_token=@access_token, access_token_expires=@access_token_expires, access_token_lastat=@access_token_lastat where id=@id");
        myDaoAccessTokenStore.setParams(params);
        WxApi2Impl wxApi2 = new WxApi2Impl();
        wxApi2.setAppid(appInfo.getAppid());
        wxApi2.setAppsecret(appInfo.getAppsecret());
        wxApi2.setEncodingAesKey(appInfo.getEncodingAESKey());
        wxApi2.setToken(appInfo.getToken());
        wxApi2.setAccessTokenStore(myDaoAccessTokenStore);
        return wxApi2;
    }
}

把模块的代码看完

什么时候会获取到新的access_token

WxApi2会自行处理

添加回复
请先登陆
回到顶部