gq 8 年之前
父节点
当前提交
b95a30d741

+ 280 - 0
server/app/Http/Controllers/Api/V1/DreamController.php

xqd
@@ -0,0 +1,280 @@
+<?php
+
+namespace App\Http\Controllers\Api\V1;
+
+use App\Models\AccountLog;
+use App\Models\BaseSettingsModel;
+use App\Models\DreamImages;
+use App\Models\DreamInfoModel;
+use App\Models\SupportDreamModel;
+use App\Models\UserCareDream;
+use App\Models\UserInfoModel;
+use Illuminate\Http\Request;
+use App\Services\Base\ErrorCode;
+
+class DreamController extends Controller
+{
+    /**
+     * @api {get} /api/dream/index    梦想
+     * @apiDescription 梦想
+     * @apiGroup Dream
+     * @apiPermission Passport
+     * @apiVersion 0.1.0
+     * @apiParam {int} type interaction paihang  默认index
+     * @apiSuccessExample {json} Success-Response:
+     * HTTP/1.1 200 OK
+     *               主页
+     *{
+     *   "status": true,
+     *  "status_code": 0,
+     *  "message": "",
+     *      "data": {
+     *      "dream": {                          梦想
+     *          "id": 7,
+     *          "user_id": 2,
+     *          "dream": "梦想标1123",
+     *          "about": "梦想烧1111213",
+     *          "video": "",
+     *          "sign": null,
+     *          "money": 10000,
+     *          "time": 720000,
+     *          "get_money": 0,
+     *          "status": 0,
+     *          "updated_at": "2017-06-22 09:47:03"
+     *     },
+     *      "imgs": [                          梦想图片
+     *          {
+     *              "title": "",
+     *              "pic": "http://w17.9026.com/img/banner/banner_3.png"
+     *         },
+     *          {
+     *               "title": "",
+     *              "pic": "http://w17.9026.com/img/banner/banner_3.png"
+     *          }
+     *     ],
+     *      "arr": {                            支持者前三(user_idh和头像)
+     *         "1": "http://www.wsfjq.com/photos/bd119684755.jpg",
+     *           "6": "http://www.wsfjq.com/photos/bd119684755.jpg"
+     *      }
+     *       "money" : "1000"    用户余额
+     *       "score" : "1000"    梦想分数
+     *      "a": "0.001",   支持乘数计算  y = -a * b +c   b = updated_at-当前时间  (分钟)   y>=1
+     *      "c": "8"
+     *  }
+     *}
+     *          排行
+     *{
+     *   "status": true,
+     *  "status_code": 0,
+     *   "message": "",
+     *  "data": [
+     *      {
+     *          "nickname": "ha",      昵称
+     *          "pic": "http://www.wsfjq.com/photos/bd119684755.jpg",     头像
+     *          "score": 112000                        支持分
+     *      },
+     *  ]
+     *}
+     *
+     * @apiErrorExample {json} Error-Response:
+     *HTTP/1.1 400 Bad Request
+     *{
+     *  "status": false,
+     *  "status_code": 1105,
+     *  "message": "用户不存在",
+     *  "data": null
+     * }
+     */
+    public function index(Request $request)
+    {
+        $user = $this->getUser();
+        $type = $request->type;    //类型
+        $id = $request->id;    //梦想ID
+        if (empty($id)) return $this->error(ErrorCode::KEY_ERROR);
+
+        if ($type == 'paihang') {
+            $data = [];
+            $info = SupportDreamModel::where('dream_id',$id)->get();
+            $arr = [] ;
+            foreach ($info as $item) {
+                if (!array_key_exists($item->user_id,$arr)) {
+                    $arr[$item->user_id] = $item->score;
+                }else{
+                    $arr[$item->user_id] += $item->score;
+                }
+            }
+            arsort($arr);
+            foreach ($arr as $k => $v){
+                $user = UserInfoModel::find($k);
+                $user->score = $v;
+                $data[] = $user;
+            }
+            return $this->api($data);
+
+        } elseif ($type == 'interaction') {
+
+        }else{
+//            梦想  图片  支持者前三    支持乘数参数    用户余额   梦想分数
+            $score = 0 ;
+            $a = SupportDreamModel::where('dream_id',$id)->get();
+            foreach ($a as $value){
+                $score += $value->score;
+            }
+            $money = $user->money;
+            $number = BaseSettingsModel::where('category','score')->select('key','value')->first();
+            $a = count($number) > 0 ? $number->key : '';
+            $c = count($number) > 0 ? $number->value : '';
+            $dream = DreamInfoModel::find($id);
+            $imgs = DreamImages::where('dream_id',$id)->orderBy('id','desc')->get();
+            $info = SupportDreamModel::where('dream_id',$id)->get();
+            $arr = [] ;
+            foreach ($info as $item) {
+                if (!array_key_exists($item->user_id,$arr)) {
+                    $arr[$item->user_id] = $item->score;
+                }else{
+                    $arr[$item->user_id] += $item->score;
+                }
+            }
+            arsort($arr);
+            foreach ($arr as $k => $v){
+                $arr[$k] = UserInfoModel::find($k)->pic;
+            }
+            return $this->api(compact('dream','imgs','money','arr','a','c','score'));
+        }
+    }
+
+    /**
+     * @api {post} /api/dream/support 支持梦想
+     * @apiDescription 支持梦想
+     * @apiGroup Dream
+     * @apiPermission Passport
+     * @apiVersion 0.1.0
+     * @apiParam {int} coin 支持梦想币数量
+     * @apiParam {int} id 梦想ID
+     * @apiParam {int} number 支持乘数
+     * @apiSuccessExample {json} Success-Response:
+     * HTTP/1.1 200 OK
+     *{
+     *      "status": true,
+     *      "status_code": 0,
+     *      "message": "",
+     *      "data": ""
+     *}
+     * @apiErrorExample {json} Error-Response:
+     *HTTP/1.1 400 Bad Request
+     * {
+     *     "state": false,
+     *     "code": 1000,
+     *     "message": "传入参数不正确",
+     *     "data": null or []
+     * }
+     *  可能出现的代码
+     * {
+     *       "status": false,
+     *       "status_code": 1303,
+     *       "message": "商户余额不足",
+     *       "data": null
+     *  }
+     *
+     */
+    public function support(Request $request)
+    {
+        $validator = \Validator::make($request->all(),
+            [
+                'coin'  => 'required',
+                'id'  => 'required',
+                'number'  => 'required',
+            ],
+            [
+                'coin.required'  => '梦想币不能为空',
+                'id.required'  => '支持梦想不能为空',
+                'number.required'  => '支持乘数不能为空',
+            ]
+        );
+        if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
+        $user = $this->getUser();
+        $dream_id = $request->id;
+        $dream_info = DreamInfoModel::find($dream_id);
+        $user_id = $dream_info->user_id;
+
+        $coin = $request->coin;
+        $number = $request->number;
+
+        if ($user->money < $coin) {
+            return $this->error(ErrorCode::MERCHANT_BALANCE_NOT_ENOUGH);
+        }else{
+            $user->money  =  $user->money - $coin;
+            $user->save();
+            $dream_info->get_money += $coin;
+            $dream_info->save();
+            $data = [
+                'user_id'=>$user->id,
+                'dream_id'=>$dream_id,
+                'to_user_id'=>$user_id,
+                'coin'=>$coin,
+                'score'=>$coin*$number,
+            ];
+
+            $ok =   SupportDreamModel::create($data);
+            if (!$ok) {
+                return $this->error(ErrorCode::MERCHANT_SERVICE_STATUS_INVALID);
+            }
+            //                记录充值记录
+            $data = [
+                'from_id' =>$user->id,
+                'to_id' => $user_id,
+                'from_amount' => $coin,
+                'to_amount' => $coin,
+                'from_type' => AccountLog::TYPE_COIN,
+                'to_type' =>  AccountLog::TYPE_COIN,
+                'op' =>  AccountLog::OP_SUPPORT,
+            ];
+            AccountLog::create($data);
+            return $this->api('');
+        }
+    }
+
+//    收藏关注梦想
+    /**
+     * @api {get} /api/dream/collection 收藏梦想
+     * @apiDescription 收藏梦想
+     * @apiGroup Dream
+     * @apiPermission Passport
+     * @apiVersion 0.1.0
+     * @apiParam {int} id 梦想ID
+     * @apiSuccessExample {json} Success-Response:
+     * HTTP/1.1 200 OK
+     *{
+     *      "status": true,
+     *      "status_code": 0,
+     *      "message": "",
+     *      "data": ""
+     *}
+     * @apiErrorExample {json} Error-Response:
+     *HTTP/1.1 400 Bad Request
+     * {
+     *     "state": false,
+     *     "code": 1000,
+     *     "message": "传入参数不正确",
+     *     "data": null or []
+     * }
+     *
+     */
+    public function collection(Request $request)
+    {
+        if (empty($request->id)) return $this->error(ErrorCode::KEY_ERROR);
+
+        $user = $this->getUser();
+        $data = [
+            'user_id' =>$user->id,
+            'dream_id' =>$request->id,
+        ];
+        $info = UserCareDream::where('user_id',$user->id)->
+            where('dream_id',$request->id)->first();
+        if (count($info) == 0) {
+           UserCareDream::create($data);
+        }
+        return $this->api('');
+
+    }
+}

+ 9 - 8
server/app/Http/Controllers/Api/V1/MyController.php

xqd xqd xqd xqd
@@ -524,7 +524,6 @@ class MyController extends Controller
         $t = 21*3600 / 60;
 
         $data = $request->except('_token','pics');
-        $data['user_id'] = $user->id;
         \Log::debug(' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t);
         if ($care_num == 0) {
             $data['score'] = (($a/$t) + $b)*100000000 ;
@@ -537,7 +536,7 @@ class MyController extends Controller
 
         if ($dream_id) {
 //            梦想创建成功 关联关系中最新动态+1
-            $info = UserCareUser::where('other_user_id',$user->id)->get();
+            $info = UserCareUser::where('other_user_id',$user->id)->paginate(20);
             foreach ($info as $item) {
                 $item->dream_num += 1;
                 $item->updated_at = date('Y-m-d H:i:s');
@@ -550,11 +549,10 @@ class MyController extends Controller
             $data1['updated_at'] = date('Y-m-d H:i:s');
             $arr = $request->pics;
             $video = $request->video;
-            if (empty($arr) && empty($video)) {
-                DreamInfoModel::destroy($dream_id);
-                return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
-            } else{
-                $arr1 = [];
+            if (empty($arr) && empty($video))   return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
+            $ok = UserDream::create($data1);
+            $arr1 = [];
+            if ($ok) {
                 if (is_array($arr)) {
                     foreach ($arr as $k => $v){
                         $arr1[] = [
@@ -565,8 +563,11 @@ class MyController extends Controller
                         ];
                     }
                     DreamImages::insert($arr1);
-                    return $this->api('');
                 }
+                return $this->api('');
+            }else{
+                DreamInfoModel::destroy($dream_id);
+                return $this->error(ErrorCode::SAVE_USER_FAILED);
             }
         }else{
             return $this->error(ErrorCode::SAVE_USER_FAILED);

+ 9 - 0
server/app/Models/AccountLog.php

xqd
@@ -8,6 +8,15 @@ class AccountLog extends Model
 {
     public $table = 'account_logs';
 
+    protected $fillable = [
+        'from_type',
+        'to_type',
+        'from_id',
+        'to_id',
+        'op',
+        'from_amount',
+        'to_amount',
+    ];
     //交易行为定义
     const OP_CHARGE         = 'CHARGE';
     const OP_SUPPORT        = 'SUPPORT';

+ 1 - 1
server/app/Models/DreamInfoModel.php

xqd
@@ -40,7 +40,7 @@ class DreamInfoModel extends BaseModel
                            'status'
                           ];
 
-    protected $hidden = ['score','created_at','deleted_at','mark'];
+    protected $hidden = ['score','deleted_at','mark'];
 
 
     public function dreamUser()

+ 4 - 0
server/app/Models/UserCareDream.php

xqd
@@ -7,4 +7,8 @@ use Illuminate\Database\Eloquent\Model;
 class UserCareDream extends Model
 {
     protected $table = 'user_care_dream';
+    protected  $fillable = [
+        'user_id',
+        'dream_id',
+        ];
 }

+ 1 - 1
server/database/migrations/2017_06_01_091444_create_user_info_table.php

xqd
@@ -24,7 +24,7 @@ class CreateUserInfoTable extends Migration
             $table->string('nickname',100)->default('')->comment('昵称');
             $table->string('pic',100)->default('')->comment('用户头像');
             $table->integer('sign')->default(0)->comment('用户标签');
-            $table->integer('money')->default(0)->comment('余额');
+            $table->integer('money')->default(0)->comment('梦想币余额');
             $table->tinyInteger('sex')->default(1)->comment('性别');
             $table->string('signture',200)->default('')->comment('个性签名');
             $table->string('tall',20)->default('')->comment('身高');

+ 44 - 0
server/database/migrations/2017_06_22_175547_create_interaction_info_table.php

xqd
@@ -0,0 +1,44 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateInteractionInfoTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('interaction_info', function (Blueprint $table) {
+            $table->increments('id')->comment('id');
+            $table->integer('dream_id')->comment('梦想ID');
+            $table->text('dream',20)->comment('互动标题');
+            $table->string('pic1')->default('')->comment('图片');
+            $table->string('pic2')->default('')->comment('图片');
+            $table->string('pic3')->default('')->comment('图片');
+            $table->string('pic4')->default('')->comment('图片');
+            $table->string('pic5')->default('')->comment('图片');
+            $table->string('pic6')->default('')->comment('图片');
+            $table->string('pic7')->default('')->comment('图片');
+            $table->string('pic8')->default('')->comment('图片');
+            $table->string('pic9')->default('')->comment('图片');
+            $table->tinyInteger('status')->default(1)->comment('互动状态');
+            $table->timestamps();
+            $table->softDeletes();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('interaction_info');
+    }
+}

+ 13 - 0
server/routes/api.php

xqd
@@ -181,4 +181,17 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\Api\V1'], function ($a
         'uses' => 'HomeController@paihang',
     ]);
 
+//    梦想
+    $api->get('dream/index', [
+        'as' => 'dream.index',
+        'uses' => 'DreamController@index',
+    ]);
+    $api->get('dream/collection', [
+        'as' => 'dream.collection',
+        'uses' => 'DreamController@collection',
+    ]);
+    $api->post('dream/support', [
+        'as' => 'dream.support',
+        'uses' => 'DreamController@support',
+    ]);
 });