118 lines
3.5 KiB
PHP
118 lines
3.5 KiB
PHP
<?php
|
||
// +------------------------------------------------+
|
||
// |http://www.cjango.com |
|
||
// +------------------------------------------------+
|
||
// | 修复BUG不是一朝一夕的事情,等我喝醉了再说吧! |
|
||
// +------------------------------------------------+
|
||
// | Author: 小陈叔叔 <Jason.Chen> |
|
||
// +------------------------------------------------+
|
||
namespace app\mobile\controller;
|
||
|
||
use app\common\model\Banner as BannerModel;
|
||
use app\common\model\Storage as StorageModel;
|
||
use app\common\service\Banner as BannerService;
|
||
|
||
class Mybanner extends _Init
|
||
{
|
||
public function _initialize()
|
||
{
|
||
parent::_initialize();
|
||
$this->nav = 5;
|
||
$this->my_uid = UID;
|
||
|
||
}
|
||
|
||
public function index()
|
||
{
|
||
$this->mybanner = BannerModel::where('uid',UID)->find();
|
||
return $this->fetch();
|
||
}
|
||
|
||
public function add()
|
||
{
|
||
if (IS_POST) {
|
||
$data = $this->request->post();
|
||
$res = BannerService::create($data);
|
||
if ($res === true) {
|
||
return $this->success('创建成功', 'mybanner/index');
|
||
} else {
|
||
return $this->error($res);
|
||
}
|
||
} else {
|
||
return $this->fetch();
|
||
}
|
||
}
|
||
|
||
|
||
public function edit()
|
||
{
|
||
if (IS_POST) {
|
||
$data = $this->request->post();
|
||
$res = BannerService::edit($data);
|
||
if ($res === true) {
|
||
return $this->success('编辑成功', 'mybanner/index');
|
||
} else {
|
||
return $this->error($res);
|
||
}
|
||
}else{
|
||
$this->mybanner = BannerModel::where('uid',UID)->find();
|
||
return $this->fetch('add');
|
||
}
|
||
}
|
||
|
||
public function del($uid)
|
||
{
|
||
$result = BannerService::del($uid);
|
||
return $this->back($result);
|
||
}
|
||
|
||
public function uploadPicture()
|
||
{
|
||
$input = $this->request->post();
|
||
$data = $input['files']['base64'];
|
||
$name = $input['files']['name'];
|
||
$hash = hash('md5', base64_decode($data));
|
||
$info = StorageModel::where('hash', $hash)->find();
|
||
if ($info) {
|
||
$return = array('code' => 1, 'msg' => '上传成功', 'id' => $info->id, 'url' => $info->path);
|
||
return $this->success($return);
|
||
} else {
|
||
// 获取图片
|
||
$temp = $data;
|
||
list($type, $data) = explode(',', $temp);
|
||
|
||
// 判断类型
|
||
if (strstr($type, 'image/jpeg') !== '') {
|
||
$ext = '.jpg';
|
||
} elseif (strstr($type, 'image/gif') !== '') {
|
||
$ext = '.gif';
|
||
} elseif (strstr($type, 'image/png') !== '') {
|
||
$ext = '.png';
|
||
}
|
||
|
||
// 生成的文件名
|
||
$path = "/uploads/image/";
|
||
$url = $path . $name;
|
||
$photo = '.' . $url;
|
||
// 生成文件
|
||
file_put_contents($photo, base64_decode($data));
|
||
$data = [
|
||
'hash' => $hash,
|
||
'type' => 'image',
|
||
'name' => $name,
|
||
'ext' => $ext,
|
||
'path' => $url,
|
||
'size' => $input['files']['size'],
|
||
];
|
||
if ($insert = StorageModel::create($data)) {
|
||
$return = array('code' => 1, 'msg' => '上传成功', 'url' => $url, 'id' => $insert->id);
|
||
return $this->success($return);
|
||
|
||
} else {
|
||
return $this->error('上传失败');
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|