www.wdadmin.cn // +---------------------------------------------------------------------- namespace app\api\controller\wdsxh; use app\api\model\wdsxh\member\Member; use app\api\model\wdsxh\UserWechat; use app\common\controller\Api; class CompanyGoods extends Api { protected $noNeedLogin = ['']; protected $noNeedRight = ['*']; protected $model = null; protected $member_id = null; public function _initialize() { parent::_initialize(); $this->model = new \app\api\model\wdsxh\member\CompanyGoods(); $wechat_id = (new UserWechat())->where('user_id',$this->auth->id)->value('id'); if (!$wechat_id) { $this->error('用户信息不存在'); } $memberObj = (new Member())->where('wechat_id',$wechat_id)->find(); if (!$memberObj) { $this->error('会员信息不存在'); } $current_date = date('Y-m-d',time()); if ($memberObj['expire_time'] < $current_date) { $this->error('会员已过期,需要缴纳会费'); } if (!in_array($memberObj['type'],[2,3])) { $this->error('仅限会员使用'); } $this->member_id = $memberObj['id']; } /** * 产品列表 */ public function index() { if (!$this->request->isGet()) { $this->error('请求方式错误'); } $list = $this->model->where('member_id', $this->member_id) ->field('id,name,images') ->order('createtime desc') ->select(); foreach ($list as &$item) { $item->hidden(['images']); } $this->success('获取成功', $list); } /** * 产品详情 */ public function detail() { if (!$this->request->isGet()) { $this->error('请求方式错误'); } $id = $this->request->get('id'); if (!$id) { $this->error('参数错误'); } // 检查产品是否属于当前会员 $goods = $this->model->where('id', $id) ->field('id,name,images,content') ->where('member_id', $this->member_id) ->find(); if (!$goods) { $this->error('产品不存在或无权查看'); } $data = $goods->toArray(); unset($data['image']); $this->success('获取成功', $data); } /** * 添加产品 */ public function add() { if (!$this->request->isPost()) { $this->error('请求方式错误'); } $params = $this->request->post(); $params['content'] = $_POST['content'] ?? ''; // 验证数据 $validate = new \app\api\validate\wdsxh\CompanyGoods(); if (!$validate->scene('add')->check($params)) { $this->error($validate->getError()); } // 添加会员ID $params['member_id'] = $this->member_id; // 保存数据 $result = $this->model->save($params); if ($result) { $this->success('添加成功'); } else { $this->error('添加失败'); } } /** * 修改产品 */ public function edit() { if (!$this->request->isPost()) { $this->error('请求方式错误'); } $params = $this->request->post(); $params['content'] = $_POST['content'] ?? ''; $id = $this->request->post('id'); if (!$id) { $this->error('参数错误'); } // 验证数据 $validate = new \app\api\validate\wdsxh\CompanyGoods(); if (!$validate->scene('edit')->check($params)) { $this->error($validate->getError()); } // 检查产品是否属于当前会员 $goods = $this->model->where('id', $id) ->where('member_id', $this->member_id) ->find(); if (!$goods) { $this->error('产品不存在或无权操作'); } // 更新数据 $result = $goods->save($params); if ($result !== false) { $this->success('修改成功'); } else { $this->error('修改失败'); } } /** * 删除产品 */ public function del() { if (!$this->request->isPost()) { $this->error('请求方式错误'); } $id = $this->request->post('id'); if (!$id) { $this->error('参数错误'); } // 检查产品是否属于当前会员 $goods = $this->model->where('id', $id) ->where('member_id', $this->member_id) ->find(); if (!$goods) { $this->error('产品不存在或无权操作'); } // 删除数据 $result = $goods->delete(); if ($result) { $this->success('删除成功'); } else { $this->error('删除失败'); } } }