Browse Source

Wesmiler 人人车 初始化项目 0816

APPLE 3 years ago
parent
commit
c2727ea86b
1 changed files with 55 additions and 1 deletions
  1. 55 1
      application/api/controller/v1/User.php

+ 55 - 1
application/api/controller/v1/User.php

@@ -4,7 +4,9 @@ namespace app\api\controller\v1;
 
 use app\api\controller\ApiController;
 use app\api\service\SmsCode;
+use app\common\model\SystemConfig;
 use app\common\model\Users;
+use app\common\model\UsersBalanceRecord;
 use app\http\IResponse;
 use EasyWeChat\Factory;
 use Lettered\Support\Upload;
@@ -680,11 +682,63 @@ class User extends ApiController
         return $this->ApiJson(0,'', get_annex_url($ret));
     }
 
+    /**
+     * 嘛呗出行红包发放
+     * @return \think\response\Json
+     * @throws \Lettered\Support\Exceptions\FailedException
+     */
     public function receiveRedbag(){
         $params = $this->request->param();
         $sid = isset($params['sid'])? $params['sid'] : 0;
+        if($sid<=0){
+            return $this->ApiJson(-1,'参数错误');
+        }
+        $redbagMoney = sys_config('user_redbag_money','user');
+        $redbagNum = sys_config('user_redbag_num','user');
+        if($redbagMoney <= 0 || $redbagNum <= 0){
+            return $this->ApiJson(-1,'红包已被领完或参数错误');
+        }
+
+        if(UsersBalanceRecord::where(['user_id'=> $sid,'source_uid'=> $this->auth->user()['id'],'type'=>2])->value('id')){
+            return $this->ApiJson(-1,'该用户红包已奖励过');
+        }
+
+        $user = Users::where(['id'=> $sid])->field('id,nickname,balance')->find();
+        if(empty($user)){
+            return $this->ApiJson(-1,'奖励用户不存在');
+        }
 
+        DB::startTrans();
+        $user->balance += $redbagMoney;
+        $user->updated_at = time();
+        if(!$user->save()){
+            Db::rollback();
+            return $this->ApiJson(-1,'发放红包失败');
+        }
+
+        if(!SystemConfig::where(['group'=>'user','name'=>'user_redbag_num'])->dec('value',1)){
+            Db::rollback();
+            return $this->ApiJson(-1,'发放红包失败');
+        }
+
+        $data = [
+            'user_id'=> $sid,
+            'source_uid'=>  $this->auth->user()['id'],
+            'type'=> 2,
+            'action'=> 1,
+            'inc_amount'=> $redbagMoney,
+            'aft_amount'=> $user->balance,
+            'remark'=> '嘛呗出行分享获得红包,金额【'.$redbagMoney.'】',
+            'created_at'=> time(),
+            'updated_at'=> time()
+        ];
+
+        if(!UsersBalanceRecord::insertGetId($data)){
+            Db::rollback();
+            return $this->ApiJson(-1,'发放红包失败');
+        }
 
-        return $this->ApiJson(0,'领取成功');
+        DB::commit();
+        return $this->ApiJson(0,'发放红包成功');
     }
 }