微信公衆號獲取素材接口調用次數限制的解決辦法

問題描述:微信公衆號開發 之前每次推送消息都需要調用素材接口,但獲取素材接口每天都有次數限制
解決辦法:
1.設置定時器 +單例模式
每分鐘更新一次素材信息,將素材信息付給單例對象屬性(成員)。
定時器:
applicationContext-configuration.xml

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


    <bean 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>
    </bean>

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();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章