Music.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. // +—————————————————————————————————————————————————————————————————————
  3. // | Created by Yunbao
  4. // +—————————————————————————————————————————————————————————————————————
  5. // | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
  6. // +—————————————————————————————————————————————————————————————————————
  7. // | Author: https://gitee.com/yunbaokeji
  8. // +—————————————————————————————————————————————————————————————————————
  9. // | Date: 2022-04-30
  10. // +—————————————————————————————————————————————————————————————————————
  11. class Model_Music extends PhalApi_Model_NotORM {
  12. /*获取分类列表*/
  13. public function classify_list(){
  14. $list=DI()->notorm->user_music_classify->select("id,title,img_url")->where("isdel=0")->order("orderno")->fetchAll();
  15. if(!$list){
  16. return 1001;
  17. }
  18. foreach ($list as $k => $v) {
  19. $list[$k]['img_url']=get_upload_path($v['img_url']);
  20. }
  21. return $list;
  22. }
  23. /*获取音乐列表*/
  24. public function music_list($classify,$uid,$p){
  25. $nums=20;
  26. $start=($p-1)*$nums;
  27. $where=" isdel=0";
  28. if($classify>0){
  29. $where.=" and classify_id={$classify}";
  30. }
  31. $list=DI()->notorm->user_music->select("id,title,author,img_url,length,file_url,use_nums")->where($where)->order("use_nums desc")->limit($start,$nums)->fetchAll();
  32. if(!$list){
  33. return 1001;
  34. }
  35. if($uid<1){ //游客
  36. foreach ($list as $k => $v) {
  37. $list[$k]['iscollect']=0;
  38. $list[$k]['img_url']=get_upload_path($v['img_url']);
  39. $list[$k]['file_url']=get_upload_path($v['file_url']);
  40. }
  41. }else{
  42. //获取本人收藏列表
  43. $collectLists=DI()->notorm->user_music_collection->select("music_id")->where("uid=? and status=1",$uid)->fetchAll();
  44. $collects=array();
  45. foreach ($collectLists as $val) {
  46. $collects[]=$val['music_id'];
  47. }
  48. foreach ($list as $k => $v) {
  49. if(in_array($v['id'],$collects)){
  50. $list[$k]['iscollect']="1";
  51. }else{
  52. $list[$k]['iscollect']="0";
  53. }
  54. $list[$k]['img_url']=get_upload_path($v['img_url']);
  55. $list[$k]['file_url']=get_upload_path($v['file_url']);
  56. }
  57. }
  58. return $list;
  59. }
  60. /*搜索音乐(模糊查询本地,按照使用量排序)*/
  61. public function searchMusic($keywords,$uid,$p){
  62. $nums=50;
  63. $start=($p-1)*$nums;
  64. $where="1=1";
  65. if($keywords!=""){
  66. $where .=" and (title like '%".$keywords."%') or author like '%".$keywords."%'";
  67. }
  68. $list=DI()->notorm->user_music->select("id,title,author,img_url,length,file_url,use_nums")->where($where)->order("use_nums desc")->limit($start,$nums)->fetchAll();
  69. if(!$list){
  70. return 1001;
  71. }
  72. if($uid<1){ //游客
  73. foreach ($list as $k => $v) {
  74. $list[$k]['iscollect']=0;
  75. $list[$k]['img_url']=get_upload_path($v['img_url']);
  76. $list[$k]['file_url']=get_upload_path($v['file_url']);
  77. }
  78. }else{
  79. //获取本人收藏列表
  80. $collectLists=DI()->notorm->user_music_collection->select("music_id")->where("uid=? and status=1",$uid)->fetchAll();
  81. $collects=array();
  82. foreach ($collectLists as $val) {
  83. $collects[]=$val['music_id'];
  84. }
  85. foreach ($list as $k => $v) {
  86. if(in_array($v['id'],$collects)){
  87. $list[$k]['iscollect']="1";
  88. }else{
  89. $list[$k]['iscollect']="0";
  90. }
  91. $list[$k]['img_url']=get_upload_path($v['img_url']);
  92. $list[$k]['file_url']=get_upload_path($v['file_url']);
  93. }
  94. }
  95. return $list;
  96. }
  97. /*收藏/取消收藏音乐*/
  98. public function collectMusic($uid,$musicid){
  99. //判断音乐是否存在
  100. $info=DI()->notorm->user_music->select("title,addtime")->where("id=?",$musicid)->fetchOne();
  101. if(!$info){
  102. return 1001;
  103. }
  104. //判断用户是否收藏过该视频
  105. $isexist=DI()->notorm->user_music_collection->select("*")->where("uid='{$uid}' and music_id='{$musicid}'")->fetchOne();
  106. //已经收藏过
  107. if($isexist){
  108. if($isexist['status']==1){ //已收藏
  109. //将状态改为取消收藏
  110. $result=DI()->notorm->user_music_collection->where("uid=? and music_id=?",$uid,$musicid)->update(array("status"=>0,"updatetime"=>time()));
  111. if($result!==false){
  112. return 200;
  113. }else{
  114. return 201;
  115. }
  116. }else{ //改为收藏
  117. //将状态改为收藏
  118. $result=DI()->notorm->user_music_collection->where("uid=? and music_id=?",$uid,$musicid)->update(array("status"=>1,"updatetime"=>time()));
  119. if($result!==false){
  120. return 300;
  121. }else{
  122. return 301;
  123. }
  124. }
  125. }else{
  126. //向收藏表中写入记录
  127. $data=array("uid"=>$uid,"music_id"=>$musicid,'addtime'=>time(),'status'=>1);
  128. $result=DI()->notorm->user_music_collection->insert($data);
  129. if($result!==false){
  130. return 300;
  131. }else{
  132. return 301;
  133. }
  134. }
  135. }
  136. /*获取用户收藏背景音乐列表*/
  137. public function getCollectMusicLists($uid,$p){
  138. $nums=20;
  139. $start=($p-1)*$nums;
  140. $where="uid='{$uid}' and status=1";
  141. $list=DI()->notorm->user_music_collection->select("music_id,addtime")->where($where)->order("addtime desc")->limit($start,$nums)->fetchAll();
  142. if(!$list){
  143. return 0;
  144. }
  145. foreach ($list as $k => $v) {
  146. $musicinfo=DI()->notorm->user_music->select("title,author,img_url,length,file_url,use_nums")->where("id=?",$v['music_id'])->fetchOne();
  147. unset($list[$k]['addtime']);
  148. $list[$k]['collecttime']=datetime($v['addtime']); //收藏时间
  149. $list[$k]['title']=$musicinfo['title'];
  150. $list[$k]['author']=$musicinfo['author'];
  151. $list[$k]['img_url']=get_upload_path($musicinfo['img_url']);
  152. $list[$k]['length']=$musicinfo['length'];
  153. $list[$k]['file_url']=get_upload_path($musicinfo['file_url']);
  154. $list[$k]['use_nums']=$musicinfo['use_nums'];
  155. $list[$k]['iscollect']='1';
  156. $list[$k]['id']=$v['music_id']; //为了app端模板解析统一,所以重新添加了一个id字段
  157. }
  158. return $list;
  159. }
  160. /*获取热门音乐列表*/
  161. public function hotLists($uid){
  162. $start=0;
  163. $nums=10;
  164. $where=" isdel=0";
  165. $list=DI()->notorm->user_music->select("id,title,author,img_url,length,file_url,use_nums")->where($where)->order("use_nums desc")->limit($start,$nums)->fetchAll();
  166. if(!$list){
  167. return 1001;
  168. }
  169. if($uid<1){ //游客
  170. foreach ($list as $k => $v) {
  171. $list[$k]['iscollect']=0;
  172. $list[$k]['img_url']=get_upload_path($v['img_url']);
  173. $list[$k]['file_url']=get_upload_path($v['file_url']);
  174. }
  175. }else{
  176. //获取本人收藏列表
  177. $collectLists=DI()->notorm->user_music_collection->select("music_id")->where("uid=? and status=1",$uid)->fetchAll();
  178. $collects=array();
  179. foreach ($collectLists as $val) {
  180. $collects[]=$val['music_id'];
  181. }
  182. foreach ($list as $k => $v) {
  183. if(in_array($v['id'],$collects)){
  184. $list[$k]['iscollect']=1;
  185. }else{
  186. $list[$k]['iscollect']=0;
  187. }
  188. $list[$k]['img_url']=get_upload_path($v['img_url']);
  189. $list[$k]['file_url']=get_upload_path($v['file_url']);
  190. }
  191. }
  192. return $list;
  193. }
  194. }