微信小程序轉發動態消息的問題

最近項目中需要用到微信小程序轉發動態消息的功能,遇到了一些坑,最終測試成功了,現在分享給大家。

微信官方的動態消息參考文檔在這裏:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share/updatable-message.html

一,實現效果

轉發小程序之後可以看到:"成員正在加入,當前 {member_count}/{room_limit} 人"動態效果。

  二,測試環境

       1)微信基礎庫爲2.6.6,在微信開發工具中詳情欄目可以設置

2)轉發動態消息的效果必須使用真機才能看到,模擬器看不到提示信息。

三,參數準備

wx.updateShareMenu({
      withShareTicket: true,
      isUpdatableMessage: true,
      activityId: '你的activityid', // 活動 ID
      targetState: 0,
      templateInfo: {
        parameterList: [{
          name: 'member_count',
          value: '3'
        }, {
          name: 'room_limit',
          value: '5'
        }]
      }
    });

當targetState設置爲0時,必須填member_counth和room_limit的值。

activityId的值需要通過調用後臺接口得到,activityid的默認有效時間是24小時。現在給出我的實現代碼

    /**
     * 獲取Access_token
     * @return
     * @throws Exception
     * @throws IOException
     */
    public String getAccess_token()throws Exception,
            IOException {

        HttpGet httpGet = new HttpGet(
                "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
                        + appid + "&secret="
                        + appsecret);
        HttpClient httpClient = HttpClients.createDefault();
        HttpResponse res = httpClient.execute(httpGet);
        HttpEntity entity = res.getEntity();
        String result = EntityUtils.toString(entity, "UTF-8");
        JSONObject jsons = JSONObject.parseObject(result);///fromObject(result);
        String expires_in = jsons.getString("expires_in");

        //緩存
        String access_token = null;
        if (Integer.parseInt(expires_in) == 7200) {
            //ok
            access_token = jsons.getString("access_token");

        } else {
            System.out.println("出錯獲取token失敗!");
            logger.info("出錯獲取token失敗!");
        }

        System.out.println("access_token:"+access_token);
        return access_token;
    }


    /**
     * 獲取Activity_id
     * @return
     * @throws Exception
     * @throws IOException
     */
    @RequestMapping("getActivity_id")
    //返回json格式,
    @ResponseBody
    public String getActivity_id()throws Exception,
    IOException{
        String access_token = this.getAccess_token();
        HttpGet httpGet = new HttpGet(
                "https://api.weixin.qq.com/cgi-bin/message/wxopen/activityid/create?access_token="
                        + access_token );
        HttpClient httpClient = HttpClients.createDefault();
        HttpResponse res = httpClient.execute(httpGet);
        HttpEntity entity = res.getEntity();
        String result = EntityUtils.toString(entity, "UTF-8");
        JSONObject jsons = JSONObject.parseObject(result);///fromObject(result);
        String errcode = jsons.getString("errcode");

        //緩存
        String activity_id = null;
        if (Integer.parseInt(errcode) == 0) {
            //ok
            activity_id = jsons.getString("activity_id");

        } else{
            System.out.println("出錯獲取activity_id失敗!errcode:"+errcode);
            logger.info("出錯獲取activity_id失敗!errcode:"+errcode);
        }
        System.out.println("activity_id:"+activity_id);
        return activity_id;
    }

現在就可以上傳一個體驗版測試一下了,在手機上轉發給微信好友應該可以看到前面的效果。

如果這是在服務器調用下面的代碼,activityid和access_token要換成剛纔小程序裏面使用的

curl -d '{"activity_id": "966_NGiqxxxxxxxxx...xxxxxxxxE33BlwX", "target_state": 0, "template_info": {"parameter_list": [{"name": "member_count", "value": "2"}, {"name":"room_limit", "value": "6"} ] } }' \
'https://api.weixin.qq.com/cgi-bin/message/wxopen/updatablemsg/send?access_token=ACCESS_TOKEN'

這是就會發現,手機裏面剛剛轉發的提示信息內容改變了。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章