UserCollect.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. return numTransform($num);
  30. }
  31. /**
  32. * @param $user_id
  33. * @param $type 1平台视频,2资料,3图文4新闻5问答6商品7活动8招聘9供应商10需求
  34. * @param $id
  35. * @param int $children_id
  36. * @return int
  37. * @throws \think\Exception
  38. * @throws \think\exception\PDOException
  39. */
  40. public static function plateCollect($user_id,$type,$id,$children_id = 0)
  41. {
  42. $check_collect = self::checkCollectByType($user_id,$type,$id,$children_id);
  43. $ret_val = 0;
  44. if($check_collect) {
  45. self::where(['user_id'=>$user_id,'coll_type'=>$type,'coll_id'=>$id,'children_id'=>$children_id])->delete();
  46. }else{
  47. Data::save('UserCollect', [
  48. 'user_id'=>$user_id,
  49. 'coll_type'=>$type,
  50. 'coll_id'=>$id,
  51. 'children_id'=>$children_id,
  52. 'create_int'=>time()],'user_id',['user_id'=>$user_id,'coll_type'=>$type, 'coll_id'=>$id,'children_id'=>$children_id]);
  53. $ret_val = 1;
  54. }
  55. return $ret_val;
  56. }
  57. public function videoItem()
  58. {
  59. return $this->belongsTo('VideoUrl','children_id')->field('id,video_id,cover,url,title,create_at');
  60. }
  61. public function datumItem()
  62. {
  63. return $this->belongsTo('DatumUrl','children_id')->field('id,datum_id,url,title,create_at');
  64. }
  65. public function articleItem()
  66. {
  67. return $this->belongsTo('DatumUrl','children_id')->field('id,article_id,cover,images,content,create_at');
  68. }
  69. }