cmf小程序登录 easywchat SDK


namespace api\wxapp\controller;

use think\Db;
use cmf\controller\RestBaseController;
use think\Validate;
use EasyWeChat\Factory;

class PublicController extends RestBaseController
{
    // 微信小程序用户登录 TODO 增加最后登录信息记录,如 ip
    public function login()
    {
        $config = [
            'app_id' => '',
            'secret' => '',
            'response_type' => 'array',
        ];

        $app = Factory::miniProgram($config);

        $validate = new Validate([
            'code'           => 'require',
            'encrypted_data' => 'require',
            'iv'             => 'require',
            'raw_data'       => 'require',
            'signature'      => 'require',
        ]);

        $validate->message([
            'code.require'           => '缺少参数code!',
            'encrypted_data.require' => '缺少参数encrypted_data!',
            'iv.require'             => '缺少参数iv!',
            'raw_data.require'       => '缺少参数raw_data!',
            'signature.require'      => '缺少参数signature!',
        ]);

        $data = $this->request->param();

        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        $code          = $data['code'];

        $response = $app->auth->session($code);


        if (!empty($response['errcode'])) {
            $this->error('操作失败!123');
        }

        $openid     = $response['openid'];

        $sessionKey = $response['session_key'];

        $decryptedData = $app->encryptor->decryptData($sessionKey, $data['iv'], $data['encrypted_data']);

        if (empty($decryptedData)) {
            $this->error('操作失败!321');
        }

        //这样就解密了用户的信息了 其他的自己搞定吧

}

你可能感兴趣的:(Thinkcmf)