123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- <?php
- // +—————————————————————————————————————————————————————————————————————
- // | Created by Yunbao
- // +—————————————————————————————————————————————————————————————————————
- // | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
- // +—————————————————————————————————————————————————————————————————————
- // | Author: https://gitee.com/yunbaokeji
- // +—————————————————————————————————————————————————————————————————————
- // | Date: 2022-04-30
- // +—————————————————————————————————————————————————————————————————————
- /**
- * 背景音乐管理
- */
- namespace app\admin\controller;
- use cmf\controller\AdminBaseController;
- use think\Db;
- use think\db\Query;
- class MusicController extends AdminbaseController {
- /*分类列表*/
- public function classify(){
- $lists = Db::name('user_music_classify')
- ->where(function (Query $query) {
- $data = $this->request->param();
- $keyword=isset($data['keyword']) ? $data['keyword']: '';
- if (!empty($keyword)) {
- $query->where('title', 'like', "%$keyword%");
- }
- })
- ->order("orderno,addtime DESC")
- ->paginate(20);
- $lists->each(function($v,$k){
-
- $v['img_url']=get_upload_path($v['img_url']);
-
- return $v;
-
- });
-
- //分页-->筛选条件参数
- $data = $this->request->param();
- $lists->appends($data);
-
- // 获取分页显示
- $page = $lists->render();
-
- $this->assign('lists', $lists);
- $this->assign("page", $page);
-
- return $this->fetch();
- }
- /*分类添加*/
- public function classify_add(){
- return $this->fetch();
- }
- /*分类添加提交*/
- public function classify_add_post(){
- if($this->request->isPost()) {
-
- $data = $this->request->param();
- $classify=Db::name("user_music_classify");
-
- $title=trim($data['title']);
- $url=$data['img_url'];
- $orderno=$data['orderno'];
- if($title==""){
- $this->error("请填写分类名称");
- }
- if($url==""){
- $this->error("请上传分类图标");
- }
- if(!is_numeric($orderno)){
- $this->error("排序号请填写数字");
- }
- if($orderno<0){
- $this->error("排序号必须大于0");
- }
-
-
- $isexit=$classify
- ->where("title='{$title}'")
- ->find();
- if($isexit){
- $this->error('该分类已存在');
- }
-
- $data['title']=$title;
- $data['orderno']=$orderno;
- $data['img_url']=$url;
- $data['addtime']=time();
-
- $result=$classify->insert($data);
- if($result){
- $this->success('添加成功','admin/Music/classify',3);
- }else{
- $this->error('添加失败');
- }
- }
- }
- /*分类删除*/
- public function classify_del(){
- $id = $this->request->param('id');
- if($id){
- $result=Db::name("user_music_classify")->where("id={$id}")->update(array("isdel"=>1));
- if($result){
- $this->success('删除成功');
- }else{
- $this->error('删除失败');
- }
- }else{
- $this->error('数据传入失败!');
- }
- }
- /*分类取消删除*/
- public function classify_canceldel(){
- $id = $this->request->param('id');
- if($id){
- $result=Db::name("user_music_classify")->where("id={$id}")->update(array("isdel"=>0));
- if($result){
- $this->success('取消成功');
- }else{
- $this->error('取消失败');
- }
- }else{
- $this->error('数据传入失败!');
- }
- }
- /*分类编辑*/
- public function classify_edit(){
-
-
- $id = $this->request->param('id');
- if($id){
- $info=Db::name("user_music_classify")->where("id={$id}")->find();
- $this->assign("classify_info",$info);
- }else{
- $this->error('数据传入失败!');
- }
-
- return $this->fetch();
- }
- /*分类编辑提交*/
- public function classify_edit_post(){
-
- if($this->request->isPost()) {
-
- $data = $this->request->param();
- $id=$data["id"];
- $title=$data["title"];
- $url=$data['img_url'];
- $orderno=$data["orderno"];
- if(!trim($title)){
- $this->error('分类标题不能为空');
- }
-
- if($url==""){
- $this->error("请上传分类图标");
- }
- if(!is_numeric($orderno)){
- $this->error("排序号请填写数字");
- }
- if($orderno<0){
- $this->error("排序号必须大于0");
- }
-
- $isexit=Db::name("user_music_classify")
- ->where("id!={$id} and title='{$title}'")
- ->find();
- if($isexit){
- $this->error('该分类已存在');
- }
-
- $result=Db::name("user_music_classify")
- ->update($data);
-
-
- if($result!==false){
- $this->success('修改成功');
- }else{
- $this->error('修改失败');
- }
- }
- }
-
- //分类排序
- public function classify_listorders(){
- $ids = $this->request->param('listorders');
- foreach ($ids as $key => $r) {
- $data['orderno'] = $r;
- Db::name("user_music_classify")->where(array('id' => $key))->update($data);
- }
-
- $status = true;
- if ($status) {
- $this->success("排序更新成功!");
- } else {
- $this->error("排序更新失败!");
- }
- }
- /*背景音乐*/
- public function index(){
- $lists = Db::name('user_music')
- ->where(function (Query $query) {
- $data = $this->request->param();
- $classify_id=isset($data['classify_id']) ? $data['classify_id']: '';
- if (!empty($classify_id)) {
- $query->where('classify_id', $classify_id);
- }
- $upload_type=isset($data['upload_type']) ? $data['upload_type']: '';
- if (!empty($upload_type)) {
- $query->where('upload_type', $upload_type);
- }
- $keyword=isset($data['keyword']) ? $data['keyword']: '';
- if (!empty($keyword)) {
- $query->where('title', 'like', "%$keyword%");
- }
- })
- ->order("use_nums DESC")
- ->paginate(20);
- $lists->each(function($v,$k){
- $v['img_url']=get_upload_path($v['img_url']);
- $v['file_url']=get_upload_path($v['file_url']);
- $classify_title=Db::name("user_music_classify")->where("id={$v['classify_id']}")->find();
-
- $v['classify_title']=$classify_title['title'];
- $userinfo=Db::name("user")
- ->field("user_nicename")
- ->where("id='$v[uploader]'")
- ->find();
- if(!$userinfo){
- $userinfo=['user_nicename'=>'用户不存在'];
- }
- $v['uploader_nicename']=$userinfo['user_nicename'];
- return $v;
-
- });
-
-
- //分页-->筛选条件参数
- $data = $this->request->param();
- $lists->appends($data);
-
- // 获取分页显示
- $page = $lists->render();
-
- //分类列表
- $classify=Db::name("user_music_classify")
- ->order("orderno")
- ->select();
- $this->assign('lists', $lists);
- $this->assign('classify', $classify);
- $this->assign("page", $page);
-
- return $this->fetch();
- }
- /*背景音乐添加*/
- public function music_add(){
-
- $classify=Db::name("user_music_classify")
- ->order("orderno")
- ->select();
-
- $this->assign('classify', $classify);
-
- return $this->fetch();
- }
- /*背景音乐添加保存*/
- public function music_add_post(){
- if($this->request->isPost()) {
-
- $data = $this->request->param();
-
-
- $data['addtime']=time();
- $data['upload_type']=1;
- $data['uploader']=get_current_admin_id(); //当前管理员id
-
-
- $img_url=$data['img_url'];
- $title=$data['title'];
- $author=$data['author'];
- $length=$data['length'];
- $use_nums=$data['use_nums'];
- if($title==""){
- $this->error("请填写音乐名称");
- }
- // 判断该音乐是否存在
- $isexist=Db::name("user_music")
- ->where(["title"=>$title])
- ->find();
- if($isexist){
- $this->error("该音乐已经存在");
- }
- if($author==""){
- $this->error("请填写演唱者");
- }
- if($img_url==""){
- $this->error("请上传音乐封面");
- }
- if($length==""){
- $this->error("请填写音乐时长");
- }
- if(!strpos($length,":")){
- $this->error("请按照格式填写音乐时长");
- }
- if(!is_numeric($use_nums)||$use_nums<0){
- $this->error("使用次数请写正整数");
- }
- $files["file"]=$_FILES["file"];
- $type='mp3';
- $uploadSetting = cmf_get_upload_setting();
- $extensions=$uploadSetting['file_types']['audio']['extensions'];
- $allow=explode(",",$extensions);
- if (!get_file_suffix($files['file']['name'],$allow)){
- $this->error("请上传正确格式的音频文件或检查上传设置中音频设置的文件类型");
- }
-
- $rs=adminUploadFiles($files,$type);
- if($rs['code']!=0){
- $this->error($rs['msg']);
- }
- $data['file_url']=$rs['filepath'];
-
- unset($data['file']);
- $result=Db::name("user_music")->insert($data);
- if($result){
- $this->success('添加成功','admin/music/music_add',3);
- }else{
- $this->error('添加失败');
- }
- }
- }
- /*音乐试听*/
- public function music_listen(){
-
- $id = $this->request->param('id');
- if(!$id||$id==""||!is_numeric($id)){
- $this->error("加载失败");
- }else{
- //获取音乐信息
- $info=Db::name("user_music")->where("id={$id}")->find();
- $this->assign("info",$info);
- }
- return $this->fetch();
- }
- /*音乐删除*/
- public function music_del(){
- $id = $this->request->param('id');
- if(!$id||$id==""||!is_numeric($id)){
- $this->error("操作失败");
- }else{
- $count=Db::name("user_video")->where("music_id={$id}")->count();
- if($count>0){
- $result=Db::name("user_music")->where("id={$id}")->update(array("isdel"=>1));
- }else{
- $result=Db::name("user_music")->where("id={$id}")->delete();
- }
- if($result){
- $this->success('删除成功');
- }else{
- $this->error('删除失败');
- }
- }
- }
- /*取消删除*/
- public function music_canceldel(){
- $id = $this->request->param('id');
- if(!$id||$id==""||!is_numeric($id)){
- $this->error("操作失败");
- }else{
- $result=Db::name("user_music")->where("id={$id}")->update(array("isdel"=>0));
- if($result){
- $this->success('取消成功');
- }else{
- $this->error('取消失败');
- }
- }
- }
- /*音乐编辑*/
- public function music_edit(){
- $id = $this->request->param('id');
-
- if($id==""){
- $this->error("操作失败");
- }else{
- $music=Db::name("user_music");
- $info=$music->where("id={$id}")->find();
- $this->assign("info",$info);
- $classify=Db::name("user_music_classify")->order("orderno")->select();
- $this->assign("classify",$classify);
- }
- return $this->fetch();
- }
- public function music_edit_post(){
- if($this->request->isPost()) {
-
- $data = $this->request->param();
- $music=Db::name("user_music");
- $data['updatetime']=time();
-
-
- $id=$data['id'];
- $img_url=$data['img_url'];
- $title=$data['title'];
- $author=$data['author'];
- $length=$data['length'];
- $use_nums=$data['use_nums'];
-
- if($title==""){
- $this->error("请填写音乐名称");
- }
- //判断该音乐是否存在
- $isexist=Db::name("user_music")
- ->where([['title','=',$title],['id','<>',$id]])
- ->find();
- if($isexist){
- $this->error("该音乐已经存在");
- }
- if($author==""){
- $this->error("请填写演唱者");
- }
- if($img_url==""){
- $this->error("请上传音乐封面");
- }
- if($length==""){
- $this->error("请填写音乐时长");
- }
- if(!strpos($length,":")){
- $this->error("请按照格式填写音乐时长");
- }
- if(!is_numeric($use_nums)||$use_nums<0){
- $this->error("使用次数请写正整数");
- }
- if($_FILES){
- $files["file"]=$_FILES["file"];
- $type='mp3';
- $uploadSetting = cmf_get_upload_setting();
- $extensions=$uploadSetting['file_types']['audio']['extensions'];
- $allow=explode(",",$extensions);
- if (!get_file_suffix($files['file']['name'],$allow)){
- $this->error("请上传正确格式的音频文件或检查上传设置中音频设置的文件类型");
- }
- $rs=adminUploadFiles($files,$type);
- if($rs['code']!=0){
- $this->error($rs['msg']);
- }
- $data['file_url']=$rs['filepath'];
-
- }
- unset($data['file']);
-
- $result=$music->update($data);
- if($result!==false){
- $this->success('修改成功');
- }else{
- $this->error('修改失败');
- }
- }
- }
-
- }
|