Showing
7 changed files
with
209 additions
and
1 deletions
... | @@ -3,9 +3,11 @@ | ... | @@ -3,9 +3,11 @@ |
3 | namespace App\Http\Controllers\V1; | 3 | namespace App\Http\Controllers\V1; |
4 | 4 | ||
5 | use App\Http\Controllers\Controller; | 5 | use App\Http\Controllers\Controller; |
6 | +use App\Models\Collect; | ||
6 | use App\Models\Immerse; | 7 | use App\Models\Immerse; |
7 | use App\Jobs\UserMakeVideo as MakeVideo; | 8 | use App\Jobs\UserMakeVideo as MakeVideo; |
8 | use App\Jobs\UserMakeImages as MakeImages; | 9 | use App\Jobs\UserMakeImages as MakeImages; |
10 | +use App\Models\Praise; | ||
9 | use Illuminate\Http\Request; | 11 | use Illuminate\Http\Request; |
10 | use Illuminate\Support\Facades\Auth; | 12 | use Illuminate\Support\Facades\Auth; |
11 | use Illuminate\Support\Facades\Validator; | 13 | use Illuminate\Support\Facades\Validator; |
... | @@ -125,4 +127,37 @@ class ImmerseController extends Controller | ... | @@ -125,4 +127,37 @@ class ImmerseController extends Controller |
125 | { | 127 | { |
126 | // | 128 | // |
127 | } | 129 | } |
130 | + | ||
131 | + public function addview($id) | ||
132 | + { | ||
133 | + try{ | ||
134 | + Immerse::query()->where('id', $id)->increment('view'); | ||
135 | + }catch (\Exception $exception){ | ||
136 | + return Response::fail($exception->getMessage()); | ||
137 | + } | ||
138 | + | ||
139 | + return Response::success(); | ||
140 | + } | ||
141 | + | ||
142 | + public function praise($id, Request $request) | ||
143 | + { | ||
144 | + $user = $request->user(); | ||
145 | + | ||
146 | + $praise = (new Praise())->zanOrCancel($id, $user->id); | ||
147 | + | ||
148 | + if (!$praise) return Response::fail('服务端异常'); | ||
149 | + | ||
150 | + else return Response::success(); | ||
151 | + } | ||
152 | + | ||
153 | + public function collect($id, Request $request) | ||
154 | + { | ||
155 | + $user = $request->user(); | ||
156 | + | ||
157 | + $coll = (new Collect())->collOrCancel($id, $user->id); | ||
158 | + | ||
159 | + if (!$coll) return Response::fail('服务端异常'); | ||
160 | + | ||
161 | + else return Response::success(); | ||
162 | + } | ||
128 | } | 163 | } | ... | ... |
... | @@ -144,7 +144,7 @@ class UserController extends Controller | ... | @@ -144,7 +144,7 @@ class UserController extends Controller |
144 | 144 | ||
145 | $user_id = Auth::user()->getAuthIdentifier(); | 145 | $user_id = Auth::user()->getAuthIdentifier(); |
146 | 146 | ||
147 | - $immerse = Immerse::query()->where('user_id', $user_id); | 147 | + $immerse = Immerse::query()->where('user_id', $user_id)->orderByDesc('created_at'); |
148 | switch ($type) | 148 | switch ($type) |
149 | { | 149 | { |
150 | case 0 : // 审核中... | 150 | case 0 : // 审核中... | ... | ... |
app/Models/Collect.php
0 → 100644
1 | +<?php | ||
2 | + | ||
3 | +namespace App\Models; | ||
4 | + | ||
5 | +use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
6 | +use Illuminate\Database\Eloquent\Model; | ||
7 | + | ||
8 | +class Collect extends Model | ||
9 | +{ | ||
10 | + use HasFactory; | ||
11 | + | ||
12 | + protected $guarded = ['']; | ||
13 | + | ||
14 | + public function collOrCancel($immerse_id,$user_id) | ||
15 | + { | ||
16 | + $immerse = Immerse::query()->where('id',$immerse_id)->first(); | ||
17 | + if (!$immerse) return false; | ||
18 | + | ||
19 | + if ($immerse->state != 1) false; | ||
20 | + | ||
21 | + try{ | ||
22 | + $coll = $this->query()->where(['user_id' => $user_id, 'immerse_id' => $immerse_id])->first(); | ||
23 | + | ||
24 | + if ($coll){ | ||
25 | + if ($coll->state == 1){ | ||
26 | + $coll->state = 0; | ||
27 | + $immerse->decrement('collect'); | ||
28 | + }else{ | ||
29 | + $coll->state = 1; | ||
30 | + $immerse->increment('collect'); | ||
31 | + } | ||
32 | + $coll->save(); | ||
33 | + }else{ | ||
34 | + $this->query()->create([ | ||
35 | + 'user_id' => $user_id, | ||
36 | + 'immerse_id' => $immerse_id, | ||
37 | + 'state' => 1, | ||
38 | + ]); | ||
39 | + | ||
40 | + $immerse->increment('collect'); | ||
41 | + } | ||
42 | + return $this; | ||
43 | + }catch (\Exception $exception){ | ||
44 | + return false; | ||
45 | + } | ||
46 | + } | ||
47 | +} |
app/Models/Praise.php
0 → 100644
1 | +<?php | ||
2 | + | ||
3 | +namespace App\Models; | ||
4 | + | ||
5 | +use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
6 | +use Illuminate\Database\Eloquent\Model; | ||
7 | + | ||
8 | +class Praise extends Model | ||
9 | +{ | ||
10 | + use HasFactory; | ||
11 | + | ||
12 | + protected $guarded = ['']; | ||
13 | + | ||
14 | + public function zanOrCancel($immerse_id, $user_id) | ||
15 | + { | ||
16 | + $immerse = Immerse::query()->where('id',$immerse_id)->first(); | ||
17 | + if (!$immerse) return false; | ||
18 | + | ||
19 | + if ($immerse->state != 1) false; | ||
20 | + | ||
21 | + try{ | ||
22 | + if ($praise = | ||
23 | + $this->query() | ||
24 | + ->where(['user_id' => $user_id, 'immerse_id' => $immerse_id]) | ||
25 | + ->first()) { | ||
26 | + if ($praise->state == 1){ | ||
27 | + $praise->state = 0; | ||
28 | + $immerse->decrement('praise'); | ||
29 | + }else{ | ||
30 | + $praise->state = 1; | ||
31 | + $immerse->increment('praise'); | ||
32 | + } | ||
33 | + $praise->save(); | ||
34 | + }else{ | ||
35 | + $this->query()->create([ | ||
36 | + 'user_id' => $user_id, | ||
37 | + 'immerse_id' => $immerse_id, | ||
38 | + 'state' => 1, | ||
39 | + ]); | ||
40 | + | ||
41 | + $immerse->increment('praise'); | ||
42 | + } | ||
43 | + return $this; | ||
44 | + }catch (\Exception $exception){ | ||
45 | + return false; | ||
46 | + } | ||
47 | + } | ||
48 | +} |
1 | +<?php | ||
2 | + | ||
3 | +use Illuminate\Database\Migrations\Migration; | ||
4 | +use Illuminate\Database\Schema\Blueprint; | ||
5 | +use Illuminate\Support\Facades\Schema; | ||
6 | + | ||
7 | +class CreatePraisesTable extends Migration | ||
8 | +{ | ||
9 | + /** | ||
10 | + * Run the migrations. | ||
11 | + * | ||
12 | + * @return void | ||
13 | + */ | ||
14 | + public function up() | ||
15 | + { | ||
16 | + Schema::create('praises', function (Blueprint $table) { | ||
17 | + $table->id(); | ||
18 | + $table->unsignedBigInteger('user_id'); | ||
19 | + $table->unsignedBigInteger('immerse_id'); | ||
20 | + $table->unsignedTinyInteger('state'); | ||
21 | + $table->timestamps(); | ||
22 | + }); | ||
23 | + } | ||
24 | + | ||
25 | + /** | ||
26 | + * Reverse the migrations. | ||
27 | + * | ||
28 | + * @return void | ||
29 | + */ | ||
30 | + public function down() | ||
31 | + { | ||
32 | + Schema::dropIfExists('praises'); | ||
33 | + } | ||
34 | +} |
1 | +<?php | ||
2 | + | ||
3 | +use Illuminate\Database\Migrations\Migration; | ||
4 | +use Illuminate\Database\Schema\Blueprint; | ||
5 | +use Illuminate\Support\Facades\Schema; | ||
6 | + | ||
7 | +class CreateCollectsTable extends Migration | ||
8 | +{ | ||
9 | + /** | ||
10 | + * Run the migrations. | ||
11 | + * | ||
12 | + * @return void | ||
13 | + */ | ||
14 | + public function up() | ||
15 | + { | ||
16 | + Schema::create('collects', function (Blueprint $table) { | ||
17 | + $table->id(); | ||
18 | + $table->unsignedBigInteger('user_id'); | ||
19 | + $table->unsignedBigInteger('immerse_id'); | ||
20 | + $table->unsignedTinyInteger('state'); | ||
21 | + $table->timestamps(); | ||
22 | + }); | ||
23 | + } | ||
24 | + | ||
25 | + /** | ||
26 | + * Reverse the migrations. | ||
27 | + * | ||
28 | + * @return void | ||
29 | + */ | ||
30 | + public function down() | ||
31 | + { | ||
32 | + Schema::dropIfExists('collects'); | ||
33 | + } | ||
34 | +} |
... | @@ -22,6 +22,9 @@ Route::prefix('v1')->namespace('App\Http\Controllers\V1')->group(function (Route | ... | @@ -22,6 +22,9 @@ Route::prefix('v1')->namespace('App\Http\Controllers\V1')->group(function (Route |
22 | 22 | ||
23 | /** 社会化用户登录*/ | 23 | /** 社会化用户登录*/ |
24 | $api->any('auth/{service}/callback', 'AuthController@apiHandleProviderCallback'); | 24 | $api->any('auth/{service}/callback', 'AuthController@apiHandleProviderCallback'); |
25 | + | ||
26 | + /** */ | ||
27 | + $api->get('/addview/{id}', 'ImmerseController@addview'); | ||
25 | }); | 28 | }); |
26 | 29 | ||
27 | 30 | ||
... | @@ -47,6 +50,13 @@ Route::prefix('v1')->namespace('App\Http\Controllers\V1')->group(function (Route | ... | @@ -47,6 +50,13 @@ Route::prefix('v1')->namespace('App\Http\Controllers\V1')->group(function (Route |
47 | /** 临境 */ | 50 | /** 临境 */ |
48 | $api->apiResource('/immersive', 'ImmerseController')->middleware('auth:sanctum'); | 51 | $api->apiResource('/immersive', 'ImmerseController')->middleware('auth:sanctum'); |
49 | 52 | ||
53 | + /** 点赞 */ | ||
54 | + $api->post('/praise/{id}', 'ImmerseController@praise')->middleware('auth:sanctum'); | ||
55 | + | ||
56 | + /** 收藏 */ | ||
57 | + $api->post('/collect/{id}', 'ImmerseController@collect')->middleware('auth:sanctum'); | ||
58 | + | ||
59 | + | ||
50 | /** 众妙 */ | 60 | /** 众妙 */ |
51 | $api->get('/packpoem', 'HomeController@packpoem'); | 61 | $api->get('/packpoem', 'HomeController@packpoem'); |
52 | 62 | ... | ... |
-
Please register or login to post a comment