微信公众号获取素材接口调用次数限制的解决办法

问题描述:微信公众号开发 之前每次推送消息都需要调用素材接口,但获取素材接口每天都有次数限制
解决办法:
1.设置定时器 +单例模式
每分钟更新一次素材信息,将素材信息付给单例对象属性(成员)。
定时器:
applicationContext-configuration.xml

   id="matetialTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="wx_job" />
        <property name="targetMethod" value="updateMatetialPro" />
        <property name="concurrent" value="false" />
    
    id="matetialTask_Time" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="matetialTask" />
        <property name="cronExpression" value="0 0/1 * * * ? *" />
    


    id="schdulerFactory" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="stat_cronTrigger" />
                <ref bean="job_cronTrigger" />
                <ref bean="order_cronTrigger" />
                <ref bean="wx_cronTrigger" />
                <ref bean="matetialTask_Time" />
            list>
        property>
    

WxManageAction.java

@Component("wx_job")
public class WxManageAction {
    @Autowired
    private SchedulerFactoryBean schedulerFactory;
    @Autowired
    private IWxTaskService wxTaskService;
    @Autowired
    private WxLoginUtil wxLoginUtil;

    public WxManageAction() {
    }
    public WxManageAction(SchedulerFactoryBean schedulerFactory) {
        this.schedulerFactory = schedulerFactory;
    }
    public void accessToken() {
        System.out.println("更新微信access_token,每小时执行一次:"+new Date());
    /*  WxTask wxTask = this.wxTaskService.getObjByProperty( "name", "access_token");
        String ac=this.wxLoginUtil.getAccess_token();
        wxTask.setValue(ac);
        this.wxTaskService.update(wxTask);*/
    }
    public void updateMatetialPro() {
        System.out.println("更新微信素材,每分钟执行一次:"+new Date());
        //更新
        WxTask wxTask = this.wxTaskService.getObjByProperty( "name", "access_token");
        String ACCESS_TOKEN=wxTask.getValue();
        Matetial.setImage(getmaterial(ACCESS_TOKEN,"image"));
        Matetial.setNews(getmaterial(ACCESS_TOKEN,"news"));
    }

    //获取微信公众号线上素材列表
    public JSONObject getmaterial(String ACCESS_TOKEN, String TYPE){
        String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" +ACCESS_TOKEN;
        String jsonStr ="{\"type\":\""+TYPE+"\",\"offset\":0,\"count\":20}";
        String response = WxLoginUtil.sendPost(jsonStr, url);
        JSONObject jsonObject = JSONObject.fromObject(response);
        System.out.println("获取"+TYPE+"素材列表:"+jsonObject);
        return jsonObject;
    }

}

单例模式:

public class Matetial {
    private static JSONObject news=null;
    private static JSONObject image=null;


    public static void setNews(JSONObject news) {
        Matetial.news = news;
    }
    public static JSONObject getNews() {
        return news;
    }

    public static void setImage(JSONObject image) {
        Matetial.image = image;
    }
    public static JSONObject getImage() {
        return image;
    }


    private Matetial() {}
    private static final  Matetial matetial=new Matetial();

    //静态工厂方法
    public static Matetial getMatetial() {
        return matetial;
    }

}

单例模式测试:

public class MatetialTest {
    public static void main(String[] args) {

        //单例模式测试
        Matetial matetial= Matetial.getMatetial();
        String jsonString = "{\"message\":\"ok\",\"status\":\"1\",\"state\":\"3\",\"data\":[{\"time\":\"2012-07-07 13:35:14\",\"context\":\"客户已签收\"},{\"time\":\"2012-07-07 09:10:10\",\"context\":\"离开 [北京石景山营业厅] 派送中,递送员[],电话[]\"},{\"time\":\"2012-07-06 19:46:38\",\"context\":\"到达 [北京石景山营业厅]\"},{\"time\":\"2012-07-06 15:22:32\",\"context\":\"离开 [北京石景山营业厅] 派送中,递送员[],电话[]\"},{\"time\":\"2012-07-06 15:05:00\",\"context\":\"到达 [北京石景山营业厅]\"},{\"time\":\"2012-07-06 13:37:52\",\"context\":\"离开 [北京_同城中转站] 发往 [北京石景山营业厅]\"},{\"time\":\"2012-07-06 12:54:41\",\"context\":\"到达 [北京_同城中转站]\"},{\"time\":\"2012-07-06 11:11:03\",\"context\":\"离开 [北京运转中心驻站班组] 发往 [北京_同城中转站]\"},{\"time\":\"2012-07-06 10:43:21\",\"context\":\"到达 [北京运转中心驻站班组]\"},{\"time\":\"2012-07-05 21:18:53\",\"context\":\"离开 [福建_厦门支公司] 发往 [北京运转中心_航空]\"},{\"time\":\"2012-07-05 20:07:27\",\"context\":\"已取件,到达 [福建_厦门支公司]\"}]}";
        JSONObject json = JSONObject.fromObject(jsonString);
        matetial.setImage(json);
        Matetial matetial1=Matetial.getMatetial();
        //matetial1.setImage("1a");
        System.out.println(matetial.getImage());
        System.out.println(matetial1.getImage());
        System.out.println(Matetial.getImage());

    }
}

2.使用素材时 直接调用单例对象属性
WechatService.java

 JSONObject jo=Matetial.getNews();

你可能感兴趣的:(项目,MyEclipse)