Index.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * 常用接口
  4. */
  5. namespace app\smartbc\controller\api\v1;
  6. use app\smartbc\controller\api\Base;
  7. use app\smartbc\model\SmartbcAdwords;
  8. use app\smartbc\model\SmartbcCoupon;
  9. use app\smartbc\model\SmartbcCouponUser;
  10. use app\smartbc\model\SmartbcConfig;
  11. use think\helper\Time;
  12. class Index extends Base{
  13. /**
  14. * 广告图片
  15. * @return void
  16. */
  17. public function adwords(){
  18. $param['ids'] = $this->request->param('ids');
  19. $rel = $this->apiSign($param);
  20. if($rel['code'] != 200){
  21. return enjson($rel['code'],'签名验证失败');
  22. }
  23. $group = explode('/',$param['ids']);
  24. $adword = [];
  25. foreach ($group as $value) {
  26. $adword[$value] = [];
  27. }
  28. $rel = SmartbcAdwords::where(['member_miniapp_id' => $this->miniapp_id,'group' => $group])->field('title,picture,link,open_type,group')->order('sort desc,id desc')->select();
  29. foreach ($rel as $rs) {
  30. $adword[$rs['group']][] = $rs;
  31. }
  32. return enjson(200,$adword);
  33. }
  34. /**
  35. * 查询优惠券
  36. * mode(new|hot|max)
  37. * @return void
  38. */
  39. public function coupon(){
  40. $param['mode'] = $this->request->param('mode/s','new');
  41. $param['page'] = $this->request->param('page/d',1);
  42. $rel = $this->apiSign($param);
  43. if ($rel['code'] != 200) {
  44. return enjson($rel['code'],'签名验证失败');
  45. }
  46. $validate = $this->validate($param,'Coupon.api');
  47. if(true !== $validate){
  48. return enjson(403,$validate);
  49. }
  50. if($param['mode'] == 'hot'){
  51. $condition = [['c.member_miniapp_id','=',$this->miniapp_id],['c.is_lock','=',0],['c.is_end','=',0]];
  52. }else{
  53. $condition = [['member_miniapp_id','=',$this->miniapp_id],['is_lock','=',0],['is_end','=',0]];
  54. }
  55. //查询语句
  56. $couponSql = SmartbcCoupon::with(['store' => function($query) {
  57. $query->field('id,name,img');
  58. }]);
  59. switch ($param['mode']) {
  60. case 'hot':
  61. $sql = $couponSql->alias('c')->where($condition)->join('SmartbcCouponUser u', 'c.id = u.coupon_id')->where(['u.member_miniapp_id' => $this->miniapp_id])
  62. ->field('c.*,count(coupon_id) as hot')->group('u.coupon_id')->order('hot desc,c.is_top desc,c.sort desc,c.create_time desc');
  63. break;
  64. case 'max':
  65. $sql = $couponSql->where($condition)->field('*,IF(types > 0,howmuch - howmuch * discount/10,price) as money')->order('money desc,is_top desc,sort desc,create_time desc');
  66. break;
  67. default:
  68. $sql = $couponSql->where($condition)->order('is_top desc,sort desc,create_time desc');
  69. break;
  70. }
  71. $coupon = $sql->page($param['page'],10)->select();
  72. if ($coupon->isEmpty()) {
  73. return enjson(204,'没有优惠券');
  74. }
  75. //判断已领取
  76. if ($this->user) {
  77. $coupon_ids = array_column($coupon->toArray(),'id');
  78. $user_coupon = SmartbcCouponUser::where(['coupon_id' => $coupon_ids,'uid' => $this->user->id,'is_end'=> 0])->field('uid,store_id,coupon_id')->select()->toArray();
  79. $user_coupon_id = array_unique(array_column($user_coupon,'coupon_id'));
  80. //给已领取的加状态
  81. foreach ($coupon as $key => $value) {
  82. $coupon[$key]->is_get_coupon = in_array($value['id'],$user_coupon_id) ? 1 : 0;
  83. }
  84. }
  85. return enjson(200,$coupon);
  86. }
  87. /**
  88. * 显示单个优惠券信息
  89. * @return void
  90. */
  91. public function couponInfo(){
  92. $param['id'] = $this->request->param('id'); //优惠券id
  93. $rel = $this->apiSign($param);
  94. if ($rel['code'] != 200) {
  95. return enjson($rel['code'],'签名验证失败');
  96. }
  97. $info = SmartbcCoupon::with(['couponuser' => function($query) {
  98. $query->field('coupon_id,is_end,create_time');
  99. },'store' => function($query) {
  100. $query->field('id,name,address,longitude,latitude,img,telphone');
  101. }])->where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id']])->find();
  102. if (empty($info)) {
  103. return enjson(404,'未找到优惠券');
  104. }
  105. $info->is_get_coupon = (empty($info->couponuser) || $info->couponuser->is_end == 1) ? 0 : 1;
  106. $info->store->img = $info->store->img."?x-oss-process=style/w100";
  107. if(!empty($this->qqgps)){
  108. $info->store->distance = getDistance($this->qqgps['lng'],$this->qqgps['lat'],$info->store->longitude,$info->store->latitude);
  109. }
  110. return enjson(200,$info);
  111. }
  112. }