微信掃碼登陸綁定賬號(hào)功能

1、前臺(tái)鏈接進(jìn)入微信官方的授權(quán)接口,當(dāng)點(diǎn)擊鏈接時(shí),則根據(jù)url中的appid自動(dòng)生成公司微信二維碼

<a ><span class="fgt pull-right"><img src="__PUBLIC__/web/images/wx.png">微信登錄</span></a>

2、先定義好微信授權(quán)接口類,以便下面授權(quán)時(shí)調(diào)用

class WeAuth
{
    protected $appId = "公司微信的appid";
    protected $appSecret = "企業(yè)微信號(hào)的密鑰";

    function __construct()
    {
       // parent::__construct(false);

        if(config('weixin.openAppId'))
        {
            $this->appId = config('weixin.openAppId');
        }

        if(config('weixin.openAppSecret'))
        {
            $this->appSecret = config('weixin.openAppSecret');
        }
    }

    /**
     * 開始授權(quán)
     * @return mixed
     */
    public function doAuth()
    {
        //如果掃碼頁面的url路徑中有code碼,則授權(quán)完成進(jìn)入回調(diào)函數(shù)
        if (isset($_REQUEST['code'])) {
            return $this->getUserInfo();
        }
        else {
        //獲取請(qǐng)求頁面時(shí)通信協(xié)議的名稱和版本。例如,“HTTP/1.0”。
        $protocal = $_SERVER['SERVER_PROTOCOL'];
        //當(dāng)前請(qǐng)求頭中 Host: 項(xiàng)的內(nèi)容,如果存在的話。
        $http_host = $_SERVER['HTTP_HOST'];
        //判斷是http請(qǐng)求還是https請(qǐng)求
        if ($protocal['5'] == 's') {
            $protocal = 'https://';
        }
        else {
            $protocal = 'http://';
        }
        $origin_url = $protocal . $http_host;
        // $origin_url = str_replace("www.", "", $origin_url);
        //進(jìn)入獲取用戶信息授權(quán)路徑
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appId . "&redirect_uri=" . urlencode($origin_url) . $_SERVER['REQUEST_URI'] . "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        header("Location:" . $url);
        exit();
        }
    }

    /**
     * 授權(quán)完成之后的回調(diào),獲取微信信息
     * @return mixed
     */
    public function getUserInfo()
    {
        $code = $_REQUEST['code'];
        $state = $_REQUEST['state'];
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appId . "&secret=" . $this->appSecret . "&code=" . $code . "&grant_type=authorization_code";
        $json = file_get_contents($url);
        $base_info = json_decode($json);
        $access_token = $base_info->access_token;
        $openid = $base_info->openid;
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";
        $json = file_get_contents($url);
        $wechat_user_info = json_decode($json,true);
        header('Content-type: text/html;charset=utf-8');
        return $wechat_user_info;
    }

}

3、當(dāng)掃描二維碼后,進(jìn)入指定的頁面和函數(shù)中,判斷如果是登陸狀態(tài)則跳轉(zhuǎn)首頁如果不是則進(jìn)入微信授權(quán)去登陸。如果授權(quán)成功,則進(jìn)入登陸判斷。如果有該微信的信息則登陸,沒有則進(jìn)入綁定微信頁面

protected $userId;
public function index()
{
    $userId = session("userId");
    if (!$userId) {
        //實(shí)例化授權(quán)接口類
        $auth = new WeAuth();
        //調(diào)用微信授權(quán)函數(shù)
        $wechatInfo = $auth->doAuth();
        // print_r($wechatInfo);exit();
        //判斷授權(quán)是否成功
        if($wechatInfo && isset($wechatInfo['unionid']) && !empty($wechatInfo['unionid']))
        {
            //獲取到用戶的微信id,查找數(shù)據(jù)庫內(nèi)是否有該微信id
            //如果沒有,則跳轉(zhuǎn)微信賬號(hào)綁定項(xiàng)目賬號(hào)功能,如果已經(jīng)存在,則直接登錄
            $unionId = $wechatInfo['unionid'];
            $msg = findById("user",array('a.unionId'=>$unionId),"a.*");
            if(empty($msg["data"])||$msg['code']==0){
                //未綁定賬號(hào),跳轉(zhuǎn)到綁定賬號(hào)頁面,并將查到的微信用戶信息發(fā)送到wxbind方法體內(nèi)
                return view('web@/index/wxbind',array('data'=>$wechatInfo));
                //$this->redirect(url('/web/Index/wxbind',array('data'=>$wechatInfo)));
            }else {
                //限制異常狀態(tài)的賬號(hào)登錄
                $user = $msg["data"];
                $userState = $user["status"];
                if ($userState == '1012002'){
                    echo '<script language="javascript">alert("微信登錄失敗:用戶狀態(tài)異常。");</script>';
                    $this->redirect(url('/web/Index/index'));
                }
                if ($userState == '1012005') {
                    echo '<script language="javascript">alert("微信登錄失?。寒?dāng)前賬戶已被凍結(jié),請(qǐng)聯(lián)系管理員。");</script>';
                    $this->redirect(url('/web/Index/index'));
                }
                //將數(shù)據(jù)寫入session
                session('user', $user);
                session('userId', $user["id"]);
                //登錄成功跳轉(zhuǎn)到首頁
                $this->redirect(url('/web/Index/index'));
            }
        }else{
            echo '<script language="javascript">alert("微信登錄失?。韩@取微信信息失敗。");</script>';
            $this->redirect(url('/web/Index/index'));
        }
    } else {
        $this->redirect(url('/web/Index/index'));
    }

}

4、綁定微信功能的頁面,并展示微信用戶信息

//微信登錄, 已有平臺(tái)賬號(hào)頁面
function wxbind(){
    //將授權(quán)獲得的微信用戶信息賦值$data,并發(fā)送給前臺(tái)
   $data = input('request.');
    if(empty($data)){
        $data = array();
    }
    return view("",array('data'=>$data));
}

5、wxbind的前臺(tái)頁面

<div class="col-lg-12 col-md-12 col-sm-12">
    <div class="col-md-12 col-sm-12">
        <div class="col-md-12 col-sm-12">
            <h3 class="welcome">
                <img src="{$data.headimgurl|default='__PUBLIC__/web/images/tx.jpg'}">Hi,{$data.nickname|default=''},歡迎登錄平臺(tái)
            </h3>
        </div>
        <div class="col-md-3 col-sm-12"></div>
        <div class="col-md-6 col-sm-12 choosewxbt">
            <div class="col-md-6 col-sm-12 wxbtnow" style="padding:0;" ><p>已有平臺(tái)賬號(hào)</p></div>
            <div class="col-md-6 col-sm-12" style="padding:0;" id="notyet"><p>沒有平臺(tái)賬號(hào)</p></div>
        </div>
        <div class="col-md-3 col-sm-12"></div>
        <div class="col-md-12 col-sm-12"><p class="sm">綁定賬號(hào)后,可實(shí)現(xiàn)一鍵登錄哦</p></div>
    </div>
    <div class="col-lg-4 col-md-4 col-sm-12"></div>
    <div class="col-md-4 col-sm-12 biaodan wxlogin">
        <div class="col-md-12 col-sm-12">
            <div class="form-group">
                <form action="" method="post" id="wxform">
                <input type="text" name="phone" id="phone" class="form-control" placeholder="登錄用戶名">
                <input type="password" name="password" id="password" style="margin-bottom: 14px;;"  class="form-control" placeholder="密碼">

                <input type="hidden" name="unionid" id="unionid" value="{$data.unionid|default=''}">  <!--微信返回的unionid-->
                <input type="hidden" name="headimgurl" id="headimgurl" value="{$data.headimgurl|default=''}"> <!---微信返回的用戶頭像地址-->
                <input type="hidden" name="nickname" id="nickname" value="{$data.nickname|default=''}">   <!--微信返回的用戶昵稱-->
                <button type="button" class="btn btn-block btn-outline btn-warning" id="submit">立即綁定</button>

                </form>

            </div>
            <div class="hr-line-dashed"></div>
        </div>
    </div>
    <div class="col-lg-4 col-md-4 col-sm-12"></div>
</div>

6、前臺(tái)發(fā)送數(shù)據(jù)的Js

$("#submit").click(function () {

   // var isPhone = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
  //  var isMob=/^((\+?86)|(\(\+86\)))?(13[012356789][0-9]{8}|15[012356789][0-9]{8}|18[012356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/;
    var phone_str=$("#phone").val();
    phone_str = $.trim(phone_str);
    if(phone_str==""){
        toastr.error('登錄用戶名必填');
        return;
    }
    /*else if(!(isPhone.test(phone_str)||isMob.test(phone_str))){
        alert('手機(jī)號(hào)碼格式錯(cuò)誤');
        return;
    }*/
    var password = $('#password').val();
    password = $.trim(password);
    if(password ==''){
        toastr.error('密碼必填');
        return ;
    }
    var unionid = $("#unionid").val();//微信的id
    var nickname = $("#nickname").val();//微信名
    var url = "{:url('/web/Index/dobind')}";
    $.post(url, {'mobile':phone_str,'password':password,'unionid':unionid,'nickname':nickname}, function (data) {
        if(data.code ==1){
            toastr.success('綁定成功');
            setTimeout('go()',1000);

        }else {
            toastr.error(data.msg);
        }
    });
});

7、綁定微信的方法體

//已有賬號(hào),綁定操作
function dobind(){
    $postData = input('request.',null,'trim');
    try{
        if(empty($postData['mobile'])){
            return array('code'=>'0','msg'=>'登錄用戶名不能為空','data'=>'');
        }
        if(empty($postData['password'])){
            return array('code'=>'0','msg'=>'密碼不能為空','data'=>'');
        }
        $map = array(
            'name'=>$postData['mobile'],
        );
        $msg = findById("user", $map, "a.*");
        //如果沒有查詢到數(shù)據(jù),或返回值為0,則返回錯(cuò)誤
        if (empty($msg["data"]) || $msg['code'] == 0) {
            return array('code'=>'0','msg'=>'該用戶不存在,無法綁定。','data'=>'');
        }else{
            $user = $msg["data"];
            //判斷查出的用戶密碼與綁定賬號(hào)頁面輸入的密碼是否一致,密碼一致則允許綁定
            if ($user["password"] !== md5($postData['password'])){
                return array('code' => '0', 'msg' => '密碼錯(cuò)誤,請(qǐng)重新輸入。', 'data' => '');
            }
            //限制賬號(hào)異常的用戶進(jìn)行登錄
            $userState = $user["status"];
            if ($userState == '1012002') {
                return array('code' => '0', 'msg' => '用戶狀態(tài)異常,不能綁定', 'data' => '');
            }
            if ($userState == '1012005') {
                return array('code' => '0', 'msg' => '當(dāng)前賬戶已被凍結(jié),無法綁定', 'data' => '');
            }
            //傳入的微信id
            $data = array(
                'unionId'=>$postData['unionid'],
            );
            //傳入的微信名
            $data['wx_name'] = $postData['nickname'];
            if(empty($user['realname'])){
                $data['realname'] = $postData['nickname'];
            }
            #todo 微信頭像暫時(shí)還沒處理
            //根據(jù)傳入的手機(jī)號(hào)將該用戶的id和掃碼的微信綁定
            $res = saveDataByCon('user',$data,array('name'=>$postData['mobile']));
            //如果保存成功則進(jìn)行登錄
            if($res['code']==1){
                //保存成功后,緩存數(shù)據(jù),相當(dāng)于直接登錄了
                //獲取logo并緩存
                $logo = '/img/111.jpg';
                //獲取孵化器的名字 并緩存
                if(strpos(','.$user["roleIds"].',',',1,')!==false && !empty($user["etprsIqbtId"])){
                    //孵化器企業(yè)管理員,應(yīng)該讀取孵化器企業(yè)的名字
                    $name = getField('etprsIqbt',array('id'=>$user['etprsIqbtId']),'name');

                }elseif(!empty($user['iqbtId'])){
                    $name = getField('incubator',array('id'=>$user['iqbtId']),'name');
                    if(!empty($user['logo'])){
                        $logo = getField('sysFile',array('id'=>$user['logo']),'savePath');
                    }
                }else{
                    $name = "海創(chuàng)匯云平臺(tái)";
                }
                session('sysName',$name);
                session('logo',$logo);
                session('user', $user);
                session('userId', $user["id"]);
                if($user["userCate"]=="1011002"){
                    session('etprsId', $user["etprsId"]);
                }
                session('iqbtId', $user["iqbtId"]);
                session("etprsIqbtId",$user["etprsIqbtId"]);
                return $res;
            }
        }

    }catch (\Exception $e) {
        //綁定失敗則記錄事務(wù)
        c_Log($e);
        return array("code" => 0, "msg" =>'綁定失敗','data'=>'');
    }
}

8、如果平臺(tái)賬號(hào)也沒有,則注冊(cè)賬號(hào)并綁定微信號(hào),注冊(cè)成功后寫入session登陸,并跳轉(zhuǎn)到首頁

//微信登錄  沒有平臺(tái)賬號(hào)情況的頁面
    function wxempty($unionid='',$nickname='',$headimgurl=''){
        $data = array(
            'unionid'=>$unionid,
            'nickname'=>$nickname,
            'headimgurl'=>$headimgurl,
        );
        return view('',array('data'=>$data));
    }

9、前臺(tái)發(fā)送注冊(cè)信息的html

//展示信息
<h3 class="welcome">
    <img src="{$data.headimgurl|default='__PUBLIC__/web/images/tx.jpg'}">Hi,{$data.nickname|default=''},歡迎登錄海創(chuàng)匯云平臺(tái)
</h3>
//表單
<form action="" method="post" id="rgst">
<input type="text" class="form-control" name="mobile" id="mobile" placeholder="聯(lián)系電話(登錄名必填)" onfocus="this.placeholder=''" onblur="this.placeholder='聯(lián)系電話(登錄名必填)'">
<input type="text" class="form-control" name="realname" id="realname" placeholder="姓名" onfocus="this.placeholder=''" onblur="this.placeholder='姓名'">
<p class="col-lg-12 col-md-12 col-sm-12 find find1">
    <input type="text" class="form-control pull-left" id="verify"  name="verify" placeholder="短信驗(yàn)證碼" onfocus="this.placeholder=''" onblur="this.placeholder='短信驗(yàn)證碼'">
    <button type="button" class="btn btn-block btn-outline btn-default pull-right a" id="sms">點(diǎn)擊獲取</button>
</p>
<input style="margin-bottom:14px;" type="password" name="password" id="password" class="form-control" placeholder="密碼(6-20位字母、數(shù)字、符號(hào)組合)" onfocus="this.placeholder=''" onblur="this.placeholder='密碼(6-20位字母、數(shù)字、符號(hào)組合)'">
<input style="margin-bottom:14px;" type="password"  name="cfmpassword" id="cfmpassword" class="form-control" placeholder="確認(rèn)密碼" onfocus="this.placeholder=''" onblur="this.placeholder='確認(rèn)密碼'">
<input type="hidden" name="unionid" id="unionid" value="{$data.unionid|default=''}">  <!--微信返回的unionid-->
<input type="hidden" name="headimgurl" id="headimgurl" value="{$data.headimgurl|default=''}"> <!---微信返回的用戶頭像地址-->
<input type="hidden" name="nickname" id="nickname" value="{$data.nickname|default=''}">   <!--微信返回的用戶昵稱-->

</form>
<button type="button" id="btnrgst" class="btn btn-block btn-outline btn-warning">完成</button>

10、沒有賬號(hào)需要注冊(cè)時(shí)發(fā)送的ajax

$("#btnrgst").click(function () {

    var isPhone = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
    var isMob=/^((\+?86)|(\(\+86\)))?(13[012356789][0-9]{8}|17[012356789][0-9]{8}|15[012356789][0-9]{8}|18[012356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/;
    var phone_str=$("#mobile").val();
    phone_str = $.trim(phone_str);
    var realname = $.trim($("#realname").val());
    var password = $.trim($("#password").val());
    var cfmpassword = $.trim($("#cfmpassword").val());
    if(phone_str==""){
        toastr.error('電話號(hào)碼不能為空');
        return false;
    }else if(!(isPhone.test(phone_str)||isMob.test(phone_str))){
        toastr.error('電話號(hào)碼格式不正確');
        return false;
    }
    if(realname==""){
        toastr.error('姓名不能為空');
        return ;
    }
    var verify = $.trim($("#verify").val());
    if(verify==''){
        toastr('短信驗(yàn)證碼不能為空');
        return false;
    }

    if(password==""){
        toastr.error('密碼不能為空');
        return;
    }
    if(password!=cfmpassword){
        toastr.error('兩次密碼不一致,請(qǐng)重新輸入');
        return;
    }
    var regExp=/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,20}$/;
    if(!regExp.test(password)){
        toastr.error('密碼必須為6-20位字母、數(shù)字或符號(hào)組合');
        return;
    }
    var form = $("#rgst");
    var url = "{:url('/web/Index/dobindempty')}";
    var data=form.serialize();
    $.post(url, data, function (data) {
        if (data.code == 1) {
            toastr.success('注冊(cè)成功,請(qǐng)登錄');
            setTimeout('go()',1000);
        } else {
            toastr.error(data.msg);
        }
    });

});

11、沒有注冊(cè)過時(shí),掃碼后進(jìn)入注冊(cè)的函數(shù),并將微信號(hào)的信息一同保存在數(shù)據(jù)中

//微信登錄,沒有注冊(cè)的,綁定首次注冊(cè)的用戶
function dobindempty(){
    $postData = input('request.',null,'trim');
    //加校驗(yàn)
    $role = [
        'mobile|聯(lián)系電話'=>'require',
        'realname|姓名'=>'require',
        'verify|短信驗(yàn)證碼'=>'require',
        'password|密碼'=>'require',
        'cfmpassword|確認(rèn)密碼'=>'require|confirm:password',
    ];
    $err = [
        'mobile'=>'聯(lián)系電話不能為空',
        'realname'=>'姓名不能為空',
        'verify'=>'短信驗(yàn)證碼必填',
        'password'=>'密碼必填',
        'cfmpassword'=>'確認(rèn)密碼必填且必須和密碼一致',
    ];
    $validate = new Validate($role,$err);
    if(! $validate->check($postData)){
        return array('code'=>'0','msg'=>$validate->getError(),'data'=>'');
    }

    try {
        $username = $postData['mobile'];
        $umsg = findById("user", array("name" => $username), "id");
        if (!empty($umsg["data"])) {
            return array("code" => 0, "msg" => "綁定失敗,該手機(jī)號(hào)已注冊(cè)");
        }
        //驗(yàn)證手機(jī)短信驗(yàn)證碼
        $verify =$postData['verify'];
        $res = verifySmsCode($username, $verify, 600);
        //驗(yàn)證失敗
        if ($res['code'] == '0') {
            return array('code' => '0', 'msg' => $res['msg'], 'data' => '');
        }

        $etprs["addtime"] = time();
        //開啟事物
        Db::startTrans();
        $msg = saveData("enterprise", $etprs, "企業(yè)注冊(cè)");
        if ($msg["code"] === '1') {
            $etprsId = $msg["data"];
            $user["name"] = $username;
            $user["realname"] = $postData['realname'];
            $user["registerTime"] = date("Y-m-d H:i", time());
            $user["addtime"] = time();
            $user["mobile"] = $username;
            $user["password"] = md5($postData['password']);
            $user["userCate"] = "1011002";
            $user["status"] = "1012003";      //這個(gè)狀態(tài)有待進(jìn)一步討論
            $user["etprsId"] = $etprsId;
            $user["etprsIqbtId"] = 1;
            $user["roleIds"] = "2";//角色:企業(yè)
            $user['realname'] = $postData['nickname'];
            $user['unionId'] = $postData['unionid'];
            $user['wx_name'] = $postData['nickname'];
            #todo  企業(yè)還有一些狀態(tài)為加上  比如,沒有對(duì)應(yīng)的孵化器
            $vld = $this->validate($user, 'User.register');
            if ($vld === true) {
                $msg2 = saveData("user", $user, "企業(yè)用戶注冊(cè)");
                if ($msg2["code"] =='0') {
                    throw new \think\Exception("綁定失敗,用戶信息保存失敗 " . $msg2["msg"]);
                }else{
                    //保存成功后,緩存數(shù)據(jù),相當(dāng)于直接登錄了
                    //獲取logo并緩存
                    session('user', $user);
                    session('userId', $msg2['data']);
                    session('etprsId', $etprsId);
                    session('iqbtId', '0');
                    session("etprsIqbtId",'1');
                }
            } else {
                throw new \think\Exception("用戶信息校驗(yàn)失敗" . $vld);
            }
        } else {
            throw new \think\Exception("綁定失敗,企業(yè)信息保存失敗" . $msg["msg"]);
        }

        Db::commit();
        return array("code" => 1, "msg" => "注冊(cè)成功",'data'=>'');
    } catch (\Exception $e) {
        //記錄事務(wù)
        c_Log($e);
        // 回滾事務(wù)
        Db::rollback();
        return array("code" => 0, "msg" =>'注冊(cè)失敗','data'=>'');
    }
}

Complete!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,893評(píng)論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評(píng)論 19 139
  • 正在讀研的吉米申請(qǐng)了學(xué)校的一個(gè)助管崗位,主要任務(wù)是給常常到外地出差的老師報(bào)賬和打印文件。這是個(gè)不錯(cuò)的選擇——一來每...
    阿超雙螺旋閱讀 639評(píng)論 2 0
  • 跌跌撞撞的路上,我們一直都在努力,愛的終點(diǎn)是婚姻,也更應(yīng)該是幸福。 每當(dāng)寶貝睡去的時(shí)候,我都習(xí)慣的等老公回家,時(shí)間...
    5號(hào)陶子閱讀 1,045評(píng)論 2 2
  • 學(xué)生A出生在一個(gè)貧困的農(nóng)村家庭,母親患有精神病,父親退殘走路不便,一家人主要靠撿破爛維持生計(jì)。他常常衣衫襤褸,蓬頭...
    梨蕊閱讀 853評(píng)論 0 3

友情鏈接更多精彩內(nèi)容