// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\api\controller; use app\api\model\User as UserModel; use app\common\model\store\StoreSettle; use app\api\service\User as UserService; use think\response\Json; use app\api\model\Square as squareModel; use app\api\model\Squarelike as squarelikeModel; use app\common\model\UploadFile; use app\common\model\Channel; use app\api\model\Goods as GoodsModel; /** * 广场动态 * Class Store * @package app\store\controller */ class Square extends Controller { /** * @notes:新增文章 * @return Json * @throws BaseException * @author: wanghousheng */ public function add(): Json { $useInfo = UserService::getCurrentLoginUser(true)->toArray(); $content = $this->request->post('content'); if (!$content) { return $this->renderError('内容不能为空'); } $cate = $this->request->post('cate'); if (!$cate) { return $this->renderError('分类不能为空'); } $data = $this->postData(); $data['merchant_id'] = $this->merchantId; $data['user_name'] = $useInfo['user_id']; $data['real_name'] = $useInfo['nick_name']; if ((new squareModel)->add($data)) { return $this->renderSuccess('添加成功'); } return $this->renderError('添加失败'); } /** * 删除门店 * @param int $shopId * @return Json */ public function delete(int $squareId): Json { // 门店详情 $model = squareModel::detail($squareId, $this->storeId); if (!$model->setDelete()) { return $this->renderError($model->getError() ?: '删除失败'); } return $this->renderSuccess('删除成功'); } /** * 列表 */ public function list(): Json { $pageSize = $this->request->param('pageSize'); $pageSize = empty($pageSize) ? 15 : $pageSize; $useInfo = UserService::getCurrentLoginUser(true)->toArray(); $model = new squareModel; $list = $model->getList($this->request->param(), intval($pageSize))->toArray(); foreach ($list['data'] as $kr => $r) { $model = new UserModel(); $user = $model->where(['user_id' => $r['user_name']])->find()->toArray(); $modelLike = new squarelikeModel(); $like = $modelLike->where(['user_name' => $useInfo['user_id'],'square_id' => $r['square_id']])->find(); $res[$kr]['imgs'] = []; $res[$kr]['goodsInfo'] = []; $list['data'][$kr]['isLike'] = !empty($like) ? true : false; $res[$kr]['avatar_url'] = []; if ($r['imgs']) { $img_ids = explode(",", $r['imgs']); $files = UploadFile::getFileList($img_ids); $list['data'][$kr]['imgs'] = $files ?: null; } if ($r['user_name']) { $files = UploadFile::getFileList([$user['avatar_id']]); $list['data'][$kr]['avatar_url'] = $files ?: null; } if ($r['goods_id']) { $modelgoods = new GoodsModel; $goods = $modelgoods->getDetails2($r['goods_id'])->toArray(); //print_r($goods);die; $list['data'][$kr]['goodsInfo'] = $goods; } } return $this->renderSuccess($list); } public function dynamicLike(int $id): Json { $useInfo = UserService::getCurrentLoginUser(true)->toArray(); $model = new squarelikeModel; $detail = squarelikeModel::detail($id, (String)$useInfo['user_id']); $data1 = []; $data1['merchant_id'] = $this->merchantId; $data1['user_name'] = $useInfo['user_id']; $data1['square_id'] = $id; $data1['real_name'] = $useInfo['nick_name']; if (empty($detail)) { if ($model->add($data1)) { $this->dynamicLikeNum($id); return $this->renderSuccess('操作成功'); } } else { $data['squarelike_id'] = $detail['squarelike_id']; if ($model::whereIn('squarelike_id', $detail['squarelike_id'])->delete()) { $this->dynamicLikeNum($id); return $this->renderSuccess('操作成功'); } } } public function dynamicLikeNum(int $id) : Json { $detail = squareModel::detail($id); $count = squarelikeModel::getCount($id); $arr = [ 'likeNum' => $count, //'square_id' => $id ]; $detail->edit($arr); return $this->renderSuccess('操作成功'); } }