<?php

namespace App\Http\Controllers\QuickApp\Oauth;

use App\Consts\SysConsts;
use App\Modules\User\Models\QappPackage;
use App\Modules\User\Services\QappPackageService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Modules\User\Services\QappUserService;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

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

    /**
     * @apiVersion 1.0.0
     * @apiDescription 登录
     * @api {post} login 登录
     * @apiParam  {String}  device_no 设备号
     * @apiParam  {String}  device_info 设备信息json字符串格式
     * @apiParam  {Int}     send_order_id send_order_id
     * @apiParam  {Int}     timestamp 时间戳10分钟过期
     * @apiParam  {String}  sign 签名(见微信支付签名https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_3)
     * @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)
    {
        $codeVersion   = $request->header('x-codeversion', '');
        $package       = $request->input('package', '');
        $send_order_id = $request->input('send_order_id', 0);
        $device_no     = $request->input('device_no', '');
        $androidid     = $request->input('androidid', '');
        $imei          = $request->input('imei', '');
        $mac           = $request->input('mac', '');
        $device_info   = $request->input('device_info', '');
        
        $origin_package = $package;
//        //FIXME 海天包传成浩瀚了,特殊处理下,这个名字就给海天用
//        if($package =='com.beidao.kuaiying.haohan'){
//            $package = 'com.beidao.kuaiying.haitian';
//        }
//
//        //FIXME 浩瀚包传的是haohannew,但是打开要跟官方包名保持一致,所以这样特殊处理
//        if($package =='com.beidao.kuaiying.haohannew'){
//            $package = 'com.beidao.kuaiying.haohan';
//        }

        // $package = get_real_package($package);

        //获取渠道id
        $channel_id = QappPackageService::getChannelId($package);
        if ($device_no && $channel_id > 0) {

            \Log::info('$package:'.$package.' $origin_package:'.$origin_package.' $send_order_id:'.$send_order_id.' device_no:'.$device_no);
            $data = (new QappUserService)->login(compact('package', 'send_order_id', 'device_no', 'androidid', 'mac', 'device_info', 'imei', 'codeVersion'));
            if($data){
                
                \Log::info('login_success_package:'.$package.' $origin_package:'.$origin_package.' $send_order_id:'.$send_order_id.' device_no:'.$device_no);
                
                return response()->success($data);
            }else{
                \Log::info('login_fail_PARAM_ERROR_package:'.$package.' $origin_package:'.$origin_package.' $send_order_id:'.$send_order_id.' device_no:'.$device_no);
                
                return response()->error('PARAM_ERROR');
            }
        } else {
            \Log::info('login_fail2_PARAM_ERROR_package:'.$package.' $origin_package:'.$origin_package.' $send_order_id:'.$send_order_id.' device_no:'.$device_no);
            
            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      = strtotime("+1 month");
            return response()->success(compact('token', 'time'));
        } catch (JWTException $e) {
            return response()->error('QAPP_NOT_LOGIN');
        }
    }
}