优化代码
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\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Member extends Authenticatable implements JWTSubject {
|
||||
use Notifiable;
|
||||
@@ -59,7 +61,9 @@ class Member extends Authenticatable implements JWTSubject {
|
||||
|
||||
public function avatar(): 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 {
|
||||
upload: {
|
||||
url: '/api/system/upload',
|
||||
url: '/api/system/file/upload',
|
||||
name: '图片上传',
|
||||
post: async function(params) {
|
||||
return request.post(this.url, params)
|
||||
post: async function(file, params) {
|
||||
return request.upload(this.url, {
|
||||
filePath: file,
|
||||
params
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ export default {
|
||||
paymentMap: {
|
||||
'微信': { name: '微信', icon: 'weixin', color: '#07C160' },
|
||||
'支付宝': { name: '支付宝', icon: 'redo', color: '#1677FF' },
|
||||
'银行卡': { name: '银行卡', icon: 'bankcard', color: '#FF6B6B' },
|
||||
'银行卡': { name: '银行卡', icon: 'camera', color: '#FF6B6B' },
|
||||
'现金': { name: '现金', icon: 'wallet', color: '#FFA500' },
|
||||
'其他': { name: '其他', icon: 'more', color: '#999999' }
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ import { isLogin, logout } from '@/utils/auth.js'
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
userInfo: {},
|
||||
userInfo: this.$store.state.user.userInfo,
|
||||
isLogin: false,
|
||||
stats: {
|
||||
billCount: 0,
|
||||
|
||||
@@ -75,7 +75,7 @@ export default {
|
||||
async loadUserInfo() {
|
||||
try {
|
||||
// 从本地存储获取用户信息
|
||||
const userInfo = uni.getStorageSync('userInfo') || {}
|
||||
const userInfo = this.$store.state.user.userInfo || {}
|
||||
this.formData = {
|
||||
username: userInfo.username || '',
|
||||
nickname: userInfo.nickname || '',
|
||||
@@ -113,47 +113,23 @@ export default {
|
||||
title: '上传中...'
|
||||
})
|
||||
|
||||
// 调用上传接口
|
||||
uni.uploadFile({
|
||||
url: commonApi.upload.url,
|
||||
filePath: filePath,
|
||||
name: 'file',
|
||||
header: {
|
||||
'Authorization': 'Bearer ' + uni.getStorageSync('token')
|
||||
},
|
||||
success: (uploadRes) => {
|
||||
uni.hideLoading()
|
||||
try {
|
||||
const data = JSON.parse(uploadRes.data)
|
||||
if (data.code === 1) {
|
||||
this.formData.avatar = data.data.url || data.data
|
||||
uni.showToast({
|
||||
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'
|
||||
})
|
||||
}
|
||||
})
|
||||
// 调用上传接口,传递 type=avatar 参数
|
||||
const result = await this.$api.common.upload.post(filePath, { type: 'avatar' })
|
||||
|
||||
uni.hideLoading()
|
||||
|
||||
if (result && result.code === 1) {
|
||||
this.formData.avatar = result.data.url || result.data
|
||||
uni.showToast({
|
||||
title: '上传成功',
|
||||
icon: 'success'
|
||||
})
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: result?.message || '上传失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
uni.hideLoading()
|
||||
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,
|
||||
email: this.formData.email,
|
||||
avatar: this.formData.avatar
|
||||
@@ -268,12 +244,6 @@ export default {
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.edit-profile-container {
|
||||
min-height: 100vh;
|
||||
background: #f5f5f5;
|
||||
padding-bottom: 40rpx;
|
||||
}
|
||||
|
||||
.avatar-section {
|
||||
background: #fff;
|
||||
padding: 60rpx 0;
|
||||
|
||||
@@ -91,12 +91,6 @@ export default {
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.profile-container {
|
||||
min-height: 100vh;
|
||||
background: #f5f5f5;
|
||||
padding-bottom: 40rpx;
|
||||
}
|
||||
|
||||
.user-card {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
margin: 20rpx 30rpx;
|
||||
|
||||
@@ -230,12 +230,6 @@ export default {
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.password-container {
|
||||
min-height: 100vh;
|
||||
background: #f5f5f5;
|
||||
padding-bottom: 40rpx;
|
||||
}
|
||||
|
||||
.form-section {
|
||||
margin: 20rpx 30rpx;
|
||||
background: #fff;
|
||||
|
||||
@@ -19,4 +19,7 @@ Route::name('system.')->prefix('system')->middleware(['auth.check:api'])->group(
|
||||
Route::get('/serve', 'serve')->name('serve');
|
||||
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