Compare commits
No commits in common. "d873f3e0163e9bcb7f057781cc17f8a46a49f8d5" and "47d798e3803aedcfb2d38801ceaf87a4f59a6492" have entirely different histories.
d873f3e016
...
47d798e380
|
|
@ -19,7 +19,6 @@ class User(AbstractUser):
|
||||||
verbose_name = '用户'
|
verbose_name = '用户'
|
||||||
verbose_name_plural = '用户'
|
verbose_name_plural = '用户'
|
||||||
db_table = 'auth_user'
|
db_table = 'auth_user'
|
||||||
ordering = ['id']
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.username} ({self.get_role_display()})"
|
return f"{self.username} ({self.get_role_display()})"
|
||||||
|
|
|
||||||
|
|
@ -71,17 +71,3 @@ class CustomTokenObtainPairSerializer(TokenObtainPairSerializer):
|
||||||
data['user'] = UserSerializer(self.user).data
|
data['user'] = UserSerializer(self.user).data
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
class ChangePasswordSerializer(serializers.Serializer):
|
|
||||||
"""
|
|
||||||
密码修改序列化器
|
|
||||||
"""
|
|
||||||
old_password = serializers.CharField(write_only=True, required=True, style={'input_type': 'password'})
|
|
||||||
new_password = serializers.CharField(write_only=True, required=True, style={'input_type': 'password'})
|
|
||||||
new_password_confirm = serializers.CharField(write_only=True, required=True, style={'input_type': 'password'})
|
|
||||||
|
|
||||||
def validate(self, attrs):
|
|
||||||
if attrs['new_password'] != attrs['new_password_confirm']:
|
|
||||||
raise serializers.ValidationError({"new_password": "新密码字段不匹配。"})
|
|
||||||
return attrs
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,11 @@
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
from rest_framework_simplejwt.views import TokenRefreshView
|
from rest_framework_simplejwt.views import TokenRefreshView
|
||||||
from .views import CustomTokenObtainPairView, UserListView, UserDetailView, current_user, change_password, reset_password
|
from .views import CustomTokenObtainPairView, UserListView, UserDetailView, current_user
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('login/', CustomTokenObtainPairView.as_view(), name='token_obtain_pair'),
|
path('login/', CustomTokenObtainPairView.as_view(), name='token_obtain_pair'),
|
||||||
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
|
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
|
||||||
path('users/', UserListView.as_view(), name='user-list'),
|
path('users/', UserListView.as_view(), name='user-list'),
|
||||||
path('users/<int:pk>/', UserDetailView.as_view(), name='user-detail'),
|
path('users/<int:pk>/', UserDetailView.as_view(), name='user-detail'),
|
||||||
path('users/<int:pk>/reset-password/', reset_password, name='user-reset-password'),
|
|
||||||
path('user/', current_user, name='current-user'),
|
path('user/', current_user, name='current-user'),
|
||||||
path('user/change-password/', change_password, name='change-password'),
|
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,8 @@ from rest_framework.permissions import AllowAny, IsAuthenticated
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework_simplejwt.views import TokenObtainPairView
|
from rest_framework_simplejwt.views import TokenObtainPairView
|
||||||
from rest_framework.exceptions import PermissionDenied
|
from rest_framework.exceptions import PermissionDenied
|
||||||
from django.shortcuts import get_object_or_404
|
|
||||||
from .models import User
|
from .models import User
|
||||||
from .serializers import UserSerializer, UserCreateSerializer, CustomTokenObtainPairSerializer, ChangePasswordSerializer
|
from .serializers import UserSerializer, UserCreateSerializer, CustomTokenObtainPairSerializer
|
||||||
|
|
||||||
RESET_PASSWORD = "abc!0000"
|
|
||||||
|
|
||||||
|
|
||||||
class CustomTokenObtainPairView(TokenObtainPairView):
|
class CustomTokenObtainPairView(TokenObtainPairView):
|
||||||
|
|
@ -79,35 +76,3 @@ def current_user(request):
|
||||||
"""
|
"""
|
||||||
serializer = UserSerializer(request.user)
|
serializer = UserSerializer(request.user)
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
|
||||||
@api_view(['POST'])
|
|
||||||
@permission_classes([IsAuthenticated])
|
|
||||||
def change_password(request):
|
|
||||||
"""
|
|
||||||
修改当前用户密码
|
|
||||||
"""
|
|
||||||
serializer = ChangePasswordSerializer(data=request.data)
|
|
||||||
serializer.is_valid(raise_exception=True)
|
|
||||||
|
|
||||||
if not request.user.check_password(serializer.validated_data['old_password']):
|
|
||||||
return Response({"detail": "原密码不正确"}, status=400)
|
|
||||||
|
|
||||||
request.user.set_password(serializer.validated_data['new_password'])
|
|
||||||
request.user.save()
|
|
||||||
return Response({"status": "密码已更新"})
|
|
||||||
|
|
||||||
|
|
||||||
@api_view(['POST'])
|
|
||||||
@permission_classes([IsAuthenticated])
|
|
||||||
def reset_password(request, pk):
|
|
||||||
"""
|
|
||||||
管理员重置用户密码
|
|
||||||
"""
|
|
||||||
if request.user.role != 'admin':
|
|
||||||
raise PermissionDenied("只有管理员可以重置密码")
|
|
||||||
|
|
||||||
user = get_object_or_404(User, pk=pk)
|
|
||||||
user.set_password(RESET_PASSWORD)
|
|
||||||
user.save()
|
|
||||||
return Response({"status": "密码已重置", "reset_password": RESET_PASSWORD})
|
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,6 @@ class Dictionary(models.Model):
|
||||||
verbose_name_plural = '数据字典'
|
verbose_name_plural = '数据字典'
|
||||||
db_table = 'dictionary'
|
db_table = 'dictionary'
|
||||||
unique_together = ('type', 'value')
|
unique_together = ('type', 'value')
|
||||||
ordering = ['id']
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.type} - {self.name}"
|
return f"{self.type} - {self.name}"
|
||||||
|
|
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
# Generated by Django 4.2.7 on 2026-03-13 01:27
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
|
||||||
|
|
||||||
dependencies = [
|
|
||||||
('factory', '0001_initial'),
|
|
||||||
]
|
|
||||||
|
|
||||||
operations = [
|
|
||||||
migrations.AlterModelOptions(
|
|
||||||
name='factory',
|
|
||||||
options={'ordering': ['id'], 'verbose_name': '工厂', 'verbose_name_plural': '工厂'},
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name='factory',
|
|
||||||
name='dealer_name',
|
|
||||||
field=models.CharField(blank=True, max_length=255, null=True, verbose_name='经销商名称'),
|
|
||||||
),
|
|
||||||
]
|
|
||||||
|
|
@ -4,7 +4,7 @@ class Factory(models.Model):
|
||||||
"""
|
"""
|
||||||
工厂模型
|
工厂模型
|
||||||
"""
|
"""
|
||||||
dealer_name = models.CharField(max_length=255, blank=True, null=True, verbose_name='经销商名称')
|
dealer_name = models.CharField(max_length=255, verbose_name='经销商名称')
|
||||||
product_category = models.CharField(max_length=255, blank=True, null=True, verbose_name='产品分类')
|
product_category = models.CharField(max_length=255, blank=True, null=True, verbose_name='产品分类')
|
||||||
factory_name = models.CharField(max_length=255, verbose_name='生产工厂全称')
|
factory_name = models.CharField(max_length=255, verbose_name='生产工厂全称')
|
||||||
factory_short_name = models.CharField(max_length=100, verbose_name='工厂简称')
|
factory_short_name = models.CharField(max_length=100, verbose_name='工厂简称')
|
||||||
|
|
@ -20,7 +20,6 @@ class Factory(models.Model):
|
||||||
verbose_name = '工厂'
|
verbose_name = '工厂'
|
||||||
verbose_name_plural = '工厂'
|
verbose_name_plural = '工厂'
|
||||||
db_table = 'factory'
|
db_table = 'factory'
|
||||||
ordering = ['id']
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.factory_name
|
return self.factory_name
|
||||||
|
|
|
||||||
|
|
@ -28,4 +28,4 @@ class FactoryListSerializer(serializers.ModelSerializer):
|
||||||
"""
|
"""
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Factory
|
model = Factory
|
||||||
fields = ['id', 'factory_name', 'factory_short_name', 'province', 'city', 'dealer_name']
|
fields = ['id', 'factory_name', 'factory_short_name', 'province', 'city']
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,6 @@ class Material(models.Model):
|
||||||
verbose_name = '材料'
|
verbose_name = '材料'
|
||||||
verbose_name_plural = '材料'
|
verbose_name_plural = '材料'
|
||||||
db_table = 'material'
|
db_table = 'material'
|
||||||
ordering = ['id']
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
@ -92,7 +91,6 @@ class MaterialCategory(models.Model):
|
||||||
verbose_name = '材料分类'
|
verbose_name = '材料分类'
|
||||||
verbose_name_plural = '材料分类'
|
verbose_name_plural = '材料分类'
|
||||||
db_table = 'material_category'
|
db_table = 'material_category'
|
||||||
ordering = ['id']
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
@ -112,7 +110,6 @@ class MaterialSubcategory(models.Model):
|
||||||
verbose_name = '材料子分类'
|
verbose_name = '材料子分类'
|
||||||
verbose_name_plural = '材料子分类'
|
verbose_name_plural = '材料子分类'
|
||||||
db_table = 'material_subcategory'
|
db_table = 'material_subcategory'
|
||||||
ordering = ['id']
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
|
||||||
|
|
@ -202,7 +202,7 @@ class MaterialCategoryViewSet(ModelViewSet):
|
||||||
"""
|
"""
|
||||||
材料分类视图集
|
材料分类视图集
|
||||||
"""
|
"""
|
||||||
queryset = MaterialCategory.objects.all()
|
queryset = MaterialCategory.objects.all().order_by('id')
|
||||||
serializer_class = MaterialCategorySerializer
|
serializer_class = MaterialCategorySerializer
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
|
@ -226,7 +226,7 @@ class MaterialSubcategoryViewSet(ModelViewSet):
|
||||||
"""
|
"""
|
||||||
材料子分类视图集
|
材料子分类视图集
|
||||||
"""
|
"""
|
||||||
queryset = MaterialSubcategory.objects.select_related('category').all()
|
queryset = MaterialSubcategory.objects.select_related('category').all().order_by('id')
|
||||||
serializer_class = MaterialSubcategorySerializer
|
serializer_class = MaterialSubcategorySerializer
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,13 +29,3 @@ export const deleteUser = async (id) => {
|
||||||
const { data } = await api.delete(`/auth/users/${id}/`)
|
const { data } = await api.delete(`/auth/users/${id}/`)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
export const changePassword = async (payload) => {
|
|
||||||
const { data } = await api.post('/auth/user/change-password/', payload)
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
export const resetUserPassword = async (id) => {
|
|
||||||
const { data } = await api.post(`/auth/users/${id}/reset-password/`)
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,6 @@
|
||||||
<div class="name">{{ user?.username || '用户' }}</div>
|
<div class="name">{{ user?.username || '用户' }}</div>
|
||||||
<div class="role">{{ isAdmin ? '管理员' : '普通账号' }}</div>
|
<div class="role">{{ isAdmin ? '管理员' : '普通账号' }}</div>
|
||||||
</div>
|
</div>
|
||||||
<el-button size="small" @click="openPassword">修改密码</el-button>
|
|
||||||
<el-button size="small" @click="onLogout">退出</el-button>
|
<el-button size="small" @click="onLogout">退出</el-button>
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
@ -34,32 +33,12 @@
|
||||||
</section>
|
</section>
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<el-dialog v-model="passwordVisible" title="修改密码" width="420px" class="dialog-scroll">
|
|
||||||
<el-form :model="passwordForm" label-width="90px">
|
|
||||||
<el-form-item label="原密码" required>
|
|
||||||
<el-input v-model="passwordForm.old_password" type="password" show-password />
|
|
||||||
</el-form-item>
|
|
||||||
<el-form-item label="新密码" required>
|
|
||||||
<el-input v-model="passwordForm.new_password" type="password" show-password />
|
|
||||||
</el-form-item>
|
|
||||||
<el-form-item label="确认密码" required>
|
|
||||||
<el-input v-model="passwordForm.new_password_confirm" type="password" show-password />
|
|
||||||
</el-form-item>
|
|
||||||
</el-form>
|
|
||||||
<template #footer>
|
|
||||||
<el-button @click="passwordVisible = false">取消</el-button>
|
|
||||||
<el-button type="primary" @click="onChangePassword">保存</el-button>
|
|
||||||
</template>
|
|
||||||
</el-dialog>
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { computed, reactive, ref } from 'vue'
|
import { computed } from 'vue'
|
||||||
import { useRoute, useRouter } from 'vue-router'
|
import { useRoute, useRouter } from 'vue-router'
|
||||||
import { ElMessage } from 'element-plus'
|
|
||||||
import { useAuth } from '@/store/auth'
|
import { useAuth } from '@/store/auth'
|
||||||
import { changePassword } from '@/api/auth'
|
|
||||||
|
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
@ -75,30 +54,6 @@ const titleMap = {
|
||||||
const title = computed(() => titleMap[`/${route.path.split('/')[1]}`] || '系统首页')
|
const title = computed(() => titleMap[`/${route.path.split('/')[1]}`] || '系统首页')
|
||||||
const user = computed(() => state.user)
|
const user = computed(() => state.user)
|
||||||
|
|
||||||
const passwordVisible = ref(false)
|
|
||||||
const passwordForm = reactive({
|
|
||||||
old_password: '',
|
|
||||||
new_password: '',
|
|
||||||
new_password_confirm: ''
|
|
||||||
})
|
|
||||||
|
|
||||||
const openPassword = () => {
|
|
||||||
passwordForm.old_password = ''
|
|
||||||
passwordForm.new_password = ''
|
|
||||||
passwordForm.new_password_confirm = ''
|
|
||||||
passwordVisible.value = true
|
|
||||||
}
|
|
||||||
|
|
||||||
const onChangePassword = async () => {
|
|
||||||
try {
|
|
||||||
await changePassword({ ...passwordForm })
|
|
||||||
ElMessage.success('密码已更新')
|
|
||||||
passwordVisible.value = false
|
|
||||||
} catch (error) {
|
|
||||||
ElMessage.error(error.response?.data?.detail || '修改失败')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const onLogout = () => {
|
const onLogout = () => {
|
||||||
clearAuth()
|
clearAuth()
|
||||||
router.push('/login')
|
router.push('/login')
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
<template>
|
<template>
|
||||||
<div class="page">
|
<div class="page">
|
||||||
<div class="page-title">工厂管理</div>
|
<div class="page-title">工厂管理</div>
|
||||||
<div class="toolbar">
|
<div class="toolbar">
|
||||||
|
|
@ -38,7 +38,7 @@
|
||||||
|
|
||||||
<el-dialog v-model="dialogVisible" :title="dialogTitle" width="640px" class="dialog-scroll">
|
<el-dialog v-model="dialogVisible" :title="dialogTitle" width="640px" class="dialog-scroll">
|
||||||
<el-form :model="form" label-width="100px">
|
<el-form :model="form" label-width="100px">
|
||||||
<el-form-item label="经销商">
|
<el-form-item label="经销商" required>
|
||||||
<el-input v-model="form.dealer_name" />
|
<el-input v-model="form.dealer_name" />
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="产品分类">
|
<el-form-item label="产品分类">
|
||||||
|
|
|
||||||
|
|
@ -14,11 +14,10 @@
|
||||||
<el-table-column prop="factory_name" label="所属工厂" />
|
<el-table-column prop="factory_name" label="所属工厂" />
|
||||||
<el-table-column prop="phone" label="手机" />
|
<el-table-column prop="phone" label="手机" />
|
||||||
<el-table-column prop="email" label="邮箱" />
|
<el-table-column prop="email" label="邮箱" />
|
||||||
<el-table-column label="操作" width="240">
|
<el-table-column label="操作" width="180">
|
||||||
<template #default="scope">
|
<template #default="scope">
|
||||||
<div class="table-actions">
|
<div class="table-actions">
|
||||||
<el-button size="small" @click="openEdit(scope.row)">编辑</el-button>
|
<el-button size="small" @click="openEdit(scope.row)">编辑</el-button>
|
||||||
<el-button size="small" type="warning" @click="onReset(scope.row)">重置密码</el-button>
|
|
||||||
<el-button size="small" type="danger" @click="onDelete(scope.row)">删除</el-button>
|
<el-button size="small" type="danger" @click="onDelete(scope.row)">删除</el-button>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
@ -77,7 +76,7 @@
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref, reactive, onMounted } from 'vue'
|
import { ref, reactive, onMounted } from 'vue'
|
||||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||||
import { fetchUsers, createUser, updateUser, deleteUser, resetUserPassword } from '@/api/auth'
|
import { fetchUsers, createUser, updateUser, deleteUser } from '@/api/auth'
|
||||||
import { fetchFactorySimple } from '@/api/factory'
|
import { fetchFactorySimple } from '@/api/factory'
|
||||||
|
|
||||||
const users = ref([])
|
const users = ref([])
|
||||||
|
|
@ -178,15 +177,6 @@ const onDelete = (row) => {
|
||||||
.catch(() => {})
|
.catch(() => {})
|
||||||
}
|
}
|
||||||
|
|
||||||
const onReset = (row) => {
|
|
||||||
ElMessageBox.confirm(`确认重置用户 ${row.username} 的密码为 abc!0000 吗?`, '提示', { type: 'warning' })
|
|
||||||
.then(async () => {
|
|
||||||
await resetUserPassword(row.id)
|
|
||||||
ElMessage.success('密码已重置为 abc!0000')
|
|
||||||
})
|
|
||||||
.catch(() => {})
|
|
||||||
}
|
|
||||||
|
|
||||||
const onPageChange = (page) => {
|
const onPageChange = (page) => {
|
||||||
pagination.page = page
|
pagination.page = page
|
||||||
loadUsers()
|
loadUsers()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue