1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Api\Api;
- use App\Facades\Servers\WechatMini\Mini;
- /**
- * 测试接口
- *
- * @author 刘相欣
- *
- * */
- class Test extends Api{
-
-
- /**
- * 同步 /api/test/sync_user
- *
- * */
- public function sync_user(){
- // 跳转链接
- $link = Mini::getUrlLink('pages/index/index');
- return json_send(['code'=>'error','msg'=>'禁用账号','data'=>$link]);
- }
- /**
- * 自动发放优惠券
- *
- */
- private function autoCoupon($uid){
- // 模型实例
- $Rule = new \App\Models\CouponRewardRule();
- $RuleProduct = new \App\Models\CouponRewardProduct();
- $OrdersProduct = new \App\Models\OrdersProduct();
- $Custom = new \App\Models\Custom();
- $Coupon = new \App\Models\Coupon();
- $CustomCoupon = new \App\Models\CustomCoupon();
- // 获取配置列表
- $ruleList = $Rule->getList();
- // 如果没有信息的话
- if( !$ruleList ) return ['success'=>'暂无活动'];
- // 获取客户城市ID
- $customCityId = (int) $Custom->getValue($uid,'city_id');
- // 循环配置列表
- foreach ( $ruleList as $value ) {
- // 如果存在城市范围,并且不在城市范围,不参与这个活动
- if( $value['city_ids'] && !in_array($customCityId,explode(',',$value['city_ids'])) ) continue;
- // 未到开始时间
- if( $value['start_time'] > time() ) continue;
- // 通过配置ID获取对应的商品范围
- $productList = $RuleProduct->getListByRule($value['id']);
- // 如果不存在产品范围,跳过
- if( !$productList ) continue;
- // 获取客户 规定时段内订单的商品ID以及购买数量
- $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();
- // 如果没有订单总数
- if( !$orderList ) continue;
- // 计算商品总量
- $total = 0;
- // 循环商品范围
- foreach ($productList as $scope) {
- // 循环订单产品
- foreach ($orderList as $order) {
- // 如果产品不相等
- if( $scope['product_id'] != $order['product_id'] ) continue;
- // 相等的计算总量
- $total += $scope['product_units'] * $order['buy_num'];
- }
- }
- // 判断总数是不是达标
- if( $total < $value['std_num'] ) continue;
- // 达标的是否已经发送过优惠券
- $havaCoupon = $CustomCoupon->query()->where([['custom_uid','=',$uid],['coupon_id','=',$value['coupon_id']]])->first(['status']);
- // 已经发过优惠券的,不发
- if( $havaCoupon ) continue;
- // 获取优惠券的可用时间
- $expTime = $Coupon->query()->where([['id','=',$value['coupon_id']]])->value('exp_time');
- // 时间转时间
- $expTime = $Coupon->getExpTime($expTime);
- // 发送优惠券
- $CustomCoupon->add(['coupon_id'=>$value['coupon_id'],'custom_uid'=>$uid,'exp_time'=>$expTime]);
- }
- // 返回成功
- return ['success'=>'操作成功'];
- }
- }
|