$push_state = $this->senMsg($openid,$templateid,$form_id,$data_arr,$page);
//推送成功拼团模板消息
public function sendOrderMsg($collage_order_id,$state = 1)
{
//取得课程详情
$sql = "select b.course_id,b.course_title,b.pay_price,b.collage_scale from collage_order a left join collage_course b on a.collage_id = b.id where a.id = $collage_order_id";
$search_info = M()->query($sql)[0];
if($search_info){
$course_name = $search_info['course_title'];
$price = $search_info['pay_price'];
$people_count = $search_info['collage_scale'];
if($state == 1){
$data_arr = array(
'keyword1' => array( "value" => $course_name, "color" => ''),
'keyword2' => array( "value" => $price, "color" => ''),
'keyword3' => array( "value" => $people_count, "color" => '')
);
$templateid = 'sPrON_KE2bLohU54isxi44vO0OMZFgjQwANrq2x2yzo';
}else{
$data_arr = array(
'keyword1' => array( "value" => $course_name, "color" => ''),
'keyword2' => array( "value" => "人数不足,拼团失败", "color" => ''),
'keyword3' => array( "value" => $people_count, "color" => '')
);
$templateid = 'mJUpfIf2uc82LradebdXTZ1Z6CJxv0GuZMBi0VUqLM8';
}
$course_id = $search_info['course_id'];
//所有发起拼团的人
$sql = "select id,collage_id,openid,from_id from collage_info where order_id = $collage_order_id and push_msg = 0";
$search = M()->query($sql);
foreach($search as $key=>$value){
$collage_id = $value['collage_id'];
$openid = $value['openid'];
$form_id = $value['from_id'];
$create_time = $value['createTime'];
$arr_create_time = array('keyword4'=>array( "value" => $create_time, "color" => ''));
$data_arr = array_merge($data_arr,$arr_create_time);
$page = "pages/collagedetails/collagedetails?collage_id=$collage_id&course_id=$course_id&order_id=$collage_order_id";
$push_state = $this->senMsg($openid,$templateid,$form_id,$data_arr,$page);
if($push_state){
$id = $value['id'];
$sql = "update collage_info set push_msg = 1 where id = $id,page='$page'";
M()->execute($sql);
}
}
}
}
//小程序推送消息
public function senMsg($openids,$templateid,$formid,$data_arr,$page)
{
// 这里的返回值是一个 JSON,可通过 json_decode() 解码成数组
$data = $this->getPostData($openids,$templateid,$formid,$data_arr,$page);
$smallwx_token = M('smallwx_token')->find();
$access_token =$smallwx_token['access_token'];
$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token;
$return = $this->send_post( $url, $data);
$state = json_decode($return,true);
if($state['errmsg'] == 'ok'){
return true;
}else{
return false;
}
}
public function getPostData($openids,$templateid,$formid,$data_arr,$page='')
{
$post_data = array (
// 用户的 openID,可用过 wx.getUserInfo 获取
"touser" => $openids,
// 小程序后台申请到的模板编号
"template_id" => $templateid,
// 点击模板消息后跳转到的页面,可以传递参数
"page" => $page,//"/pages/check/result?orderID=".$orderID,
// 第一步里获取到的 formID
"form_id" => $formid,
// 数据
"data" => $data_arr,
// 需要强调的关键字,会加大居中显示
"emphasis_keyword" => "keyword2.DATA"
);
// 将数组编码为 JSON
$data = json_encode($post_data, true);
return $data;
}
function send_post( $url, $post_data ) {
$options = array(
'http' => array(
'method' => 'POST',
// header 需要设置为 JSON
'header' => 'Content-type:application/json',
'content' => $post_data,
// 超时时间
'timeout' => 60
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
return $result;
}