微信小程序TP3.2模板消息發送

微信小程序模板消息發送

       微信小程序模板消息,在微信小程序中不可缺少的,但是有些時候,一個小程序發送很多不一樣的模板消息,這需要我們封裝,還有發送模板消息,需要formid,prepay_id,他們都是有時間限制,7天過期,不能使用已用的formid,prepay_id,不然發送會報錯。發送失敗;下面是我已經在項目運用的。目前運行穩定,但是記得去除7天過期,因爲項目急,我獲取的formid,prepay_id沒有考慮,希望你在封裝中加上;我是在後臺,處理了。希望注意一下;

        PHP代碼:

 

//一口價發個拍賣者模板消息
    public function Template_message4($shop_list_id,$user_ids,$total_money,$other_openid){
        $Template="***************************";
//        $shop_list_id;
//        $openid//拍賣者
//        $other_openid//用戶
//        $total_money//金額
        if($shop_list_id&&$user_ids&&$other_openid&&$total_money){
            $access_token=M("config")->field("access_token")->where('id=1')->find();
            $ACCESS_TOKEN=$access_token['access_token'];
            $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$ACCESS_TOKEN";
                //拍賣者
                $userinfo = M('user')->field("username,phone,openid,remak_username")->where(array('id'=>$user_ids))->find();//拍賣者
                if($userinfo['remak_username']){
                    $userinfo['username']=$userinfo['remak_username'];
                }
                $other_userinfo = M('user')->field("username,remak_username")->where(array('openid'=>$other_openid))->find();//用戶
                if($other_userinfo['remak_username']){
                    $other_userinfo['username']=$other_userinfo['remak_username'];
                }
                $openid=$userinfo['openid'];
                $form_idd=$this->formId($openid);
                $skill_name=M('shop_list')->field("title")->where('id='.$shop_list_id)->find();
                if($form_idd) {
                    $page = "pages/auctionPriceSummary/auctionPriceSummary?shop_list_id=" . $shop_list_id;
                    $data = array('touser' => $openid,   //拍賣人
                        'template_id' => $Template,   //訂單發貨提醒
                        'page' => $page,
                        'form_id' => $form_idd,
                        'data' => array(
                            'keyword1' => array(
                                'value' => $skill_name['title'],    // 競拍名稱
                                'color' => '#173177'
                            ),
                            'keyword2' => array(
                                'value' => urldecode($other_userinfo['username']),  //競拍人
                                'color' => '#173177'
                            ),
                            'keyword3' => array(
                                'value' => $total_money,  //競拍金額
                                'color' => '#173177'
                            ),
                            'keyword4' => array(
                                'value' => '【'.urldecode($other_userinfo['username']) . '】剛搶拍成功,快去確認發貨吧!',
                                'color' => '#FFA500'
                            )
                        )
                    );
                    $data = json_encode($data);
                    $this->curt($url,$data);
                }
            $form_idds=$this->formId($other_openid);
            $Template_one = "*************************";
            $page_one = "pages/auctionPriceSummary/auctionPriceSummary?shop_list_id=" . $shop_list_id;
            $data_info = array('touser' => $other_openid,   //發給誰(競拍第一名)
                'template_id' => $Template_one,   //訂單發貨提醒
                'page' => $page_one,
                'form_id' => $form_idds,
                'data' => array(
                    'keyword1' => array(
                        'value' => $total_money.'元',    // 成交價
                        'color' => '#173177'
                    ),
                    'keyword2' => array(
                        'value' => $userinfo['phone'],  //賣家電話
                        'color' => '#173177'
                    ),
                    'keyword3' => array(
                        'value' => date("Y-m-d H:i:s"),  //成交時間
                        'color' => '#173177'
                    ),
                    'keyword4' => array(
                        'value' => urldecode($userinfo['username']),//賣家名稱
                        'color' => '#173177'
                    ),
                    'keyword5' => array(
                        'value' => $skill_name['title'],//競拍名稱
                        'color' => '#173177'
                    ),
                    'keyword6' => array(
                        'value' => '你已經搶拍【'.urldecode($userinfo['username']) .'】的【'.$skill_name['title'].'】成功趕緊聯繫賣家',
                        'color' => '#FFA500'
                    )
                )
            );
            $data_info = json_encode($data_info);
            $this->curt($url, $data_info);
        }else{
            return('參數缺失');
        }
    }
//formid,prepay_id的獲取
private  function formId($openid){
    $form_ids = M('form')->field("id,prepay_id,use_num,use_time,is_use")->where(array('prepay_id' => array('neq', ""), 'openid' => $openid, "is_use" => 1, 'use_num' => array('lt', 3)))->find();
    $form_idd = $form_ids['prepay_id'];
    if (!$form_idd) {
        $form_idr = M('form')->field("id,form_id,use_time,is_use")->where(array('form_id' => array('neq', ""), 'openid' => $openid, "is_use" => 1))->order('c_time desc')->find();
        $form_idd = $form_idr['form_id'];
        if ($form_idd) {
            //更改formid
            $timed = time();
            $rowd = M("form")->where("id=" . $form_idr['id'])->save(array("is_use" => 2, "use_time" => $timed));
            if (!$rowd) {
                return('更新失敗1');
            }
            return $form_idd;
        }
    }else{
        $use_num = $form_ids['use_num'] + 1;
        if ($use_num == 3) {
            //三次用完
            $timed = time();
            $row = M("form")->where("id=" . $form_ids['id'])->save(array('use_num' => $use_num, "is_use" => 2, "use_time" => $timed));
        } else {
            $row = M("form")->where("id=" . $form_ids['id'])->save(array('use_num' => $use_num, "use_time" => time()));
        }
        if (!$row) {
            return('更新失敗');
        }
        return $form_idd;
    }
}
  //發送模板消息
    private  function curt($url,$data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
//        if (curl_errno($ch)) {
//            return curl_error($ch);
//        }
        curl_close($ch);
    }
報錯指南:
         40037  	                template_id不正確
      41028	                form_id不正確,或者過期
      41029	                form_id已被使用
      41030	                page不正確
      45009	                接口調用超過限額(目前默認每個帳號日調用限額爲100萬)

有些還可以封裝,寫的不好,可以給我留言,謝謝!

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