99 lines
2.8 KiB
PHP
99 lines
2.8 KiB
PHP
<?php
|
|
namespace app\api\controller\asset;
|
|
|
|
use app\api\controller\BaseApiController;
|
|
use app\common\model\AssetUser;
|
|
use app\common\model\AssetResource;
|
|
use app\common\model\AssetUserResource;
|
|
use think\facade\Db;
|
|
|
|
class AssetAppController extends BaseApiController
|
|
{
|
|
// 免登录验证的接口
|
|
public array $notNeedLogin = ['login', 'getResourceList'];
|
|
|
|
/**
|
|
* @notes 小程序端登录
|
|
*/
|
|
public function login()
|
|
{
|
|
$phone = $this->request->post('phone');
|
|
$password = $this->request->post('password');
|
|
|
|
if (empty($phone) || empty($password)) {
|
|
return $this->fail('手机号或密码不能为空');
|
|
}
|
|
|
|
$user = AssetUser::where('phone', $phone)->find();
|
|
if (!$user) {
|
|
return $this->fail('账号不存在');
|
|
}
|
|
|
|
if ($user->status != 1) {
|
|
return $this->fail('账号已被禁用');
|
|
}
|
|
|
|
if (!password_verify($password, $user->password)) {
|
|
return $this->fail('密码错误');
|
|
}
|
|
|
|
// 简单生成 token (实际项目中建议用 JWT 或 Redis 存储 token)
|
|
$token = md5($user->id . time() . 'secret');
|
|
cache('asset_token_' . $token, $user->id, 86400 * 7);
|
|
|
|
return $this->success('登录成功', [
|
|
'token' => $token,
|
|
'user' => [
|
|
'id' => $user->id,
|
|
'phone' => $user->phone
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @notes 获取用户关联的资源列表
|
|
*/
|
|
public function getResourceList()
|
|
{
|
|
$token = $this->request->header('token');
|
|
if (empty($token)) {
|
|
return $this->fail('请先登录', [], 401);
|
|
}
|
|
|
|
$userId = cache('asset_token_' . $token);
|
|
if (!$userId) {
|
|
return $this->fail('登录已过期', [], 401);
|
|
}
|
|
|
|
$type = $this->request->get('type', 1); // 1:图片 2:视频 3:语音
|
|
$pageNo = $this->request->get('page_no', 1);
|
|
$pageSize = $this->request->get('page_size', 20);
|
|
|
|
// 获取用户关联的资源 ID 列表
|
|
$resourceIds = AssetUserResource::where('user_id', $userId)->column('resource_id');
|
|
|
|
if (empty($resourceIds)) {
|
|
return $this->data([
|
|
'lists' => [],
|
|
'count' => 0,
|
|
'page_no' => $pageNo,
|
|
'page_size' => $pageSize,
|
|
]);
|
|
}
|
|
|
|
$count = AssetResource::whereIn('id', $resourceIds)->where('type', $type)->count();
|
|
$lists = AssetResource::whereIn('id', $resourceIds)
|
|
->where('type', $type)
|
|
->order('id', 'desc')
|
|
->page($pageNo, $pageSize)
|
|
->select();
|
|
|
|
return $this->data([
|
|
'lists' => $lists,
|
|
'count' => $count,
|
|
'page_no' => $pageNo,
|
|
'page_size' => $pageSize,
|
|
]);
|
|
}
|
|
}
|