接入微信公眾號登入-回撥

MEILI發表於2021-07-30
 /**
     * 微信處理回撥
     */
    public static function callBack($data)
    {
        if (!empty($data)) {
            if (isset($data['MsgType']) && $data['MsgType'] == 'event') {
                //獲取使用者資訊
                $userInfo = login::get_user_info($data['FromUserName']);
                $userInfo = json_decode($userInfo, true);
                //更換/繫結手機號
                if (stristr($data['EventKey'],'bindingPhone')){
                    $arr = array(
                        'EventKey' => $data['EventKey'],//唯一標識
                    );
                    if ($data['Event'] == 'SCAN') {
                        $res = Db::table('user')->where('wx_open_id', $data['FromUserName'])->update($arr);
                        if ($res !== false) {
                            login::replacePhone($data);
                            return true;
                        }
                    }
                }
                if (stristr($data['EventKey'],'unbindPhone')){
                    $arr = array(
                        'EventKey' => $data['EventKey'],//唯一標識
                        'phone'=>'',
                        'password'=>''
                    );
                    if ($data['Event'] == 'SCAN') {
                        $res = Db::table('user')->where('wx_open_id', $data['FromUserName'])->update($arr);
                        if ($res !== false) {
                            login::unbindPhone($data);
                            return true;
                        }
                    }
                }
                $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    </xml>";
                if ($data['Event'] == 'subscribe') {
                    $str = '提示訊息';
                    $time = time();
                    $resultStrq = sprintf($textTpl, $data['FromUserName'], 'czh_sjpt', $time, 'text', $str);
                    echo $resultStrq;
                }
                if ($data['Event'] == 'SCAN') {
                    $str = '登入成功';
                    $time = time();
                    $resultStrq = sprintf($textTpl, $data['FromUserName'], 'czh_sjpt', $time, 'text', $str);
                    echo $resultStrq;
                }
                $arr = array(
                    'EventKey' => $data['EventKey'],//唯一標識
                    'event' => $data['Event'],//微信事件型別
                    'head_portrait' => $userInfo['headimgurl'],//頭像
                    'nickname' => login::filterEmoji($userInfo['nickname']),//暱稱
                    'wx_open_id' => $data['FromUserName'],//微信id
                );
                return true;
            }
        }
    }

    /**
     * @param $str
     * @return string|string[]|null
     * 獲取使用者資訊
     */
    public static function get_user_info($openId){
        $accessToken = login::get_access_token();//獲取token
        $url  = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$accessToken."&openid=".$openId."&lang=zh_CN";
        $data = login::httpRequest($url);
        return $data;
    }
    ~~~
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章