Kirin 3 년 전
부모
커밋
789b024dd6
2개의 변경된 파일52개의 추가작업 그리고 2개의 파일을 삭제
  1. 22 0
      app/api/controller/user/UserController.php
  2. 30 2
      app/models/user/User.php

+ 22 - 0
app/api/controller/user/UserController.php

@@ -639,4 +639,26 @@ class UserController
             return app('json')->fail('支付失败:' . User::getErrorInfo('支付错误'));
         }
     }
+
+    public function tradeYue(Request $request)
+    {
+        $user = $request->user();
+        $uid = $user['uid'];
+        list($num, $to_user_account) = UtilService::postMore(
+            [
+                ['num', 0],
+                ['to_user_account', ''],
+            ], $request, true);
+        if (!$num || !$to_user_account) {
+            return app('json')->fail('参数不足');
+        }
+        $to_uid = User::where('account', $to_user_account)->value('uid');
+        if (!$to_uid) return app('json')->fail('目标用户不存在');
+        $res = User::tradeYue($uid, $to_uid, $num);
+        if ($res) {
+            return app('json')->success('支付成功');
+        } else {
+            return app('json')->fail('支付失败:' . User::getErrorInfo('支付错误'));
+        }
+    }
 }

+ 30 - 2
app/models/user/User.php

@@ -768,14 +768,42 @@ class User extends BaseModel
     {
         if ($uid == $to_uid) return self::setErrorInfo('不可以转账给自己');
         $user = self::get($uid);
+        if (!User::where('spread_uid', $uid)->where('self_achievement', '>', 0)->find()) {
+            return self::setErrorInfo('您需要至少推荐一名会员购买礼包商品才可进行积分转账');
+        }
         $to_user = self::get($to_uid);
         if ($user['integral'] < $num) return self::setErrorInfo('积分不足');
         BaseModel::beginTrans();
         try {
             $res1 = self::bcDec($uid, 'integral', $num, 'uid')
                 && UserBill::expend('积分转出', $uid, 'integral', 'trade_out', $num, 0, $user['integral'] - $num, '积分转出' . $num);
-            $res2 = self::bcInc($to_uid, 'integral', $num, 'uid')
-                && UserBill::income('积分转入', $to_uid, 'integral', 'trade_in', $num, 0, $to_user['integral'] + $num, '积分转入' . $num);
+            $res2 = self::bcInc($to_uid, 'integral', bcmul($num, 0.9, 2), 'uid')
+                && UserBill::income('积分转入', $to_uid, 'integral', 'trade_in', bcmul($num, 0.9, 2), 0, $to_user['integral'] + (float)bcmul($num, 0.9, 2), '积分转入' . $num . ',扣除手续费后实际到账' . bcmul($num, 0.9, 2));
+            if ($res1 && $res2) {
+                BaseModel::commitTrans();
+                return true;
+            } else {
+                BaseModel::rollbackTrans();
+                return self::setErrorInfo('转账失败');
+            }
+        } catch (\Exception $e) {
+            BaseModel::rollbackTrans();
+            return self::setErrorInfo($e->getMessage());
+        }
+    }
+
+    public static function tradeYue($uid, $to_uid, $num)
+    {
+        if ($uid == $to_uid) return self::setErrorInfo('不可以转账给自己');
+        $user = self::get($uid);
+        $to_user = self::get($to_uid);
+        if ($user['now_money'] < $num) return self::setErrorInfo('积分不足');
+        BaseModel::beginTrans();
+        try {
+            $res1 = self::bcDec($uid, 'now_money', $num, 'uid')
+                && UserBill::expend('余额转出', $uid, 'now_money', 'trade_out_yue', $num, 0, $user['now_money'] - $num, '余额转出' . $num);
+            $res2 = self::bcInc($to_uid, 'now_money', $num, 'uid')
+                && UserBill::income('余额转入', $to_uid, 'now_money', 'trade_in_yue', $num, 0, $to_user['now_money'] + $num, '余额转入' . $num);
             if ($res1 && $res2) {
                 BaseModel::commitTrans();
                 return true;