您好,登錄后才能下訂單哦!
本篇內容介紹了“phpcms如何實現微信登陸”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
phpcms實現微信登錄的方法:1、在根目錄新建“wechat.php”;2、在“\phpcms\modules\member\index.php”下增加“public function wechat() {...}”;3、在“foreground.class.php”文件中通過wechat函數判斷用戶是否登錄即可。
phpcms實現微信登陸(無需注冊,直接存入)
思路:回調參數,直接register (代碼不夠規范,自己整理)
在根目錄新建wechat.php
訪問: http://www.xxxxxx.cn/wechat.php
注意回調地址
phpcms實現微信登陸(無需注冊,直接存入)
wechat.php
<?php
if(!empty( $_GET['code']) && !empty( $_GET['state'])){
$code = $_GET['code'];
$state = $_GET['state'];
$url = "http://www.xxxxxx.cn/index.php?m=member&c=index&a=wechat&code=$code&state=$state";
header('location:'.$url);
exit;
}
$appid = 'wx5a3878682fa32bd5';
$url = "https://open.weixin.qq.com/connect/qrconnect?appid=$appid&redirect_uri=http://www.xxxxxx.cn/wechat.php&response_type=code&scope=snsapi_login&state=1&connect_redirect=1#wechat_redirect";
header('location:'.$url);
?>
\phpcms\modules\member\index.php下增加
注:注冊狀態輸出 $status, 可以返回錯誤信息 根據錯誤信息修改參數內容
用戶名稱和密碼做了簡單區別處理,根據實際情況修改,郵箱為必須字段,隨便放了一個。
<?php
public function wechat() {
$this->_session_start();
//獲取用戶siteid
$siteid = isset($_REQUEST['siteid']) && trim($_REQUEST['siteid']) ? intval($_REQUEST['siteid']) : 1;
//定義站點id常量
if (!defined('SITEID')) {
define('SITEID', $siteid);
}
//加載用戶模塊配置
//加載用戶模塊配置
$member_setting = getcache('member_setting');
if(!$member_setting['allowregister']) {
showmessage(L('deny_register'), '/login.html');
}
/*----------------------微信獲取用戶信息------------------------*/
//換成自己的接口信息
$code = $_GET['code'];
$state = $_GET['state'];
$appid = 'xxxxxxxx';
$appsecret = 'xxxxxxxx';
if (empty($code)) $this->error('授權失敗');
$token_url = 'https://api.weixin.qq.com/sns/oauth3/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
$token = json_decode(file_get_contents($token_url));
if (isset($token->errcode)) {
showmessage(L('<br/><h3>錯誤信息:</h3>'.$token->errmsg), HTTP_REFERER);
exit;
}
$access_token_url = 'https://api.weixin.qq.com/sns/oauth3/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
//轉成對象
$access_token = json_decode(file_get_contents($access_token_url));
if (isset($access_token->errcode)) {
showmessage(L('<br/><h3>錯誤信息:</h3>'.$access_token->errmsg), HTTP_REFERER);
exit;
}
$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';
//轉成對象
$user_info = json_decode(file_get_contents($user_info_url));
if (isset($user_info->errcode)) {
showmessage(L( '<br/><h3>錯誤信息:</h3>'.$user_info->errmsg), HTTP_REFERER);
exit;
}
$rs = json_decode(json_encode($user_info),true);//轉換成數組
/*------------------------獲取用戶信息代碼結束-----------------------*/
header("Cache-control: private");
$checkname = trim($rs['nickname'])."_".substr(md5($rs['unionid']),0,5) ;
$this->_init_phpsso();
$cstatus = $this->client->ps_checkname($checkname);
//如果存在用戶 用戶登陸
if($cstatus == -4 || $cstatus == -1) {
$username = $rs['nickname']."_".substr(md5($rs['unionid']),0,5) ;
$password = substr(md5($rs['unionid']),0,10);
//is_password($_POST['password']) && is_badword($_POST['password'])==false ? trim($_POST['password']) : showmessage(L('password_format_incorrect'), HTTP_REFERER);
$cookietime = intval($_POST['cookietime']);
$synloginstr = ''; //同步登陸js代碼
if(pc_base::load_config('system', 'phpsso')) {
$this->_init_phpsso();
$status = $this->client->ps_member_login($username, $password);
$memberinfo = unserialize($status);
if(isset($memberinfo['uid'])) {
//查詢帳號
$r = $this->db->get_one(array('phpssouid'=>$memberinfo['uid']));
if(!$r) {
//插入會員詳細信息,會員不存在 插入會員
$info = array(
'phpssouid'=>$memberinfo['uid'],
'username'=>$memberinfo['username'],
'password'=>$memberinfo['password'],
'encrypt'=>$memberinfo['random'],
'email'=>$memberinfo['email'],
'regip'=>$memberinfo['regip'],
'regdate'=>$memberinfo['regdate'],
'lastip'=>$memberinfo['lastip'],
'lastdate'=>$memberinfo['lastdate'],
'groupid'=>$this->_get_usergroup_bypoint(), //會員默認組
'modelid'=>10, //普通會員
);
//如果是connect用戶
if(!empty($_SESSION['connectid'])) {
$userinfo['connectid'] = $_SESSION['connectid'];
}
if(!empty($_SESSION['from'])) {
$userinfo['from'] = $_SESSION['from'];
}
unset($_SESSION['connectid'], $_SESSION['from']);
$this->db->insert($info);
unset($info);
$r = $this->db->get_one(array('phpssouid'=>$memberinfo['uid']));
}
$password = $r['password'];
$synloginstr = $this->client->ps_member_synlogin($r['phpssouid']);
} else {
if($status == -1) { //用戶不存在
showmessage(L('user_not_exist'), '/login.html');
} elseif($status == -2) { //密碼錯誤
showmessage(L('password_error'), '/login.html');
} else {
showmessage(L('login_failure'), '/login.html');
}
}
} else {
//密碼錯誤剩余重試次數
$this->times_db = pc_base::load_model('times_model');
$rtime = $this->times_db->get_one(array('username'=>$username));
if($rtime['times'] > 4) {
$minute = 60 - floor((SYS_TIME - $rtime['logintime']) / 60);
showmessage(L('wait_1_hour', array('minute'=>$minute)));
}
//查詢帳號
$r = $this->db->get_one(array('username'=>$username));
if(!$r) showmessage(L('user_not_exist'),'/login.html');
//驗證用戶密碼
$password = md5(md5(trim($password)).$r['encrypt']);
if($r['password'] != $password) {
$ip = ip();
if($rtime && $rtime['times'] < 5) {
$times = 5 - intval($rtime['times']);
$this->times_db->update(array('ip'=>$ip, 'times'=>'+=1'), array('username'=>$username));
} else {
$this->times_db->insert(array('username'=>$username, 'ip'=>$ip, 'logintime'=>SYS_TIME, 'times'=>1));
$times = 5;
}
showmessage(L('password_error', array('times'=>$times)), '/login.html', 3000);
}
$this->times_db->delete(array('username'=>$username));
}
//如果用戶被鎖定
if($r['islock']) {
showmessage(L('user_is_lock'));
}
$userid = $r['userid'];
$groupid = $r['groupid'];
$username = $r['username'];
$nickname = empty($r['nickname']) ? $username : $r['nickname'];
$updatearr = array('lastip'=>ip(), 'lastdate'=>SYS_TIME);
//vip過期,更新vip和會員組
if($r['overduedate'] < SYS_TIME) {
$updatearr['vip'] = 0;
}
//檢查用戶積分,更新新用戶組,除去郵箱認證、禁止訪問、游客組用戶、vip用戶,如果該用戶組不允許自助升級則不進行該操作
if($r['point'] >= 0 && !in_array($r['groupid'], array('1', '7', '8')) && empty($r[vip])) {
$grouplist = getcache('grouplist');
if(!empty($grouplist[$r['groupid']]['allowupgrade'])) {
$check_groupid = $this->_get_usergroup_bypoint($r['point']);
if($check_groupid != $r['groupid']) {
$updatearr['groupid'] = $groupid = $check_groupid;
}
}
}
//如果是connect用戶
if(!empty($_SESSION['connectid'])) {
$updatearr['connectid'] = $_SESSION['connectid'];
}
if(!empty($_SESSION['from'])) {
$updatearr['from'] = $_SESSION['from'];
}
unset($_SESSION['connectid'], $_SESSION['from']);
$this->db->update($updatearr, array('userid'=>$userid));
if(!isset($cookietime)) {
$get_cookietime = param::get_cookie('cookietime');
}
$_cookietime = $cookietime ? intval($cookietime) : ($get_cookietime ? $get_cookietime : 0);
$cookietime = $_cookietime ? SYS_TIME + $_cookietime : 0;
$phpcms_auth = sys_auth($userid."\t".$password, 'ENCODE', get_auth_key('login'));
param::set_cookie('auth', $phpcms_auth, $cookietime);
param::set_cookie('_userid', $userid, $cookietime);
param::set_cookie('_username', $username, $cookietime);
param::set_cookie('_groupid', $groupid, $cookietime);
param::set_cookie('_nickname', $nickname, $cookietime);
//print_r($_COOKIE);
//exit;
//param::set_cookie('cookietime', $_cookietime, $cookietime);
//https 與 http 傳遞用戶名 跨域跳轉cooike
$forward = isset($_POST['forward']) && !empty($_POST['forward']) ? urldecode($_POST['forward']) : 'index.php?m=member&c=index';
$url = "http://www.zhiliaoke.com.cn/set_cookie_www.php?nickname=".$_COOKIE['vuGYB__nickname']."&username=".$_COOKIE['vuGYB__username']."&userid=".$_COOKIE['vuGYB__userid']."&groupid=".$_COOKIE['vuGYB__groupid'];
header("Location: ".$url."");
}
//如果不存在用戶注冊
$userinfo = array();
$userinfo['encrypt'] = create_randomstr(6);
$userinfo['username'] = $rs['nickname']."_".substr(md5($rs['unionid']),0,5) ;
$userinfo['nickname'] = $rs['nickname']."_".substr(md5($rs['unionid']),0,5) ;
$userinfo['email'] = time().'@yangpeili.com';
$userinfo['password'] = substr(md5($rs['unionid']),0,10);
$userinfo['modelid'] = isset($_POST['modelid']) ? intval($_POST['modelid']) : 10;
$userinfo['regip'] = ip();
$userinfo['point'] = $member_setting['defualtpoint'] ? $member_setting['defualtpoint'] : 0;
$userinfo['amount'] = $member_setting['defualtamount'] ? $member_setting['defualtamount'] : 0;
$userinfo['regdate'] = $userinfo['lastdate'] = SYS_TIME;
$userinfo['siteid'] = $siteid;
$userinfo['connectid'] = isset($_SESSION['connectid']) ? $_SESSION['connectid'] : '';
$userinfo['from'] = isset($_SESSION['from']) ? $_SESSION['from'] : '';
//手機強制驗證
//附表信息驗證 通過模型獲取會員信息
if($member_setting['choosemodel']) {
require_once CACHE_MODEL_PATH.'member_input.class.php';
require_once CACHE_MODEL_PATH.'member_update.class.php';
$member_input = new member_input($userinfo['modelid']);
$_POST['info'] = array_map('new_html_special_chars',$_POST['info']);
$user_model_info = $member_input->get($_POST['info']);
}
if(pc_base::load_config('system', 'phpsso')) {
$this->_init_phpsso();
$status = $this->client->ps_member_register($userinfo['username'], $userinfo['password'], $userinfo['email'], $userinfo['regip'], $userinfo['encrypt']);
echo $status;
if($status > 0) {
$userinfo['phpssouid'] = $status;
//傳入phpsso為明文密碼,加密后存入phpcms_v9
$password = $userinfo['password'];
$userinfo['password'] = password($userinfo['password'], $userinfo['encrypt']);
$userid = $this->db->insert($userinfo, 1);
if($member_setting['choosemodel']) { //如果開啟選擇模型
$user_model_info['userid'] = $userid;
//插入會員模型數據
$this->db->set_model($userinfo['modelid']);
$this->db->insert($user_model_info);
}
if($userid > 0) {
//執行登陸操作
if(!$cookietime) $get_cookietime = param::get_cookie('cookietime');
$_cookietime = $cookietime ? intval($cookietime) : ($get_cookietime ? $get_cookietime : 0);
$cookietime = $_cookietime ? TIME + $_cookietime : 0;
if($userinfo['groupid'] == 7) {
param::set_cookie('_username', $userinfo['username'], $cookietime);
param::set_cookie('email', $userinfo['email'], $cookietime);
} else {
$phpcms_auth = sys_auth($userid."\t".$userinfo['password'], 'ENCODE', get_auth_key('login'));
//$this->db->update(array('groupid'=> "6"), array('userid'=>$userid));
$sql = "UPDATE `v9_member` SET `groupid`='6' WHERE `userid` = $userid ";//修改用戶級別為注冊用戶
mysql_query($sql);
param::set_cookie('auth', $phpcms_auth, $cookietime);
param::set_cookie('_userid', $userid, $cookietime);
param::set_cookie('_username', $userinfo['username'], $cookietime);
param::set_cookie('_nickname', $userinfo['nickname'], $cookietime);
param::set_cookie('_groupid', $userinfo['groupid'], $cookietime);
param::set_cookie('cookietime', $_cookietime, $cookietime);
$forward = isset($_POST['forward']) && !empty($_POST['forward']) ? urldecode($_POST['forward']) : 'index.php?m=member&c=index';
//https 與 http 傳遞用戶名 跨域跳轉cooike
$url = "http://www.zhiliaoke.com.cn/set_cookie_www.php?nickname=".$_COOKIE['vuGYB__nickname']."&username=".$_COOKIE['vuGYB__username']."&userid=".$_COOKIE['vuGYB__userid']."&groupid=".$_COOKIE['vuGYB__groupid'];
header("Location: ".$url."");
}
}
}
} else {
showmessage(L('enable_register').L('enable_phpsso'), '/login.html');
}
showmessage(L('operation_failure'), HTTP_REFERER);
}
?>
補充最后一步
很重要,也是最簡單容易忽視的 !!!
有幾個人都卡在這里了!
需要 在phpcms\modules\member\classes\foreground.class.php 文件 21行左右 判斷用戶是否登陸通過wechat 函數
“phpcms如何實現微信登陸”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。