335 lines
		
	
	
		
			9.5 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			335 lines
		
	
	
		
			9.5 KiB
		
	
	
	
		
			PHP
		
	
	
| <?php
 | |
| 
 | |
| namespace app\controller\api;
 | |
| 
 | |
| use app\controller\api\Base;
 | |
| use app\exception\RepositoryException;
 | |
| use app\model\AccountLevel;
 | |
| use app\model\AccountRecord;
 | |
| use app\model\AccountRole;
 | |
| use app\model\Spu as SpuModel;
 | |
| use app\model\Disease;
 | |
| use app\model\SpuActivity;
 | |
| use app\model\Staff;
 | |
| use app\repository\OrderRepository;
 | |
| use app\repository\SpuRepository;
 | |
| use Exception;
 | |
| use think\Collection;
 | |
| use think\db\exception\DataNotFoundException;
 | |
| use think\db\exception\DbException;
 | |
| use think\db\exception\ModelNotFoundException;
 | |
| use think\facade\Log;
 | |
| use think\response\Json;
 | |
| 
 | |
| class Spu extends Base
 | |
| {
 | |
|     protected $exceptExtra = ['list', 'category', 'detail'];
 | |
| 
 | |
|     /**
 | |
|      * 商品列表筛选条件
 | |
|      *
 | |
|      * @throws ModelNotFoundException
 | |
|      * @throws DbException
 | |
|      * @throws DataNotFoundException
 | |
|      */
 | |
|     public function condition(): Json
 | |
|     {
 | |
|         $list = [
 | |
|             'disease'      => Disease::getListByPid(0, ['pid', 'name', 'id', 'sort']),
 | |
|             'doctor_roles' => AccountRole::findAccountRolesByGroupName(AccountRole::ROLE_GROUP_DOCTOR),
 | |
|             'activity'     => SpuModel::activity(),
 | |
|         ];
 | |
| 
 | |
|         return $this->json(0, 'success', $list);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 获取已发布的商品列表
 | |
|      *
 | |
|      * @return Json
 | |
|      * @throws RepositoryException
 | |
|      * @throws Exception
 | |
|      */
 | |
|     public function list(): Json
 | |
|     {
 | |
|         $repo = SpuRepository::getInstance();
 | |
| 
 | |
|         $fields = SpuModel::spuListFields();
 | |
| 
 | |
|         $params             = input();
 | |
|         $params['fields']   = $fields;
 | |
|         $params['is_score'] = SpuModel::COMMON_OFF;//排除积分商品
 | |
| 
 | |
|         $list = $repo->listForFront($params, function ($q) {
 | |
|             return $q->with([
 | |
|                 'activity_info' => function ($query) {
 | |
|                     $query->withoutField('content');
 | |
|                 }
 | |
|             ]);
 | |
|         });
 | |
| 
 | |
|         return $this->json(0, 'success', $list);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 获取已发布的积分商品列表
 | |
|      *
 | |
|      * @return Json
 | |
|      * @throws RepositoryException
 | |
|      * @throws Exception
 | |
|      */
 | |
|     public function score(): Json
 | |
|     {
 | |
|         $repo = SpuRepository::getInstance();
 | |
| 
 | |
|         $type      = input('type/s', SpuModel::TYPE_NORMAL);//normal=综合 newest=最新
 | |
|         $sortField = input('sort_field/s', '');// score=积分 num=兑换量
 | |
|         $sortValue = input('sort_value/s', '');//desc=降序 asc=升序
 | |
| 
 | |
|         $rules = [
 | |
|             'page|页数'         => 'integer|gt:0',
 | |
|             'size|每页数量'       => 'integer|gt:0',
 | |
|             'type|类型'         => 'in:newest,'.SpuModel::TYPE_NORMAL,
 | |
|             'sort_field|排序字段' => 'in:score,amount',
 | |
|             'sort_value|排序值'  => 'in:asc,desc',
 | |
|         ];
 | |
| 
 | |
|         $message = [
 | |
|             'type.in'       => '类型错误',
 | |
|             '$sortField.in' => '排序字段错误',
 | |
|             'sort_value.in' => '排序值错误',
 | |
|         ];
 | |
| 
 | |
|         $params = input();
 | |
| 
 | |
|         $validate = $this->validateByApi($params, $rules, $message);
 | |
|         if ($validate !== true) {
 | |
|             return $validate;
 | |
|         }
 | |
| 
 | |
|         $order = [];//排序
 | |
| 
 | |
|         // 综合排序
 | |
|         if ($type === SpuModel::TYPE_NORMAL) {
 | |
|             $order = [
 | |
|                 'sort' => 'desc',
 | |
|                 'id'   => 'desc',
 | |
|             ];
 | |
|         }
 | |
| 
 | |
|         // 最新排序
 | |
|         if ($type === 'newest') {
 | |
|             $order = ['published_at' => 'desc'];
 | |
|         }
 | |
| 
 | |
|         // 兑换量排序
 | |
|         if (!empty($sortField)) {
 | |
|             if (empty($sortValue)) {
 | |
|                 return $this->json(4003, '排序参数错误');
 | |
|             }
 | |
|             $order = [
 | |
|                 $sortField => $sortValue
 | |
|             ];
 | |
|         }
 | |
| 
 | |
|         $params['is_score'] = SpuModel::COMMON_ON;
 | |
|         $params['fields']   = SpuModel::scoreListFields();
 | |
| 
 | |
|         $list = $repo->listForFront($params, function ($q) {
 | |
|             return $q->with([
 | |
|                 'activity_info' => function ($query) {
 | |
|                     $query->withoutField('content');
 | |
|                 }
 | |
|             ]);
 | |
|         }, $order);
 | |
| 
 | |
|         return $this->json(0, 'success', $list);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 收藏列表
 | |
|      *
 | |
|      * @return Json
 | |
|      * @throws Exception
 | |
|      */
 | |
|     public function collection(): Json
 | |
|     {
 | |
|         $rules = [
 | |
|             'page|页数'   => 'integer',
 | |
|             'size|每页数量' => 'integer',
 | |
|         ];
 | |
| 
 | |
|         $params    = input();
 | |
|         $page      = $params['page'] ?? 1;
 | |
|         $size      = $params['size'] ?? 10;
 | |
|         $accountId = $this->request->user['user_id'] ?? 0;
 | |
| 
 | |
| 
 | |
|         $params['page'] = 1;
 | |
|         $params['size'] = 0;
 | |
| 
 | |
|         $validate = $this->validateByApi($params, $rules);
 | |
|         if ($validate !== true) {
 | |
|             return $validate;
 | |
|         }
 | |
| 
 | |
|         //获取收藏相关
 | |
|         $collection = AccountRecord::where('type', AccountRecord::TYPE_SPU)
 | |
|             ->where('action', AccountRecord::ACTION_COLLECT)
 | |
|             ->where('account_id', $accountId)
 | |
|             ->where('is_record', AccountRecord::COMMON_ON)
 | |
|             ->order('recorded_at', 'desc');
 | |
| 
 | |
|         $total = $collection->count();
 | |
|         if ($total <= 0) {
 | |
|             return $this->json(0, 'success', [
 | |
|                 'total'   => 0,
 | |
|                 'current' => $page,
 | |
|                 'size'    => $size,
 | |
|                 'list'    => new Collection(),
 | |
|             ]);
 | |
|         }
 | |
| 
 | |
|         $recordList = $collection->page($page)->limit($size)->field('relation_id,recorded_at')->select();
 | |
| 
 | |
|         $where   = [];
 | |
|         $where[] = ['id', 'in', $recordList->column('relation_id')];
 | |
| 
 | |
|         $list = SpuRepository::getInstance()->listForFront($params, function ($q) {
 | |
|             return $q->with([
 | |
|                 'activity_info' => function ($query) {
 | |
|                     $query->withoutField('content');
 | |
|                 }
 | |
|             ]);
 | |
|         }, [], $where);
 | |
| 
 | |
|         $data = [];
 | |
|         $spuList = $list['list']->toArray();
 | |
|         foreach ($recordList as $record) {
 | |
|             foreach ($spuList as $key => $spu) {
 | |
|                 if ($record['relation_id'] == $spu['id']) {
 | |
|                     $data[] = $spu;
 | |
|                     unset($spuList[$key]);
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         $list['total']   = $total;
 | |
|         $list['current'] = $page;
 | |
|         $list['size']    = $size;
 | |
|         $list['list']    = $data;
 | |
| 
 | |
|         return $this->json(0, 'success', $list);
 | |
|     }
 | |
| 
 | |
| 
 | |
|     /**
 | |
|      * SPU 详情
 | |
|      */
 | |
|     public function detail(): Json
 | |
|     {
 | |
|         $repo       = SpuRepository::getInstance();
 | |
|         $id         = input('id/d', 0);
 | |
|         $shareId    = input('share_id/d', 0);//分享人ID
 | |
|         $isActivity = input('is_activity/d', 0);//分享人ID
 | |
|         $accountId  = $this->request->user['user_id'] ?? 0;
 | |
| 
 | |
|         try {
 | |
|             $data = $repo->detail($id, $accountId, $shareId, (bool) $isActivity);
 | |
|             return $this->json(0, 'success', $data);
 | |
|         } catch (RepositoryException $e) {
 | |
|             return $this->json(4001, $e->getMessage());
 | |
|         } catch (Exception $e) {
 | |
|             $repo->log($e->getMessage(), $e);
 | |
|             return $this->json(5000, '获取详情失败');
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 获取指定活动商品的拼团列表 仅限拼团活动商品
 | |
|      */
 | |
|     public function groupList(): Json
 | |
|     {
 | |
|         $id = input('id/d', 0);
 | |
| 
 | |
|         try {
 | |
|             $data = OrderRepository::getInstance()->getGroupList($id);
 | |
|             return $this->json(0, 'success', $data);
 | |
|         } catch (RepositoryException $e) {
 | |
|             return $this->json(4001, $e->getMessage());
 | |
|         } catch (Exception $e) {
 | |
|             SpuRepository::log($e->getMessage(), $e);
 | |
|             return $this->json(5000, '获取拼团列表失败');
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 收藏
 | |
|      */
 | |
|     public function record(): Json
 | |
|     {
 | |
|         if (!$this->request->isPost()) {
 | |
|             return $this->json(4000, '无效请求');
 | |
|         }
 | |
| 
 | |
|         $accountId = $this->request->user['user_id'] ?? 0;
 | |
|         $id        = $this->request->param('id/d', 0);
 | |
|         $action    = $this->request->param('action/s', '');
 | |
| 
 | |
|         try {
 | |
|             if ($accountId <= 0 || $id <= 0) {
 | |
|                 return $this->json(4001, '无效请求');
 | |
|             }
 | |
| 
 | |
|             if (!in_array($action, AccountRecord::allowActions())) {
 | |
|                 return $this->json(4001, '操作类型参数错误');
 | |
|             }
 | |
| 
 | |
|             if (!SpuModel::findById($id)) {
 | |
|                 return $this->json(4001, '商品不存在');
 | |
|             }
 | |
| 
 | |
|             AccountRecord::record($accountId, AccountRecord::TYPE_SPU, $action, $id);
 | |
|         } catch (Exception $e) {
 | |
|             Log::error('[商品记录失败]'.$e->getMessage());
 | |
|             return $this->json(5000, '操作失败');
 | |
|         }
 | |
| 
 | |
|         return $this->json();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 取消 收藏
 | |
|      */
 | |
|     public function unRecord(): Json
 | |
|     {
 | |
|         if (!$this->request->isPost()) {
 | |
|             return $this->json(4000, '无效请求');
 | |
|         }
 | |
| 
 | |
|         $accountId = $this->request->user['user_id'] ?? 0;
 | |
|         $id        = $this->request->param('id/d', 0);
 | |
|         $action    = $this->request->param('action/s', '');
 | |
| 
 | |
|         try {
 | |
|             if ($accountId <= 0 || $id <= 0) {
 | |
|                 return $this->json(4001, '无效请求');
 | |
|             }
 | |
| 
 | |
|             if (!in_array($action, AccountRecord::allowActions())) {
 | |
|                 return $this->json(4001, '操作类型参数错误');
 | |
|             }
 | |
| 
 | |
|             if (!SpuModel::findById($id)) {
 | |
|                 return $this->json(4001, '商品不存在');
 | |
|             }
 | |
| 
 | |
|             AccountRecord::unRecord($accountId, $id, AccountRecord::TYPE_SPU, $action);
 | |
|         } catch (Exception $e) {
 | |
|             Log::error('[取消商品记录失败]'.$e->getMessage());
 | |
|             return $this->json(5000, '操作失败');
 | |
|         }
 | |
| 
 | |
|         return $this->json();
 | |
|     }
 | |
| } |