lh 3 سال پیش
والد
کامیت
87f07dfbf8
1فایلهای تغییر یافته به همراه193 افزوده شده و 3 حذف شده
  1. 193 3
      src/Controllers/CompanyAuth/AppController.php

+ 193 - 3
src/Controllers/CompanyAuth/AppController.php

@@ -247,15 +247,41 @@ class AppController extends Controller
         }
     }
 
+    /**
+     * 添加书币
+     * @apiVersion 1.0.0
+     * @apiName addBookCoin
+     * @apiGroup CompanyAuth
+     * @apiParam {String} channel_id 站点id
+     * @apiParam {String} uid 用户ID
+     * @apiParam {String} openid 用户openid
+     * @apiParam {String} amount 书币
+     * @apiParam {String} app_id 分配好的{app_id}
+     * @apiParam {String} nonce_str 随机字符串
+     * @apiParam {String} timestamp 时间戳
+     * @apiParam {String} sign 签名 规则同微信支付签名MD5(排序好的请求字符串&key=分配好的{app_secret})
+     * @apiSuccess {int}         code 状态码
+     * @apiSuccess {String}      msg  信息
+     * @apiSuccess {Object}      data 结果集
+     *     HTTP/1.1 200 OK
+     *   {
+     *       "code": 0,
+     *       "msg": "",
+     *       "data": {
+     *           "success": 1
+     *       }
+     *  }
+     */
     public function addBookCoin(ChannelQueryRequest $request)
     {
         $channel_id = $request->get('channel_id');
         $uid = $request->get('uid');
         $openid = $request->get('openid');
         $amount = (int)$request->get('amount');
+        $limit_amount = 2000;
 
         // 参数判断
-        if ((empty($openid) && empty($uid)) || empty($channel_id) || empty($amount) || $amount < 0 || $amount > 2000) {
+        if ((empty($openid) && empty($uid)) || empty($channel_id) || empty($amount) || $amount < 0 || $amount > $limit_amount) {
             return response()->json(['code'=>-1, 'msg'=>'传参有误!']);
         }
 
@@ -275,7 +301,6 @@ class AppController extends Controller
         // 当日该站点该用户获得的总书币
         $sum = DB::connection('mysql')->table('user_coin_logs')->where(['distribution_channel_id'=>$channel_id, 'uid'=>$user_info['id'], 'day'=>date('Y-m-d')])->sum('amount');
 
-        $limit_amount = 2000;
         if ($sum + $amount > $limit_amount) {
             return response()->json(['code' => -1, 'msg' => '每个用户每个站点每日加书币的总额不得超过'.$limit_amount.'!']);
         }
@@ -313,7 +338,172 @@ class AppController extends Controller
             DB::connection('mysql')->rollback();
             return response()->json(['code'=>-1, 'msg'=>$e->getMessage()]);
         }
-        return response()->json(['code'=>1, 'msg'=>'', 'data'=>['success' => $result]]);
+        return response()->json(['code'=>1, 'msg'=>'', 'data'=>['success' => 1]]);
+    }
+
+    /**
+     * 添加书币或会员有效期
+     * @apiVersion 1.0.0
+     * @apiName addBookCoinV2
+     * @apiGroup CompanyAuth
+     * @apiParam {String} channel_id 站点id
+     * @apiParam {String} uid 用户ID
+     * @apiParam {String} openid 用户openid
+     * @apiParam {String} type 类型(coin: 加书币 day: 加会员有效期,单位: 天)
+     * @apiParam {String} [amount]  书币(type类型为coin时必填)
+     * @apiParam {String} [day]  书币(type类型为day时必填)
+     * @apiParam {String} app_id 分配好的{app_id}
+     * @apiParam {String} nonce_str 随机字符串
+     * @apiParam {String} timestamp 时间戳
+     * @apiParam {String} sign 签名 规则同微信支付签名MD5(排序好的请求字符串&key=分配好的{app_secret})
+     * @apiSuccess {int}         code 状态码
+     * @apiSuccess {String}      msg  信息
+     * @apiSuccess {Object}      data 结果集
+     *     HTTP/1.1 200 OK
+     *   {
+     *       "code": 0,
+     *       "msg": "",
+     *       "data": {
+     *           "success": 1
+     *       }
+     *  }
+     */
+    public function addBookCoinV2(ChannelQueryRequest $request)
+    {
+        $channel_id = $request->get('channel_id');
+        $uid = $request->get('uid');
+        $openid = $request->get('openid');
+        $amount = (int)$request->get('amount');
+        $type = $request->get('type');
+        $day = $request->get('day');
+        $limit_amount = 2000;
+        $limit_users = 20;
+
+        // 参数判断
+        if ((empty($openid) && empty($uid)) || empty($channel_id)  || empty($type) || !in_array($type, ['coin', 'day'])) {
+            return response()->json(['code'=>-1, 'msg'=>'传参有误!']);
+        }
+
+        if ($type == 'coin' && (empty($amount) || $amount < 0 || $amount > $limit_amount)) return response()->json(['code'=>-1, 'msg'=>'传参有误!']);
+        if ($type == 'day' && (empty($day) || $day < 0 || $day > 366)) return response()->json(['code'=>-1, 'msg'=>'传参有误!']);
+
+        // 获取当前用户信息
+        $user_info = '';
+        if (!empty($uid)) {
+            $user_info = $prev_coin = DB::connection('mysql')->table('users')->where(['id'=>$uid])
+                ->select('id', 'distribution_channel_id', 'balance', 'reward_balance')->first();
+        }else if(!empty($openid)) {
+            $user_info = $prev_coin = DB::connection('mysql')->table('users')
+                ->where(['distribution_channel_id'=>$channel_id, 'openid'=>$openid])
+                ->select('id', 'distribution_channel_id', 'balance', 'reward_balance')->first();
+        }
+        $user_info = (array)$user_info;
+        if (!$user_info) return response()->json(['code'=>-1, 'msg'=>'用户不存在!']);
+
+        if ($type == 'day') {   // 加包时日期
+            $year_order = DB::connection('mysql')->table('year_orders')->where(['distribution_channel_id'=>$channel_id, 'uid'=>$user_info['id']])->select('id', 'end_time')->first();
+
+            try {
+                DB::connection('mysql')->beginTransaction();
+
+                if ($year_order) {
+                    $year_order = (array)$year_order;
+                    $before_end_time = $year_order['end_time'];
+                    $after_end_time = date('Y-m-d H:i:s', (strtotime($before_end_time) + $day * 86400));
+                    $result = DB::connection('mysql')->table('year_orders')->where(['id'=>$year_order['id']])->update([
+                        'end_time'      => $after_end_time,
+                        'updated_at'    => date('Y-m-d H:i:s')
+                    ]);
+                }else {
+                    $before_end_time = date('Y-m-d H:i:s');
+                    $after_end_time = date('Y-m-d H:i:s', (time() + $day * 86400));
+                    $result = DB::connection('mysql')->table('year_orders')->insert([
+                        'uid'                       => $user_info['id'],
+                        'begin_time'                => $before_end_time,
+                        'end_time'                  => $after_end_time,
+                        'distribution_channel_id'   => $channel_id,
+                        'send_order_id'             => 0,
+                        'created_at'                => date('Y-m-d H:i:s'),
+                        'updated_at'                => date('Y-m-d H:i:s'),
+                    ]);
+                }
+
+                if (!$result) {
+                    DB::connection('mysql')->rollback();
+                    return response()->json(['code'=>-1, 'msg'=>'数据库异常!']);
+                }
+
+                $boolen = DB::connection('mysql')->table('user_coin_logs')->insert([
+                    'distribution_channel_id'   => $user_info['distribution_channel_id'],
+                    'uid'                       => $user_info['id'],
+                    'day'                       => date('Y-m-d'),
+                    'before_end_time'           => $before_end_time,
+                    'day_num'                   => $day,
+                    'after_end_time'            => $after_end_time,
+                    'type'                      => 2,
+                    'created_at'                => date('Y-m-d H:i:s'),
+                    'updated_at'                => date('Y-m-d H:i:s'),
+                ]);
+
+                if (!$boolen) {
+                    DB::connection('mysql')->rollback();
+                    return response()->json(['code'=>-1, 'msg'=>'数据库异常!']);
+                }
+
+                DB::connection('mysql')->commit();
+            }catch(\Exception $e) {
+                DB::connection('mysql')->rollback();
+                return response()->json(['code'=>-1, 'msg'=>$e->getMessage()]);
+            }
+            return response()->json(['code'=>1, 'msg'=>'', 'data'=>['success' => 1]]);
+        }else {     // 加书币
+            // 当日该站点该用户获得的总书币
+            $sum = DB::connection('mysql')->table('user_coin_logs')->where(['distribution_channel_id'=>$channel_id, 'uid'=>$user_info['id'], 'day'=>date('Y-m-d')])->sum('amount');
+
+            if ($sum + $amount > $limit_amount) {
+                return response()->json(['code' => -1, 'msg' => '每个用户每个站点每日加书币的总额不得超过'.$limit_amount.'!']);
+            }
+
+            // 当日该站点加过书币的总用户数
+            $today_users = DB::connection('mysql')->table('user_coin_logs')->where(['distribution_channel_id'=>$channel_id, 'day'=>date('Y-m-d')])->groupBy(['uid'])->get()->pluck('uid')->toArray();
+            if (count($today_users) > $limit_users) return response()->json(['code' => -1, 'msg' => '每个站点每日加书币的用户数不得超过'.$limit_users.'!']);
+            if (count($today_users) == $limit_users && !in_array($uid, $today_users)) return response()->json(['code' => -1, 'msg' => '每个站点每日加书币的用户数不得超过'.$limit_users.'!']);
+
+            try {
+                DB::connection('mysql')->beginTransaction();
+                $result = DB::connection('mysql')->table('users')->where(['id'=>$user_info['id']])->update([
+                    'balance' => $user_info['balance'] + $amount,
+                    'reward_balance' => $user_info['reward_balance'] + $amount,
+                ]);
+
+                if (!$result) {
+                    DB::connection('mysql')->rollback();
+                    return response()->json(['code'=>-1, 'msg'=>'数据库异常!']);
+                }
+
+                $boolen = DB::connection('mysql')->table('user_coin_logs')->insert([
+                    'distribution_channel_id'   => $user_info['distribution_channel_id'],
+                    'uid'                       => $user_info['id'],
+                    'day'                       => date('Y-m-d'),
+                    'before'                    => $user_info['balance'],
+                    'amount'                    => $amount,
+                    'after'                     => $user_info['balance'] + $amount,
+                    'created_at'                => date('Y-m-d H:i:s'),
+                    'updated_at'                => date('Y-m-d H:i:s'),
+                ]);
+
+                if (!$boolen) {
+                    DB::connection('mysql')->rollback();
+                    return response()->json(['code'=>-1, 'msg'=>'数据库异常!']);
+                }
+
+                DB::connection('mysql')->commit();
+            }catch(\Exception $e) {
+                DB::connection('mysql')->rollback();
+                return response()->json(['code'=>-1, 'msg'=>$e->getMessage()]);
+            }
+            return response()->json(['code'=>1, 'msg'=>'', 'data'=>['success' => 1]]);
+        }
     }
 
     /**