class Wechat_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->appid = 'wxe5df2d864b88ea30';
$this->secret = '18f5f2800eb418376c0157cc230d9650';
}
/**
*发送微信模板消息
*/
public function tplMsg($uid,$content,$link,$type){
$url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$this->_get_access_token();
$openid = '';
$openid = $this->db->select('openid')->get_where('imt_1_weixin_user',array('uid' => $uid))->row_array()['openid'];
if($openid ==''){
MT_log('模板消息发送失败 '.$uid.' openid 不存在');
MT_log($this->db->last_query());
}
$data ='';
switch ($type) {
case 1:
$tpl = '3St_aO7YnULW0OXpjAus38zrJRjV0EA8H06ckpht3AA';
$data = json_encode(array(
'touser' => $openid,
'template_id' => $tpl,
'url' => $link,
'data' => array(
'first' => array(
'value' => $content[0],
'color' => '#000000',
),
'keyword1' => array(
'value' => $content[1],
'color' => '#666666',
),
'keyword2' => array(
'value' => $content[2],
'color' => '#666666',
),
'keyword3' => array(
'value' => $content[3],
'color' => '#ff8d2d',
),
'keyword4' => array(
'value' => $content[4],
'color' => '#00e267',
),
)
));
break;
case 2:
$tpl = 'tDNfP9rz2vbf2Bh1qaYgADtZKi4aEIRpP1GMdYaXtf8';
$data = json_encode(array(
'touser' => $openid,
'template_id' => $tpl,
'url' => $link,
'data' => array(
'first' => array(
'value' => $content[0],
'color' => '#000000',
),
'keyword1' => array(
'value' => $content[1],
'color' => '#666666',
),
'keyword2' => array(
'value' => $content[2],
'color' => '#666666',
),
'keyword3' => array(
'value' => $content[3],
'color' => '#ff8d2d',
),
'keyword4' => array(
'value' => $content[4],
'color' => '#00e267',
),
'keyword5' => array(
'value' => $content[5],
'color' => '#666666',
),
'remark' => array(
'value' => $content[6],
'color' => '#666666',
),
)
));
break;
default:
// code...
break;
}
$res = json_decode($this->_post($url,$data), true);
if(!$res['errcode']){
return 1;
} else {
MT_log('uid:'.$uid.'|'.dr_array2string($content).'|'.$type.'|'.$res['errcode'].'|'.$res['errmsg']);
return 0;
}
}
protected function _post($url, $params) {
if (function_exists('curl_init')) { // curl方式
$oCurl = curl_init();
if (stripos($url, 'https://') !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
}
$string = $params;
if (is_array($params)) {
$aPOST = array();
foreach ($params as $key => $val){
$aPOST[] = $key.'='.urlencode($val);
}
$string = join('&', $aPOST);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_POST, TRUE);
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $string);
$response = curl_exec($oCurl);
curl_close($oCurl);
return $response;
} elseif (function_exists('stream_context_create')) { // php5.3以上
$opts = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($params),
)
);
$_opts = stream_context_get_params(stream_context_get_default());
$context = stream_context_create(array_merge_recursive($_opts['options'], $opts));
return file_get_contents($url, false, $context);
} else {
return FALSE;
}
}
// 获取公众号access token
protected function _get_access_token() {
$file = WEBPATH.'cache/weixin/access_token.txt';
$data = @json_decode(@file_get_contents($file), true);
if (isset($data['time']) && $data['time'] > SYS_TIME && isset($data['access_token']) && $data['access_token']) {
return $data['access_token'];
}
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->secret;
$data = json_decode(@dr_catcher_data($url), true);
if (!$data) {
@unlink($name);
MT_log('获取access_token失败,请检查服务器是否支持远程链接');
}
if (isset($data['errmsg']) && $data['errmsg']) {
@unlink($name);
MT_log('错误代码('.$data['errcode'].'):'.$data['errmsg']);
}
$data['time'] = SYS_TIME + $data['expires_in'];
@file_put_contents($file, json_encode($data));
return $data['access_token'];
}
}