CollectController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\controllers\v1;
  8. use app\models\Store;
  9. use app\models\UserCollect;
  10. use app\modules\client\controllers\BaseController;
  11. use yii\base\BaseObject;
  12. use yii\data\Pagination;
  13. class CollectController extends BaseController
  14. {
  15. /**
  16. * 收藏
  17. * @return \yii\web\Response
  18. */
  19. public function actionSave() {
  20. $collect_id = get_params('collect_id');
  21. $type = get_params('type', UserCollect::TYPE_STORE);
  22. if ($type == UserCollect::TYPE_STORE) {
  23. $store = Store::findOne($collect_id);
  24. if (!$store) {
  25. return $this->asJson([
  26. 'code' => 1,
  27. 'msg' => '参数不正确'
  28. ]);
  29. }
  30. }
  31. $mobile = get_user()->binding;
  32. $collect = UserCollect::findOne(['mobile' => $mobile, 'collect_id' => $collect_id, 'type' => $type, 'is_delete' => 0]);
  33. if ($collect) {
  34. return $this->asJson([
  35. 'code' => 1,
  36. 'msg' => '请勿重复收藏'
  37. ]);
  38. }
  39. $collect = new UserCollect();
  40. $collect->mobile = $mobile;
  41. $collect->collect_id = $collect_id;
  42. $collect->type = $type;
  43. $collect->is_delete = 0;
  44. $collect->created_at = time();
  45. $collect->updated_at = time();
  46. if (!$collect->save()) {
  47. return $this->asJson([
  48. 'code' => 1,
  49. 'msg' => '操作失败'
  50. ]);
  51. } else {
  52. return $this->asJson([
  53. 'code' => 0,
  54. 'msg' => '操作成功'
  55. ]);
  56. }
  57. }
  58. /**
  59. * 取消收藏
  60. * @return \yii\web\Response
  61. */
  62. public function actionCancel() {
  63. $collect_id = get_params('collect_id');
  64. $type = get_params('type', UserCollect::TYPE_STORE);
  65. if ($type == UserCollect::TYPE_STORE) {
  66. $store = Store::findOne($collect_id);
  67. if (!$store) {
  68. return $this->asJson([
  69. 'code' => 1,
  70. 'msg' => '参数不正确'
  71. ]);
  72. }
  73. }
  74. $mobile = get_user()->binding;
  75. $collect = UserCollect::findOne(['mobile' => $mobile, 'collect_id' => $collect_id, 'type' => $type]);
  76. if (!$collect) {
  77. return $this->asJson([
  78. 'code' => 1,
  79. 'msg' => '记录不存在'
  80. ]);
  81. }
  82. if ($collect->is_delete == 1) {
  83. return $this->asJson([
  84. 'code' => 1,
  85. 'msg' => '请勿重复取消收藏'
  86. ]);
  87. }
  88. $collect->is_delete = 1;
  89. $collect->updated_at = time();
  90. if (!$collect->save()) {
  91. return $this->asJson([
  92. 'code' => 1,
  93. 'msg' => '操作失败'
  94. ]);
  95. } else {
  96. return $this->asJson([
  97. 'code' => 0,
  98. 'msg' => '操作成功'
  99. ]);
  100. }
  101. }
  102. /**
  103. * 收藏列表
  104. */
  105. public function actionList() {
  106. $type = get_params('type', UserCollect::TYPE_STORE);
  107. $page = get_params('page', 1);
  108. $limit = get_params('limit', 10);
  109. $mobile = get_user()->binding;
  110. $query = UserCollect::find()->where(['mobile' => $mobile, 'type' => $type, 'is_delete' => 0]);
  111. $count = $query->count();
  112. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]);
  113. $list = $query->select('id, mobile, collect_id, type, FROM_UNIXTIME(created_at) as created_at')->limit($pagination->limit)
  114. ->offset($pagination->offset)
  115. ->asArray()->orderBy(['created_at' => SORT_DESC])->all();
  116. foreach ($list as &$item) {
  117. if ($item['type'] == $type) {
  118. $store = Store::findOne($item['collect_id']);
  119. $item['info'] = [
  120. 'id' => $store->id,
  121. 'name' => $store->name,
  122. 'logo' => $store->logo
  123. ];
  124. }
  125. }
  126. return $this->asJson([
  127. 'code' => 0,
  128. 'msg' => 'success',
  129. 'data' => [
  130. 'row_count' => $count,
  131. 'page_count' => $pagination->pageCount,
  132. 'list' => $list,
  133. ]
  134. ]);
  135. }
  136. }