优化代码
This commit is contained in:
122
app/Http/Controllers/Api/System/File.php
Normal file
122
app/Http/Controllers/Api/System/File.php
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
<?php
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
namespace App\Http\Controllers\Api\System;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use App\Http\Controllers\BaseController;
|
||||||
|
|
||||||
|
class File extends BaseController {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @title 上传文件
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function upload(Request $request){
|
||||||
|
$type = $request->input('type', 'images');
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch ($type) {
|
||||||
|
case 'avatar':
|
||||||
|
$this->data['data'] = $this->avatar($request);
|
||||||
|
break;
|
||||||
|
case 'images':
|
||||||
|
$this->data['data'] = $this->images($request);
|
||||||
|
break;
|
||||||
|
case 'files':
|
||||||
|
$this->data['data'] = $this->files($request);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$this->data['data'] = $this->images($request);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
$this->data['code'] = 0;
|
||||||
|
$this->data['message'] = $th->getMessage();
|
||||||
|
}
|
||||||
|
return response()->json($this->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @title 上传头像
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function avatar(Request $request){
|
||||||
|
$file = $request->file('file');
|
||||||
|
|
||||||
|
if ($file->isValid()) {
|
||||||
|
$ext = $file->extension();
|
||||||
|
$name = $file->hashName();
|
||||||
|
|
||||||
|
$path = $file->store('avatar/'.date('Ymd'));
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'path' => $path,
|
||||||
|
'url' => Storage::disk()->url($path),
|
||||||
|
'src' => Storage::disk()->url($path),
|
||||||
|
'name'=> $name,
|
||||||
|
'size'=> $file->getSize(),
|
||||||
|
'ext' => $ext,
|
||||||
|
];
|
||||||
|
return $data;
|
||||||
|
}else{
|
||||||
|
throw new \Exception('上传失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function images(Request $request){
|
||||||
|
$file = $request->file('file');
|
||||||
|
|
||||||
|
if ($file->isValid()) {
|
||||||
|
$ext = $file->extension();
|
||||||
|
$name = $file->hashName();
|
||||||
|
|
||||||
|
$path = $file->store('images/'.date('Ymd'));
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'path' => $path,
|
||||||
|
'url' => Storage::disk()->url($path),
|
||||||
|
'src' => Storage::disk()->url($path),
|
||||||
|
'name'=> $name,
|
||||||
|
'size'=> $file->getSize(),
|
||||||
|
'ext' => $ext,
|
||||||
|
];
|
||||||
|
return $data;
|
||||||
|
}else{
|
||||||
|
throw new \Exception('上传失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function files(Request $request){
|
||||||
|
$file = $request->file('file');
|
||||||
|
|
||||||
|
if ($file->isValid()) {
|
||||||
|
$ext = $file->extension();
|
||||||
|
$name = $file->hashName();
|
||||||
|
|
||||||
|
$path = $file->store('files/'.date('Ymd'));
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'path' => $path,
|
||||||
|
'url' => Storage::disk()->url($path),
|
||||||
|
'src' => Storage::disk()->url($path),
|
||||||
|
'name'=> $name,
|
||||||
|
'size'=> $file->getSize(),
|
||||||
|
'ext' => $ext,
|
||||||
|
];
|
||||||
|
return $data;
|
||||||
|
}else{
|
||||||
|
throw new \Exception('上传失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,8 @@ use Illuminate\Notifications\Notification;
|
|||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
class Member extends Authenticatable implements JWTSubject {
|
class Member extends Authenticatable implements JWTSubject {
|
||||||
use Notifiable;
|
use Notifiable;
|
||||||
@@ -59,7 +61,9 @@ class Member extends Authenticatable implements JWTSubject {
|
|||||||
|
|
||||||
public function avatar(): Attribute {
|
public function avatar(): Attribute {
|
||||||
return new Attribute(
|
return new Attribute(
|
||||||
get: fn ($value) => $value ?? request()->root() . '/storage/avatar/default_avatar.jpg',
|
get: function($value){
|
||||||
|
return $value ? (Str::startsWith($value, 'http') ? $value : Storage::url($value)) : '';
|
||||||
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,10 +2,13 @@ import request from '@/utils/request'
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
upload: {
|
upload: {
|
||||||
url: '/api/system/upload',
|
url: '/api/system/file/upload',
|
||||||
name: '图片上传',
|
name: '图片上传',
|
||||||
post: async function(params) {
|
post: async function(file, params) {
|
||||||
return request.post(this.url, params)
|
return request.upload(this.url, {
|
||||||
|
filePath: file,
|
||||||
|
params
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ export default {
|
|||||||
paymentMap: {
|
paymentMap: {
|
||||||
'微信': { name: '微信', icon: 'weixin', color: '#07C160' },
|
'微信': { name: '微信', icon: 'weixin', color: '#07C160' },
|
||||||
'支付宝': { name: '支付宝', icon: 'redo', color: '#1677FF' },
|
'支付宝': { name: '支付宝', icon: 'redo', color: '#1677FF' },
|
||||||
'银行卡': { name: '银行卡', icon: 'bankcard', color: '#FF6B6B' },
|
'银行卡': { name: '银行卡', icon: 'camera', color: '#FF6B6B' },
|
||||||
'现金': { name: '现金', icon: 'wallet', color: '#FFA500' },
|
'现金': { name: '现金', icon: 'wallet', color: '#FFA500' },
|
||||||
'其他': { name: '其他', icon: 'more', color: '#999999' }
|
'其他': { name: '其他', icon: 'more', color: '#999999' }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -147,7 +147,7 @@ import { isLogin, logout } from '@/utils/auth.js'
|
|||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
userInfo: {},
|
userInfo: this.$store.state.user.userInfo,
|
||||||
isLogin: false,
|
isLogin: false,
|
||||||
stats: {
|
stats: {
|
||||||
billCount: 0,
|
billCount: 0,
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ export default {
|
|||||||
async loadUserInfo() {
|
async loadUserInfo() {
|
||||||
try {
|
try {
|
||||||
// 从本地存储获取用户信息
|
// 从本地存储获取用户信息
|
||||||
const userInfo = uni.getStorageSync('userInfo') || {}
|
const userInfo = this.$store.state.user.userInfo || {}
|
||||||
this.formData = {
|
this.formData = {
|
||||||
username: userInfo.username || '',
|
username: userInfo.username || '',
|
||||||
nickname: userInfo.nickname || '',
|
nickname: userInfo.nickname || '',
|
||||||
@@ -113,47 +113,23 @@ export default {
|
|||||||
title: '上传中...'
|
title: '上传中...'
|
||||||
})
|
})
|
||||||
|
|
||||||
// 调用上传接口
|
// 调用上传接口,传递 type=avatar 参数
|
||||||
uni.uploadFile({
|
const result = await this.$api.common.upload.post(filePath, { type: 'avatar' })
|
||||||
url: commonApi.upload.url,
|
|
||||||
filePath: filePath,
|
uni.hideLoading()
|
||||||
name: 'file',
|
|
||||||
header: {
|
if (result && result.code === 1) {
|
||||||
'Authorization': 'Bearer ' + uni.getStorageSync('token')
|
this.formData.avatar = result.data.url || result.data
|
||||||
},
|
uni.showToast({
|
||||||
success: (uploadRes) => {
|
title: '上传成功',
|
||||||
uni.hideLoading()
|
icon: 'success'
|
||||||
try {
|
})
|
||||||
const data = JSON.parse(uploadRes.data)
|
} else {
|
||||||
if (data.code === 1) {
|
uni.showToast({
|
||||||
this.formData.avatar = data.data.url || data.data
|
title: result?.message || '上传失败',
|
||||||
uni.showToast({
|
icon: 'none'
|
||||||
title: '上传成功',
|
})
|
||||||
icon: 'success'
|
}
|
||||||
})
|
|
||||||
} else {
|
|
||||||
uni.showToast({
|
|
||||||
title: data.message || '上传失败',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error('解析上传结果失败:', e)
|
|
||||||
uni.showToast({
|
|
||||||
title: '上传失败',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
fail: (error) => {
|
|
||||||
uni.hideLoading()
|
|
||||||
console.error('上传失败:', error)
|
|
||||||
uni.showToast({
|
|
||||||
title: '上传失败',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
uni.hideLoading()
|
uni.hideLoading()
|
||||||
console.error('上传头像失败:', error)
|
console.error('上传头像失败:', error)
|
||||||
@@ -192,7 +168,7 @@ export default {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// 调用更新用户信息接口
|
// 调用更新用户信息接口
|
||||||
const result = await memberApi.edit.put({
|
const result = await this.$api.member.edit.post({
|
||||||
nickname: this.formData.nickname,
|
nickname: this.formData.nickname,
|
||||||
email: this.formData.email,
|
email: this.formData.email,
|
||||||
avatar: this.formData.avatar
|
avatar: this.formData.avatar
|
||||||
@@ -268,12 +244,6 @@ export default {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
.edit-profile-container {
|
|
||||||
min-height: 100vh;
|
|
||||||
background: #f5f5f5;
|
|
||||||
padding-bottom: 40rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.avatar-section {
|
.avatar-section {
|
||||||
background: #fff;
|
background: #fff;
|
||||||
padding: 60rpx 0;
|
padding: 60rpx 0;
|
||||||
|
|||||||
@@ -91,12 +91,6 @@ export default {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
.profile-container {
|
|
||||||
min-height: 100vh;
|
|
||||||
background: #f5f5f5;
|
|
||||||
padding-bottom: 40rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.user-card {
|
.user-card {
|
||||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||||
margin: 20rpx 30rpx;
|
margin: 20rpx 30rpx;
|
||||||
|
|||||||
@@ -230,12 +230,6 @@ export default {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
.password-container {
|
|
||||||
min-height: 100vh;
|
|
||||||
background: #f5f5f5;
|
|
||||||
padding-bottom: 40rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.form-section {
|
.form-section {
|
||||||
margin: 20rpx 30rpx;
|
margin: 20rpx 30rpx;
|
||||||
background: #fff;
|
background: #fff;
|
||||||
|
|||||||
@@ -19,4 +19,7 @@ Route::name('system.')->prefix('system')->middleware(['auth.check:api'])->group(
|
|||||||
Route::get('/serve', 'serve')->name('serve');
|
Route::get('/serve', 'serve')->name('serve');
|
||||||
Route::post('/jssdk', 'jssdk')->name('jssdk');
|
Route::post('/jssdk', 'jssdk')->name('jssdk');
|
||||||
});
|
});
|
||||||
|
Route::controller(App\Http\Controllers\Api\System\File::class)->prefix('file')->name('file.')->group(function () {
|
||||||
|
Route::post('/upload', 'upload')->name('upload');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user