拿到code,获取openid,并入库
public function getcode(){
$appid=C('APPID');
$redirect_uri=urlencode("http://newoa.jd61.com/weix/getuserinfo");
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
header("location:".$url);
}
public function getuserinfo(){
header("Content-Type: text/html; charset=utf-8");
$appid = C('APPID');
$secret = C('APPSECRET');
//这里获取到了code
$code = $_GET['code'];
//第一步:取得openid
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
$oauth2 = $this->http_curl($oauth2Url);
//accestoken
$access_token = $oauth2["access_token"];
$openid = $oauth2['openid'];
//第二步:根据全局access_token和openid查询用户信息
$get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
$userinfo = $this->http_curl($get_user_info_url);
$data['openid'] = $oauth2['openid'];
$data['userid'] = $_SESSION['aid'];
$res = M('wei_user')->where($data)->find();
if(!$res){
M('wei_user')->add($data);
echo '成功';
}else{
echo '已存在';
}
}
function http_curl($url){
//用curl传参
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//关闭ssl验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch,CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
发送微信模板消息
public function pushMsg(){
$appid = C('APPID');
$secret = C('APPSECRET');
$info = new \Org\MyClass\OrderPush($appid,$secret);
$get_data = I('get.');
$data =array(
'first'=>array('value'=>'您好,'.$get_data['adTitle'].'广告任务关键词已有更新','color'=>"#222"),
'keyword1'=>array('value'=>'关键词:'.$get_data['keyword'].';'.$get_data['planNum'].'量级','color'=>'#222'),
'keyword2'=>array('value'=>$get_data['planType'],'color'=>'#222'),
'keyword3'=>array('value'=>date('Y-m-d H:i:s',time()),'color'=>'#222'),
'remark'=>array('value'=>'目前排名第'.$get_data['rank'],'color'=>'#222'),
);
$tenpalate_id = "Hfj3-LO1KUvc2VivswMvEVksaM8nd0sESuesUjZwMBM";
$openid = empty(trim(I('openid')))? 'o6mViw5yGcFkMCa74Y0IxMi6lrEo' : trim(I('openid'));
$url = 'http://'.$_SERVER['SERVER_NAME'].U('Index/index');
$result = $info->doSend($openid,$tenpalate_id,'',$data);
if($result){
$this->ajaxReturn(array('code' => 1,'msg' => 'success'));
}else{
$this->ajaxReturn(array('code' => 0,'msg' => 'error'));
}
}
还要获取到access_token,我们才能发送模板消息
/**
* @param $appid
* @param $appsecret
* @return mixed
* 获取token
*/
protected function getToken($appid, $appsecret)
{
if (S($appid)) {
$access_token = S($appid);
} else {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
$token = $this->request_get($url);
$token = json_decode(stripslashes($token));
$arr = json_decode(json_encode($token), true);
$access_token = $arr['access_token'];
S($appid, $access_token, 720);
}
return $access_token;
}
/**
* 发送自定义的模板消息
* @param $touser
* @param $template_id
* @param $url
* @param $data
* @param string $topcolor
* @return bool
*/
public function doSend($touser, $template_id, $url, $data, $topcolor = '#7B68EE')
{
/*
* data=>array(
'first'=>array('value'=>urlencode("您好,您已购买成功"),'color'=>"#743A3A"),
'name'=>array('value'=>urlencode("商品信息:微时代电影票"),'color'=>'#EEEEEE'),
'remark'=>array('value'=>urlencode('永久有效!密码为:1231313'),'color'=>'#FFFFFF'),
)
*/
$template = array(
'touser' => $touser,
'template_id' => $template_id,
'url' => $url,
'topcolor' => $topcolor,
'data' => $data
);
$json_template = json_encode($template);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->accessToken;
$dataRes = $this->request_post($url, urldecode($json_template));
if ($dataRes['errcode'] == 0) {
return true;
} else {
return false;
}
}