Shop.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. namespace app\api\controller\service;
  3. use app\api\model\service\JoinShop;
  4. use app\common\controller\Api;
  5. use think\Db;
  6. use think\Exception;
  7. use think\Loader;
  8. /**
  9. * 首页接口
  10. */
  11. class Shop extends Api
  12. {
  13. protected $noNeedLogin = ['searchShop','shopDetail','getCodeShop','getSampleShop','searchShopGoods'];
  14. protected $noNeedRight = ['*'];
  15. /**
  16. * 申请成为商家
  17. * @return void
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. * @throws \think\exception\DbException
  21. */
  22. public function apply()
  23. {
  24. $uid = $this->auth->id;
  25. $userInfo = model('app\api\model\service\UserInfo')->where(['user_id'=>$uid])->field('id,is_shop')->find();
  26. $info = model('app\api\model\service\ApplyShop')->where(['user_id'=>$uid])->find();
  27. if (!$this->request->isPost()) {
  28. $this->error('请求方式异常');
  29. }
  30. $post = input('post.','','trim,strip_tags,htmlspecialchars,xss_clean');
  31. $types = input('types','');
  32. if (!$this->request->isPost()) {
  33. $this->error('请求方式异常');
  34. }
  35. $applyShop = new \app\api\model\service\ApplyShop();
  36. $post['user_id'] = $uid;
  37. $post['updatetime'] = time();
  38. if(in_array($types,['add','edit']))
  39. {
  40. $validate = Loader::validate('service.ApplyShop');
  41. if(!$validate->scene($types)->check($post)){
  42. $this->error($validate->getError());
  43. }
  44. }
  45. if($types == 'add')
  46. {
  47. unset($post['types']);
  48. $applyShop->data($post)->save();
  49. $this->success('申请已提交,等待审核');
  50. }elseif($types == 'edit') {
  51. unset($post['types']);
  52. if($userInfo['is_shop'] == 1)
  53. {
  54. $post['applytype'] = 1;
  55. }
  56. $post['state'] = 0;
  57. $applyShop->allowField(true)->save($post,['id'=>$info['id']]);
  58. $this->success('申请已提交,等待审核');
  59. }
  60. $this->success('信息返回成功',$info);
  61. }
  62. /**
  63. * 查询商家列表
  64. * @return void
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. * @throws \think\exception\DbException
  68. */
  69. public function searchShop()
  70. {
  71. if (!$this->request->isPost()) {
  72. $this->error('请求方式异常');
  73. }
  74. $post = input('post.','','trim,strip_tags');
  75. !$post['city'] && $this->error('城市信息缺失');
  76. (!$post['lng'] || !$post['lat']) && $this->error('定位信息异常');
  77. $list = \app\api\model\service\Shop::searchShop($post);
  78. $this->success('信息列表返回成功',$list);
  79. }
  80. public function searchShopGoods()
  81. {
  82. if (!$this->request->isPost()) {
  83. $this->error('请求方式异常');
  84. }
  85. $shopId = input('shop_id','');
  86. $shopGoodsIds = \app\api\model\service\Shop::where('id',$shopId)->value('goods_ids');
  87. $list = \app\api\model\service\Goods::getNewShopGoods($shopGoodsIds,1);
  88. $this->success('信息返回成功',$list);
  89. }
  90. /**
  91. * 简单商户列表
  92. * @return void
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. * @throws \think\exception\DbException
  96. */
  97. public function shopList()
  98. {
  99. if (!$this->request->isPost()) {
  100. $this->error('请求方式异常');
  101. }
  102. $uid = $this->auth->id;
  103. $skillCity = \app\api\model\service\Skill::where('user_id',$uid)->value('city');
  104. $page = input('page/d',1);
  105. $limit = 10;
  106. $list = \app\api\model\service\Shop::where(['state'=>1,'city'=>$skillCity])->field('id,code,logo_image,intro,user_id,name,abbr,leader_name,leader_mobile')->order('weigh desc')->page($page)->limit($limit)->select();
  107. foreach ($list as &$value)
  108. {
  109. $value['followNum'] = \app\api\model\service\Follow::getCount(['follow_id'=>$value['id'],'state'=>1]);
  110. $value['commentScore'] = \app\api\model\service\Comment::getCommentScore(['shop_id'=>$value['id'],'state'=>1]);
  111. }
  112. $this->success('信息返回成功',$list);
  113. }
  114. public function getSampleShop()
  115. {
  116. if (!$this->request->isPost()) {
  117. $this->error('请求方式异常');
  118. }
  119. $id = input('id/d','');
  120. $shop = \app\api\model\service\Shop::where(['id'=>$id])->field('id,code,logo_image,intro,user_id,name,abbr,license_image')->find();
  121. $shop['followNum'] = \app\api\model\service\Follow::getCount(['follow_id'=>$shop['id'],'state'=>1]);
  122. $shop['commentScore'] = \app\api\model\service\Comment::getCommentScore(['shop_id'=>$shop['id'],'state'=>1]);
  123. $this->success('信息返回成功',$shop);
  124. }
  125. public function shopDetail()
  126. {
  127. if (!$this->request->isPost()) {
  128. $this->error('请求方式异常');
  129. }
  130. $id = input('id/d','');
  131. $page = input('page/d',1);
  132. $info = \app\api\model\service\Shop::getShopDetail(['id'=>$id,'page'=>$page]);
  133. $uid = $this->auth->isLogin()?$this->auth->id:'';
  134. !$info && $this->error('商家信息异常');
  135. $info['followNum'] = \app\api\model\service\Follow::getCount(['follow_id'=>$info['id'],'state'=>1]);
  136. $info['commentScore'] = \app\api\model\service\Comment::getCommentScore(['shop_id'=>$info['id'],'state'=>1]);
  137. $info['followState'] = $uid?\app\api\model\service\Follow::getState(['user_id'=>$uid,'follow_id'=>$id]):0;
  138. $this->success('信息返回成功',$info);
  139. }
  140. /**
  141. * 商户处理服务人员申请
  142. * @return void
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. * @throws \think\exception\DbException
  146. */
  147. public function joinShopList()
  148. {
  149. if (!$this->request->isPost()) {
  150. $this->error('请求方式异常');
  151. }
  152. $uid = $this->auth->id;
  153. $startTime = strtotime(date("Y-m-d",time()));
  154. $shop = \app\api\model\service\Shop::where(['user_id'=>$uid])->field('id,accept_nums,service_nums,already_service_nums,percent')->find();
  155. $shop['already_accept_nums'] = \app\api\model\service\Order::where(['shop_id'=>$shop['id'],'status'=>['>',1],'starttime'=>['>=',$startTime]])->count();
  156. $type = input('type','');
  157. if($type == 'check')
  158. {
  159. $post = input('post.','');
  160. ($post['state'] == 1 && $post['percent']>$shop['percent']) && $this->error('订单分成请勿超出商户分成');
  161. ($post['state'] == 1 && $shop['already_service_nums']>=$shop['service_nums']) && $this->error('商户服务者数量已达到上限');
  162. $re = \app\api\model\service\JoinShop::check($post);
  163. !$re && $this->error('审核失败');
  164. $this->success('已审核');
  165. }
  166. $state = input('state/d',0);
  167. $page = input('page/d','');
  168. $list = \app\api\model\service\JoinShop::getApplyShop(['shop_id'=>$shop['id'],'state'=>$state,'page'=>$page]);
  169. $this->success('信息返回成功',$list);
  170. }
  171. public function allocationOrder()
  172. {
  173. if (!$this->request->isPost()) {
  174. $this->error('请求方式异常');
  175. }
  176. $uid = $this->auth->id;
  177. $shop_id = \app\api\model\service\Shop::where(['user_id'=>$uid])->value('id');
  178. $post = input('post.','');
  179. $post['shop_id'] = $shop_id;
  180. try{
  181. \app\api\model\service\Order::allocationOrder($post);
  182. } catch (Exception $e) {
  183. $this->error($e->getMessage());
  184. }
  185. $this->success('订单已分配');
  186. }
  187. public function getShopSkill()
  188. {
  189. if (!$this->request->isPost()) {
  190. $this->error('请求方式异常');
  191. }
  192. $uid = $this->auth->id;
  193. $shop_id = \app\api\model\service\Shop::where(['user_id'=>$uid])->value('id');
  194. $id = input('id/d','');
  195. $orderAddress = \app\api\model\service\OrderAddress::where(['order_id'=>$id])->field('lng,lat')->find();
  196. $goodsId = \app\api\model\service\Order::where(['id'=>$id])->value('goods_id');
  197. $page = input('page/d','');
  198. $where['shop_id'] = $shop_id;
  199. $where['state'] = 1;
  200. $where[] =['exp',Db::raw("FIND_IN_SET('$goodsId',goods_ids)")];
  201. $list = \app\api\model\service\Skill::getShopSkill($where,$page);
  202. foreach ($list as $key=>$value)
  203. {
  204. $list[$key]['distance'] = \addons\service\library\Common::distance($value['lng'],$value['lat'],$orderAddress['lng'],$orderAddress['lat']);
  205. }
  206. $this->success('信息返回成功',$list);
  207. }
  208. public function getShopSkillInfo()
  209. {
  210. if (!$this->request->isPost()) {
  211. $this->error('请求方式异常');
  212. }
  213. $post = input('post.','','trim,strip_tags');
  214. $uid = $this->auth->id;
  215. $shopId = \app\api\model\service\Shop::where(['user_id'=>$uid])->value('id');
  216. $skill = \app\api\model\service\Skill::where(['id'=>$post['id'],'shop_id'=>$shopId])->field('id,ensure_price,is_rest,image,percent,name,sex,city,user_id')->find();
  217. $post['shop_id'] = $shopId;
  218. $skill['orderList'] = \app\api\model\service\Order::getShopSkillOrder($post);
  219. if(isset($post['page']))
  220. {
  221. unset($post['page']);
  222. }
  223. $skill['skillDetail'] = \app\api\model\service\Skill::skillShopDetail(['shop_id'=>$shopId,'id'=>$skill['id'],'user_id'=>$skill['user_id']]);
  224. $skill['starttime'] = $post['starttime'] ?? '';
  225. $skill['endtime'] = $post['endtime'] ?? '';
  226. $this->success('信息返回成功',$skill);
  227. }
  228. /**
  229. * 商户项目列表
  230. * @return void
  231. * @throws \think\db\exception\DataNotFoundException
  232. * @throws \think\db\exception\ModelNotFoundException
  233. * @throws \think\exception\DbException
  234. */
  235. public function shopGoods()
  236. {
  237. if (!$this->request->isPost()) {
  238. $this->error('请求方式异常');
  239. }
  240. $uid = $this->auth->id;
  241. $shop = \app\api\model\service\Shop::where(['user_id'=>$uid])->field('id,to_shop,name,intro,logo_image')->find();
  242. $page = input('page/d',1);
  243. $shop_state = input('shop_state/d',0);
  244. $list = \app\api\model\service\Goods::where(['shop_id'=>$shop['id'],'shop_state'=>$shop_state])->field('id,name,cost_seconds,to_shop,note,image,tag_name,price,shop_state,status')->page($page)->limit(10)->select();
  245. $data['list'] = $list;
  246. $data['shop'] = $shop;
  247. $this->success('信息返回成功',$data);
  248. }
  249. /**
  250. * 商户个人中心
  251. * @return void
  252. * @throws \think\db\exception\DataNotFoundException
  253. * @throws \think\db\exception\ModelNotFoundException
  254. * @throws \think\exception\DbException
  255. */
  256. public function shopIndex()
  257. {
  258. if (!$this->request->isPost()) {
  259. $this->error('请求方式异常');
  260. }
  261. $uid = $this->auth->id;
  262. $data['userInfo'] = \app\api\model\service\UserInfo::where(['user_id'=>$uid])->field('shop_user_money,money')->find();
  263. $shopInfo = \app\api\model\service\Shop::where(['user_id'=>$uid])->field('id,ensure_price,category_ids,to_shop,goods_ids,accept_nums,already_service_nums,service_nums,name,abbr,code,logo_image')->find();
  264. $shopInfo['already_accept_nums'] = \app\api\model\service\Order::where(['shop_id'=>$shopInfo['id'],'status'=>['>',1],'starttime'=>['>=',strtotime("Y-m-d",time())]])->count();
  265. $data['skillCount'] = \app\api\model\service\Skill::getCount(['shop_id'=>$shopInfo['id']]);
  266. $shopInfo['goodsList'] = explode(',',$shopInfo['goods_ids']);
  267. $data['goodsCount'] = count(\app\api\model\service\Goods::getShopPlatformGoods($shopInfo['goods_ids'],$uid));
  268. $data['shopInfo'] = $shopInfo;
  269. $data['shopGoodsCount'] = \app\api\model\service\Goods::getCount(['shop_id'=>$shopInfo['id'],'shop_state'=>1]);
  270. $data['applyCount'] = \app\api\model\service\JoinShop::getCount(['shop_id'=>$shopInfo['id']]);
  271. $this->success('信息返回成功',$data);
  272. }
  273. /**
  274. * 商户端首页
  275. * @return void
  276. * @throws \think\Exception
  277. */
  278. public function shopHome()
  279. {
  280. if (!$this->request->isPost()) {
  281. $this->error('请求方式异常');
  282. }
  283. $uid = $this->auth->id;
  284. $shopId = \app\api\model\service\Shop::where(['user_id'=>$uid])->value('id');
  285. $post = input('post.','','trim,strip_tags');
  286. $post['shop_id'] = $shopId;
  287. //筛选时间,订单数量,订单金额
  288. $starttime = strtotime(date("Y-m-d",time()));
  289. $data['dayNum'] = \app\api\model\service\Rebate::getNum(['user_id'=>$uid,'type'=>1,'createtime'=>['>',$starttime]]);
  290. $data['poolNum'] = \app\api\model\service\Order::getOrderCount(['is_pool'=>1,'status'=>1,'is_service'=>['in',[0,-1]]]);
  291. $data['dayNewCount'] = \app\api\model\service\Order::getOrderCount(['shop_id'=>$shopId,'createtime'=>['>',$starttime],'is_service'=>['in',[0,-1]]]);
  292. $data['dayFinishCount'] = \app\api\model\service\Order::getOrderCount(['shop_id'=>$shopId,'status'=>['>',5],'finishtime'=>['>',$starttime],'is_service'=>['in',[0,-1]]]);
  293. $data['dayDispatchCount'] = \app\api\model\service\Order::getOrderCount(['shop_id'=>$shopId,'status'=>1,'to_shop'=>'door','is_service'=>['in',[0,-1]],'skill_id'=>['null','']]);
  294. $data['dayServiceList'] = \app\api\model\service\Order::getDayServiceList(['shop_id'=>$shopId,'status'=>['in','1,2,3,4,5'],'is_service'=>['in',[0,-1]]]);
  295. $data['form'] = \app\api\model\service\Order::getForm($post);
  296. $this->success('信息返回成功',$data);
  297. }
  298. /**
  299. * 商户下服务者列表
  300. * @return void
  301. */
  302. public function shopSkillList()
  303. {
  304. if (!$this->request->isPost()) {
  305. $this->error('请求方式异常');
  306. }
  307. $uid = $this->auth->id;
  308. $shopId = \app\api\model\service\Shop::where(['user_id'=>$uid])->value('id');
  309. $page = input('page/d',1);
  310. $list = \app\api\model\service\Skill::getShopSkillList(['shop_id'=>$shopId],$page);
  311. $this->success('信息返回成功',$list);
  312. }
  313. public function updateShopInfo()
  314. {
  315. if (!$this->request->isPost()) {
  316. $this->error('请求方式异常');
  317. }
  318. $uid = $this->auth->id;
  319. $shop = \app\api\model\service\Shop::where(['user_id'=>$uid])->field('id,withdrawtype,day')->find();
  320. $type = input('type','','trim,strip_tags');
  321. if($type == 'update')
  322. {
  323. $post = input('post.','','trim,strip_tags,htmlspecialchars,xss_clean');
  324. model('app\api\model\service\Shop')->allowField('withdrawtype,day')->save($post,['user_id'=>$uid]);
  325. $this->success('信息已更新');
  326. }
  327. $this->success('信息返回成功',$shop);
  328. }
  329. public function shopCoupon()
  330. {
  331. if (!$this->request->isPost()) {
  332. $this->error('请求方式异常');
  333. }
  334. $uid = $this->auth->id;
  335. $post = input('post.','','trim,strip_tags');
  336. $shopId = \app\api\model\service\Shop::where(['user_id'=>$uid])->value('id');
  337. $post['shop_id'] = $shopId;
  338. $list = \app\api\model\service\Coupon::getCouponList($post);
  339. $this->success('信息返回成功',$list);
  340. }
  341. public function getCodeShop()
  342. {
  343. $code = input('code','');
  344. $shop = \app\api\model\service\Shop::where(['code'=>$code,'state'=>1])->field('id,name,abbr,intro,logo_image')->find();
  345. !$shop && $this->error('未获取到相关商户信息');
  346. $this->success('信息返回成功',$shop);
  347. }
  348. public function editCoupon()
  349. {
  350. if (!$this->request->isPost()) {
  351. $this->error('请求方式异常');
  352. }
  353. $uid = $this->auth->id;
  354. $shopId = \app\api\model\service\Shop::where(['user_id'=>$uid])->value('id');
  355. $types = input('types','');
  356. if($types == 'add')
  357. {
  358. $post = input('post.','','trim,strip_tags,htmlspecialchars,xss_clean');
  359. $goodsList = \app\api\model\service\Goods::where(['shop_id'=>$shopId])->column('id');
  360. ($post['type'] == 3 && !in_array($post['goods_id'],$goodsList)) && $this->error('商品信息不符');
  361. $post['shop_id'] = $shopId;
  362. $post['state'] = 0;
  363. $coupon = new \app\api\model\service\Coupon($post);
  364. $coupon->allowField(true)->save();
  365. $this->success('信息已提交,等待管理员审核');
  366. }elseif ($types == 'edit')
  367. {
  368. $post = input('post.','','trim,strip_tags,htmlspecialchars,xss_clean');
  369. $coupon = \app\api\model\service\Coupon::where(['id'=>$post['id'],'shop_id'=>$shopId])->find();
  370. !$coupon && $this->error('优惠券信息异常');
  371. $coupon = new \app\api\model\service\Coupon();
  372. $post['is_check'] = 0;
  373. $coupon->allowField(true)->save($post,['id'=>$post['id']]);
  374. $this->success('已提交,等待管理员审核');
  375. }elseif ($types == 'info')
  376. {
  377. $id = input('id/d','');
  378. $coupon = \app\api\model\service\Coupon::where(['id'=>$id,'shop_id'=>$shopId])->find();
  379. !$coupon && $this->error('优惠券信息异常');
  380. $this->success('信息返回成功',$coupon);
  381. }
  382. $this->error('接口信息异常');
  383. }
  384. }