1 line
3.3 KiB
PHP
Executable File
1 line
3.3 KiB
PHP
Executable File
<?php
|
||
namespace app\applet\controller\wechat\material;
|
||
|
||
use app\applet\controller\Controller;
|
||
use app\applet\model\Material as MaterialModel;
|
||
use app\applet\model\Wechat as WechatModel;
|
||
use think\facade\View;
|
||
|
||
/**
|
||
* 音频素材
|
||
*/
|
||
class Voice extends Controller
|
||
{
|
||
/**
|
||
* 列表
|
||
*/
|
||
public function index()
|
||
{
|
||
$wechat = WechatModel::detail($this->applet_id);
|
||
$model = new MaterialModel;
|
||
$list = $model->getList(20,$this->applet_id);
|
||
return View::fetch('../../admin/view/wechat/material/voice/index', compact('list','wechat'));
|
||
}
|
||
|
||
/**
|
||
* 添加
|
||
*/
|
||
public function add()
|
||
{
|
||
if ($this->request->isPost()) {
|
||
if(!$wechat = WechatModel::detail($this->applet_id)){
|
||
return $this->renderError('还未绑定公众号');
|
||
}
|
||
if($wechat['status']['value'] == 0){
|
||
return $this->renderError('还未绑定公众号');
|
||
}
|
||
$model = new MaterialModel;
|
||
// 新增记录
|
||
$file = request()->file('file');
|
||
// 文件扩展名
|
||
$fileExt = strtolower($file->extension());
|
||
if(!in_array($fileExt, ['mp3','wma','wav','amr'])){
|
||
return $this->renderError('上传文件类型只能为:mp3、wma、wav、amr');
|
||
}
|
||
// 文件大小(字节)不能大于2M(1024*1024*2 = 2097152)
|
||
$fileSize = $file->getSize();
|
||
if($fileSize > 2097152){
|
||
return $this->renderError('上传文件大于2M');
|
||
}
|
||
$fileInfo = pathinfo($file);
|
||
$folder = date('Ymd');//日期文件夹
|
||
$savePath = web_path() . 'uploads/user/'.$folder;
|
||
$info = $file->move($savePath);
|
||
$file_name = $file->hashName(function () {
|
||
return md5(uniqid((string)mt_rand(), true));
|
||
});
|
||
rename($savePath . DIRECTORY_SEPARATOR . $fileInfo['basename'],$savePath . DIRECTORY_SEPARATOR . $file_name);
|
||
$file_path = 'user/'.$folder.'/'.$file_name;
|
||
// 新增记录
|
||
if($model->add($this->postData('data'),$file_path,$this->applet_id)){
|
||
return $this->renderSuccess('添加成功', url('wechat.material.voice/index'));
|
||
}
|
||
$error = $model->getError() ?: '添加失败';
|
||
return $this->renderError($error);
|
||
}
|
||
return redirect(url('wechat.material.voice/index'));
|
||
}
|
||
|
||
/**
|
||
* 编辑
|
||
*/
|
||
public function edit($id)
|
||
{
|
||
$model = MaterialModel::get($id);
|
||
if ($this->request->isGet()) {
|
||
if($model){
|
||
return $this->renderSuccess('', '', compact('model'));
|
||
}
|
||
return $this->renderError('获取失败');
|
||
}
|
||
// 更新记录
|
||
if ($model->edit($this->postData('data'))) {
|
||
return $this->renderSuccess('更新成功', url('wechat.material.voice/index'));
|
||
}
|
||
$error = $model->getError() ?: '更新失败';
|
||
return $this->renderError($error);
|
||
|
||
}
|
||
|
||
/**
|
||
* 删除
|
||
*/
|
||
public function delete($id)
|
||
{
|
||
$model = MaterialModel::get($id);
|
||
if (!$model->remove($this->applet_id)) {
|
||
$error = $model->getError() ?: '删除失败';
|
||
return $this->renderError($error);
|
||
}
|
||
return $this->renderSuccess('删除成功');
|
||
}
|
||
|
||
}
|
||
|