Commit 547c553d by 杨树贤

添加按钮文字返回

parent f7e4e263
<?php
namespace App\Http\Controllers;
use App\Model\GoodsModel;
use App\Model\UploadGoodsModel;
use Illuminate\Http\Request;
class ApiController extends Controller
{
public function GoodsInfo(Request $request){
$Field = ['goods_name/like','status/eq','start_time','end_time','goods_id/eq','user_id/eq','offset','p'];
$collert = TrimX($Field);
if(!empty($collert['start_time'])){
$collert['start_time'] = strtotime($collert['start_time']);
$collert['update_time/gt'] = $collert['start_time'];
unset($collert['start_time']);
}
if(!empty($collert['end_time'])){
$collert['end_time'] = strtotime($collert['end_time']);
$collert['update_time/lt'] = $collert['end_time'];
unset($collert['end_time']);
}
//设置有图片的优先
$collert ['goods_images/order'] = 'desc';
if(!empty($request->user->user_id))
$collert['user_id'] = $request->user->user_id;
else{
$collert['status/eq'] = 1;
$collert['audit_status/eq'] = 1;
}
$GoodsModel = new GoodsModel();
$result = $GoodsModel->FindGoods($collert);
if(!$result)
return $this->Export(ErrorCode(001,1),'没有数据');
return $this->Export(0,'ok',$result);
}
public function AddGoods(Request $request){
$Filed = ['goods_name','brand_name','encap','price','stock','delivery_time','is_auto','goods_images','source',
'currency','ladder_price'];
$collert = TrimX($Filed,true);
$MustFile = [
'goods_name'=> [
'errcode'=>005,'errmsg'=>'请输入商品型号'
], 'brand_name'=>[
'errcode'=>002,'errmsg'=>'请输入品牌'
],'delivery_time'=>[
'errcode'=>003,'errmsg'=>'请输入货期'
],'stock'=>[
'errcode'=>004,'errmsg'=>'请输入库存'
]];
foreach ($MustFile as $k=>$v)
if(empty($collert[$k])) return $this->Export(ErrorCode($v['errcode'],1),$v['errmsg']);
$collert['user_id'] = $request->user->user_id;
$GoodsModel = new GoodsModel();
$result = $GoodsModel->AddGoods($collert);
if($result === false)
return $this->Export(ErrorCode(001,5),'新增失败');
if($result !== true)
return $this->Export(ErrorCode(006,1),'新增失败');
return $this->Export(0,'新增成功');
}
public function SaveGoods($request){
$Filed = ['goods_name','brand_name','encap','price','stock','delivery_time','is_auto','goods_images',
'currency','ladder_price','goods_id'];
$collert = TrimX($Filed);
if(empty($collert['goods_id'])) return $this->Export(ErrorCode(010,1),'请选择要编辑的商品');
$GoodsModel = new GoodsModel();
$result = $GoodsModel ->GoodsInfo($collert['goods_id']);
if(!$result || empty($result[$collert['goods_id']]))
return $this->Export(ErrorCode(002,5),'没有查找到这个型号');
if($result[$collert['goods_id']]['user_id'] != $request->user->user_id)
return $this->Export(ErrorCode(8,1),'没有编辑这个商品的权限');
$result = $GoodsModel->SaveGoods($collert);
if($result === false)
return $this->Export(ErrorCode(003,5),'编辑失败');
if($result !== true)
return $this->Export(ErrorCode(9,1),'编辑失败');
return $this->Export(0,'ok','新增成功');
}
public function GoodsStatus(Request $request){
$collert = TrimX(['goods_id','status']);
$MustField = ['goods_id'=>'未选择商品','status'=>'未选择操作状态'];
foreach ($MustField as $k=>$v)
if(empty($collert[$k]))
return $this->Export(ErrorCode(004,1),$v);
$MustFile = [
'goods_id'=> [
'errcode'=>010,'errmsg'=>'请选择商品'
], 'status'=>[
'errcode'=>011,'errmsg'=>'未选择操作状态'
]];
foreach ($MustFile as $k=>$v)
if(empty($collert[$k])) return $this->Export(ErrorCode($v['errcode'],1),$v['errmsg']);
$Url = config('website.GoodsInfoUrl').'/goods/status';
$collert['user_id'] = $request->user->user_id;
$result = reportCurl($Url,$collert,true);
$result = json_decode($result,true);
if(!isset($result['errcode']) || empty($result['data']))
return $this->Export(ErrorCode(004,5),'操作失败');
if($result['errcode'] !==0 )
return $this->Export(ErrorCode(005,5),'数据异常,操作失败');
if(is_string($collert['goods_id'])){
if(empty($result['data'][$collert['goods_id']]) && $result['data'][$collert['goods_id']] !==true )
return $this->Export(ErrorCode(006,5),'操作失败');
return $this->Export(0,'操作成功');
}else{
$Fail=0;
$su=0;
foreach ($collert['goods_id'] as $k=>$v){
if(empty($result['data'][$v]) || $result['data'][$v] !==true )
$Fail++;
else $su++;
}
if($su > 0){
if($Fail === 0) return $this->Export(0,'操作成功');
else return $this->Export(0,'操作成功数量:'.$su.',操作失败数量:'.$Fail,['data'=>$result['data']]);
} else{
return $this->Export(ErrorCode(007,5),'操作失败',['data'=>$result['data']]);
}
}
}
public function MysqlGoodsCount(Request $request){
$data['count']['mygoods'] = 0;
$data['count']['newadd'] = 0;
$data['count']['imperfect'] = 0;
$map['user_id/eq'] = $request->user->user_id;
$Url = config('website.ES_URL').'/search/get_data';
$result = reportCurl($Url,$map,true);
$result = json_decode($result,true);
if(!empty($result['data']['total']))
$data['count']['mygoods'] = $result['data']['total'];
$map['add_time/gt'] = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));//本周0点
$result = json_decode(reportCurl($Url,$map,true),true);
if(!empty($result['data']['total']))
$data['count']['newadd'] = $result['data']['total'];
unset($map['add_time/gt']);
$map['goods_images/eq'] = 2;//1是有图,2是没图
$result = json_decode(reportCurl($Url,$map,true),true);
if(!empty($result['data']['total']))
$data['count']['imperfect'] = $result['data']['total'];
return $this->Export(0,'ok',$data);
}
public function UploadGoods(Request $request)
{
$file = $request->file('file');
if($file -> isValid()) {
$realPath = $file -> getRealPath();
$data['user_id'] = $request->user->user_id;
$Model = new UploadGoodsModel();
$ID = 0;
$result = $Model->UploadGoods($data,$realPath,$ID);
if(!$result)
return $this->Export(ErrorCode(8,5),'上传失败');
if($result !== true)
return $this->Export(ErrorCode(010,1),'上传失败');
$Url = config('website.GoodsInfoUrl').'/goods/task';
$map['user_id'] = $request->user->user_id;
$map['id'] = $ID;
$result = reportCurl($Url,$map,true);
$result = json_decode($result,true);
if(!isset($result['errcode']) || $result['errcode'] !== 0)
return $this->Export(ErrorCode(9,5),'推送任务失败,请联系客服处理');
return $this->Export(0,'上传成功,请等待处理结果');
}else
return $this->Export(ErrorCode(011,1),'没有上传文件');
}
public function UploadGoodsList(Request $request){
$collert = TrimX(['start_time','end_time','limit']);
if(!empty($collert['start_time'])){
$collert['start_time'] = strtotime($collert['start_time']);
$collert['update_time/gt'] = $collert['start_time'];
unset($collert['start_time']);
}
if(!empty($collert['end_time'])){
$collert['end_time'] = strtotime($collert['end_time']);
$collert['update_time/lt'] = $collert['end_time'];
unset($collert['end_time']);
}
if(!empty($collert['limit'])){
$limit = $collert['limit'];
unset($collert['limit']);
}else $limit = 10;
$collert['user_id'] = $request->user->user_id;
$Model = new UploadGoodsModel();
$result = $Model->UploadGoodsList($collert,$limit);
return $this->Export(0,'ok',$result);
}
}
<?php
namespace App\Http\Controllers;
class ExampleController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
//
}
......@@ -40,7 +40,7 @@ class IntegralsController extends Controller
$userIntegral = $userIntegralService->getUserIntegral(['id' => $userId]);
if ($userIntegral) {
$result['integral'] = $userIntegral['data']['integral'];
$result['code'] = 'RD' . $userId;
$result['code'] = 'RDD' . $userId;
}
return $this->Export(0, 'ok', $result);
......
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;
use OSS\OssClient; // OSS客户端类
use OSS\Core\OssException; // OSS异常类
$path = preg_replace('/public/', '', $_SERVER['DOCUMENT_ROOT']);
require_once $path.'/vendor/aliyuncs/oss-sdk-php/autoload.php';
use App\Model\Oss\FileModel;
use App\Model\Oss\ImageModel;
use App\Model\Oss\UploadLogModel;
class OssController extends Controller
{
// 允许的请求类型列表
protected static $allowMethod = array('POST', 'GET', 'PUT', 'DELETE');
/**
* 接收数据
* @return [Json] [返回信息给前端]
*/
public function upload(Request $request)
{
$requireMethod = strtoupper($_SERVER['REQUEST_METHOD']); // 请求类型
if (!in_array($requireMethod, self::$allowMethod)) return $this->Export(ErrorCode(205));
$res = $this->validUser($request); // 校验用户
if ($res['errcode'] != 0) return $this->Export($res['errcode']);
$uid = $res['data']['uid']; // 用户ID
$uploadNum = $res['data']['uploadNum']; // 最大上传数量
$res = $this->checkFile($_FILES, $uploadNum); // 校验文件
if ($res !== true) return $this->Export($res['errcode']);
$res = $this->returnFileUrl($_REQUEST, $_FILES, $uid); // 上传到OSS,并返回URL
$datas['data'] = $res['data'];
return $this->Export($res['errcode'], $res['errmsg'], $datas);
}
// 校验上传用户(source:1-内部用户,2-外部用户)
public function validUser($request)
{
// 检查传递过来的参数
$source = $request->input('source', '');
if (!$source) return $this->_json(ErrorCode(201), '参数缺失,请检查传递的参数');
// 内部或外部---图片限制和登录校验
if ($source == 1) {
$uploadNum = Config('alioss.InternalNum');
if (!$this->auth($request)) return $this->_json(ErrorCode(202), '内部免验证不通过');
$uid = 0; // admin---后台上传
} else {
$uploadNum = Config('alioss.ExternalNum');
//检查登录
$res = $this->checkLogin($request);
if ($res['errcode'] != 0) return $this->_json($res['errcode'], $res['errmsg']);
$uid = $res['data']['user_id'];
}
$data = ['uid' => $uid, 'uploadNum' => $uploadNum];
return $this->_json(0, '', $data);
}
/**
* 文件校验
* @param [Array] $file [文件]
* @param [Integer] $num [文件数量限制]
* @return [Boolen / Json]
*/
public function checkFile($file, $num)
{
$FileModel = new FileModel();
// 多文件上传
if (is_array($file['upload']['name'])) {
$fileNum = count($file['upload']['name']); // 上传文件数量
if ($fileNum > $num) return $this->_json(ErrorCode(203), '上传文件数量超出限制,最大限制为'.$num);
$data = array();
for ($i = 0; $i < $fileNum; $i++){
$type = $FileModel->fileType($file['upload']['type'][$i]);
if ($type === false) $this->_json(ErrorCode(204), '暂时不支持此文件类型上传');
$data['upload']['name'] = $file['upload']['name'][$i];
$data['upload']['type'] = $file['upload']['type'][$i];
$data['upload']['tmp_name'] = $file['upload']['tmp_name'][$i];
$data['upload']['error'] = $file['upload']['error'][$i];
$data['upload']['size'] = $file['upload']['size'][$i];
$res = $this->verifyFileType($type, $data);
if ($res !== true) return $res;
unset($data);
}
return true;
} else {
$type = $FileModel->fileType($file['upload']['type']);
if ($type === false) $this->_json(ErrorCode(204), '暂时不支持此文件类型上传');
return $this->verifyFileType($type, $file);
}
}
/**
* 根据文件类型验证文件
* @param [String] $type [类型]
* @param [Array] $file [文件]
* @return [Boolen / Json]
*/
public function verifyFileType($type, $file)
{
$ImageModel = new ImageModel();
$FileModel = new FileModel();
$size = $file['upload']['size'];
switch ($type) {
case 'images':
$explode = explode('.', $file['upload']['name']);
$ext = $explode[count($explode)-1];
if (!$ImageModel->checkImageExt($ext)) return $this->_json(ErrorCode(204), $file['upload']['name'].'暂不支持此文件类型上传');
if (!$ImageModel->checkImageSize($size)) return $this->_json(ErrorCode(206), $file['upload']['name'].'过大,最大限制为2M,请重新上传');
return true;
break;
case 'doc/word':
case 'doc/excel':
case 'doc/ppt':
case 'doc/pdf':
case 'doc/txt':
case 'doc/zip':
if (!$FileModel->checkDocSize($size)) return $this->_json(ErrorCode(207), $file['upload']['name'].'过大,最大限制为20M,请重新上传');
return true;
break;
case 'video':
if (!$FileModel->checkVideoSize($size)) return $this->_json(ErrorCode(208), $file['upload']['name'].'过大,最大限制为50M,请重新上传');
return true;
break;
case 'audio/mp3':
case 'audio/wmv':
if (!$FileModel->checkAudioSize($size)) return $this->_json(ErrorCode(209), $file['upload']['name'].'过大,最大限制为30M,请重新上传');
return true;
break;
default:
return $this->_json(ErrorCode(210), '没匹配到对应的文件类型!');
break;
}
}
/**
* ossClient实例化
* @return [Object]
*/
public function ossClient()
{
$accessKeyId = Config('alioss.AccessKeyId');
$accessKeySecret = Config('alioss.AccessKeySecret');
$endPoint = Config('alioss.EndPoint');
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endPoint);
return $ossClient;
}
/**
* 返回URL给客户端
* @param [Array] $request [上传参数]
* @param [Array] $file [上传文件]
* @param [Integer] $uid [用户ID]
* @return [Json]
*/
public function returnFileUrl($request, $file, $uid)
{
// 多文件上传
if (is_array($file['upload']['name'])) {
$fileNum = count($file['upload']['name']); // 上传文件数量
for ($i = 0; $i < $fileNum; $i++) {
$data['upload']['name'] = $file['upload']['name'][$i];
$data['upload']['type'] = $file['upload']['type'][$i];
$data['upload']['tmp_name'] = $file['upload']['tmp_name'][$i];
$response = $this->setObject($request, $data); // 设置object和水印
$res = $this->uploadToOss($request, $response, $data, $uid);
if ($res['errcode'] != 0) return $res;
$object[$i] = $response['object'];
// 拼接URL
$return_url[$i][] = $res['data'];
$return_url[$i][] = $file['upload']['name'][$i];
}
return $this->_json(ErrorCode(200), 'success', $return_url);
} else {
$response = $this->setObject($request, $file); // 设置object和水印
$res = $this->uploadToOss($request, $response, $file, $uid);
if ($res['errcode'] != 0) return $res;
$return_url[] = $res['data'];
$return_url[] = $file['upload']['name'];
return $this->_json(ErrorCode(200), 'success', $return_url);
}
}
// 获取来源页面
public function getRefererUrl()
{
$origin_url = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
if (!$origin_url) {
$origin_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; // 来源页面
}
return $origin_url ? $origin_url : '';
}
/**
* / 设置object
* @param [type] $request [description]
* @param [type] $file [description]
*/
public function setObject($request, $file)
{
$dir_name = ''; // 目录
$test_dir_name = ''; // 测试目录
$goods_name = isset($request['goods_name']) ? $request['goods_name'] : ''; // 基石商品名称变量
$is_rename = isset($request['is_rename']) ? $request['is_rename'] : 1; // 是否改变原文件名,默认1,0-否,1-是
$set_dir = isset($request['set_dir']) ? $request['set_dir'] : ''; // 自定义目录,默认空
$file_name = $file['upload']['name']; // 文件名
$file_type = $file['upload']['type']; // 文件类型
$file_tmp_name = $file['upload']['tmp_name']; // 临时路径
$origin_url = $this->getRefererUrl(); // 来源页面
// 检查是否为测试数据,设置测试目录
// if (preg_match('/icsales.cc/', $origin_url))
if (preg_match('/^http(s?)\:\/\/[sz|szm|t|tm]/', $origin_url) || preg_match('/icsales.cc/', $origin_url)) {
$test_dir_name = 'test/';
$dir_name .= $test_dir_name;
}
$explode = explode('.', $file_name);
$ext = $explode[count($explode)-1]; // 获取文件后缀
$date = explode('-', date('Y-m-d', time())); // 分割日期
// 是否改变文件名
if ($is_rename) {
$rename = md5(time().rand('10000', '99999')).'.'.strtolower($ext); // 新文件名
} else {
$rename = $file_name;
}
$FileModel = new FileModel();
$file_type_dir = $FileModel->fileType($file_type); // 获取对应文件类型
$dir_name .= $file_type_dir.'/';
// 自定义目录
if ($set_dir) {
$response['object'] = $set_dir.$rename;
$response['imageWater'] = '';
} else {
// 根据网站类型设置目录
if (preg_match('/^http(s?)\:\/\/[sz|szm|home|www|m]/', $origin_url)) {
$dir_name .= 'icsales/';
} else if (preg_match('/^http\:\/\/[footstone]/', $origin_url) && $goods_name != 'undefined' && $goods_name != '') {
$dir_name .= 'goods/';
} else {
$dir_name .= 'cms/';
}
$response['object'] = $dir_name.$date[0].$date[1].'/'.$date[2].'/'.$rename;
$response['imageWater'] = '';
}
return $response;
}
/**
* 上传到OSS
* @param [type] $request [上传参数]
* @param [type] $upload [object和水印]
* @param [type] $file [上传文件]
* @param [type] $uid [用户ID]
* @return [type] [description]
*/
public function uploadToOss($request, $upload, $file, $uid)
{
$ossClient = $this->ossClient();
$bucket = Config('alioss.Bucket');
$object = $upload['object'];
$endPoint = Config('alioss.EndPoint');
$endPoint = substr($endPoint, stripos($endPoint, '//')+2);
if ($upload['imageWater']) {
$localPath = $upload['imageWater'];
} else {
$localPath = $file['upload']['tmp_name']; // 本地临时路径
}
try {
$res = $ossClient->uploadFile($bucket, $object, $localPath);
// unlink($localPath);
// 上传后处理
if (isset($res['info']['http_code']) && $res['info']['http_code'] == 200) {
$oss_url = $bucket.'.'.$endPoint.'/'.$object;
$img_url = Config('alioss.OSS_URL').$object;
$this->uploadLog($request, $uid, $file['upload']['name'], 1, $oss_url, $img_url); // 记录到日志表
return $this->_json(0, '上传成功', $img_url);
} else {
$this->uploadLog($request, $uid, $file['upload']['name'], 0); // 记录到日志表
return $this->_json(ErrorCode(211), json_encode($res));
}
} catch (Exception $e) {
return $e->getMessage();
}
}
/**
* 记录到日志表
* @param [Integer] $request [上传参数]
* @param [Integer] $uid [用户ID]
* @param [Varchar] $file_name [文件名]
* @param [Integer] $status [状态]
* @param [String] $oss_url [oss url]
* @param [String] $img_url [img url]
* @return null / Exception
*/
public function uploadLog($request, $uid, $file_name, $status, $oss_url='', $img_url='')
{
$UploadLogModel = new UploadLogModel();
$data['uid'] = $uid;
$data['file_name'] = $file_name;
$data['source'] = $request['source'];
$data['status'] = $status;
$data['oss_url'] = $oss_url;
$data['img_url'] = $img_url;
$data['referer_url'] = $this->getRefererUrl();
$data['ip'] = get_client_ip(0, true);
$data['create_time'] = time();
try{
$UploadLogModel->insert($data);
}catch(Exception $e){
return $e->getMessage();
}
}
}
......@@ -7,12 +7,48 @@ use Common\Model\RedisModel;
class IntegralService
{
const INTEGRAL_TYPE_INVITE_FRIEND = 1;
const INTEGRAL_TYPE_UPLOAD_GOODS = 2;
const INTEGRAL_TYPE_INTEGRAL_CODE = 3;
const INTEGRAL_TYPE_SHARE = 4;
const INTEGRAL_TYPE_OFFER = 5;
const INTEGRAL_TYPE_CHECK_IN = 6;
public function getIntegralList($map = [])
{
$url = config('website.BaseUrl') . '/integrals/list';
$result = reportCurl($url, $map, true);
$result = json_decode($result, true);
//针对红包码兑换这块
//因为红包码兑换只能操作一次,所以要给一个单独的状态值告诉前端是否已经兑换过红包了
if (isset($result['data'])) {
foreach ($result['data'] as $key => $value) {
switch ($value['id']) {
case self::INTEGRAL_TYPE_INVITE_FRIEND:
$result['data'][$key]['button_text'] = '去邀请';
break;
case self::INTEGRAL_TYPE_UPLOAD_GOODS:
$result['data'][$key]['button_text'] = '去上传';
break;
case self::INTEGRAL_TYPE_INTEGRAL_CODE:
$result['data'][$key]['button_text'] = '去兑换';
break;
case self::INTEGRAL_TYPE_SHARE:
$result['data'][$key]['button_text'] = '去分享';
break;
case self::INTEGRAL_TYPE_OFFER:
$result['data'][$key]['button_text'] = '去报价';
break;
case self::INTEGRAL_TYPE_CHECK_IN:
$result['data'][$key]['button_text'] = '签到';
break;
}
}
}
return $result;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment