Goods.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace app\api\controller\service;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Loader;
  6. use think\Exception;
  7. /**
  8. * 首页接口
  9. */
  10. class Goods extends Api
  11. {
  12. protected $noNeedLogin = ['categoryGoods','getList','goodsInfo'];
  13. protected $noNeedRight = ['*'];
  14. /**
  15. * 搜索查询项目
  16. * @return void
  17. */
  18. public function getList()
  19. {
  20. $uid = $this->auth->isLogin()?$this->auth->id:'';
  21. if (!$this->request->isPost()) {
  22. $this->error('请求方式异常');
  23. }
  24. $post = input('post.','','trim,strip_tags');
  25. $post['limit'] = 10;
  26. $post['page'] = input('page/d',1);
  27. if(array_key_exists('name',$post) && $uid)
  28. {
  29. \app\api\model\service\SearchLog::create(['user_id'=>$uid,'name'=>$post['name']]);
  30. }
  31. $list = \app\api\model\service\Goods::searchGoods($post);
  32. if($list)
  33. {
  34. foreach ($list as &$value)
  35. {
  36. $value['shopname'] = $value['shop_id']?\app\api\model\service\Shop::getName($value['shop_id']):'';;
  37. }
  38. }
  39. $this->success('信息返回成功',$list);
  40. }
  41. public function goodsAddList()
  42. {
  43. if (!$this->request->isPost()) {
  44. $this->error('请求方式异常');
  45. }
  46. $id = input('id/d','');
  47. $list = \app\api\model\service\GoodsAdd::where(['goods_id'=>$id,'state'=>1])->field('id,name,price,cost_seconds')->select();
  48. $this->success('信息返回成功',$list);
  49. }
  50. /**
  51. * 首页分类项目列表
  52. * @return void
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. * @throws \think\exception\DbException
  56. */
  57. public function categoryGoods()
  58. {
  59. $city = input('city','');
  60. $categoryList = model('app\api\model\service\Category')
  61. ->where(['status'=>'normal','pid'=>0,'is_show'=>1])
  62. ->field('id,name')
  63. ->order('weigh desc')
  64. ->select();
  65. if($categoryList)
  66. {
  67. foreach ($categoryList as &$value)
  68. {
  69. $value['goodsList'] = \app\api\model\service\Goods::searchGoods(['category_id'=>$value['id'],'page'=>1,'limit'=>4,'city'=>$city]);
  70. }
  71. }
  72. $this->success('信息返回成功',$categoryList);
  73. }
  74. /**
  75. * 用户端项目详情
  76. * @return void
  77. */
  78. public function goodsInfo()
  79. {
  80. if (!$this->request->isPost()) {
  81. $this->error('请求方式异常');
  82. }
  83. $uid = $this->auth->isLogin()?$this->auth->id:'';
  84. $id = input('id/d','');
  85. $uid = $this->auth->isLogin()?$this->auth->id:'';
  86. $info = \app\api\model\service\Goods::getInfo($id,$uid);
  87. !$info && $this->error('项目信息异常');
  88. $info['isPlus'] = $uid?\app\api\model\service\UserInfo::where('user_id',$uid)->value('is_plus'):0;
  89. $this->success('信息返回成功',$info);
  90. }
  91. /**
  92. * 服务人员更新服务项目
  93. * @return void
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. * @throws \think\exception\DbException
  97. */
  98. public function skillCategoryGoods()
  99. {
  100. $uid = $this->auth->id;
  101. $shop_id = \app\api\model\service\Skill::where(['user_id'=>$uid])->value('shop_id');
  102. $categoryList = \app\api\model\service\Category::where(['status'=>'normal','pid'=>0])->field('id,name')->select();
  103. $where['shop_id'] = $shop_id?['in',[0,$shop_id]]:0;
  104. $where['state'] = 1;
  105. foreach ($categoryList as &$value)
  106. {
  107. $where['category_id'] = $value['id'];
  108. $value['goodsList'] = \app\api\model\service\Goods::where($where)->field('id,name,image')->select();
  109. }
  110. $this->success('信息返回成功',$categoryList);
  111. }
  112. /**
  113. * 店铺添加项目
  114. * @return void
  115. * @throws \think\db\exception\DataNotFoundException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. * @throws \think\exception\DbException
  118. */
  119. public function shopAddGoods()
  120. {
  121. if (!$this->request->isPost()) {
  122. $this->error('请求方式异常');
  123. }
  124. $uid = $this->auth->id;
  125. $shop = \app\api\model\service\Shop::where(['user_id'=>$uid])->field('id,to_shop,province,city,district')->find();
  126. $post = input('post.','','trim,strip_tags');
  127. $post['shop_id'] = $shop['id'];
  128. $post['shop_user_id'] = $uid;
  129. $post['type'] = 1;
  130. $post['province'] = $shop['province'];
  131. $post['city'] = $shop['city'];
  132. $post['district'] = $shop['district'];
  133. (strstr($post['to_shop'],'shop') && $shop['to_shop'] == 0) && $this->error('未开通上门服务权限');
  134. $validate = Loader::validate('service.Goods');
  135. if(!$validate->scene('add')->check($post)){
  136. $this->error($validate->getError());
  137. }
  138. Db::startTrans();
  139. try{
  140. \app\api\model\service\Goods::addGoods($post);
  141. Db::commit();
  142. } catch (Exception $e) {
  143. Db::rollback();
  144. $this->error('项目添加失败',$e->getMessage());
  145. }
  146. $this->success('项目已添加,等待管理员审核');
  147. }
  148. /**
  149. * 商户获得添加项目信息
  150. * @return void
  151. */
  152. public function shopGoodsInfo()
  153. {
  154. if (!$this->request->isPost()) {
  155. $this->error('请求方式异常');
  156. }
  157. $id = input('id/d','');
  158. $goods = \app\api\model\service\Goods::getShopGoods($id);
  159. !$goods['id'] && $this->error('商品信息异常');
  160. $this->success('信息返回成功',$goods);
  161. }
  162. /**
  163. * 商户修改项目
  164. * @return void
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. * @throws \think\exception\DbException
  168. */
  169. public function shopEditGoods()
  170. {
  171. if (!$this->request->isPost()) {
  172. $this->error('请求方式异常');
  173. }
  174. $uid = $this->auth->id;
  175. $id = input('id/d','');
  176. $shop = \app\api\model\service\Shop::where(['user_id'=>$uid])->field('id,to_shop')->find();
  177. $goodsId = \app\api\model\service\Goods::where(['id'=>$id,'shop_user_id'=>$uid])->value('id');
  178. !$goodsId && $this->error('商品信息异常');
  179. $post = input('post.','','trim,strip_tags');
  180. (strstr($post['to_shop'],'shop') && $shop['to_shop'] == 0) && $this->error('未开通上门服务权限');
  181. $validate = Loader::validate('service.Goods');
  182. if(!$validate->scene('edit')->check($post)){
  183. $this->error($validate->getError());
  184. }
  185. Db::startTrans();
  186. try{
  187. \app\api\model\service\Goods::editGoods($post);
  188. Db::commit();
  189. } catch (Exception $e) {
  190. Db::rollback();
  191. $this->error('项目编辑失败',$e->getMessage());
  192. }
  193. $this->success('项目已编辑,等待管理员审核');
  194. }
  195. /**
  196. * 商户下架项目
  197. * @return void
  198. */
  199. public function downGoods()
  200. {
  201. if (!$this->request->isPost()) {
  202. $this->error('请求方式异常');
  203. }
  204. $uid = $this->auth->id;
  205. $id = input('id/d','');
  206. $goodsId = \app\api\model\service\Goods::where(['id'=>$id,'shop_user_id'=>$uid,'status'=>'normal'])->value('id');
  207. !$goodsId && $this->error('当前商品无法下架');
  208. \app\api\model\service\Goods::downGoods($id);
  209. $this->success('商品已下架');
  210. }
  211. /**
  212. * 商户上架项目
  213. * @return void
  214. *
  215. */
  216. public function upGoods()
  217. {
  218. if (!$this->request->isPost()) {
  219. $this->error('请求方式异常');
  220. }
  221. $uid = $this->auth->id;
  222. $id = input('id/d','');
  223. $goodsId = \app\api\model\service\Goods::where(['id'=>$id,'shop_user_id'=>$uid,'shop_state'=>1,'status'=>'hidden'])->value('id');
  224. !$goodsId && $this->error('当前商品无法上架');
  225. \app\api\model\service\Goods::upGoods($id);
  226. $this->success('商品已上架');
  227. }
  228. public function getUserGoods()
  229. {
  230. if (!$this->request->isPost()) {
  231. $this->error('请求方式异常');
  232. }
  233. $uid = $this->auth->id;
  234. $type = input('type/d','');
  235. $userInfo = \app\api\model\service\UserInfo::where(['user_id'=>$uid])->field('id,user_id,is_skill,is_shop')->find();
  236. (($type == 0 && $userInfo['is_skill'] != 1) || ($type == 1 && $userInfo['is_shop'] != 1)) && $this->error('当前身份不符,无法查看');
  237. $goodsIds = $type == 0?\app\api\model\service\Skill::where(['user_id'=>$uid])->value('goods_ids'):\app\api\model\service\Shop::where(['user_id'=>$uid])->value('goods_ids');
  238. $goodsList = $type == 0?\app\api\model\service\Goods::getSkillGoods($goodsIds):\app\api\model\service\Goods::getShopPlatformGoods($goodsIds,$uid);
  239. $this->success('信息返回成功',$goodsList);
  240. }
  241. public function getApplyGoods()
  242. {
  243. if (!$this->request->isPost()) {
  244. $this->error('请求方式异常');
  245. }
  246. $uid = $this->auth->id;
  247. $type = input('type/d','');
  248. $city = input('city','','trim,strip_tags');
  249. $category_id = input('category_id/d');
  250. $page = input('page/d','');
  251. $userInfo = \app\api\model\service\UserInfo::where(['user_id'=>$uid])->field('id,user_id,is_skill,is_shop')->find();
  252. $page = empty($page)?0:($page-1)*10;
  253. $limit = $page.',10';
  254. $config = config('database');
  255. $db = $config['prefix'].'service_goods';
  256. if(($type == 0 && $userInfo['is_skill'] == 0) || $type == 1)
  257. {
  258. $list = Db::query("SELECT id,name,shop_id,image,price,tag_name,response_hour,salenums,type,skill_cate_ids,category_id,city FROM {$db} WHERE (category_id={$category_id} AND status='normal' AND shop_id=0 AND shop_state=1) AND (city='{$city}' OR city IS NULL) AND (deletetime IS NULL) ORDER BY id DESC LIMIT {$limit}");
  259. }elseif($type == 0 && $userInfo['is_skill'] == 1)
  260. {
  261. $shop_id = \app\api\model\service\Skill::where(['user_id'=>$uid])->value('shop_id');
  262. if($shop_id){
  263. $list = Db::query("SELECT id,name,shop_id,image,price,tag_name,response_hour,salenums,type,skill_cate_ids,category_id,city FROM {$db} WHERE (category_id={$category_id} AND shop_state=1 AND status='normal' AND shop_id IN(0,'{$shop_id}')) AND (city='{$city}' OR city IS NULL) AND (deletetime IS NULL) ORDER BY id DESC LIMIT {$limit}");
  264. }else{
  265. $list = Db::query("SELECT id,name,shop_id,image,price,tag_name,response_hour,salenums,type,skill_cate_ids,category_id,city FROM {$db} WHERE (category_id={$category_id} AND shop_state=1 AND status='normal' AND shop_id=0) AND (city='{$city}' OR city IS NULL) AND (deletetime IS NULL) ORDER BY id DESC LIMIT {$limit}");
  266. }
  267. }
  268. $this->success('信息返回成功',$list);
  269. }
  270. }