处理微信小程序 Babel regenerator 的 "a" is read-only 问题

This commit is contained in:
huhao
2026-08-21 09:03:18 +08:00
parent b32cda3e85
commit 20c3255185
4 changed files with 102 additions and 89 deletions
+9 -3
View File
@@ -263,17 +263,23 @@ export const reqSaveWeldAppearanceCheck = (data = {}) => post('/WeldAppearanceCh
* 按班组查询任务单列表 * 按班组查询任务单列表
* @param {Object} params - { projectId, teamGroupId?, unitWorkId? } * @param {Object} params - { projectId, teamGroupId?, unitWorkId? }
*/ */
export const reqWeldTaskListByTeamGroup = (params = {}) => get('WeldTask/GetTaskListByTeamGroup', params) export const reqWeldTaskListByTeamGroup = (params) => request('WeldTask/GetTaskListByTeamGroup', {
method: 'GET',
data: params || {},
})
/** /**
* 查询任务单详情(含 Joints) * 查询任务单详情(含 Joints)
* @param {Object} params - { projectId, taskCode, unitWorkId? } * @param {Object} params - { projectId, taskCode, unitWorkId? }
*/ */
export const reqWeldTaskDetail = (params = {}) => get('WeldTask/GetTaskDetail', params) export const reqWeldTaskDetail = (params) => request('WeldTask/GetTaskDetail', {
method: 'GET',
data: params || {},
})
/** /**
* 根据任务单生成出库申请 * 根据任务单生成出库申请
* @param {Object} data - { ProjectId, TaskCode, PersonId } * @param {Object} data - { ProjectId, TaskCode, PersonId }
*/ */
export const reqWeldTaskGenerateOutPlan = (data = {}) => post('WeldTask/GenerateOutPlan', data) export const reqWeldTaskGenerateOutPlan = (data) => post('WeldTask/GenerateOutPlan', data || {})
+37 -35
View File
@@ -33,7 +33,7 @@
</view> </view>
<view class="card__row"> <view class="card__row">
<text class="card__label">焊口数量</text> <text class="card__label">焊口数量</text>
<text class="card__value">{{ detail.WeldJointCount ?? joints.length }}</text> <text class="card__value">{{ detail.WeldJointCount != null ? detail.WeldJointCount : joints.length }}</text>
</view> </view>
</view> </view>
</view> </view>
@@ -106,15 +106,14 @@
<script setup> <script setup>
/** /**
* 焊接任务单详情:展示 Joints,支持生成出库申请单 * 焊接任务单详情:展示 Joints,支持生成出库申请单
* 使用 Promise 链而非 async/await,规避微信小程序 Babel regenerator 的 "a" is read-only
*/ */
import { computed, ref } from 'vue' import { computed, ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app' import { onLoad } from '@dcloudio/uni-app'
import { storeToRefs } from 'pinia'
import { useUserStore } from '@/store' import { useUserStore } from '@/store'
import { reqWeldTaskDetail, reqWeldTaskGenerateOutPlan } from '@/api/hj.js' import { reqWeldTaskDetail, reqWeldTaskGenerateOutPlan } from '@/api/hj.js'
const userStore = useUserStore() const userStore = useUserStore()
const { userInfo, token } = storeToRefs(userStore)
const projectId = ref('') const projectId = ref('')
const taskCode = ref('') const taskCode = ref('')
@@ -123,12 +122,15 @@ const detail = ref({})
const loading = ref(false) const loading = ref(false)
const submitting = ref(false) const submitting = ref(false)
const joints = computed(() => { const joints = computed(function () {
const list = detail.value?.Joints const d = detail.value
const list = d ? d.Joints : null
return Array.isArray(list) ? list : [] return Array.isArray(list) ? list : []
}) })
const canGenerate = computed(() => !!(projectId.value && taskCode.value)) const canGenerate = computed(function () {
return !!(projectId.value && taskCode.value)
})
const fmtDate = (val) => { const fmtDate = (val) => {
if (!val) return '--' if (!val) return '--'
@@ -137,7 +139,7 @@ const fmtDate = (val) => {
const y = d.getFullYear() const y = d.getFullYear()
const m = String(d.getMonth() + 1).padStart(2, '0') const m = String(d.getMonth() + 1).padStart(2, '0')
const day = String(d.getDate()).padStart(2, '0') const day = String(d.getDate()).padStart(2, '0')
return `${y}-${m}-${day}` return y + '-' + m + '-' + day
} }
const formatSize = (val) => { const formatSize = (val) => {
@@ -145,28 +147,29 @@ const formatSize = (val) => {
return String(val) return String(val)
} }
const loadDetail = async () => { const loadDetail = () => {
if (!projectId.value || !taskCode.value) { if (!projectId.value || !taskCode.value) {
uni.showToast({ title: '入口参数错误', icon: 'none' }) uni.showToast({ title: '入口参数错误', icon: 'none' })
return return
} }
loading.value = true loading.value = true
try { const params = {
const res = await reqWeldTaskDetail({ projectId: projectId.value,
projectId: projectId.value, taskCode: taskCode.value,
taskCode: taskCode.value, unitWorkId: unitWorkId.value || '',
unitWorkId: unitWorkId.value || undefined, }
}) reqWeldTaskDetail(params).then(function (res) {
detail.value = res?.data || {} const data = res ? res.data : null
if (!res?.data) { detail.value = data || {}
if (!data) {
uni.showToast({ title: '未找到任务单', icon: 'none' }) uni.showToast({ title: '未找到任务单', icon: 'none' })
} }
} catch (e) { }).catch(function (e) {
console.error('任务单详情加载失败:', e) console.error('任务单详情加载失败:', e)
detail.value = {} detail.value = {}
} finally { }).then(function () {
loading.value = false loading.value = false
} })
} }
const handleGenerate = () => { const handleGenerate = () => {
@@ -174,38 +177,37 @@ const handleGenerate = () => {
uni.showModal({ uni.showModal({
title: '确认生成', title: '确认生成',
content: '确定根据当前任务单生成出库申请单吗?已生成过的不会重复建单。', content: '确定根据当前任务单生成出库申请单吗?已生成过的不会重复建单。',
success: async (modalRes) => { success: function (modalRes) {
if (!modalRes.confirm) return if (!modalRes.confirm) return
const personId = userInfo.value?.PersonId || token.value const user = userStore.userInfo
const personId = (user && user.PersonId) || userStore.token
if (!personId) { if (!personId) {
uni.showToast({ title: '缺少登录人信息', icon: 'none' }) uni.showToast({ title: '缺少登录人信息', icon: 'none' })
return return
} }
submitting.value = true submitting.value = true
try { reqWeldTaskGenerateOutPlan({
await reqWeldTaskGenerateOutPlan({ ProjectId: projectId.value,
ProjectId: projectId.value, TaskCode: taskCode.value,
TaskCode: taskCode.value, PersonId: personId,
PersonId: personId, }).then(function () {
})
// 接口成功也可能 data 为 null(首次/已存在/无材料均可能 code:1)
uni.showToast({ uni.showToast({
title: '生成请求已处理', title: '生成请求已处理',
icon: 'none', icon: 'none',
}) })
} catch (e) { }).catch(function (e) {
console.error('生成出库申请失败:', e) console.error('生成出库申请失败:', e)
} finally { }).then(function () {
submitting.value = false submitting.value = false
} })
}, },
}) })
} }
onLoad((opt) => { onLoad(function (opt) {
projectId.value = decodeURIComponent(opt.projectId || '') projectId.value = decodeURIComponent((opt && opt.projectId) || '')
taskCode.value = decodeURIComponent(opt.taskCode || '') taskCode.value = decodeURIComponent((opt && opt.taskCode) || '')
unitWorkId.value = decodeURIComponent(opt.unitWorkId || '') unitWorkId.value = decodeURIComponent((opt && opt.unitWorkId) || '')
loadDetail() loadDetail()
}) })
</script> </script>
+36 -32
View File
@@ -16,7 +16,7 @@
> >
<view class="card-header"> <view class="card-header">
<text class="card-code">{{ item.TaskCode || '--' }}</text> <text class="card-code">{{ item.TaskCode || '--' }}</text>
<text class="card-tag status-1">焊口 {{ item.WeldJointCount ?? 0 }}</text> <text class="card-tag status-1">焊口 {{ formatJointCount(item) }}</text>
</view> </view>
<view class="card-body"> <view class="card-body">
<view class="card-row"> <view class="card-row">
@@ -55,38 +55,35 @@
<script setup> <script setup>
/** /**
* 焊接任务单列表:自动带入登录人 teamGroupId / unitWorkId + 当前项目查询 * 焊接任务单列表:自动带入登录人 teamGroupId / unitWorkId + 当前项目查询
* 使用 Promise 链而非 async/await,规避微信小程序 Babel regenerator 的 "a" is read-only
*/ */
import { ref } from 'vue' import { ref } from 'vue'
import { onShow } from '@dcloudio/uni-app' import { onShow } from '@dcloudio/uni-app'
import { storeToRefs } from 'pinia'
import { useUserStore } from '@/store' import { useUserStore } from '@/store'
import { reqWeldTaskListByTeamGroup } from '@/api/hj.js' import { reqWeldTaskListByTeamGroup } from '@/api/hj.js'
const userStore = useUserStore() const userStore = useUserStore()
const { currentProject, userInfo } = storeToRefs(userStore)
const listData = ref([]) const listData = ref([])
const loading = ref(false) const loading = ref(false)
const refreshing = ref(false) const refreshing = ref(false)
/**
* 登录人班组 ID
* @param {object|null} user
*/
const resolveTeamGroupId = (user) => { const resolveTeamGroupId = (user) => {
if (!user) return '' if (!user) return ''
return user.teamGroupId || user.TeamGroupId || '' return user.teamGroupId || user.TeamGroupId || ''
} }
/**
* 登录人单位工程 ID
* @param {object|null} user
*/
const resolveUnitWorkId = (user) => { const resolveUnitWorkId = (user) => {
if (!user) return '' if (!user) return ''
return user.unitWorkId || user.UnitWorkId || '' return user.unitWorkId || user.UnitWorkId || ''
} }
const formatJointCount = (item) => {
if (!item) return 0
if (item.WeldJointCount == null) return 0
return item.WeldJointCount
}
const fmtDate = (val) => { const fmtDate = (val) => {
if (!val) return '--' if (!val) return '--'
const d = new Date(val) const d = new Date(val)
@@ -94,34 +91,40 @@ const fmtDate = (val) => {
const y = d.getFullYear() const y = d.getFullYear()
const m = String(d.getMonth() + 1).padStart(2, '0') const m = String(d.getMonth() + 1).padStart(2, '0')
const day = String(d.getDate()).padStart(2, '0') const day = String(d.getDate()).padStart(2, '0')
return `${y}-${m}-${day}` return y + '-' + m + '-' + day
} }
const fetchList = async () => { const fetchList = () => {
const projectId = currentProject.value?.ProjectId const project = userStore.currentProject
const projectId = project ? project.ProjectId : ''
if (!projectId) { if (!projectId) {
uni.showToast({ title: '请先选择项目', icon: 'none' }) uni.showToast({ title: '请先选择项目', icon: 'none' })
listData.value = [] listData.value = []
refreshing.value = false
return return
} }
if (loading.value) return if (loading.value) return
loading.value = true loading.value = true
try { const user = userStore.userInfo
const res = await reqWeldTaskListByTeamGroup({ const teamGroupId = resolveTeamGroupId(user)
projectId, const unitWorkId = resolveUnitWorkId(user)
teamGroupId: resolveTeamGroupId(userInfo.value) || undefined, const query = {
unitWorkId: resolveUnitWorkId(userInfo.value) || undefined, projectId: projectId,
}) teamGroupId: teamGroupId || '',
const data = res?.data unitWorkId: unitWorkId || '',
listData.value = Array.isArray(data) ? data : [] }
} catch (e) {
reqWeldTaskListByTeamGroup(query).then(function (res) {
const data = res ? res.data : null
listData.value = Array.isArray(data) ? data.slice() : []
}).catch(function (e) {
console.error('任务单列表加载失败:', e) console.error('任务单列表加载失败:', e)
listData.value = [] listData.value = []
} finally { }).then(function () {
loading.value = false loading.value = false
refreshing.value = false refreshing.value = false
} })
} }
const handleRefresh = () => { const handleRefresh = () => {
@@ -130,21 +133,22 @@ const handleRefresh = () => {
} }
const handleItemClick = (item) => { const handleItemClick = (item) => {
const projectId = item.ProjectId || currentProject.value?.ProjectId || '' const project = userStore.currentProject
const taskCode = item.TaskCode || '' const projectId = (item && item.ProjectId) || (project && project.ProjectId) || ''
const unitWorkId = item.UnitWorkId || item.unitWorkId || resolveUnitWorkId(userInfo.value) || '' const taskCode = (item && item.TaskCode) || ''
const unitWorkId = (item && (item.UnitWorkId || item.unitWorkId)) || resolveUnitWorkId(userStore.userInfo) || ''
if (!projectId || !taskCode) { if (!projectId || !taskCode) {
uni.showToast({ title: '任务单信息不完整', icon: 'none' }) uni.showToast({ title: '任务单信息不完整', icon: 'none' })
return return
} }
let url = `/scanpages/hj/weld_task_detail?projectId=${encodeURIComponent(projectId)}&taskCode=${encodeURIComponent(taskCode)}` let url = '/scanpages/hj/weld_task_detail?projectId=' + encodeURIComponent(projectId) + '&taskCode=' + encodeURIComponent(taskCode)
if (unitWorkId) { if (unitWorkId) {
url += `&unitWorkId=${encodeURIComponent(unitWorkId)}` url = url + '&unitWorkId=' + encodeURIComponent(unitWorkId)
} }
uni.navigateTo({ url }) uni.navigateTo({ url: url })
} }
onShow(() => { onShow(function () {
fetchList() fetchList()
}) })
</script> </script>
+20 -19
View File
@@ -28,14 +28,13 @@ function toQueryString(params) {
* {boolean} loading - 是否显示 loading,默认 true * {boolean} loading - 是否显示 loading,默认 true
* {boolean} toast - 是否在失败时弹 toast,默认 true * {boolean} toast - 是否在失败时弹 toast,默认 true
*/ */
export function request(url, options = {}) { export function request(url, options) {
const { const opts = options || {}
method = 'GET', const method = opts.method || 'GET'
data, const data = opts.data
header = {}, const header = opts.header || {}
loading = true, const loading = opts.loading !== false
toast = true const toast = opts.toast !== false
} = options
const userStore = useUserStore() const userStore = useUserStore()
const headers = { const headers = {
@@ -47,22 +46,22 @@ export function request(url, options = {}) {
} }
// GET: 手动拼 query string(不依赖 uni.request 各平台差异) // GET: 手动拼 query string(不依赖 uni.request 各平台差异)
let fullUrl = `${baseUrl}${url}` let fullUrl = baseUrl + url
if (method === 'GET' && data) { if (method === 'GET' && data) {
fullUrl += toQueryString(data) fullUrl = fullUrl + toQueryString(data)
} }
if (loading) { if (loading) {
uni.showLoading({ title: '加载中...', mask: true }) uni.showLoading({ title: '加载中...', mask: true })
} }
return new Promise((resolve, reject) => { return new Promise(function (resolve, reject) {
uni.request({ uni.request({
url: fullUrl, url: fullUrl,
method, method: method,
data: method === 'POST' ? data : undefined, data: method === 'POST' ? data : undefined,
header: headers, header: headers,
success: (res) => { success: function (res) {
if (loading) uni.hideLoading() if (loading) uni.hideLoading()
const body = res.data const body = res.data
if (body.code !== 1) { if (body.code !== 1) {
@@ -81,7 +80,7 @@ export function request(url, options = {}) {
} }
resolve(body) resolve(body)
}, },
fail: (err) => { fail: function (err) {
if (loading) uni.hideLoading() if (loading) uni.hideLoading()
uni.showToast({ title: '网络连接失败', icon: 'none' }) uni.showToast({ title: '网络连接失败', icon: 'none' })
reject(err) reject(err)
@@ -95,18 +94,20 @@ export function request(url, options = {}) {
* 兼容两种调用方式: * 兼容两种调用方式:
* get('/path?key=val') 旧风格(URL 自带参数) * get('/path?key=val') 旧风格(URL 自带参数)
* get('/path', { key: 'val' }) 新风格(参数对象,自动编码) * get('/path', { key: 'val' }) 新风格(参数对象,自动编码)
* 不可对形参 url 做 += 重赋值,正式版 Babel 会把形参编译为只读,触发 "a" is read-only
*/ */
export const get = (url, params) => { export function get(url, params) {
const fullUrl = params ? `${url}${toQueryString(params)}` : url let fullUrl = url
if (params) {
fullUrl = url + toQueryString(params)
}
return request(fullUrl, { method: 'GET' }) return request(fullUrl, { method: 'GET' })
} }
/** /**
* POST 快捷方法 * POST 快捷方法
*/ */
export const post = (url, data) => { export function post(url, data) {
return request(url, { method: 'POST', data }) return request(url, { method: 'POST', data: data })
} }
/** /**