<?php
namespace eapie\source\request\shop;
use eapie\main;
use eapie\error;
class admin_goods_file extends \eapie\source\request\shop {
public function api_edit_check(){
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_FILE_EDIT);
return true;
}
public function api_edit($data = array()){
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_FILE_EDIT);
object(parent::ERROR)->check($data, 'shop_goods_file_id', parent::TABLE_SHOP_GOODS_FILE, array('args'));
if( isset($data['file_name']) )
object(parent::ERROR)->check($data, 'file_name', parent::TABLE_FILE, array('format', 'length'));
if( isset($data['file_sort']) )
object(parent::ERROR)->check($data, 'file_sort', parent::TABLE_FILE, array('args'));
$shop_goods_file_data = object(parent::TABLE_SHOP_GOODS_FILE)->find($data['shop_goods_file_id']);
if( empty($shop_goods_file_data) ){
throw new error("ID有误,数据不存在");
}
$shop_goods_data = object(parent::TABLE_SHOP_GOODS)->find($shop_goods_file_data['shop_goods_id']);
if( empty($shop_goods_data) ){
throw new error("商品ID有误,数据不存在");
}
if( !object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_ADMINISTRATOR, true) ){
if( $shop_goods_data['user_id'] != $_SESSION['user_id'] ){
throw new error("权限不足,不能操作非自己添加的数据");
}
}
if( empty($shop_goods_file_data['file_id']) ){
throw new error("文件ID为空");
}
$file_data = object(parent::TABLE_FILE)->find($shop_goods_file_data['file_id']);
if( empty($file_data) ){
throw new error("文件ID有误,数据不存在");
}
$update = array(
"where" => array( array('file_id=[+]', (string)$file_data['file_id']) ),
"data" => array()
);
$whitelist = array(
'file_name',
'file_sort',
);
$update["data"] = cmd(array($data, $whitelist), 'arr whitelist');
if( !empty($update["data"]) ){
foreach($update["data"] as $key => $value){
if( isset($file_data[$key]) ){
if($file_data[$key] == $value){
unset($update["data"][$key]);
}
}
}
}
if( empty($update["data"]) ){
throw new error("没有需要更新的数据");
}
$update["data"]['file_update_time'] = time();
if( object(parent::TABLE_FILE)->update( $update["where"], $update["data"]) ){
object(parent::TABLE_SHOP_GOODS)->update(
array( array('shop_goods_id=[+]', $shop_goods_file_data['shop_goods_id']) ),
array('shop_goods_update_time' => time() )
);
object(parent::TABLE_ADMIN_LOG)->insert($data, $update);
return $data['shop_goods_file_id'];
}else{
throw new error("操作失败");
}
}
public function api_list($data = array()){
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_READ);
$config = array(
'orderby' => array(),
'where' => array(),
'limit' => object(parent::REQUEST)->limit($data, parent::REQUEST_ADMIN),
);
$config["orderby"] = object(parent::REQUEST)->orderby($data, array(
'sort_desc' => array('file_sort', true),
'sort_asc' => array('file_sort', false),
'name_desc' => array('file_name', true),
'name_asc' => array('file_name', false),
'type_desc' => array('file_type', true),
'type_asc' => array('file_type', false),
'size_desc' => array('file_size', true),
'size_asc' => array('file_size', false),
'state_desc' => array('file_state', true),
'state_asc' => array('file_state', false),
'insert_time_desc' => array('file_insert_time', true),
'insert_time_asc' => array('file_insert_time', false),
'update_time_desc' => array('file_update_time', true),
'update_time_asc' => array('file_update_time', false),
));
$config["orderby"][] = array('shop_goods_file_id', false);
if( !object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_ADMINISTRATOR, true) ){
$config["where"][] = array('[and] sg.user_id=[+]', $_SESSION['user_id']);
}
if(!empty($data['search'])){
if( isset($data['search']['shop_goods_id']) && is_string($data['search']['shop_goods_id']) ){
$config["where"][] = array('[and] sgf.shop_goods_id=[+]', $data['search']['shop_goods_id']);
}
}
return object(parent::TABLE_SHOP_GOODS_FILE)->select_page($config);
}
public function api_qiniu_uptoken($data = array()){
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_FILE_UPLOAD);
object(parent::ERROR)->check($data, 'shop_goods_id', parent::TABLE_SHOP_GOODS, array('args'));
object(parent::ERROR)->check($data, 'file_name', parent::TABLE_FILE, array('format', 'length'));
object(parent::ERROR)->check($data, 'file_type', parent::TABLE_FILE, array('format'));
object(parent::ERROR)->check($data, 'file_size', parent::TABLE_FILE, array('args', 'empty'));
object(parent::ERROR)->check($data, 'file_format', parent::TABLE_FILE, array('args'));
$shop_goods_data = object(parent::TABLE_SHOP_GOODS)->find($data['shop_goods_id']);
if( empty($shop_goods_data) ){
throw new error("商品ID有误,数据不存在");
}
if( !object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_ADMINISTRATOR, true) ){
if( $shop_goods_data['user_id'] != $_SESSION['user_id'] ){
throw new error("权限不足,不能操作非自己添加的数据");
}
}
$qiniu_config = object(parent::TABLE_CONFIG)->data(object(parent::TABLE_CONFIG)->find("qiniu_access"), true);
if( empty($qiniu_config["bucket"]) ){
throw new error("配置异常");
}
$whitelist = array(
'file_name',
'file_type',
'file_size',
'file_format',
);
$file_insert_data = cmd(array($data, $whitelist), 'arr whitelist');
$file_insert_data['file_id'] = object(parent::TABLE_FILE)->get_unique_id();
$file_insert_data['user_id'] = $_SESSION['user_id'];
$file_insert_data['file_path'] = $qiniu_config["bucket"] $file_insert_data['file_state'] = 0;
$file_insert_data['file_insert_time'] = time();
$file_insert_data['file_update_time'] = time();
if( !object(parent::TABLE_FILE)->insert($file_insert_data) ){
throw new error("文件登记失败");
}
$insert_data = array(
"shop_goods_file_id" => object(parent::TABLE_SHOP_GOODS_FILE)->get_unique_id(),
"user_id" => $_SESSION['user_id'],
"shop_goods_id" => $data["shop_goods_id"],
"file_id" => $file_insert_data['file_id'],
"shop_goods_file_time" => time(),
);
if( !object(parent::TABLE_SHOP_GOODS_FILE)->insert($insert_data) ){
object(parent::TABLE_FILE)->remove($file_insert_data['file_id']);
throw new error("商品文件登记失败");
}
$qiniu_config["expires"] = 7200 $qiniu_config["policy"] = array(
'returnBody' => '{"key":"$(key)","hash":"$(etag)","type":$(mimeType),"size":$(fsize),"name":$(fname),"bucket":"$(bucket)"}',
);
$qiniu_uptoken = object(parent::PLUGIN_QINIU)->uptoken($qiniu_config);
if( !empty($qiniu_uptoken["errno"]) ){
object(parent::TABLE_FILE)->remove($file_insert_data['file_id']);
object(parent::TABLE_SHOP_GOODS_FILE)->remove($insert_data["shop_goods_file_id"]);
throw new error($qiniu_uptoken["error"]);
}
object(parent::TABLE_SHOP_GOODS)->update(
array( array('shop_goods_id=[+]', $data['shop_goods_id']) ),
array('shop_goods_update_time' => time() )
);
object(parent::TABLE_ADMIN_LOG)->insert($data, array("shop_goods_file"=>$insert_data, "file"=>$file_insert_data));
return array("qiniu_uptoken" => $qiniu_uptoken["data"], "file_id" => $file_insert_data["file_id"], "shop_goods_file_id" => $insert_data["shop_goods_file_id"]);
}
public function api_qiniu_state($data = array()){
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_FILE_UPLOAD);
object(parent::ERROR)->check($data, 'shop_goods_file_id', parent::TABLE_SHOP_GOODS_FILE, array('args'));
object(parent::ERROR)->check($data, 'file_type', parent::TABLE_FILE, array('format'));
object(parent::ERROR)->check($data, 'file_size', parent::TABLE_FILE, array('args'));
object(parent::ERROR)->check($data, 'file_hash', parent::TABLE_FILE, array('args'));
object(parent::ERROR)->check($data, 'file_path', parent::TABLE_FILE, array('args'));
$shop_goods_file_data = object(parent::TABLE_SHOP_GOODS_FILE)->find($data['shop_goods_file_id']);
if( empty($shop_goods_file_data) ){
throw new error("ID有误,数据不存在");
}
$shop_goods_data = object(parent::TABLE_SHOP_GOODS)->find($shop_goods_file_data['shop_goods_id']);
if( empty($shop_goods_data) ){
throw new error("商品ID有误,数据不存在");
}
if( !object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_ADMINISTRATOR, true) ){
if( $shop_goods_data['user_id'] != $_SESSION['user_id'] ){
throw new error("权限不足,不能操作非自己添加的数据");
}
}
if( empty($shop_goods_file_data['file_id']) ){
throw new error("文件ID为空");
}
$update = array(
"where" => array( array('file_id=[+]', (string)$shop_goods_file_data['file_id']) ),
"data" => array()
);
$whitelist = array(
'file_type',
'file_size',
'file_hash',
'file_path',
);
$update["data"] = cmd(array($data, $whitelist), 'arr whitelist');
$update["data"]['file_state'] = 1;
$update["data"]['file_update_time'] = time();
if( object(parent::TABLE_FILE)->update( $update['where'], $update["data"]) ){
object(parent::TABLE_SHOP_GOODS)->update(
array( array('shop_goods_id=[+]', $shop_goods_file_data['shop_goods_id']) ),
array('shop_goods_update_time' => time() )
);
object(parent::TABLE_ADMIN_LOG)->insert($data, $update);
return $data['shop_goods_file_id'];
}else{
throw new error("操作失败");
}
}
public function api_qiniu_remove($data = array()){
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_FILE_REMOVE);
object(parent::ERROR)->check($data, 'shop_goods_file_id', parent::TABLE_SHOP_GOODS_FILE, array('args'));
$shop_goods_file_data = object(parent::TABLE_SHOP_GOODS_FILE)->find($data['shop_goods_file_id']);
if( empty($shop_goods_file_data) ){
throw new error("ID有误,数据不存在");
}
$shop_goods_data = object(parent::TABLE_SHOP_GOODS)->find($shop_goods_file_data['shop_goods_id']);
if( empty($shop_goods_data) ){
throw new error("商品ID有误,数据不存在");
}
if( !object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_ADMINISTRATOR, true) ){
if( $shop_goods_data['user_id'] != $_SESSION['user_id'] ){
throw new error("权限不足,不能操作非自己添加的数据");
}
}
if( !empty($shop_goods_file_data['file_id']) ){
$qiniu_config = object(parent::TABLE_CONFIG)->data(object(parent::TABLE_CONFIG)->find("qiniu_access"), true);
if( empty($qiniu_config) ){
throw new error("配置异常");
}
$qiniu_config["key"] = $shop_goods_file_data["file_id"];
$qiniu_uptoken = object(parent::PLUGIN_QINIU)->delete($qiniu_config);
if( !empty($qiniu_uptoken["errno"]) ){
throw new error($qiniu_uptoken["error"]);
}
object(parent::TABLE_FILE)->remove($shop_goods_file_data['file_id']);
}
if( object(parent::TABLE_SHOP_GOODS_FILE)->remove($data['shop_goods_file_id']) ){
object(parent::TABLE_SHOP_GOODS)->update(
array( array('shop_goods_id=[+]', $shop_goods_file_data['shop_goods_id']) ),
array('shop_goods_update_time' => time() )
);
object(parent::TABLE_ADMIN_LOG)->insert($data, $shop_goods_file_data);
return $data['shop_goods_file_id'];
}else{
throw new error("操作失败");
}
}
}
?>