You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

424 lines
17 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// +----------------------------------------------------------------------
// | 徐州梦创信息科技有限公司—专业的游戏运营,推广解决方案.
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.vlcms.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: kefu@vlcms.com QQ97471547
// +----------------------------------------------------------------------
namespace Admin\Event;
use Think\Controller;
/**
* 后台首页控制器
*
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
class SourceEvent extends Controller
{
public function add_source()
{
$model = D('Game_source');
$plist = A("Plist");
$data = $_REQUEST;
$data['file_size'] = round($data['file_size'] / pow(1024, 2), 2) . "MB";
/*if ($data['file_type'] == 2) {
if (preg_match("/.ipa/", $data['file_name'])) {
//copy($data['file_url'] . "/" . $data['file_name'], "./Uploads/Ios/original/" . $data['file_name']);
//@unlink($data['file_url'] . "/" . $data['file_name']);
//$data['file_url'] = "./Uploads/Ios/original";
}
}*/
$data['file_url'] = $data['file_url'] . "/" . $data['file_name'];
$data['sdk_version'] = $data['file_type'];
$data['op_id'] = UID;
$data['version'] = $_POST['version'];
$data['op_account'] = session("user_auth.username");
$data['create_time'] = NOW_TIME;
$remark = str_replace(array("\r\n", "\r", "\n"), "@@@", $_POST['remark']);
$data['remark'] = json_encode(explode('@@@', $remark));
if ($data['file_type'] == 2) {
$data['plist_url'] = "./Uploads/SourcePlist/" . $data['game_id'] . ".Plist";
$data['org_plist_url'] = "./Uploads/OrgSourcePlist/" . $data['game_id'] . ".Plist";
$plist -> create_plist($data['game_id'], 0, $data['bao_name'], $data['file_url']);
}
$res = $model -> add($data);
if ($res) {
//原包打包
$this -> soure_pack($data['game_id'], $data['file_url'], $data['file_type'], 1, $data['bao_name']);
$this -> update_game_size($data);
$this -> success('添加成功', U('GameSource/lists', array('type' => $data['type'])));
} else {
$this -> error('添加失败', U('GameSource/lists', array('type' => $data['type'])));
}
}
/**
*修改游戏原包
*/
public function update_source($id = null, $file_name, $from = "")
{
$id || $this -> error('id不能为空');
$model = D('Game_source');
$plist = A("Plist");
$data = $_REQUEST;
$url = $data['file_url'];
$urll = $data['file_url2'];
/*if ($data['file_type'] == 2) {
if (preg_match("/.ipa/", $data['file_name']) && ($data['file_name'] != $file_name)) {
//copy($data['file_url'] . "/" . $data['file_name'], "./Uploads/Ios/original/" . $data['file_name']);
//@unlink($data['file_url'] . "/" . $data['file_name']);
//$data['file_url'] = "./Uploads/Ios/original";
//$url = "./Uploads/Ios/original";
}
}*/
$data['file_size'] = strpos($data['file_size'], 'MB') ? $data['file_size'] : round($data['file_size'] / pow(1024, 2), 2) . "MB";
/*$extend = substr($data['file_url'], strlen($str) - 3, 3);
if ($extend != "apk" && $extend != "ipa") {
$data['file_url'] = $data['file_url'] . "/" . $data['file_name'];
}*/
$data['file_url'] = $data['file_url'] . "/" . $data['file_name'];
$data['id'] = $id;
$data['op_id'] = UID;
$data['op_account'] = session("user_auth.username");
$data['create_time'] = NOW_TIME;
$remark = str_replace(array("\r\n", "\r", "\n"), "@@@", $_POST['remark']);
$data['remark'] = json_encode(explode('@@@', $remark));
$game_source = $model -> where(array('id' => $id)) -> find();
if($game_source['file_name'] == $data['file_name']) {
if ($from == "dev") {
$this -> success('修改成功', U('Developers/source'));
} else {
$this -> success('修改成功', U('GameSource/lists'));
}
}
if ($data['file_type'] == 2) {
$data['plist_url'] = "./Uploads/SourcePlist/" . $data['game_id'] . ".Plist";
$data['org_plist_url'] = "./Uploads/OrgSourcePlist/" . $data['game_id'] . ".Plist";
$plist -> create_plist($data['game_id'], 0, $data['bao_name'], $data['file_url']);
}
$data['source_version'] = $game_source['source_version'] + 1;
$res = $model -> save($data);
if ($res) {
//原包打包
$this -> soure_pack($game_source['game_id'], $data['file_url'], $data['file_type'], $data['source_version'], $data['bao_name'], 1, $file_name);
//渠道包加入打包队列
$appmodel = M('Apply', 'tab_');
$app_map['status'] = 1;
$app_map['game_id'] = $game_source['game_id'];
$app_data = $appmodel -> where($app_map) -> select();
if ($app_data) {
M() -> startTrans();
$app_res = $appmodel -> where($app_map) ->save(['enable_status'=>2, 'pack_url'=>'', 'plist_url'=>'']);
M('apply_launch', 'tab_') -> where(['launch_game_id'=>$game_source['game_id']]) -> setField('launch_packge', 0);
if ($app_res === false) {
M() -> rollback();
if ($from == "dev") {
$this -> error('渠道包打包失败', U('Developers/source'));
} else {
$this -> error('渠道包打包失败', U('GameSource/lists'));
}
} else {
M() -> commit();
}
}
@unlink($url . "/" . $file_name);
@unlink($urll . "/" . str_replace('.ipa', '.mobileprovision', $file_name));
$this -> update_game_size($data);
//发送站内信
$user_id = M('ucenter_member') -> where('status=1') -> field('id') -> select();
$content = '游戏:' . get_game_name($data['game_id']) . ' 已更新,请尽快处理渠道包';
$sdk_version = M('game', 'tab_') -> field('sdk_version') -> where(['id' => $data['game_id']]) -> find();
D('Msg') -> sendMsg2($user_id, $content, $sdk_version['sdk_version'], $data['game_id']);
if ($from == "dev") {
$this -> success('修改成功', U('Developers/source'));
} else {
$this -> success('修改成功', U('GameSource/lists'));
}
} else {
if ($from == "dev") {
$this -> error('修改失败', U('Developers/source'));
} else {
$this -> error('修改失败', U('GameSource/lists'));
}
}
}
protected function update_game_size($param = null)
{
$model = D('Game');
$map['id'] = $param['game_id'];
$data['game_size'] = $param['file_size'];
// $data['version'] = $param['version'];
if ($param['file_type'] == 1) {
$data['and_dow_address'] = $param['file_url'];
$ggame = $model -> where(array('id' => $map['id'])) -> find();
if ($ggame['sdk_version'] != - 1) {
if ($ggame['sdk_version'] == 2) {
$data['sdk_version'] = 0;
}
} else {
$data['sdk_version'] = 1;
}
} else {
if ($ggame['sdk_version'] != - 1) {
if ($ggame['sdk_version'] == 1) {
$data['sdk_version'] = 0;
}
} else {
$data['sdk_version'] = 2;
}
$data['ios_dow_address'] = $param['file_url'];
}
$model -> where($map) -> save($data);
}
/**
*原包打包
*/
protected function soure_pack($game_id = 0, $file_url = "", $file_type = 1, $source_version = 1, $baoName='', $flag=0, $file_name='')
{
$game_info = M("game", "tab_") -> find($game_id);
$data = array(
"game_id" => $game_info['id'],
"game_name" => $game_info['game_name'],
"game_appid" => $game_info['game_appid'],
"promote_id" => 0,
"promote_account" => "自然注册",
'source_version' => $source_version,
);
$real_file_url = ROOTTT.ltrim($file_url, './');
$temp = explode('/', $file_url);
$newFileName = $temp[count($temp)-1];
$to = $file_url;
$originalUrl = '';
if ($file_type == 1) {
$new_file_url = $real_file_url;
$zip = new \ZipArchive;
$res = $zip -> open($real_file_url, \ZipArchive::CREATE);
$zip -> addFromString('META-INF/mch.properties', json_encode($data));
$zip -> close();
} elseif ($file_type == 2) {
if (preg_match("/.ipa/", $file_url)) {
if($flag == 0 || ($flag ==1 && $newFileName != $file_name)) {
$originalUrl = $file_url;
$new_file_url = str_replace('Uploads/SourcePack', 'Uploads/Ios/original', $real_file_url);
$to = str_replace('Uploads/SourcePack', 'Uploads/Ios/original', $file_url);
$zip_open_game_source = zip_open($real_file_url);
if ($zip_open_game_source) {
while ($zip_entry = zip_read($zip_open_game_source)) {
if (preg_match("/.app/", zip_entry_name($zip_entry))) {
$ios_app = substr(zip_entry_name($zip_entry), 8) . "<br/>";
}
$new_ios_1 = explode("/", $ios_app);
}
zip_close($zip_open_game_source);
}
$url_ver = "Payload/" . $new_ios_1[0] . "/_CodeSignature/TXChannel";
$zip = new \ZipArchive;
copy($real_file_url, $new_file_url);
$res = $zip -> open($new_file_url, \ZipArchive::CREATE);
$zip -> addFromString($url_ver, json_encode($data));
$zip -> close();
// unlink($real_file_url);
}
}
}
$new_path = str_replace("./Uploads/", "", $file_url);
if (get_tool_status("oss_storage") == 1) {
if(C("oss_storage.is_cname")){
$to = "http://".C("oss_storage.domain")."/".str_replace("Ios/original","SourcePack",$new_path);
}else{
$to = "https://".C("oss_storage.bucket").".".C("oss_storage.domain")."/".str_replace("Ios/original","SourcePack",$new_path);
$to = str_replace('-internal', '', $to);
}
$updata['savename'] = $newFileName;
$updata['path'] = $new_file_url;
$this -> upload_game_pak_oss($updata);
} elseif (get_tool_status("cos_storage") == 1) {
$updata['path'] = $new_file_url;
$updata['savename'] = 'SourcePack/'. $newFileName;
$to = $this->upload_cos($updata);
}
$model = D('Game_source');
$model->where(['game_id'=>$game_id])->save(['file_url'=>$to, 'original_url' => $originalUrl]);
$plist = A("Plist");
if ($file_type == 2) {
$data['plist_url'] = "./Uploads/SourcePlist/" . $data['game_id'] . ".Plist";
$plist -> create_plist($game_id, 0, $baoName, $to);
$plist -> create_org_plist($game_id, 0, $baoName, $originalUrl);
}
}
public function upload_game_pak_oss($return_data = null)
{
/**
* 根据Config配置得到一个OssClient实例
*/
try {
Vendor('OSS.autoload');
$ossClient = new \OSS\OssClient(C("oss_storage.accesskeyid"), C("oss_storage.accesskeysecr"), C("oss_storage.domain"), C("oss_storage.is_cname"));
} catch (OssException $e) {
$this -> error($e -> getMessage());
}
$bucket = C('oss_storage.bucket');
// if(preg_match('/.apk/',$return_data['savename']) ){
$oss_name = "SourcePack";
// }else{
// $oss_name="IosGamePack";
// }
$oss_file_path = $oss_name . "/" . $return_data["savename"];
$avatar = $return_data["path"];
try {
$this -> multiuploadFile($ossClient, $bucket, $oss_file_path, $avatar);
return true;
} catch (OssException $e) {
/* 返回JSON数据 */
$this -> error($e -> getMessage());
}
}
public function multiuploadFile($ossClient, $bucket, $url, $file)
{
//$file = __FILE__;
$options = array();
try {
#初始化分片上传文件
$uploadId = $ossClient -> initiateMultipartUpload($bucket, $url);
//$ossClient->multiuploadFile($bucket, $url, $file, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": initiateMultipartUpload FAILED\n");
$this -> error('1' . $e -> getMessage() . "\n");
return;
}
/*
* step 2. 上传分片
*/
$partSize = 5 * 1000 * 1024;
$uploadFile = $file;
$uploadFileSize = filesize($uploadFile);
$pieces = $ossClient -> generateMultiuploadParts($uploadFileSize, $partSize);
$responseUploadPart = array();
$uploadPosition = 0;
$isCheckMd5 = true;
foreach ($pieces as $i => $piece) {
$fromPos = $uploadPosition + (integer) $piece[$ossClient ::OSS_SEEK_TO];
$toPos = (integer) $piece[$ossClient ::OSS_LENGTH] + $fromPos - 1;
$upOptions = array(
$ossClient ::OSS_FILE_UPLOAD => $uploadFile,
$ossClient ::OSS_PART_NUM => ($i + 1),
$ossClient ::OSS_SEEK_TO => $fromPos,
$ossClient ::OSS_LENGTH => $toPos - $fromPos + 1,
$ossClient ::OSS_CHECK_MD5 => $isCheckMd5,
);
if ($isCheckMd5) {
$contentMd5 = \OSS\Core\OssUtil ::getMd5SumForFile($uploadFile, $fromPos, $toPos);
$upOptions[$ossClient ::OSS_CONTENT_MD5] = $contentMd5;
}
//2. 将每一分片上传到OSS
try {
$responseUploadPart[] = $ossClient -> uploadPart($bucket, $url, $uploadId, $upOptions);
} catch (OssException $e) {
printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} FAILED\n");
$this -> error($e -> getMessage() . "\n");
return;
}
}
$uploadParts = array();
foreach ($responseUploadPart as $i => $eTag) {
$uploadParts[] = array(
'PartNumber' => ($i + 1),
'ETag' => $eTag,
);
}
/**
* step 3. 完成上传
*/
try {
$ossClient -> completeMultipartUpload($bucket, $url, $uploadId, $uploadParts);
} catch (OssException $e) {
printf(__FUNCTION__ . ": completeMultipartUpload FAILED\n");
$this -> error($e -> getMessage() . "\n");
return;
}
}
/**
* 腾讯云上传
*
* @param null $data
*
* @return mixed
*
* @author: 鹿文学[lwx]<fyj301415926@126.com>
* @since: 2019\4\18 0018 17:33
*/
public function upload_cos($data=null) {
Vendor('COS5.vendor.autoload');
$schema=C("cos_storage.schema")?:"https";
$secretId = C("cos_storage.SecretId"); //"云 API 密钥 SecretId";
$secretKey = C("cos_storage.SecretKey"); //"云 API 密钥 SecretKey";
$region = C("cos_storage.domain"); //设置一个默认的存储桶地域
$cosClient = new \Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => $schema, //协议头部默认为http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey
)
)
);
$bucket = C("cos_storage.bucket"); //存储桶名称 格式BucketName-APPID
$key = $data['savename'];
$srcPath = $data["path"];//本地文件绝对路径
try {
$result = $cosClient->Upload(
$bucket = $bucket,
$key = $key,
$body = fopen($srcPath, 'rb')
);
$location = $result['Location'];
if(!preg_match('/^(http)|(https)/', $location)) {
$result['Location'] = 'https://' . $result['Location'];
}
return $result['Location'];
} catch (\Exception $e) {
$this->error($e);
}
}
}