<?php

namespace App\Http\Controllers\QuickApp\Oauth;

use App\Consts\SysConsts;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Modules\User\Services\QappUserService;
use JWTAuth;

class UsersController extends Controller
{
    /**
     * @apiDefine Login 登录
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 登录
     * @api {post} login 登录
     * @apiParam  {String}  js_code js_code
     * @apiParam  {Int}     distribution_channel_id distribution_channel_id
     * @apiParam  {String}  sign    签名
     * @apiGroup Login
     * @apiName index
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccess {String}      data.uid 用户uid
     * @apiSuccess {String}      data.token token
     * @apiSuccess {Int}         data.time 过期时间
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data:  {
     *            token:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
     *            time:123455,
     *            uid:1
     *           }
     *       }
     */
    public function index(Request $request)
    {
        $send_order_id = $request->input('send_order_id', 0);
        $device_no = $request->input('device_no', '');
        $device_info = $request->input('device_info', '');
        if ($device_no) {
            $data = QappUserService::loginStatic(compact('send_order_id', 'device_no', 'device_info'));
            return response()->success($data);
        } else {
            return response()->error('PARAM_ERROR');
        }
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 刷新token
     * @api {get} RefreshToken 刷新token
     * @apiParam {String}  [token]  token
     * @apiHeader {String} [Authorization]  token 两个token任选其一
     * @apiGroup Login
     * @apiName RefreshToken
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccess {String}      data.token token
     * @apiSuccess {Int}         data.time 过期时间
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data:  {
     *            token:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
     *            time:123455
     *           }
     *       }
     */
    public function RefreshToken()
    {
        try {
            $old_token = JWTAuth::getToken();
            $token = JWTAuth::refresh($old_token);
            $time = time() + SysConsts::ONE_HOUR_SECONDS * 2;
            return response()->success(compact('token', 'time'));
        } catch (Exception $e) { }
        return response()->error('XCX_NOT_LOGIN');
    }

    /**
     * 检验数据的真实性,并且获取解密后的明文.
     * @param $encryptedData string 加密的用户数据
     * @param $iv string 与用户数据一同返回的初始向量
     * @param $data string 解密后的原文
     *
     * @return int 成功0,失败返回对应的错误码
     */
    private function decryptData($encryptedData, $iv, &$data)
    {
        if (strlen($iv) != 24) {
            return ErrorCode::$IllegalIv;
        }
        $aesIV = base64_decode($iv);
        $aesCipher = base64_decode($encryptedData);
        $aesKey = "";
        $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
        $dataObj = json_decode($result);
        if ($dataObj  == NULL) {
            return ErrorCode::$IllegalBuffer;
        }
        $data = $result;
        return ErrorCode::$OK;
    }
}
class ErrorCode
{
    public static $OK = 0;
    public static $IllegalAesKey = -41001;
    public static $IllegalIv = -41002;
    public static $IllegalBuffer = -41003;
    public static $DecodeBase64Error = -41004;
}