Login
欢迎访问木星教程网_技术成就梦想 - 为知识分享服务
【我要投稿】 【请登录】 【免费注册】 【安全退出】

您现在的位置是: 首页 > 技术 > cms教程

cms教程

PbootCMS对接微信开放平台扫码登录

2023-03-06 cms教程 加入收藏
<?php/** * 微信公众号配置 * ============================================================================ * 版权所有 云管家网络科技有限公司 * 网站地址: https://www.kaifacn.com * =============================================

1676007054206162.png

<?php

/**

 * 微信公众号配置

 * ============================================================================

 * 版权所有  云管家网络科技有限公司

 * 网站地址: https://www.kaifacn.com

 * ============================================================================

 */

namespace coreasic;

use coreasicConfig;


class Wechat

{

    protected $appid;

    protected $secret;


    public function __construct()

    {

        // 配置参数

        $this->appid = Config::get('weixin_appid');

        $this->secret = Config::get('weixin_secret');

    }


    // 获取登录二维码

    public function getqrcode()

    {

        // 回调地址

        $redirect_uri= get_http_url().Url::home('Member/login');

        // 该回调需要url编码

        $redirect_uri=urlencode($redirect_uri);

        $url = "https://open.weixin.qq.com/connect/qrconnect?appid=".$this->appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_login&state=".session('access_token')."#wechat_redirect";

        $result = get_url($url);

        // 修改二维码路径

        $result = str_replace("/connect/qrcode/", "https://open.weixin.qq.com/connect/qrcode/", $result);

        // 简单优化一下页面样式

        $result = str_replace('<body>','<style>.impowerBox .qrcode{width: 220px;margin-top:0px;}.impowerBox .icon38_msg {width: 30px;height: 30px;margin-left: 27px;}</style><body>',$result);

        $result = str_replace('<div>微信登录</div>','',$result);

        $result = str_replace('<p>“CMS开发网”</p>','',$result);

        $result = str_replace('<h4>扫描成功</h4>','<h4>扫描成功,请点击授权!</h4>',$result);

        $result = str_replace('<p>在微信中轻触允许即可登录</p>','',$result);

        $result = str_replace('<p>你可再次扫描登录,或关闭窗口</p>','',$result);

        $result = str_replace('<p>使用微信扫一扫登录</p>','<div><i></i>使用微信扫一扫登录</div>',$result);


        echo $result;

    }


    // 获取登录授权信息

    public function getWxUser()

    {

        $code = get('code');

        $state = get('state');

        if ($code && $state && session('access_token') == $state)

        {

            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->secret."&code=".$code."&grant_type=authorization_code";

            $jsonResult = file_get_contents($url);

            $result = json_decode($jsonResult, true);

            $data = $this->getAuthUserInfo($result['access_token'],$result['openid']);

            return $data;

        }

        location(Url::home('member/login'));

    }


    // 获取用户信息

    private function getAuthUserInfo($token, $openid)

    {

        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$token."&openid=".$openid."&lang=zh_CN";

        $result = json_decode(get_url($url));

        if (isset($result->errcode) && $result->errcode)

        {

            location(Url::home('member/login'));

        }

        return $result;

    }

}

放在/core/basic里面,需自行处理登录业务逻辑,本教程使用微信开放平台配置

原文地址:https://www.kaifacms.com/develop/10183.html