<?php
namespace app\lumaapi\controller;
use app\utils\encryption;
use think\Controller;
use think\Db;
use think\exception\HttpResponseException;
use think\Request;
use think\Session;
class Common extends Controller
{
	// 公共
    protected  $pageSize=10;
    protected $uid=0;//头条id
    protected $user_id=0;//鹿马用户id
    protected $member="";
    //加密后的uid
    public $token='';
    protected $key="&luma&video%uid*time";

    // 公共
    public function _initialize(){
        $this->token=$token = $this->request->param('token','');
        $this->user_id=$user_id=encryption::think_decrypt($token,$this->key);
        if(!empty($user_id)){
            $this->member=$user=Db::name('member')->where(['user_id'=>$this->user_id])->find();
            $this->uid=$user['id'];
        }
    }

    /**
     * 操作失败返回的数据
     * @param string $msg   提示信息
     * @param mixed $data   要返回的数据
     * @param int   $code   错误码，默认为1
     * @param string $type  输出类型
     * @param array $header 发送的 Header 信息
     */
    protected function error($msg = 'error', $code = -1, $data = [], $type = null, array $header = [])
    {
        $this->result($msg, $data, $code, $type, $header);
    }


    /**
     * 操作成功返回的数据
     * @param string $msg   提示信息
     * @param mixed $data   要返回的数据
     * @param int   $code   错误码，默认为0
     * @param string $type  输出类型
     * @param array $header 发送的 Header 信息
     */
    protected function success($data = [], $msg = 'success', $code = 0, $type = null, array $header = [])
    {
        $this->result($msg, $data, $code, $type, $header);
    }
    /**
     * 返回封装后的 API 数据到客户端
     * @access protected
     * @param mixed  $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码，默认为0
     * @return void
     * @throws HttpResponseException
     */
    protected function result($msg, $data = [], $code = 0, $type = null, array $header = [])
    {
        if (is_string($msg)) {
            $result = json_decode($msg, true);
            if ($result==null) {
                $result = [
                    'code' => $code,
                    'msg'  => $msg,
                    'data' => $data,
                    'time' => $this->request->server('REQUEST_TIME'),
                ];
            }
        } else {
            $result = (array)$msg;
        }

        // 如果未设置类型则自动判断
        $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : 'json');

        if (isset($header['statuscode'])) {
            $code = $header['statuscode'];
            unset($header['statuscode']);
        } else {
            //未设置状态码,根据code值判断
            $code = $code >= 600 || $code < 200 ? 200 : $code;
        }
        $response = \think\Response::create($result, $type, $code)->header($header);
        throw new HttpResponseException($response);
    }

    public function need_login(){
        $type =  $this->request->param('type','');
        if(!$this->uid && $type != "cashier"){
            return $this->error('请先登录', -10800);
        }
    }
}
