xinli/ruoyi-ui/src/views/psychology/assessment/start.vue
2025-11-06 18:06:15 +08:00

242 lines
8.8 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div class="app-container">
<el-card shadow="never">
<div slot="header" class="clearfix">
<span>选择量表开始测评</span>
<el-button style="float: right;" type="text" @click="handleBack">返回</el-button>
</div>
<el-form :model="form" :rules="rules" ref="form" label-width="120px">
<el-form-item label="选择量表" prop="scaleId">
<el-select v-model="form.scaleId" placeholder="请选择要测评的量表" style="width: 100%;" filterable>
<el-option
v-for="scale in scaleList"
:key="scale.scaleId"
:label="scale.scaleName"
:value="scale.scaleId">
<span style="float: left">{{ scale.scaleName }}</span>
<span style="float: right; color: #8492a6; font-size: 13px">{{ scale.itemCount }}题</span>
</el-option>
</el-select>
</el-form-item>
<el-divider content-position="left">选择被测评人</el-divider>
<el-form-item label="选择用户档案" prop="profileId">
<el-select v-model="form.profileId" placeholder="请从存档用户中选择" style="width: 100%; filterable">
<template v-if="profileList.length === 0">
<el-option disabled :label="'暂无存档用户,请先添加'" :value="''" />
</template>
<template v-else>
<el-option
v-for="profile in profileList"
:key="profile.profileId"
:label="profile.userName + '(' + (profile.userPhone || '无电话') + ')'"
:value="profile.profileId">
<span style="float: left">{{ profile.userName }}</span>
<span style="float: right; color: #8492a6; font-size: 13px">{{ profile.userPhone || '无电话' }}</span>
</el-option>
</template>
</el-select>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm" :loading="loading" :disabled="profileList.length === 0">开始测评</el-button>
<el-button @click="handleBack">取消</el-button>
<el-button type="info" @click="redirectToProfile">管理用户档案</el-button>
</el-form-item>
</el-form>
</el-card>
<el-card shadow="never" style="margin-top: 20px;">
<div slot="header" class="clearfix">
<span>我的暂停测评</span>
</div>
<el-table :data="pausedList" border>
<el-table-column label="量表" prop="scaleId" width="100" />
<el-table-column label="姓名" prop="assesseeName" width="120" />
<el-table-column label="开始时间" prop="startTime" width="180">
<template slot-scope="scope">
<span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
</template>
</el-table-column>
<el-table-column label="操作" align="center" width="150">
<template slot-scope="scope">
<el-button
size="mini"
type="text"
icon="el-icon-edit-outline"
@click="handleContinue(scope.row)"
>继续测评</el-button>
</template>
</el-table-column>
</el-table>
</el-card>
</div>
</template>
<script>
import { startAssessment, pausedAssessmentList, resumeAssessment } from "@/api/psychology/assessment";
import { listScale } from "@/api/psychology/scale";
import { listProfile } from "@/api/psychology/profile";
export default {
name: "AssessmentStart",
data() {
return {
loading: false,
scaleList: [],
profileList: [],
pausedList: [],
form: {
scaleId: undefined,
profileId: undefined
},
rules: {
scaleId: [
{ required: true, message: "请选择量表", trigger: "change" }
],
profileId: [
{ required: true, message: "请从存档用户中选择", trigger: "change" }
]
}
};
},
created() {
this.loadScales();
this.loadProfiles();
this.loadPaused();
},
methods: {
/** 加载量表列表 */
loadScales() {
// 获取当前登录用户ID
const userId = this.$store.getters.userId;
// 如果是管理员,显示所有量表;否则只显示有权限的量表
if (userId === 1) {
// 管理员显示所有量表
listScale({ status: '0' }).then(response => {
this.scaleList = response.rows.filter(scale => scale.itemCount > 0);
});
} else {
// 普通用户只显示有权限的量表
import('@/api/psychology/permission').then(module => {
module.getUserScaleIds(userId).then(permissionResponse => {
const allowedScaleIds = permissionResponse.data || [];
if (allowedScaleIds.length === 0) {
this.scaleList = [];
this.$message.warning('您还没有被分配任何量表权限,请联系管理员');
return;
}
// 获取所有量表,然后过滤
listScale({ status: '0' }).then(response => {
this.scaleList = response.rows.filter(scale =>
scale.itemCount > 0 && allowedScaleIds.includes(scale.scaleId)
);
});
});
});
}
},
/** 加载用户档案列表 */
loadProfiles() {
const params = {
pageNum: 1,
pageSize: 1000 // 加载更多用户档案
}
listProfile(params).then(response => {
// 确保获取到正确的数据结构
if (response.rows && Array.isArray(response.rows)) {
this.profileList = response.rows
} else if (Array.isArray(response.data)) {
this.profileList = response.data
} else {
this.profileList = []
}
console.log('Loaded user profiles:', this.profileList)
}).catch(error => {
console.error('Failed to load user profiles:', error)
this.$message.error('加载用户档案失败,请重试')
})
},
/** 加载暂停的测评 */
loadPaused() {
pausedAssessmentList().then(response => {
this.pausedList = response.data || [];
});
},
/** 提交表单 */
submitForm() {
this.$refs["form"].validate(valid => {
if (valid) {
this.loading = true;
// 找到选中的用户档案
const selectedProfile = this.profileList.find(p => p.profileId === this.form.profileId);
if (selectedProfile) {
// 构建测评表单数据
const assessmentData = {
scaleId: this.form.scaleId,
// 移除profileId后端AssessmentStartVO不需要这个字段
assesseeName: selectedProfile.userName,
// 从userName等信息中提取性别和年龄或使用默认值
assesseeGender: '0', // 默认性别
assesseeAge: 0, // 默认年龄
assesseePhone: selectedProfile.phone,
anonymous: false // 添加anonymous字段设置为非匿名
};
startAssessment(assessmentData).then(response => {
if (response.code === 200) {
this.$modal.msgSuccess("测评已开始");
const assessmentId = response.data;
// 确保使用正确的路由路径
this.$router.push({ path: '/psychology/assessment/taking', query: { assessmentId: assessmentId } });
}
this.loading = false;
}).catch(error => {
console.error('Failed to start assessment:', error);
this.$modal.msgError("开始测评失败,请重试");
this.loading = false;
});
} else {
this.$modal.msgError("请选择有效的用户档案");
this.loading = false;
}
}
});
},
/** 继续测评 */
handleContinue(row) {
// 如果测评状态是暂停,先恢复测评状态
if (row.status === '3') {
resumeAssessment(row.assessmentId).then(() => {
this.$router.push({ path: '/psychology/assessment/taking', query: { assessmentId: row.assessmentId } });
}).catch(() => {
// 即使恢复失败,也允许继续测评
this.$router.push({ path: '/psychology/assessment/taking', query: { assessmentId: row.assessmentId } });
});
} else {
// 直接跳转到测评页面
this.$router.push({ path: '/psychology/assessment/taking', query: { assessmentId: row.assessmentId } });
}
},
/** 返回 */
handleBack() {
this.$router.push('assessment');
},
/** 跳转到用户档案管理页面 */
redirectToProfile() {
this.$router.push('/psychology/profile');
}
}
};
</script>
<style scoped>
.app-container {
padding: 20px;
}
</style>