<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/4 0004
 * Time: 19:45
 */
class Upload extends MY_Controller{
    public $postData = array('frombg' => 1, 'mod' => '', 'act' => '', 'lid' => 1, 'sid' => 1);

    public function __construct()
    {
        parent::__construct();
        $this->load->library('curl');

    }


    public function setPath($config){
        $this->load->library('uploader', $config);
    }

    public function landUpload(){
        $levelId = $this->input->post('levelId');
        $this->setPath([
            'savePath'=>'public/game/image/island',
            'maxSize'  => 1024*5,
            'allowFiles' => array('.png','.jpg')
        ]);
        $file = $this->uploader->getFileInfo();
        $pictureId = $levelId.'_'.time();
        rename($file['url'],'public/game/image/island/'.$pictureId.$file['type']);
        $posts = $this->postData;
        $posts['mod'] = 'Config';
        $posts['act'] = 'saveLevelPic';
        $posts['pictureId'] = $pictureId;
        $posts['levelId'] = $levelId;
        $url = $this->config->item('officialUrl');
        $retData = $this->curl->req($url, $posts);
        if($retData == 0){
            return $this->response(1, '操作失败');
        }else{
            $this->actionSyncMinigJson("Mining".time().".json");
            return $this->response(0, '操作成功');
        }
    }

    public function cardUpload(){
        $ciid = $this->input->post('ciid');
        $picture = $ciid.'_'.time();
        $path = 'public/game/image/newcard';
        $this->setPath([
            'savePath'=>$path,
            'maxSize'  => 1024*5,
            'allowFiles' => array('.png','.jpg')
        ]);
        $file = $this->uploader->getFileInfo();
        rename($file['url'],$path.'/'.$picture.$file['type']);

        $posts = $this->postData;
        $posts['mod'] = 'Card';
        $posts['act'] = 'saveCardPic';
        $posts['ciid'] = $ciid;
        $posts['picture'] = $picture;
        $url = $this->config->item('officialUrl');
        $retData = $this->curl->req($url, $posts);
        if($retData == 0){
            return $this->response(1, '操作失败');
        }else{
            $this->actionSyncJson("CardCollect".time().".json");
            return $this->response(0, '操作成功');
        }
    }

    public function actionSyncMinigJson($filename){
        $posts = $this->postData;
        $posts['mod'] = 'Config';
        $posts['act'] = 'makeMiningJson';
        $posts['filename'] = $filename;
        $url = $this->config->item('officialUrl');
        $retData = $this->curl->req($url, $posts);
        $retData = json_decode($retData,1);
        if($retData['code'] == 1){
            $data = $retData['data'];
            $this->SaveJson($data,$filename);
        }
    }

    public function actionSyncJson($filename){
        $posts = $this->postData;
        $posts['mod'] = 'card';
        $posts['act'] = 'makeCardJson';
        $posts['filename'] = $filename;
        $url = $this->config->item('officialUrl');
        $retData = $this->curl->req($url, $posts);
        $retData = json_decode($retData,1);
        if($retData['code'] == 1){
            $data = $retData['data'];
            $this->SaveJson($data,$filename);
        }
    }

    public function SaveJson($data,$filename){
        $dir = $this->staticPath;
        if (!is_dir($dir))
        {
            $old = umask(0);
            //mkdir($dir, PRODUCTION_SERVER ? 0775 : 0777, true);
            mkdir($dir,0775 , true);
            umask($old);
        }
        $filepath = "{$dir}{$filename}";
        file_put_contents($filepath, json_encode($data,JSON_NUMERIC_CHECK));

        if(method_exists($this->common,'upToOss')){
            $dir = str_replace("./","",$dir);
            $file_path = str_replace("\\","/",FCPATH . $dir.$filename);

            $this->common->upToOss($dir.$filename,$file_path);
        }
    }
    
    //上传活动资源图片
    public function actioneUploadImgs($pathName = ''){
    	$path = 'public/game/image/activity_2/'.$pathName;
        $this->setPath([
            'savePath'=>$path,
            'maxSize'  => 1024*1024*5,
            'allowFiles' => array('.png','.jpg')
        ]);
        $file = $this->uploader->getFileInfo();
        
        rename($file['url'],$path.'/'.$file['originalName']);
        return $this->response(0, '操作成功');
    }
    
    //上传图片
    public function actioneUploadImgs2(){
    	$path = 'public/images/uploads/'.date("Ymd");
    	if (!is_dir($path))
        {
            $old = umask(0);
            mkdir($path,0775 , true);
            umask($old);
        }
        $this->setPath([
            'savePath'=>$path,
            'maxSize'  => 1024*1024*5,
            'allowFiles' => array('.png','.jpg')
        ]);
        $file = $this->uploader->getFileInfo();
        $url =  ($_SERVER['HTTPS']?"https://":"http://").$_SERVER['HTTP_HOST']. "/" . $path.'/'.$file['originalName'];
        rename($file['url'],$path.'/'.$file['originalName']);
        return $this->response(0, $url);
    }
}