Onex ecstore 微信扫码登录
微信扫描二维码登录网站是微信开放平台下网站应用的一种接口实现的功能。微信开放平台的网址是 https://open.weixin.qq.com
//需在 app/trustlogin/lib/plugin/weixin.php function get_logo 修改
public function get_logo()
{
$_SESSION['weixinst'] = md5(uniqid(rand(), TRUE));
$status = app::get('trustlogin')->getConf('trustlogin_plugin_weixin');
$data['status'] = $status['status'];
$data['image'] = $this->app->res_url.'/weixin.png';
$data['url'] = $this->dialog_url.'?appid='.$this->get_appkey()."&redirect_uri=" . urlencode($this->my_url)."&response_type=code&scope=snsapi_login&state=".$_SESSION['weixinst']."#wechat_redirect";
return $data;
}
//配置微信开发平台 配置信任登录 见 https://jinjiajin.cn/1321.html
//需在 app/trustlogin/lib/plugin/weixin.php 添加回调方法里处理。
public function callback($data)
{
if($data['state'] == $_SESSION['weixinst'])
{
$token_url = $this->token_url."?grant_type=authorization_code&"
."appid=".$this->get_appkey()."&secret=".$this->get_appSecret()."&code=".$data['code'];
$response = file_get_contents($token_url);
$result = json_decode($response,true);
if (isset($result->errcode))
{
echo "
error:
" . $result->errcode;
echo "
msg :
" . $result->errmsg;
exit;
}
//通过接口获取用户信息
$userinfo_url = $this->user_url."?access_token=".$result['access_token']."&openid=".$result['openid'];
$info = file_get_contents($userinfo_url);
$userinfo = json_decode($info,true);
//echo '
';print_r($userinfo);exit();
$userdata = $this->getUserInfo($userinfo);
#是否已绑定
$this->checkWeixinBind($userdata);
$datainfo = array(
'rsp'=>'succ',
'data'=>$userdata,
'type'=>'pc',
);
return $datainfo;
}
else
{
echo("The state does not match. You may be a victim of CSRF.");
}
}
//需在 app/trustlogin/lib/plugin/weixin.php 添加如下几个方法。
public function checkWeixinBind($wxUser)
{
$bindObj=app::get('pam')->model('bind_tag');
$bind_tag = $bindObj->getRow('*',array('open_id'=>$wxUser['openid'],'tag_type'=>'weixin'));
if($bind_tag){
#防止该会员被删除了-查询确认下
$sql="select sbm.member_id,spm.login_account from `sdb_pam_members` as spm left join `sdb_b2c_members` as sbm on spm.member_id=sbm.member_id where sbm.member_id='{$wxUser['member_id']}' and spm.login_type='local'";
$memInfo = kernel::database()->selectrow($sql);
if($memInfo){
$member_id=$memInfo['member_id'];
$logname=$memInfo['login_account'];
}else{//不存在自动生成会员
$member=$this->save_member($wxUser,$bind_tag);
$member_id=$bind_tag['member_id'];
$logname=$member['login_account'];
}
}else{
$member=$this->save_member($wxUser);
$member_id=$member['member_id'];
$logname=$member['login_account'];
}
$logname=kernel::single('b2c_user_object')->get_member_name(false,$member_id);
$this->send_weixin_message($wxUser,$logname);
kernel::single('b2c_user_object')->set_member_session($member_id);
kernel::single('b2c_frontpage')->bind_member($member_id);
kernel::single('b2c_frontpage')->set_cookie('loginName',$logname,time()+31536000);//用于记住用户名
}
public function save_member($wxUser,$user=array()){
//获取会员默认等级
$def_lv=app::get('b2c')->model('member_lv')->get_default_lv();
//===================会员表新增=====================================
//根据openID获取登录的用户名
$members = array(
'name'=>$wxUser['nickname'],//此处用微信名称
'regtime' =>time(),
'source' =>'weixin',
'sex' =>$wxUser['sex'],
'reg_ip' =>base_request::get_remote_addr(),
'member_lv_id' =>$def_lv?$def_lv:1,
'weixin_openid'=>$wxUser['openid']
);
//echo '';var_dump($members,$user);exit;
if(!empty($user)){
$members['member_id']=$user['member_id'];
app::get('b2c')->model('members')->save($members);
$memId = $user['member_id'];
}else{
$memId = app::get('b2c')->model('members')->insert($members);
}
// $memId = app::get('b2c')->model('members')->insert($members);
//===================会员登陆表新增=====================================
$login_name=$this->get_login_name($wxUser['openid']);
$use_pass_data['login_name'] =$login_name; //会员登录账号
$log_password=$login_name;//此处用户名和密码均相同
$use_pass_data['createtime'] =time();
$login_password = pam_encrypt::get_encrypted_password(trim($log_password),'member',$use_pass_data);
$pamArr=array(
'member_id' =>$memId,
'login_account'=>$use_pass_data['login_name'],
'login_password'=>$login_password,
'password_account'=>$use_pass_data['login_name'],
'createtime'=>$use_pass_data['createtime'],
'login_type'=>'local'
);
//echo '';var_dump($pamArr,$user);exit;
//pam-members新增
app::get('pam')->model('members')->save($pamArr);
//===================绑定微信信任登陆表新增=====================================
if(empty($user)){
$bindArr=array(
'member_id' =>$memId,
'open_id'=>$wxUser['openid'],
'tag_name'=>$wxUser['nickname'],
'disabled'=>'false',
'createtime'=>$use_pass_data['createtime'],
'tag_type'=>'weixin'
);
app::get('pam')->model('bind_tag')->save($bindArr);
}
return $pamArr;
}
//获取access_token
public function get_weixin_access_token()
{
$wxBind=app::get('weixin')->model('bind');
$weixin=$wxBind->getList('appid,appsecret',array('weixin_type'=>'service'));
$wxSet=$weixin[0];
// echo '';var_dump($wxSet);exit;
$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$wxSet['appid'].'&secret='.$wxSet['appsecret'];
$res=$this->sendGet($url);
// echo '';var_dump($res,$url);exit;
$result = json_decode($res,true);
if($result['access_token']){
//echo ''; var_dump($result);exit;
return $result['access_token'];
}else{
return false;
}
}
//发送get请求
public function sendGet($url) {
if(function_exists('file_get_contents')) {
$_res = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$_res = curl_exec($ch);
curl_close($ch);
}
return $_res;
}
//发送微信用户名和密码
public function send_weixin_message($wxUser,$userName){
$access_token=$this->get_weixin_access_token();
// echo '';var_dump($wxUser,$userName);exit;
if($access_token){
$url='https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$access_token;
$params['access_token']=$access_token;
$params['touser']=$wxUser['openid'];
$content="恭喜您在XXXX官网已关联账号!初始化用户名和密码如下:\n用户名:".$userName."\n密码:".$userName."\n\n".'【点击登陆】';
$params['text']['content']=urlencode($content);
$params['msgtype']='text';
$params=urldecode(json_encode($params));
$res=$this->sendByPost($url,$params);
$result = json_decode($res, true);
}
}
public function sendByPost($url,$postdata){
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postdata );
$return = curl_exec ( $ch );
curl_close ( $ch );
return $return;
}