UserCollect.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\common\model;
  3. use library\tools\Data;
  4. use think\Model;
  5. // 收藏
  6. class UserCollect extends Model
  7. {
  8. //protected $resultSetType = 'collection';// 设置返回类型
  9. static $type_desc = [
  10. 1=>'平台视频', 2=>'资料', 3=>'图文', 4=>'新闻', 5=>'问答', 6=>'商品', 7=>'活动', 8=>'招聘',9=>'供应商商品',10=>'需求'
  11. ];
  12. public static function checkCollectByType($user_id,$type,$id,$children_id = 0)
  13. {
  14. $where = [];
  15. $where['user_id'] = $user_id;
  16. $where['coll_type'] = $type;
  17. $where['coll_id'] = $id;
  18. if($children_id) $where['children_id'] = $children_id;
  19. return self::where($where)->value('id') ? 1:0;
  20. }
  21. // 收藏数量
  22. public static function getCollectNum($type,$id,$children_id = 0)
  23. {
  24. $where = [];
  25. $where['coll_type'] = $type;
  26. $where['coll_id'] = $id;
  27. if($children_id) $where['children_id'] = $children_id;
  28. $num =self::where($where)->count();
  29. $num= numTransform($num);
  30. return$num ;
  31. }
  32. /**
  33. * @param $user_id
  34. * @param $type 1平台视频,2资料,3图文4新闻5问答6商品7活动
  35. * @param $id
  36. * @param int $children_id
  37. * @return int
  38. * @throws \think\Exception
  39. * @throws \think\exception\PDOException
  40. */
  41. public static function plateCollect($user_id,$type,$id,$children_id = 0)
  42. {
  43. $check_collect = self::checkCollectByType($user_id,$type,$id,$children_id);
  44. $ret_val = 0;
  45. if($check_collect) {
  46. self::where(['user_id'=>$user_id,'coll_type'=>$type,'coll_id'=>$id,'children_id'=>$children_id])->delete();
  47. }else{
  48. Data::save('UserCollect', [
  49. 'user_id'=>$user_id,
  50. 'coll_type'=>$type,
  51. 'coll_id'=>$id,
  52. 'children_id'=>$children_id,
  53. 'create_int'=>time()],'user_id',['user_id'=>$user_id,'coll_type'=>$type, 'coll_id'=>$id,'children_id'=>$children_id]);
  54. $ret_val = 1;
  55. }
  56. return $ret_val;
  57. }
  58. public function videoItem()
  59. {
  60. return $this->belongsTo('VideoUrl','children_id')->field('id,video_id,cover,url,title,create_at');
  61. }
  62. public function datumItem()
  63. {
  64. return $this->belongsTo('DatumUrl','children_id')->field('id,datum_id,url,title,create_at');
  65. }
  66. public function articleItem()
  67. {
  68. return $this->belongsTo('DatumUrl','children_id')->field('id,article_id,cover,images,content,create_at');
  69. }
  70. }