/**
* @explain
* 发送消息通知
* @return array|mixed
* @remark
* 获取到用户的openid之后可以判断用户是否有数据,可以直接跳过获取access_token,也可以继续获取access_token
* access_token每日获取次数是有限制的,access_token有时间限制,可以存储到数据库7200s. 7200s后access_token失效
*
*/
function sendMsg($tem_id,$data,$openid,$return_url='')
{
if($tem_id == ''){
$tem_id = "7wuhorQScRZHbn_NpPWlR4uoCN4CcZKtxEL_hwnNVZI";
}
$return_url = "https://".$_SERVER['HTTP_HOST'].'/h5/#/'; // 你的消息详情页面,如果为空。点进去之后是空白页面
//获取access_token
$access_token = \think\Cache::get('ACCESS_TOKEN');
if (!$access_token||empty($access_token)){
$WechatLogin=config('WechatLogin');
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$appid = $WechatLogin['appid'];
$secret = $WechatLogin['appsecret'];
//获取ACCESS_TOKEN
$url= "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
if (function_exists('file_get_contents')){
$access_token = file_get_contents($url, false, stream_context_create($arrContextOptions));
}else{
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$access_token = curl_exec($ch);
curl_close($ch);
}
$access = json_decode($access_token,true);
$access_token = $access['access_token'];
\think\Cache::set('ACCESS_TOKEN',$access_token,'7000');
}
if($access_token){
$params1 = json_encode(json_tempalte($openid,$data,$return_url,$tem_id),JSON_UNESCAPED_UNICODE);
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
$params = https_request($url,urldecode($params1));
$params = json_decode($params,true);

if ($params['errcode'] == '40001'){
\think\Cache::set('ACCESS_TOKEN','');
sendMsg($tem_id,$data,$openid,$return_url);
}

if ($params['errcode']==0){
return true;
}else{
return false;
}
}else{
return false;
}
}