Coupon.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /**
  3. * 优惠券
  4. */
  5. namespace app\smartbc\controller\api\v1;
  6. use app\smartbc\controller\api\Base;
  7. use app\smartbc\model\SmartbcCoupon;
  8. use app\smartbc\model\SmartbcStore;
  9. use app\smartbc\model\SmartbcCouponUser;
  10. use app\smartbc\model\SmartbcConfig;
  11. use app\smartbc\model\SmartbcStoreUnion;
  12. use think\Db;
  13. class Coupon extends Base{
  14. protected $config;
  15. /**
  16. * 初始化当前应用管理员是不是联盟城市账户
  17. * @return void
  18. */
  19. public function initialize() {
  20. parent::initialize();
  21. $this->isUserAuth();
  22. $this->config = SmartbcConfig::getConfig($this->member_miniapp_id);
  23. }
  24. /**
  25. * 领取优惠券
  26. * @return void
  27. */
  28. public function getCoupon(){
  29. $param['id'] = $this->request->param('id');
  30. $param['store_id'] = $this->request->param('store_id'); //引荐的店铺id
  31. $sign = $this->apiSign($param);
  32. if ($sign['code'] != 200) {
  33. return enjson($sign['code'],'签名验证失败');
  34. }
  35. $couponUser = SmartbcCouponUser::where(['member_miniapp_id' => $this->miniapp_id,'coupon_id' => $param['id'],'uid' => $this->user->id,'is_end' => 0])->find();
  36. if(!empty($couponUser)){
  37. return enjson(200,'已经拥有该优惠券',$couponUser);
  38. }
  39. if($param['id']){
  40. $info = SmartbcCoupon::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id'],'is_end' => 0])->find();
  41. if(!empty($info)){
  42. $is_group = SmartbcStoreUnion::isGroup($param['id'],$param['store_id'],$this->miniapp_id);
  43. $data['member_miniapp_id'] = $this->miniapp_id;
  44. $data['store_id'] = $info->store_id;
  45. $data['coupon_id'] = $info->id;
  46. $data['uid'] = $this->user->id;
  47. $data['is_end'] = 0;
  48. $data['types'] = $info->types;
  49. $data['name'] = $info->name;
  50. $data['price'] = $info->price;
  51. $data['discount'] = $info->discount;
  52. $data['howmuch'] = $info->howmuch;
  53. $data['amount'] = $info->amount;
  54. $data['tips'] = $info->tips;
  55. $data['group_ids'] = $is_group ?? 0;
  56. $data['parent_store_id'] = !empty($param['store_id']) ? $param['store_id'] : 0;
  57. $data['update_time'] = time();
  58. $data['create_time'] = time();
  59. $rel = SmartbcCouponUser::create($data);
  60. if($rel){
  61. return enjson(200,$rel);
  62. }
  63. return enjson(204);
  64. }
  65. }
  66. return enjson(403,'未找到优惠券');
  67. }
  68. /**
  69. * 已领取的优惠券信息
  70. * @return void
  71. */
  72. public function couponInfo(){
  73. $param['id'] = $this->request->param('id');
  74. $rel = $this->apiSign($param);
  75. if ($rel['code'] != 200) {
  76. return enjson($rel['code'],'签名验证失败');
  77. }
  78. $info = SmartbcCouponUser::with(['coupon' => function($query) {
  79. $query->field('id,create_time');
  80. },'store' => function($query) {
  81. $query->field('id,name,address,longitude,latitude,img,telphone');
  82. }])->where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id']])->find();
  83. if (empty($info)) {
  84. return enjson(404,'未找到优惠券');
  85. }
  86. $info->store->img = $info->store->img."?x-oss-process=style/w100";
  87. if(!empty($this->qqgps)){
  88. $info->store->distance = getDistance($this->qqgps['lng'],$this->qqgps['lat'],$info->store->longitude,$info->store->latitude);
  89. }
  90. return enjson(200,$info);
  91. }
  92. /**
  93. * 用户已领优惠券
  94. * @return void
  95. */
  96. public function userCoupon(){
  97. $param['type'] = $this->request->param('type/d',0);
  98. $param['page'] = $this->request->param('page/d',1);
  99. $rel = $this->apiSign($param);
  100. if ($rel['code'] != 200) {
  101. return enjson($rel['code'],'签名验证失败');
  102. }
  103. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  104. $condition[] = ['uid','=',$this->user->id];
  105. switch ($param['type']){
  106. case 1:
  107. $condition[] = ['is_end','=',0];
  108. $condition[] = ['types','=',0];
  109. break;
  110. case 2:
  111. $condition[] = ['types','=',1];
  112. $condition[] = ['money','=',0];
  113. break;
  114. case 3:
  115. $condition[] = ['is_end','=',1];
  116. $condition[] = ['money','=',0];
  117. default;
  118. $condition[] = ['is_end','=',0];
  119. break;
  120. }
  121. $coupon = SmartbcCouponUser::with(['store' => function($query) {
  122. $query->field('id,name,img');
  123. }])->where($condition)->order('create_time desc')->page($param['page'],10)->select();
  124. if ($coupon->isEmpty()) {
  125. return enjson(204);
  126. }
  127. foreach ($coupon as $key => $info){
  128. $coupon[$key]->end_time = $info->create_time + $this->config->end_time * 60 * 60;
  129. }
  130. return enjson(200,$coupon);
  131. }
  132. /**
  133. * 根据定位推荐附近同圈子的优惠券
  134. * @return void
  135. */
  136. public function couponTop(){
  137. $param['store_id'] = $this->request->param('store_id/d',0); //来自哪个商家
  138. $rel = $this->apiSign($param);
  139. if ($rel['code'] != 200) {
  140. return enjson($rel['code'],'签名验证失败');
  141. }
  142. $store = SmartbcStore::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['store_id'],'is_lock' => 0])->find();
  143. if(empty($store)){
  144. return enjson(204);
  145. }
  146. if(empty($this->qqgps['lat']) || empty($this->qqgps['lng'])){
  147. return enjson(204);
  148. }
  149. $prefix = config('database.prefix');
  150. $latitude = $this->qqgps['lat'];
  151. $longitude = $this->qqgps['lng'];
  152. $sql = 'select id,img,name from '.$prefix.'smartbc_store where latitude > '.$latitude .'-1 and latitude < '.$latitude .'+1 and longitude > '.$longitude.'-1 and longitude < '.
  153. $longitude.'+1 %s and id != '.$param['store_id'].' order by ACOS(SIN(('.$latitude .' * 3.1415) / 180 ) *SIN((latitude * 3.1415) / 180 ) +COS(('.$latitude .
  154. ' * 3.1415) / 180 ) * COS((latitude * 3.1415) / 180 ) *COS(('.$longitude.'* 3.1415) / 180 - (longitude * 3.1415) / 180 ) ) * 6380 asc limit 20';
  155. $ex = 'and id in(select store_id from '.$prefix.'smartbc_store_union where group_id in (select group_id from '.$prefix.'smartbc_store_union where member_miniapp_id = '.$this->miniapp_id.' and store_id = '.$param['store_id'].
  156. ' and type = 0 ) and type = 0 )';
  157. $unionList = Db::query(sprintf($sql,$ex)); //附近商圏店铺查询
  158. if(count($unionList) == 0){
  159. $ex = 'and cate_id = '. $store->cate_id;
  160. $unionList = Db::query(sprintf($sql,$ex)); //附近同行业店铺查询
  161. }
  162. if(count($unionList) == 0){
  163. $unionList = Db::query(sprintf($sql,'')); //附近店铺查询
  164. }
  165. $store = [];
  166. foreach($unionList as $key => $val){
  167. $store[$val['id']] = $val;
  168. }
  169. $store_ids = implode(',',array_column($unionList,'id'));
  170. $list = Db::query('SELECT * FROM '.$prefix.'smartbc_coupon WHERE id >= (SELECT floor( RAND() * ((SELECT MAX(id) FROM '.$prefix.'smartbc_coupon)-(SELECT MIN(id) FROM '.$prefix.'smartbc_coupon)) + (SELECT MIN(id) FROM '.$prefix.'smartbc_coupon))) and member_miniapp_id = '.
  171. $this->miniapp_id.' and is_lock = 0 and is_end = 0 and store_id in ('.$store_ids.') ORDER BY id LIMIT 15;');
  172. if (count($list)) {
  173. return enjson(200,'success',['coupon' => $list,'store' => $store]);
  174. }
  175. return enjson(204);
  176. }
  177. //如果要切换优惠券,可以选择的列表
  178. public function couponSelect(){
  179. $param['store_id'] = $this->request->param('store_id/d', 0);
  180. $rel = $this->apiSign($param);
  181. if ($rel['code'] != 200) {
  182. return enjson($rel['code'],'签名验证失败');
  183. }
  184. $list = SmartbcCoupon::where(['member_miniapp_id' => $this->miniapp_id,'store_id' => $param['store_id'],'is_lock' => 0,'is_end' => 0])->order('is_top desc,sort desc,create_time desc')->paginate(20);
  185. if ($list->isEmpty()) {
  186. return enjson(204,'error');
  187. }
  188. return enjson(200,'success',$list);
  189. }
  190. //用户到店更换优惠券
  191. public function couponSwitch(){
  192. $param['id'] = $this->request->param('id'); //用户优惠券id SmartbcCouponUser
  193. $param['coupon_id'] = $this->request->param('coupon_id'); //要切换的优惠券id
  194. $rel = $this->apiSign($param);
  195. if ($rel['code'] != 200) {
  196. return enjson($rel['code'], '签名验证失败');
  197. }
  198. $info = SmartbcCouponUser::where(['member_miniapp_id' => $this->miniapp_id, 'id' => $param['id'], 'is_end' => 0])->find();
  199. $newInfo = SmartbcCoupon::where(['member_miniapp_id' => $this->miniapp_id, 'id' => $param['coupon_id'], 'is_end' => 0,'is_lock' => 0])->find();
  200. if (!empty($info) && !empty($newInfo) && $info->store_id == $newInfo->store_id) {
  201. $data['member_miniapp_id'] = $this->miniapp_id;
  202. $data['store_id'] = $newInfo->store_id;
  203. $data['coupon_id'] = $newInfo->id;
  204. $data['uid'] = $this->user->id;
  205. $data['is_end'] = 0;
  206. $data['types'] = $newInfo->types;
  207. $data['name'] = $newInfo->name;
  208. $data['price'] = $newInfo->price;
  209. $data['discount'] = $newInfo->discount;
  210. $data['howmuch'] = $newInfo->howmuch;
  211. $data['tips'] = $newInfo->tips;
  212. $data['update_time'] = $info->create_time;
  213. $data['create_time'] = $info->create_time;
  214. $code = SmartbcCouponUser::create($data);
  215. if ($code) {
  216. $info->is_end = 1;
  217. $info->save();
  218. return enjson(200, 'success');
  219. }
  220. return enjson(204, 'error');
  221. }
  222. return enjson(204, 'error');
  223. }
  224. /**
  225. * 以下商家管理
  226. * ##########################################
  227. * @return void
  228. * 添加和修改优惠券
  229. */
  230. public function edit(){
  231. $param['id'] = $this->request->param('id');
  232. $param['types'] = $this->request->param('types/d',0);
  233. $param['price'] = $this->request->param('price/d',0);
  234. $param['amount'] = $this->request->param('amount/d',0);
  235. $param['discount'] = $this->request->param('discount/s',0);
  236. $param['howmuch'] = $this->request->param('howmuch/d',0);
  237. $param['name'] = $this->request->param('name/s');
  238. $param['tips'] = $this->request->param('tips/s');
  239. $rel = $this->apiSign($param);
  240. if ($rel['code'] != 200) {
  241. return enjson($rel['code'],'签名验证失败');
  242. }
  243. $validate = $this->validate($param,'Coupon.edit');
  244. if(true !== $validate){
  245. return enjson(403,$validate);
  246. }
  247. $store = SmartbcStore::manageStore($this->user->id);
  248. if(!$store){
  249. return enjson(404,'无法找到该商家');
  250. }
  251. switch ($param['types']) {
  252. case 1:
  253. $param['price'] = 0;
  254. $param['amount'] = 0;
  255. break;
  256. case 2:
  257. $param['price'] = 0;
  258. $param['discount'] = 0;
  259. break;
  260. default:
  261. $param['discount'] = 0;
  262. $param['amount'] = 0;
  263. break;
  264. }
  265. $param['is_top'] = 0;
  266. $coupon = new SmartbcCoupon();
  267. if($param['id']){
  268. $info = $coupon->where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id']])->find();
  269. if(!$info){
  270. return enjson(403,'未找到要修改的优惠券');
  271. }
  272. if($info->is_super){
  273. return enjson(403,'超级券禁止修改');
  274. }
  275. if($info->is_lock){
  276. return enjson(403,'锁定的券禁止修改');
  277. }
  278. $result = $coupon->allowField(true)->save($param,['id' => $param['id']]);
  279. }else{
  280. $param['member_miniapp_id'] = $this->miniapp_id;
  281. $param['store_id'] = $store->id;
  282. $param['create_time'] = time();
  283. $param['update_time'] = time();
  284. $result = $coupon->create($param);
  285. }
  286. return enjson(200,'优惠券设置成功');
  287. }
  288. /**
  289. * 指定商家的优惠券(管理员)
  290. * @return void
  291. */
  292. public function storeCoupon(){
  293. $rel = $this->apiSign();
  294. if ($rel['code'] != 200) {
  295. return enjson($rel['code'],'签名验证失败');
  296. }
  297. //判断是修改还是创建
  298. $store = SmartbcStore::manageStore($this->user->id);
  299. if(empty($store)){
  300. return enjson(404,'你不是商家');
  301. }
  302. $coupon = SmartbcCoupon::where(['member_miniapp_id' => $this->miniapp_id,'store_id' => $store->id,'is_end' => 0])->order('is_top desc,sort desc,create_time desc')->select();
  303. if ($coupon->isEmpty()) {
  304. return enjson(204);
  305. }
  306. return enjson(200,$coupon);
  307. }
  308. /**
  309. * 显示单个优惠券信息
  310. * @return void
  311. */
  312. public function storeCouponInfo(){
  313. $param['id'] = $this->request->param('id'); //优惠券id
  314. $rel = $this->apiSign($param);
  315. if ($rel['code'] != 200) {
  316. return enjson($rel['code'],'签名验证失败');
  317. }
  318. //判断是修改还是创建
  319. $store = SmartbcStore::manageStore($this->user->id);
  320. if(empty($store)){
  321. return enjson(404,'你不是商家');
  322. }
  323. $info = SmartbcCoupon::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id'],'store_id' => $store->id])->find();
  324. if (empty($info)) {
  325. return enjson(404,'未找到优惠券');
  326. }
  327. $count = SmartbcCouponUser::where(['coupon_id' => $info->id])->count(); //已领取
  328. $order = SmartbcCouponUser::where(['coupon_id' => $info->id,'is_end' => 0,'money' => 0])->count(); //待使用
  329. $money = SmartbcCouponUser::where(['coupon_id' => $info->id,'is_end' => 1])->sum('money'); //共优惠
  330. return enjson(200,['coupon' => $info,'count' => $count,'order' => $order,'money' => $money]);
  331. }
  332. //商户删除优惠券
  333. public function storeCouponDel(){
  334. $param['id'] = $this->request->param('id/d',0);
  335. $rel = $this->apiSign($param);
  336. if ($rel['code'] != 200) {
  337. return enjson($rel['code'],'签名验证失败');
  338. }
  339. //判断是修改还是创建
  340. $store = SmartbcStore::manageStore($this->user->id);
  341. if(empty($store)){
  342. return enjson(404,'你不是商家');
  343. }
  344. $coupon = SmartbcCoupon::where(['member_miniapp_id' => $this->miniapp_id,'store_id' => $store->id,'id' => $param['id'],'is_lock' => 0,'is_super' => 0,'is_end' => 0])->find();
  345. if (empty($coupon)) {
  346. return enjson(404,'当前优惠券禁止删除');
  347. }
  348. $coupon->is_end = 1;
  349. $coupon->save();
  350. return enjson(200,'成功删除优惠券');
  351. }
  352. }