| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\controllers\v1;
- use app\models\Store;
- use app\models\UserCollect;
- use app\modules\client\controllers\BaseController;
- use yii\base\BaseObject;
- use yii\data\Pagination;
- class CollectController extends BaseController
- {
- /**
- * 收藏
- * @return \yii\web\Response
- */
- public function actionSave() {
- $collect_id = get_params('collect_id');
- $type = get_params('type', UserCollect::TYPE_STORE);
- if ($type == UserCollect::TYPE_STORE) {
- $store = Store::findOne($collect_id);
- if (!$store) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数不正确'
- ]);
- }
- }
- $mobile = get_user()->binding;
- $collect = UserCollect::findOne(['mobile' => $mobile, 'collect_id' => $collect_id, 'type' => $type, 'is_delete' => 0]);
- if ($collect) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '请勿重复收藏'
- ]);
- }
- $collect = new UserCollect();
- $collect->mobile = $mobile;
- $collect->collect_id = $collect_id;
- $collect->type = $type;
- $collect->is_delete = 0;
- $collect->created_at = time();
- $collect->updated_at = time();
- if (!$collect->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作失败'
- ]);
- } else {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '操作成功'
- ]);
- }
- }
- /**
- * 取消收藏
- * @return \yii\web\Response
- */
- public function actionCancel() {
- $collect_id = get_params('collect_id');
- $type = get_params('type', UserCollect::TYPE_STORE);
- if ($type == UserCollect::TYPE_STORE) {
- $store = Store::findOne($collect_id);
- if (!$store) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数不正确'
- ]);
- }
- }
- $mobile = get_user()->binding;
- $collect = UserCollect::findOne(['mobile' => $mobile, 'collect_id' => $collect_id, 'type' => $type]);
- if (!$collect) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '记录不存在'
- ]);
- }
- if ($collect->is_delete == 1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '请勿重复取消收藏'
- ]);
- }
- $collect->is_delete = 1;
- $collect->updated_at = time();
- if (!$collect->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作失败'
- ]);
- } else {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '操作成功'
- ]);
- }
- }
- /**
- * 收藏列表
- */
- public function actionList() {
- $type = get_params('type', UserCollect::TYPE_STORE);
- $page = get_params('page', 1);
- $limit = get_params('limit', 10);
- $mobile = get_user()->binding;
- $query = UserCollect::find()->where(['mobile' => $mobile, 'type' => $type, 'is_delete' => 0]);
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]);
- $list = $query->select('id, mobile, collect_id, type, FROM_UNIXTIME(created_at) as created_at')->limit($pagination->limit)
- ->offset($pagination->offset)
- ->asArray()->orderBy(['created_at' => SORT_DESC])->all();
- foreach ($list as &$item) {
- if ($item['type'] == $type) {
- $store = Store::findOne($item['collect_id']);
- $item['info'] = [
- 'id' => $store->id,
- 'name' => $store->name,
- 'logo' => $store->logo
- ];
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $list,
- ]
- ]);
- }
- }
|