init
This commit is contained in:
28
app/Api/Controllers/AuthController.php
Normal file
28
app/Api/Controllers/AuthController.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Api\Controllers;
|
||||
|
||||
use App\Api\Resources\AuthResource;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
public function login(Request $request)
|
||||
{
|
||||
$address = $request->address;
|
||||
$mnemonic = $request->mnemonic;
|
||||
$address = '12dUut3dG5xWi6JPDMjSSK6s2JPcfeKYL1';
|
||||
|
||||
$user = User::where('username', $address)->first();
|
||||
|
||||
if (! $user) {
|
||||
$user = User::create([
|
||||
'username' => $address,
|
||||
'mnemonic' => $mnemonic,
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->success(new AuthResource($user));
|
||||
}
|
||||
}
|
||||
13
app/Api/Controllers/Controller.php
Normal file
13
app/Api/Controllers/Controller.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Api\Controllers;
|
||||
|
||||
use Jason\Api\Traits\ApiResponse;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
|
||||
class Controller extends BaseController
|
||||
{
|
||||
|
||||
use ApiResponse;
|
||||
|
||||
}
|
||||
158
app/Api/Controllers/DynamicController.php
Normal file
158
app/Api/Controllers/DynamicController.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace App\Api\Controllers;
|
||||
|
||||
use App\Api\Requests\DynamicRequest;
|
||||
use App\Api\Resources\CommentResource;
|
||||
use App\Api\Resources\DynamicCollection;
|
||||
use App\Models\Comment;
|
||||
use App\Models\Dynamic;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Jason\Api\Api;
|
||||
|
||||
class DynamicController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$createdAt = $request->created_at ?? '';
|
||||
$dynamics = Dynamic::where('status', 1)
|
||||
->when($createdAt, function ($query) use ($createdAt) {
|
||||
$query->where('created_at', '<', $createdAt);
|
||||
})
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate();
|
||||
|
||||
return $this->success(new DynamicCollection($dynamics));
|
||||
}
|
||||
/**
|
||||
* @Notes : 添加动态
|
||||
*
|
||||
* @Date : 2022/6/17 11:29
|
||||
* @param DynamicRequest $request
|
||||
* @return JsonResponse
|
||||
* @author : Mr.wang
|
||||
*/
|
||||
public function store(DynamicRequest $request): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
|
||||
$dynamic = Dynamic::create([
|
||||
'user_id' => $user->id,
|
||||
'description' => $request->description ?? '',
|
||||
'pictures' => $request->pictures ?? '',
|
||||
'status' => 1,
|
||||
]);
|
||||
if ($dynamic) {
|
||||
return $this->success();
|
||||
} else {
|
||||
return $this->failed('失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @Notes : 删除动态
|
||||
*
|
||||
* @Date : 2022/6/17 11:33
|
||||
* @author : Mr.wang
|
||||
* @param Dynamic $dynamic
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function destroy(Dynamic $dynamic): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
if ($user->id != $dynamic->user_id) {
|
||||
return $this->failed('没有删除权限');
|
||||
}
|
||||
|
||||
if ($dynamic->delete()) {
|
||||
return $this->success(true);
|
||||
} else {
|
||||
return $this->failed('失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @Notes : 点赞/取消
|
||||
*
|
||||
* @Date : 2022/6/17 13:18
|
||||
* @author : Mr.wang
|
||||
* @param Dynamic $dynamic
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function like(Dynamic $dynamic): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
|
||||
if ($user->hasLiked($dynamic)) {
|
||||
$user->unlike($dynamic);
|
||||
return $this->success([
|
||||
'is_like' => false,
|
||||
]);
|
||||
} else {
|
||||
$user->like($dynamic);
|
||||
return $this->success([
|
||||
'is_like' => true,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @Notes : 添加评论
|
||||
*
|
||||
* @Date : 2022/6/20 15:43
|
||||
* @author : Mr.wang
|
||||
* @param Request $request
|
||||
* @param Dynamic $dynamic
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function comment(Request $request, Dynamic $dynamic): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
if (empty($request->input('content'))) {
|
||||
return $this->failed('评论内容不能为空');
|
||||
}
|
||||
$parentId = $request->parent_id ?? 0;
|
||||
$result = $dynamic->comments()->create([
|
||||
'user_id' => $user->id,
|
||||
'parent_id' => $parentId,
|
||||
'content' => $request->input('content'),
|
||||
'status' => 1,
|
||||
]);
|
||||
if ($result) {
|
||||
return $this->success(new CommentResource($result));
|
||||
} else {
|
||||
return $this->failed('失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes :
|
||||
*
|
||||
* @Date : 2022/10/27 11:00
|
||||
* @Author : <Jason.C>
|
||||
* @param Dynamic $dynamic
|
||||
* @param Comment $comment
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function commentDestroy(Dynamic $dynamic, Comment $comment): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
|
||||
if ($comment->user()->isNot($user)) {
|
||||
return $this->failed('没有删除权限');
|
||||
}
|
||||
|
||||
$dynComment = $dynamic->comments()->where('id', $comment->id)->first();
|
||||
if ($dynComment) {
|
||||
$dynComment->status = 0;
|
||||
if ($dynComment->save()) {
|
||||
return $this->success(true);
|
||||
} else {
|
||||
return $this->failed(false);
|
||||
}
|
||||
} else {
|
||||
return $this->failed('没有相应评论');
|
||||
}
|
||||
}
|
||||
}
|
||||
15
app/Api/Controllers/IndexController.php
Normal file
15
app/Api/Controllers/IndexController.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Api\Controllers;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class IndexController extends Controller
|
||||
{
|
||||
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
return $this->success('Json Api is ready');
|
||||
}
|
||||
|
||||
}
|
||||
158
app/Api/Controllers/InitController.php
Normal file
158
app/Api/Controllers/InitController.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace App\Api\Controllers;
|
||||
|
||||
use App\Jobs\CheckUser;
|
||||
use App\Jobs\ImportFriends;
|
||||
use App\Jobs\ImportUser;
|
||||
use App\Models\OriginUser;
|
||||
use App\Models\User;
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class InitController extends Controller
|
||||
{
|
||||
public function check()
|
||||
{
|
||||
$params = [
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => 1,
|
||||
'method' => 'Chain33.Query',
|
||||
'params' => [
|
||||
[
|
||||
'execer' => 'chat',
|
||||
'funcName' => 'GetFriends',
|
||||
'payload' => [
|
||||
'mainAddress' => '12dUut3dG5xWi6JPDMjSSK6s2JPcfeKYL1',
|
||||
'count' => 2000,
|
||||
]
|
||||
]
|
||||
],
|
||||
];
|
||||
$client = new Client([
|
||||
'base_uri' => '152.136.224.167:8901'
|
||||
]);
|
||||
$result = $client->post('', [
|
||||
'body' => json_encode($params),
|
||||
]);
|
||||
|
||||
$json = json_decode($result->getBody()->getContents(), true);
|
||||
$collect = collect($json['result']['friends']);
|
||||
dump($collect);
|
||||
|
||||
// foreach (User::all() as $user) {
|
||||
// CheckUser::dispatch($user);
|
||||
// }
|
||||
}
|
||||
|
||||
public function users()
|
||||
{
|
||||
foreach (User::all() as $user) {
|
||||
ImportUser::dispatch($user);
|
||||
}
|
||||
}
|
||||
|
||||
public function friend()
|
||||
{
|
||||
$params = [
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => 1,
|
||||
'method' => 'Chain33.Query',
|
||||
'params' => [
|
||||
[
|
||||
'execer' => 'chat',
|
||||
'funcName' => 'GetFriends',
|
||||
'payload' => [
|
||||
'mainAddress' => '1125cmMhWBYFwDwyMsr3Kwg4WxxAVPV2Pw',
|
||||
'count' => 2000,
|
||||
]
|
||||
]
|
||||
],
|
||||
];
|
||||
$client = new Client([
|
||||
'base_uri' => '152.136.224.167:8901'
|
||||
]);
|
||||
$result = $client->post('', [
|
||||
'body' => json_encode($params),
|
||||
]);
|
||||
|
||||
$json = json_decode($result->getBody()->getContents(), true);
|
||||
$collect = collect($json['result']['friends']);
|
||||
|
||||
dd($collect);
|
||||
$imParams = [
|
||||
'From_Account' => '1',
|
||||
'StartIndex' => 0,
|
||||
'StandardSequence' => 0,
|
||||
'CustomSequence' => 0,
|
||||
];
|
||||
|
||||
$vars = app('im')->send('sns', 'friend_get', $imParams);
|
||||
dd($vars);
|
||||
|
||||
// foreach (User::all() as $user) {
|
||||
// ImportFriends::dispatch($user);
|
||||
// }
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
// $imParams = [
|
||||
// 'DeleteItem' => [
|
||||
// [
|
||||
// "UserID" => "1",
|
||||
// ]
|
||||
// ],
|
||||
// ];
|
||||
//
|
||||
// $vars = app('im')->send('im_open_login_svc', 'account_delete', $imParams);
|
||||
// dd($vars);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 同步用户
|
||||
*
|
||||
* @Date : 2022/10/21 15:15
|
||||
* @Author : <Jason.C>
|
||||
*/
|
||||
public function sync()
|
||||
{
|
||||
// $users = OriginUser::all();
|
||||
// // $users = OriginUser::inRandomOrder()->limit(3)->get();
|
||||
// foreach ($users as $user) {
|
||||
// User::create([
|
||||
// 'username' => $user->address,
|
||||
// 'phone' => $user->phone,
|
||||
// 'email' => $user->email,
|
||||
// ]);
|
||||
// }
|
||||
}
|
||||
|
||||
public function info()
|
||||
{
|
||||
$params = [
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => 1,
|
||||
'method' => 'Chain33.Query',
|
||||
'params' => [
|
||||
[
|
||||
'execer' => 'chat',
|
||||
'funcName' => 'GetUser',
|
||||
'payload' => [
|
||||
'mainAddress' => '112CBev1zHsTsjxxRZoKwyuqpiXcedbfMn',
|
||||
'targetAddress' => '112CBev1zHsTsjxxRZoKwyuqpiXcedbfMn',
|
||||
]
|
||||
]
|
||||
],
|
||||
];
|
||||
$client = new Client([
|
||||
'base_uri' => '152.136.224.167:8901'
|
||||
]);
|
||||
$result = $client->post('', [
|
||||
'body' => json_encode($params),
|
||||
]);
|
||||
|
||||
$json = json_decode($result->getBody()->getContents(), true);
|
||||
$collect = collect($json['result']['fields']);
|
||||
dump($collect);
|
||||
}
|
||||
}
|
||||
31
app/Api/Controllers/MessageController.php
Normal file
31
app/Api/Controllers/MessageController.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Api\Controllers;
|
||||
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
|
||||
class MessageController extends Controller
|
||||
{
|
||||
public function send()
|
||||
{
|
||||
$imParams = [
|
||||
'SyncOtherMachine' => 2,
|
||||
'From_Account' => '8',
|
||||
'To_Account' => '1',
|
||||
'MsgLifeTime' => 360,
|
||||
'MsgSeq' => rand(1, 999999),
|
||||
'MsgRandom' => rand(1, 999999),
|
||||
'MsgBody' => [
|
||||
[
|
||||
'MsgType' => 'TIMTextElem',
|
||||
'MsgContent' => [
|
||||
'Text' => 'hi, beauty',
|
||||
],
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
$vars = app('im')->send('openim', 'sendmsg', $imParams);
|
||||
dd($vars);
|
||||
}
|
||||
}
|
||||
65
app/Api/Controllers/StorageController.php
Normal file
65
app/Api/Controllers/StorageController.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Api\Controllers;
|
||||
|
||||
use App\Models\Storage as StorageModel;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class StorageController extends Controller
|
||||
{
|
||||
private string $uploadPath;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->uploadPath = Config::get('storage.upload_path').date('/Y/m/d');
|
||||
}
|
||||
|
||||
public function upload(Request $request): JsonResponse
|
||||
{
|
||||
$upload = $request->file('upload');
|
||||
|
||||
$size = File::size($upload->path());
|
||||
|
||||
if ($size > Config::get('storage.max_upload_size')) {
|
||||
return $this->failed('超过最大允许上传大小', 422);
|
||||
}
|
||||
|
||||
$exists = true;
|
||||
$hash = File::hash($upload->path());
|
||||
|
||||
$existFile = StorageModel::where('driver', Config::get('filesystems.default'))->where('hash', $hash)->first();
|
||||
|
||||
if ($existFile) {
|
||||
$fullName = $existFile->path;
|
||||
} else {
|
||||
$path = $this->uploadPath;
|
||||
$fileName = $hash.'.'.$upload->getClientOriginalExtension();
|
||||
$fullName = $path.'/'.$fileName;
|
||||
|
||||
$uploaded = Storage::putFileAs($path, $upload, $fileName);
|
||||
|
||||
if (! $uploaded) {
|
||||
return $this->failed('文件上传失败', 422);
|
||||
}
|
||||
$exists = false;
|
||||
StorageModel::create([
|
||||
'hash' => $hash,
|
||||
'driver' => Config::get('filesystems.default'),
|
||||
'type' => $upload->getClientMimeType(),
|
||||
'size' => $size,
|
||||
'path' => $fullName,
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->success([
|
||||
'exists' => $exists,
|
||||
'size' => $size,
|
||||
'path' => $fullName,
|
||||
'url' => Storage::url($fullName),
|
||||
]);
|
||||
}
|
||||
}
|
||||
49
app/Api/Controllers/UserController.php
Normal file
49
app/Api/Controllers/UserController.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Api\Controllers;
|
||||
|
||||
use App\Api\Resources\UserSearchCollection;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Jason\Api\Api;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function setting(Request $request, string $field): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
|
||||
$user->info()->update([$field => $request->value]);
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
public function search(Request $request): JsonResponse
|
||||
{
|
||||
$keyword = $request->keyword;
|
||||
|
||||
$list = User::where('username', 'like', "%$keyword%")
|
||||
->where('privacy', 0)
|
||||
->limit(3)
|
||||
->get();
|
||||
|
||||
return $this->success(new UserSearchCollection($list));
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 设置隐私状态
|
||||
*
|
||||
* @Date : 2022/11/1 10:26
|
||||
* @Author : <Jason.C>
|
||||
*/
|
||||
public function privacy(): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
|
||||
$user->privacy = ! $user->privacy;
|
||||
$user->save();
|
||||
|
||||
return $this->success($user->privacy);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user