3,143
社区成员
发帖
与我相关
我的任务
分享
public function joinUs(){
if(!is_weixin()) return false;
$wechat_info = $this->winfo;
$w_id = getWid();
if($wechat_info['w_auth']!=1){
//代理官方认证号
if(SITEDOMAIN!='www.'.C('officialDomain')){
$off_wid = D('User')->findProxyAuthen($w_id);
if(!$off_wid) return false;
$wechat_info = D('Wechat')->getWechatCache($off_wid);
}else{
//官方认证号
$wechat_info = M('wechat_extend')->where(array('lower(w_weixin)'=>C('officialWx')))->find();
}
}
if($_GET['code']){
$cfg['appid'] = $wechat_info['w_appid'];
$cfg['secret'] = $wechat_info['w_secret'];
$s = json_decode(getRemoteContent('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$cfg['appid'].'&secret='.$cfg['secret'].'&code='.$_GET['code'].'&grant_type=authorization_code'),true);
$urs = json_decode(getRemoteContent('https://api.weixin.qq.com/sns/userinfo?access_token='.$s['access_token'].'&openid='.$s['openid'].'&lang=zh_CN'),true);
if($urs['openid']){
$r = M('wechat_user');
$uid = getUid();
$nickname = filterNickName($urs['nickname']);
//当前公众号入库数据补充
$sfile = './Public/Uploads/weixin/avatar/'.md5($urs['openid']).'.jpg';
$data['headimgurl'] = D('WechatUser')->getAvatar($urs['headimgurl'],$sfile);
$data = array('nickname'=>$nickname,'headimgurl'=>$sfile,'sex'=>$urs['sex'],'country'=>$urs['country'],'province'=>$urs['province'],'city'=>$urs['city']);
M('wechat_user')->where(array('id'=>$uid))->save($data);
if($uid && $nickname && $nickname!='网友'){
$this->assign('msg','授权完成');
}elseif(!$uid){
$this->assign('msg','用户信息未入库,请检查公众号配置参数是否正确');
}else{
$this->assign('msg','很遗憾,请确认您的微信是否设置了昵称');
}
$this->display('index');
}else{
$this->assign('msg','授权失败');
$this->display('index');
}
}else{
$this->assign('msg','您取消了授权');
$this->display('index');
}
}