Store.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 好店
  7. */
  8. namespace app\allwin\controller\api\v4;
  9. use app\allwin\controller\api\Base;
  10. use app\allwin\model\AllwinStore;
  11. use app\allwin\model\AllwinShop;
  12. use app\allwin\model\AllwinStoreCate;
  13. use app\allwin\model\Coupon;
  14. use app\allwin\model\CouponUser;
  15. use app\allwin\model\StoreWorker;
  16. use app\common\model\SystemMemberSms;
  17. use app\common\event\User;
  18. use app\common\facade\Inform;
  19. use app\common\facade\Upload;
  20. use filter\Filter;
  21. class Store extends Base{
  22. /**
  23. * 首页显示
  24. */
  25. public function index(){
  26. $param['signkey'] = $this->request->param('signkey');
  27. $param['sign'] = $this->request->param('sign');
  28. $rel = $this->apiSign($param);
  29. if($rel['code'] != 200){
  30. return enjson(500,'签名验证失败');
  31. }
  32. $where = [];
  33. $where[] = ['member_miniapp_id','=',$this->miniapp_id];
  34. $where[] = ['is_lock','=',0];
  35. $where[] = ['is_top','=',1];
  36. $rel = AllwinStore::where($where)->field('id,name,longitude,latitude,img,telphone,address,score,tips,tags,is_top,state_text')->order('sort desc,id desc')->paginate(10)->toArray();
  37. if(empty($rel['data'])){
  38. return json(['code'=>204,'msg'=>'没有内容']);
  39. }else{
  40. $data = $rel['data'];
  41. foreach ($rel['data'] as $key => $value) {
  42. $data[$key]['tags'] = explode(',',$value['tags']);
  43. $data[$key]['img'] = $value['img']."?x-oss-process=style/w100";
  44. $data[$key]['distance'] = getDistance($this->qqgps['lng'],$this->qqgps['lat'],$value['longitude'],$value['latitude']);
  45. }
  46. return json(['code'=>200,'msg'=>'成功','data' => $data]);
  47. }
  48. }
  49. /**
  50. * 点赞
  51. **/
  52. public function like(){
  53. $this->isUserAuth();
  54. $param['id'] = $this->request->param('id/d',0);
  55. $param['sign'] = $this->request->param('sign');
  56. $rel = $this->apiSign($param);
  57. if($rel['code'] != 200){
  58. return enjson($rel['code'],'签名验证失败');
  59. }
  60. $condition['member_miniapp_id'] = $this->miniapp_id;
  61. $condition['id'] = $param['id'];
  62. $rel = AllwinStore::where($condition)->setInc('views',1);
  63. if(empty($rel)){
  64. return enjson(204);
  65. }else{
  66. return enjson(200);
  67. }
  68. }
  69. /**
  70. * 店铺预览
  71. */
  72. public function getView(){
  73. $param['store_id'] = $this->request->param('store_id/d',0);
  74. $param['sign'] = $this->request->param('sign');
  75. $rel = $this->apiSign($param);
  76. if($rel['code'] != 200){
  77. return enjson(500,'签名验证失败');
  78. }
  79. $where['id'] = $param['store_id'];
  80. $where['member_miniapp_id'] = $this->miniapp_id;
  81. $where['is_lock'] = 0;
  82. $info = AllwinStore::getView($where);
  83. if(empty($info)){
  84. return json(['code'=>204,'msg'=>'没有内容']);
  85. }else{
  86. $info['distance'] = getDistance($this->qqgps['lng'],$this->qqgps['lat'],$info['longitude'],$info['latitude']);
  87. $chain = [];
  88. foreach ($info['chains'] as $key => $value) {
  89. $chain[$key]['title'] = $value['title'];
  90. $chain[$key]['address'] = $value['address'];
  91. $chain[$key]['telphone'] = $value['telphone'];
  92. $chain[$key]['distance'] = getDistance($this->qqgps['lng'],$this->qqgps['lat'],$value->longitude,$value->latitude);
  93. }
  94. $info['chain'] = $chain;
  95. unset($info['chains']);
  96. AllwinStore::where(['id'=> $param['store_id']])->setInc('views',1);
  97. return json(['code'=>200,'msg'=>'成功','data' => $info]);
  98. }
  99. }
  100. /**
  101. * 读取行业信息列表(待删除) v4.0
  102. * @param integer 读取ID
  103. * @return json
  104. */
  105. public function cate(){
  106. $param['signkey'] = $this->request->param('signkey');
  107. $param['cate_id'] = $this->request->param('cate_id',0);
  108. $param['lists'] = $this->request->param('lists/d',0);
  109. $param['sign'] = $this->request->param('sign');
  110. $rel = $this->apiSign($param);
  111. if($rel['code'] != 200){
  112. return enjson($rel['code'],'签名验证失败');
  113. }
  114. $where['parent_id'] = $param['cate_id'];
  115. $where['member_miniapp_id'] = $this->miniapp_id;
  116. $data = AllwinStoreCate::where($where)->field('id,name,picture,title')->order('sort desc,id desc')->select()->toArray();
  117. if(empty($data)){
  118. return json(['code'=>404,'msg'=>'没有好店分类']);
  119. }else{
  120. $nav = [];
  121. if($param['cate_id'] == 0 && $param['lists'] == 0){
  122. $i = -1;
  123. foreach($data as $key => $value){
  124. if($key%10 == 0){
  125. $i++;
  126. $nav[$i][] = $value;
  127. }else{
  128. $nav[$i][] = $value;
  129. }
  130. }
  131. }else{
  132. $nav = $data;
  133. }
  134. return json(['code'=>200,'msg'=>'成功','data' => $data]);
  135. }
  136. }
  137. /**
  138. * 读取行业信息列表 v4.1
  139. * @param integer 读取ID
  140. * @return json
  141. */
  142. public function category(){
  143. $param['signkey'] = $this->request->param('signkey');
  144. $param['cate_id'] = $this->request->param('cate_id',0);
  145. $param['lists'] = $this->request->param('lists/d',0);
  146. $param['sign'] = $this->request->param('sign');
  147. $rel = $this->apiSign($param);
  148. if($rel['code'] != 200){
  149. return enjson($rel['code'],'签名验证失败');
  150. }
  151. $where['parent_id'] = $param['cate_id'];
  152. $where['member_miniapp_id'] = $this->miniapp_id;
  153. $data = AllwinStoreCate::where($where)->field('id,name,picture,title')->order('sort desc,id desc')->select()->toArray();
  154. if(empty($data)){
  155. return enjson(404,'没有数据');
  156. }
  157. return enjson(200,'成功',$data);
  158. }
  159. /**
  160. * 店铺列表
  161. */
  162. public function lists(){
  163. $param['keyword'] = $this->request->param('keyword','');
  164. $param['cate_id'] = $this->request->param('cate_id',0);
  165. $param['cate_sid'] = $this->request->param('cate_sid/d',0);
  166. $param['page'] = $this->request->param('page/d',0);
  167. $param['sign'] = $this->request->param('sign');
  168. $rel = $this->apiSign($param);
  169. if($rel['code'] != 200){
  170. return enjson($rel['code'],'签名验证失败');
  171. }
  172. $where = [];
  173. if($param['cate_sid']){
  174. $where[] = ['cate_sid','=',$param['cate_sid']];
  175. }
  176. if($param['cate_id']){
  177. $where[] = ['cate_id','=',$param['cate_id']];
  178. }
  179. $where[] = ['member_miniapp_id','=',$this->miniapp_id];
  180. $where[] = ['is_lock','=',0];
  181. $rel = AllwinStore::where($where)->withSearch(['name'],['name' => $param['keyword']])
  182. ->field('id,name,longitude,latitude,img,telphone,address,score,tips,tags,is_top,state_text')
  183. ->order('is_top desc,sort desc,id desc')->paginate(10)->toArray();
  184. if(empty($rel['data'])){
  185. return enjson(204,'没有内容');
  186. }else{
  187. $data = $rel['data'];
  188. foreach ($rel['data'] as $key => $value) {
  189. $data[$key]['tags'] = explode(',',$value['tags']);
  190. $data[$key]['img'] = $value['img']."?x-oss-process=style/w100";
  191. $data[$key]['distance'] = getDistance($this->qqgps['lng'],$this->qqgps['lat'],$value['longitude'],$value['latitude']);
  192. }
  193. return enjson(200,'成功',$data);
  194. }
  195. }
  196. /**
  197. * 店铺首页产品
  198. */
  199. public function getShop(){
  200. $param['store_id'] = $this->request->param('store_id/d',0);
  201. $param['page'] = $this->request->param('page/d',0);
  202. $param['sign'] = $this->request->param('sign');
  203. $rel = $this->apiSign($param);
  204. if($rel['code'] != 200){
  205. return enjson(500,'签名验证失败');
  206. }
  207. $condition['member_miniapp_id'] = $this->miniapp_id;
  208. $condition['store_id'] = $param['store_id'];
  209. $condition['is_sale'] = 1;
  210. $condition['is_del'] = 0;
  211. //统计数量
  212. $shop_num = 0;
  213. if($param['page'] == 0){
  214. $shop_num = AllwinShop::where($condition)->count();
  215. }
  216. //好店推荐
  217. $condition['types'] = 4;
  218. $data = AllwinShop::where($condition)->field('id,category_id,name,title,img,sell_price,end_time,market_price')->limit(3)->select();
  219. return enjson(200,'成功',['shop' => $data,'shop_num' => $shop_num]);
  220. }
  221. /**
  222. * 店铺首页产品
  223. */
  224. public function getCoupon(){
  225. $param['store_id'] = $this->request->param('store_id/d',0);
  226. $param['page'] = $this->request->param('page/d',0);
  227. $param['sign'] = $this->request->param('sign');
  228. $rel = $this->apiSign($param);
  229. if($rel['code'] != 200){
  230. return enjson(403,'签名失败');
  231. }
  232. $condition = [];
  233. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  234. $condition[] = ['store_id','=',$param['store_id']];
  235. $condition[] = ['is_platform','=',0];
  236. $condition[] = ['is_lock','=',0];
  237. $condition[] = ['is_end','=',0];
  238. $condition[] = ['num','>',0];
  239. $condition[] = ['endtime','>',time()];
  240. //优惠券列表
  241. if($param['page'] == 0){
  242. $condition[] = ['is_top','=',1];
  243. $coupon = Coupon::where($condition)->order('is_top desc,sort desc,size desc')->limit(4)->select();
  244. }else{
  245. $coupon = Coupon::where($condition)->order('is_top desc,sort desc,size desc')->paginate(10);
  246. }
  247. if ($coupon->isEmpty()) {
  248. return enjson(204,'没有优惠券');
  249. }
  250. //优惠券数量
  251. $coupon_num = 0;
  252. if($param['page'] == 0){
  253. $coupon_num = Coupon::where($condition)->count();
  254. }
  255. //判断用户已领取优惠券
  256. $user_coupon_id = [];
  257. if ($this->user) {
  258. $ids = CouponUser::userCouponIds($this->user->id);
  259. if (!empty($ids)) {
  260. $user_coupon_id = $ids;
  261. }
  262. }
  263. $data = [];
  264. foreach ($coupon as $key => $value) {
  265. $data[$key]['id'] = $value['id'];
  266. $data[$key]['store_id'] = $value->store->id;
  267. $data[$key]['store'] = $value->store->name;
  268. $data[$key]['address'] = $value->store->address;
  269. $data[$key]['img'] = $value['img'].'?x-oss-process=style/w100';
  270. $data[$key]['name'] = $value['name'];
  271. $data[$key]['size'] = $value['size'];
  272. $data[$key]['tips'] = $value['tips'];
  273. $data[$key]['price'] = $value['price'];
  274. $data[$key]['pay_price'] = $value['pay_price'];
  275. $data[$key]['theme'] = $value['theme'];
  276. $data[$key]['starttime'] = date('Y-m-d',$value['starttime']);
  277. $data[$key]['endtime'] = date('Y-m-d',$value['endtime']);
  278. $data[$key]['state'] = in_array($value['id'],$user_coupon_id) ? 1 : 0;
  279. $data[$key]['rate'] = rate($value['num'],$value['num_of']);
  280. }
  281. return enjson(200,'成功',['coupon' => $data,'coupon_num' => $coupon_num]);
  282. }
  283. /**
  284. * 申请入驻商家
  285. */
  286. public function regstore(){
  287. $this->isUserAuth();
  288. //读取判断是否好店老板
  289. $store = AllwinStore::manageStore($this->user->id);
  290. if (!empty($store)) {
  291. return json(['code'=>404,'msg'=>'您已加入商业联盟,无需重复入驻.']);
  292. }
  293. $worker = StoreWorker::isManage($this->miniapp_id,$this->user->id);
  294. if (!empty($worker)) {
  295. return json(['code'=>404,'msg'=>'你是好店管理员无权入驻,请联系客服.']);
  296. }
  297. if (request()->isPost()) {
  298. $data = [
  299. 'member_miniapp_id' => $this->miniapp_id,
  300. 'name' => input('post.name/s','','htmlspecialchars'),
  301. 'state' => input('post.state/d','','htmlspecialchars'),
  302. 'work_time' => input('post.work_time/s','','htmlspecialchars'),
  303. 'address' => input('post.address/s','','htmlspecialchars'),
  304. 'telphone' => input('post.telphone/s','','htmlspecialchars'),
  305. 'tips' => input('post.tips/s','','htmlspecialchars'),
  306. 'tags' => input('post.tags/s','','htmlspecialchars'),
  307. 'imgs' => input('post.imgs/a','','htmlspecialchars'),
  308. 'manage_uid' => intval($this->user->id),
  309. 'money_coupon' => 0,
  310. 'is_lock' => 1
  311. ];
  312. if(!empty($data['imgs'])){
  313. $data['imgs'] = Filter::filter_escape($data['imgs']);
  314. $data['img'] = $data['imgs'][0];
  315. }
  316. $data['state_text'] = $data['state'] ? '营业中' : '未营业';
  317. $validate = $this->validate($data, 'Store.reg');
  318. if (true !== $validate) {
  319. return json(['code'=>403,'msg'=>$validate]);
  320. }
  321. $data['imgs'] = json_encode($data['imgs']);
  322. $rel = AllwinStore::insert($data);
  323. if($rel){
  324. //通知到后台
  325. SystemMemberSms::sms($this->miniapp_id,'您有一条好店['.$data['name'].']信息待审核',url('allwin/store/index',['types' => 1]));
  326. //通知申请者到微信
  327. Inform::sms($this->user->id,$this->miniapp_id,['title' =>'业务进展通知','type' => '店铺入住申请','content' =>'您的店铺['.$data['name'].']入住申请正在审核中']);
  328. //通知到后台管理的微信
  329. Inform::sms(User::isFounder($this->miniapp_id)->user_id,$this->miniapp_id,['title' =>'店铺入住申请','type' => '店铺入住申请','content' =>'店铺['.$data['name'].']入住申请待审核']);
  330. return enjson(200,"好店入驻信息已提交,请等待审核。");
  331. }else{
  332. return enjson(200,"申请失败,请稍后重试");
  333. }
  334. }
  335. }
  336. /**
  337. * 商城图片
  338. * @return void
  339. */
  340. public function upImg(){
  341. $this->isUserAuth();
  342. $store = StoreWorker::isManage($this->user->member_miniapp_id,$this->user->id);
  343. if (empty($store)) {
  344. return enjson(404,'非好店管理员');
  345. }
  346. if(request()->isPost()){
  347. $rel = Upload::index();
  348. if($rel['error'] == 0){
  349. return enjson(200,'成功',$rel['url']);
  350. }else{
  351. return enjson(204);
  352. }
  353. }
  354. }
  355. }