Test.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php namespace App\Http\Controllers\Api;
  2. use App\Http\Controllers\Api\Api;
  3. use App\Facades\Servers\WechatMini\Mini;
  4. /**
  5. * 测试接口
  6. *
  7. * @author 刘相欣
  8. *
  9. * */
  10. class Test extends Api{
  11. /**
  12. * 同步 /api/test/sync_user
  13. *
  14. * */
  15. public function sync_user(){
  16. // 跳转链接
  17. $link = Mini::getUrlLink('pages/index/index');
  18. return json_send(['code'=>'error','msg'=>'禁用账号','data'=>$link]);
  19. }
  20. /**
  21. * 自动发放优惠券
  22. *
  23. */
  24. private function autoCoupon($uid){
  25. // 模型实例
  26. $Rule = new \App\Models\CouponRewardRule();
  27. $RuleProduct = new \App\Models\CouponRewardProduct();
  28. $OrdersProduct = new \App\Models\OrdersProduct();
  29. $Custom = new \App\Models\Custom();
  30. $Coupon = new \App\Models\Coupon();
  31. $CustomCoupon = new \App\Models\CustomCoupon();
  32. // 获取配置列表
  33. $ruleList = $Rule->getList();
  34. // 如果没有信息的话
  35. if( !$ruleList ) return ['success'=>'暂无活动'];
  36. // 获取客户城市ID
  37. $customCityId = (int) $Custom->getValue($uid,'city_id');
  38. // 循环配置列表
  39. foreach ( $ruleList as $value ) {
  40. // 如果存在城市范围,并且不在城市范围,不参与这个活动
  41. if( $value['city_ids'] && !in_array($customCityId,explode(',',$value['city_ids'])) ) continue;
  42. // 未到开始时间
  43. if( $value['start_time'] > time() ) continue;
  44. // 通过配置ID获取对应的商品范围
  45. $productList = $RuleProduct->getListByRule($value['id']);
  46. // 如果不存在产品范围,跳过
  47. if( !$productList ) continue;
  48. // 获取客户 规定时段内订单的商品ID以及购买数量
  49. $orderList = $OrdersProduct->query()->where([['custom_uid','=',$uid],['status','=',1],['insert_time','>=',$value['start_time']],['insert_time','<=',$value['end_time']]])->get(['product_id','buy_num'])->toArray();
  50. // 如果没有订单总数
  51. if( !$orderList ) continue;
  52. // 计算商品总量
  53. $total = 0;
  54. // 循环商品范围
  55. foreach ($productList as $scope) {
  56. // 循环订单产品
  57. foreach ($orderList as $order) {
  58. // 如果产品不相等
  59. if( $scope['product_id'] != $order['product_id'] ) continue;
  60. // 相等的计算总量
  61. $total += $scope['product_units'] * $order['buy_num'];
  62. }
  63. }
  64. // 判断总数是不是达标
  65. if( $total < $value['std_num'] ) continue;
  66. // 达标的是否已经发送过优惠券
  67. $havaCoupon = $CustomCoupon->query()->where([['custom_uid','=',$uid],['coupon_id','=',$value['coupon_id']]])->first(['status']);
  68. // 已经发过优惠券的,不发
  69. if( $havaCoupon ) continue;
  70. // 获取优惠券的可用时间
  71. $expTime = $Coupon->query()->where([['id','=',$value['coupon_id']]])->value('exp_time');
  72. // 时间转时间
  73. $expTime = $Coupon->getExpTime($expTime);
  74. // 发送优惠券
  75. $CustomCoupon->add(['coupon_id'=>$value['coupon_id'],'custom_uid'=>$uid,'exp_time'=>$expTime]);
  76. }
  77. // 返回成功
  78. return ['success'=>'操作成功'];
  79. }
  80. }