微信小程序用戶推送消息

小程序頁面按鈕
<contact-button class='cBtn' type="default-dark" size="20" session-from="webApp" bindtap='sendMsg'>
</contact-button>
小程序js
  /**
   * 小程序向用戶發送消息
   */
  sendMsg:function(){
    var that = this;
    wx.request({
      url: "http://www.kuzhazha.com/xcx/message/sendNfTz",
      data: {
        openId: that.data.openId,
        SessionFrom: '20優惠券',
        describe: '祛眼袋黑眼圈除皺,安神助眠,配usb充電線,每天15分鐘眼部恆溫45度SPA護理 睛彩人生,從護眼開始,送運費險,退換無憂',
        conUrl: 'http://coupon.m.jd.com/coupons/show.action?key=2ce8e8bb396e4a038ec33beaf3a9a076&roleId=11078046&to=item.jd.com/11203771866.html',
        goodsImg: 'http://img.jingtuitui.com/d9b93201803301115301252.jpg?imageView2/2/w/260/h/260/q/100/imageMogr2/strip/format/jpg'
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        console.log(res)
      }
    })
  },
java 後臺代碼
 @RequestMapping("/sendNfTz")
 public static void sendNfTz(String openId ,String SessionFrom,String describe,String conUrl,String goodsImg){  
        
        AccessToken at = WeixinUtil.getAccessToken(APP_ID, SECRET);
        
        String access_token = at.getToken();
        
        String strJson = "{\"touser\" :\""+openId+"\",";
        strJson += "\"msgtype\":\"link\",";
        strJson += "\"link\":{";
        strJson += "\"title\":\""+SessionFrom+"\",";
        strJson += "\"description\":\""+describe+"\",";
        strJson += "\"url\":\""+conUrl+"\",";
        strJson += "\"thumb_url\":\""+goodsImg+"\",";
        strJson += "}}";
        String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?&body=0&access_token=" + access_token;
        
        //URLConnectionHelper是一個模擬發送http請求的類
        JSONObject jsonObject = WeixinUtil.httpRequest(url, "POST", strJson); 
        
        int result = 0;
        if (null != jsonObject) {  
             if (0 != jsonObject.getInt("errcode")) {  
                 result = jsonObject.getInt("errcode");  
                 System.out.println("錯誤 errcode:{} errmsg:{}"+jsonObject.getInt("errcode")+jsonObject.getString("errmsg"));  
             }  
         }
        System.out.println("模板消息發送結果:"+result);  
    }
 

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