169 lines
4.4 KiB
PHP
169 lines
4.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @author Any
|
|
* @description KISS
|
|
* @date 2020-12-2
|
|
* @version 1.0.0
|
|
*
|
|
* _____LOG_____
|
|
*
|
|
*/
|
|
|
|
namespace app\modules\api\models;
|
|
|
|
use function AlibabaCloud\Client\value;
|
|
use app\components\FlashStorage;
|
|
use app\components\SiteHelper;
|
|
use app\models\Coach;
|
|
use app\models\DeviceUniqueBindUser;
|
|
use app\models\Goods;
|
|
use app\models\GoodsHub;
|
|
use app\models\Store;
|
|
use app\models\User;
|
|
use app\components\auth\AToken;
|
|
use app\components\EncryptHelper;
|
|
use app\models\UserCollect;
|
|
use app\modules\api\components\ApiHelper;
|
|
use app\modules\api\components\GetDistance;
|
|
use yii\data\Pagination;
|
|
|
|
|
|
class UserCollectForm extends ApiModel
|
|
{
|
|
|
|
public $limit;//条数
|
|
public $page; //页数
|
|
public $pageCount; // 总页数
|
|
|
|
|
|
public $user_id;
|
|
public $goods_id;
|
|
|
|
public $id;
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['limit', 'page'], 'integer'],
|
|
[['page'], 'default', 'value' => 1],
|
|
];
|
|
}
|
|
|
|
|
|
/**
|
|
* 收藏列表
|
|
*/
|
|
public function search()
|
|
{
|
|
|
|
if (empty($this->limit)) {
|
|
$this->limit = 10;
|
|
}
|
|
if (empty($this->page)) {
|
|
$this->page = 1;
|
|
}
|
|
$query = UserCollect::find()->alias('user')
|
|
->leftJoin(['goods' => Goods::tableName()], 'user.goods_id=goods.id')
|
|
->leftJoin(['goodHub' => GoodsHub::tableName()], 'goods.goods_hub_id=goodHub.id')
|
|
->select('user.id,goods.id as goods_id,goodHub.cover_pic')
|
|
->where([
|
|
'user.user_id' => $this->user_id,
|
|
]);
|
|
|
|
$count = $query->count();
|
|
$pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit]);
|
|
$list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['user.id' => SORT_DESC])->asArray()->all();
|
|
|
|
foreach ($list as $key => $value) {
|
|
$value['cover_pic'] = SiteHelper::getFullUrl($value['cover_pic']);
|
|
$list[$key] = $value;
|
|
}
|
|
|
|
$data = [];
|
|
$data['code'] = 0;
|
|
$data['msg'] = 'ok';
|
|
$data['data'] = $list;
|
|
$data['count'] = $count;
|
|
return $data;
|
|
}
|
|
|
|
|
|
public function collect()
|
|
{
|
|
if (empty($this->goods_id)) {
|
|
return $this->apiReturnError('收藏的冠军错误');
|
|
}
|
|
|
|
$goods = Goods::findOne(['id' => $this->goods_id]);
|
|
if ($goods == null) {
|
|
return $this->apiReturnError('冠军不存在');
|
|
|
|
}
|
|
|
|
$model = new UserCollect();
|
|
$model->user_id = $this->user_id;
|
|
$model->goods_id = $this->goods_id;
|
|
if (!$model->save()) {
|
|
return $this->getModelError($model);
|
|
}
|
|
return [
|
|
'code' => 0,
|
|
'msg' => '收藏成功'
|
|
];
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* @return array
|
|
* @throws \Throwable
|
|
* @throws \yii\db\StaleObjectException
|
|
*/
|
|
public function del()
|
|
{
|
|
if (empty($this->id)) {
|
|
return $this->apiReturnError('收藏的ID错误');
|
|
}
|
|
|
|
$model = UserCollect::findOne(['id' => $this->id]);
|
|
if ($model == null) {
|
|
return $this->apiReturnError('收藏不存在');
|
|
|
|
}
|
|
|
|
if (!$model->delete()) {
|
|
return $this->getModelError($model);
|
|
}
|
|
return [
|
|
'code' => 0,
|
|
'msg' => '取消收藏成功'
|
|
];
|
|
}
|
|
|
|
|
|
public function hot()
|
|
{
|
|
$query = Goods::find()->alias('goods')
|
|
->leftJoin(['goodHub' => GoodsHub::tableName()], 'goods.goods_hub_id=goodHub.id')
|
|
->select('goods.id as goods_id,goodHub.cover_pic')
|
|
->where([
|
|
'goods.is_delete' => 0,
|
|
]);
|
|
|
|
$count = $query->count();
|
|
$pagination = new Pagination(['totalCount' => $count, 'pageSize' => 20]);
|
|
$list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['goods.id' => SORT_DESC])->asArray()->all();
|
|
foreach ($list as $key => $value) {
|
|
$value['cover_pic'] = SiteHelper::getFullUrl($value['cover_pic']);
|
|
$list[$key] = $value;
|
|
}
|
|
$data = [];
|
|
$data['code'] = 0;
|
|
$data['msg'] = 'ok';
|
|
$data['data'] = $list;
|
|
$data['count'] = $count;
|
|
return $data;
|
|
|
|
}
|
|
} |