微信小程序 php easywechat 后台验签不通过

小程序 首次登录 (第一次获取授权)获取了code rawData signature 传给 服务器后 服务器 根据code 获得得session_key 和 rawData 凭借得 sha1加密值 和signature 不一致 但是第二次(再次获取授权后)服务器 验签得值确是一致得 ! 导致我小程序用户授权要授权两次 !! 这是什么情况啊?

授权wxml代码

<view class="container">
    <view class="authorize">
        <text class="aut_title">需要微信授权登录</text>
        <text class="aut_content">微信授权会获取您的头像和昵称信息,仅用于绑定账号,方便您查看自己过往发过的树洞,以及提供交互通知给您。</text>
        <button open-type="getUserInfo" bindgetuserinfo="onGotUserInfo" class="aut_but" bindtap="authCheck"> 授权登录 </button>
        <image src="../../static/images/authorize.png"></image>
    </view>
</view>

授权js代码

 authCheck: function(success) {
        wx.showLoading({
            title: '加载中',
        })
        wx.getSetting({ // 查看是否授权
            success: res => {
                wx.hideLoading();
                // if (res.authSetting['scope.userInfo']) {}
            }
        })
    },
    onGotUserInfo: function(e) {
        let rawData = e.detail.rawData || '';
        if (rawData) {
            wx.showLoading({
                title: '加载中',
            })
            this.ckLogin(rawData, e.detail.signature);
        } else {
            wx.showToast({
                title: '需要授权才能继续使用服务',
                icon: "none"
            })
        }
    },
    ckLogin: function(loginrawData, loginSignature) {
        setTimeout(() => {
            wx.login({ //登录
                success: res => {
                    if (res.code) {
                        let loginCode = res.code,
                            param = {};
                        param.url = "login/signIn";
                        param.data = {};
                        param.data.code = loginCode;
                        param.data.raw_data = loginrawData;
                        param.data.signature = loginSignature;
                        util.requests(param, res => {
                            wx.setStorage({
                                key: 'token',
                                data: res.data.data.token,
                            })
                            wx.setStorage({
                                key: 'nick_name',
                                data: res.data.data.nick_name,
                            })
                            util.setStorageAll();
                            wx.navigateBack();
                        });
                    } else {
                        wx.showToast({
                            title: '登录失败,请重新授权',
                            icon: "none"
                        })
                    }
                }
            });
        }, 200)
    }

PHP代码 用的easywechat 包 自己封装了一次

$data['signature']);

    function start()
    {
        $options = [
            'debug' => true,
            'mini_program.app_id' => 'XXXXXXXXX',
            'mini_program.secret' => 'XXXXXXXXXXXXXX',
            'log' => [
                'level' => 'debug',
                'file' => '/tmp/easywechat.log',
            ],
            // ...
        ];

        $this->mini_program = new Application($options);
//        $data  = $this->mini_program->mini_program->sns->getSessionKey();
//        $data->toArray()
        return $this;
    }

    /**
     * 获取session_key open_id union_id
     * @param $code
     * @return $this|void
     */
    public function getSessionKey($code)
    {
        $data = $this->mini_program->mini_program->sns->getSessionKey($code)->toArray();
        if (isset($data['errcode'])) {
            list($this->code, $this->msg) = [0, '无效code'];
            return;
        }
        list($this->session_key, $this->open_id, $this->union_id) = [$data['session_key'], $data['openid'], isset($data['union_id']) ? $data['union_id'] : ''];
        return $this;
    }

    /**
     * 验证同一微信用户
     * @param $rawData
     * @param $signature
     * @return bool
     */
    public function verify($rawData, $signature)
    {
        $session_key = &$this->session_key;
        $str = $rawData.$session_key;
        $en_code = sha1($str);
        if ($en_code === $signature) {
            return true;
        } else {
            return false;
        }
    }

PHP登录方法

 public function signIn()
    {
        if ($this->request->isGet()) {
            list($this->code, $this->msg) = [0, '请求方法错误'];
            return;
        }
        if ($this->request->isPost()) {
            //获取小程序code
            $data = $this->request->post();
            $res = $this->validate($data, 'app\api\validate\Login.login');
            if (true !== $res) {
                list($this->code, $this->msg) = [0, $res];
            }
            //请求验证
            $weChat = new WeChat();
            //验证
            $is_weChat = $weChat->start()->getSessionKey($data['code'])->verify($data['raw_data'], $data['signature']);
            if (true === $is_weChat) {
                //通过验证 存下微信用户信息 并下发token
                $data = $weChat->getUserInfo($data['raw_data']);
                $userModel = new WeUsers();
                $user_info = $userModel->saveUser($data);
                if (empty($user_info)) {
                    list($this->code, $this->msg) = [0, '注册失败'];
                    return;
                } else {
                    $tokenModel = new Token();
                    $token = $tokenModel->makeToken($user_info['id']);
                    $user_info['token'] = $token;
                    list($this->code, $this->msg, $this->data) = [1, '登录成功', $user_info];
                    return;
                }
            } else {
                list($this->code, $this->msg) = [0, '微信验证异常请重新登录'];
            }
        }
    }

麻烦大佬指点下
php7.1 tp5.0

阅读 2.5k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏