<commit_before><?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2017-06-16
 * Time: 16:43
 */

namespace app\app\controller;


use app\src\base\enum\ErrorCode;
use app\src\base\helper\PageHelper;
use app\src\base\helper\ValidateHelper;
use app\src\clients\action\ClientsDetailAction;
use app\src\user\action\UserHelperAction;
use think\controller\Rest;
use think\Request;

class App extends Rest
{
    protected $clientId;
    protected $sessionId;

    public function __construct()
    {
        parent::__construct();
        // TODO 校验 app_id 是否有效
        $this->initParams();
        $this->initConfig();

        // 检测
        $this->checkParams();

    }

    public function checkParams(){

        $result = (new ClientsDetailAction())->detailByAppID($this->clientId);
        if(ValidateHelper::legalArrayResult($result) && $result['info']['app_id'] == $this->clientId){
            return true;
        }
        // app_id 无效
        $this->fail($this->clientId.' app_id invalid');
    }

    public function initConfig(){

    }

    public function initParams(){

        $this->sessionId = $this->_param('session_id','');
        $header = Request::instance()->header();
        $sessionId = isset($header['sessionid']) ? $header['sessionid'] : null;

        if(!empty($sessionId)) {
            session_start();
            session_id($sessionId);
        }
        if(empty($this->sessionId)){
            $this->sessionId = $sessionId;
        }

        $this->clientId = $this->_param('app_id','');
        $clientId = isset($header['clientid']) ? $header['clientid'] : null;
        if(empty($this->clientId)){
            $this->clientId = $clientId;
        }
        if(empty($this->clientId)){
            $this->fail('缺少 app_id');
        }
        if(empty($this->sessionId)){
            $this->fail('缺少 sessionId');
        }

    }

    public function returnResult($result){
        if(isset($result['status'])){
            if($result['status']){
                $this->success($result['info'],'操作成功');
            }else{
                $this->fail($result['info']);
            }
        }else{
            if(isset($result['code'])) {
                if ($result['code'] === 0) {
                    $this->success($result['data'], '操作成功');
                } else {
                    $this->fail($result['msg']);
                }
            }
        }
    }

    /**
     * 返回 json 格式
     * @param int $code
     * @param string $msg
     * @param array $data
     */
    protected function jsonReturn($code=0,$msg='',$data=[]){
        $response = $this->response(['code'=>$code,'msg'=>$msg,'data'=>$data], "json",200);

        $response->header("Access-Control-Allow-Origin","*")
            ->header("Access-Control-Allow-Methods","GET, POST,OPTIONS")
            ->header("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept, sessionId,clientId ")

            ->header("X-Powered-By","WWW.ITBOYE.COM")->send();
        exit(0);
    }

    protected function failNeedLogin($msg){<commit_msg>【完善】增加视频演员关联表<commit_after><?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2017-06-16
 * Time: 16:43
 */

namespace app\app\controller;


use app\src\base\enum\ErrorCode;
use app\src\base\helper\PageHelper;
use app\src\base\helper\ValidateHelper;
use app\src\clients\action\ClientsDetailAction;
use app\src\user\action\UserHelperAction;
use think\controller\Rest;
use think\Request;

class App extends Rest
{
    protected $appId;
    protected $sessionId;

    public function __construct()
    {
        parent::__construct();
        // TODO 校验 app_id 是否有效
        $this->initParams();
        $this->initConfig();

        // 检测
        $this->checkParams();

    }

    public function checkParams(){

        $result = (new ClientsDetailAction())->detailByAppID($this->appId);
        if(ValidateHelper::legalArrayResult($result) && $result['info']['app_id'] == $this->appId){
            return true;
        }
        // app_id 无效
        $this->fail($this->appId.' app_id invalid');
    }

    public function initConfig(){

    }

    public function initParams(){

        $this->sessionId = $this->_param('session_id','');
        $header = Request::instance()->header();
        $sessionId = isset($header['sessionid']) ? $header['sessionid'] : null;

        if(!empty($sessionId)) {
            session_start();
            session_id($sessionId);
        }
        if(empty($this->sessionId)){
            $this->sessionId = $sessionId;
        }

        $this->appId = $this->_param('app_id','');
        $clientId = isset($header['appId']) ? $header['clientid'] : null;
        if(empty($this->appId)){
            $this->appId = $clientId;
        }
        if(empty($this->appId)){
            $this->fail('缺少 app_id');
        }
        if(empty($this->sessionId)){
            $this->fail('缺少 sessionId');
        }

    }

    public function returnResult($result){
        if(isset($result['status'])){
            if($result['status']){
                $this->success($result['info'],'操作成功');
            }else{
                $this->fail($result['info']);
            }
        }else{
            if(isset($result['code'])) {
                if ($result['code'] === 0) {
                    $this->success($result['data'], '操作成功');
                } else {
                    $this->fail($result['msg']);
                }
            }
        }
    }

    /**
     * 返回 json 格式
     * @param int $code
     * @param string $msg
     * @param array $data
     */
    protected function jsonReturn($code=0,$msg='',$data=[]){
        $response = $this->response(['code'=>$code,'msg'=>$msg,'data'=>$data], "json",200);

        $response->header("Access-Control-Allow-Origin","*")
            ->header("Access-Control-Allow-Methods","GET, POST,OPTIONS")
            ->header("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept, Session-ID,App-ID ")

            ->header("X-Powered-By","WWW.ITBOYE.COM")->send();
        exit(0);
    }

    protected function failNeedLogin($msg){
{'fraction_non_alphanumeric': 0.18041912246234446, 'fraction_numerical': 0.006221349050425672, 'mean_word_length': 2.1768070722828914, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1988869', 'n_tokens_mistral': 1907, 'n_tokens_neox': 1873, 'n_words': 289}