Favorite.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace app\api\model\user;
  3. use app\common\model\user\Favorite as FavoriteModel;
  4. use app\common\model\supplier\Supplier as SupplierModel;
  5. use app\common\model\product\Product as ProductModel;
  6. /**
  7. * 收藏模型类
  8. */
  9. class Favorite extends FavoriteModel
  10. {
  11. public function getList($where,$param){
  12. if($where['type']==20){
  13. return parent::getList($where,$param);
  14. }else{
  15. return parent::getMySupplier($where,$param);
  16. }
  17. }
  18. /**
  19. * 取消收藏
  20. */
  21. public function cancelFav(){
  22. $this->startTrans();
  23. try {
  24. // 取消店铺关注
  25. if($this['type'] == 10){
  26. (new SupplierModel())->where('shop_supplier_id', '=', $this['pid'])
  27. ->dec('fav_count')->update();
  28. }
  29. $this->delete();
  30. // 事务提交
  31. $this->commit();
  32. return true;
  33. } catch (\Exception $e) {
  34. $this->error = $e->getMessage();
  35. $this->rollback();
  36. return false;
  37. }
  38. }
  39. public function fav($data){
  40. $data['app_id'] = self::$app_id;
  41. $this->startTrans();
  42. try {
  43. // 店铺收藏
  44. if($data['type'] == 10){
  45. $data['shop_supplier_id'] = $data['pid'];
  46. (new SupplierModel())->where('shop_supplier_id', '=', $data['pid'])
  47. ->inc('fav_count')->update();
  48. }else{
  49. $product = ProductModel::detail($data['pid']);
  50. $data['shop_supplier_id'] = $product['shop_supplier_id'];
  51. }
  52. $this->save($data);
  53. // 事务提交
  54. $this->commit();
  55. return true;
  56. } catch (\Exception $e) {
  57. $this->error = $e->getMessage();
  58. $this->rollback();
  59. return false;
  60. }
  61. }
  62. public static function isFollow($pid,$user_id,$type){
  63. return (new static())->where('pid', '=', $pid)
  64. ->where('user_id', '=', $user_id)
  65. ->where('type', '=', $type)
  66. ->count();
  67. }
  68. }