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, ] ]); } }