zjh
2 天以前 ba492b6fbc7a6430a932c3d47c5594e0dd176ede
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<template>
  <view class="sn-scan-page">
    <view class="title">SN确认</view>
 
    <view v-if="current === 'true'" class="remark">
      备注:点击"扫码"按钮打开专业扫码界面(免费官方组件)
    </view>
 
    <view class="scan-table">
      <view class="table-header">
        <text class="col name">项目名称</text>
        <text class="col sn">SN码</text>
        <text v-if="current === 'true'" class="col action">操作</text>
      </view>
 
      <view class="table-row" v-for="(item, index) in scanItems" :key="item.id">
        <text class="col name">{{ item.scanItem }}</text>
 
        <!-- 可编辑状态:显示输入框 -->
        <input
          v-if="current === 'true'"
          class="col sn-input"
          type="text"
          v-model="item.snNo"
          placeholder="请输入或扫码"
        />
        
        <!-- 只读状态:显示文本 -->
        <text v-if="current !== 'true'" class="col sn-text">{{ item.snNo }}</text>
 
        <button
          v-if="current === 'true'"
          class="col scan-btn"
          type="primary"
          @click="onScan(index)"
        >扫码</button>
      </view>
    </view>
 
    <view class="action-bar">
      <button v-if="current === 'true'" type="success" @click="submit">保存</button>
    </view>
  </view>
</template>
 
<script>
export default {
  data() {
    return {
      mid: null, // MES主表ID
      scanItems: [], // 从 MES_SJ_SCAN_ITEM_CK 获取
      current: 'true', // 控制是否可编辑
      currentScanIndex: -1, // 当前扫描的索引
    };
  },
 
  onLoad(options) {
    // 调试信息:打印接收到的参数
    console.log('ScanCode页面接收到的参数:', options);
    
    // 假设从上一页传入 mid
    this.mid = options.id || null;
    this.current = options.current || 'true'; // 默认为可编辑状态
    
    // 调试信息:打印mid值
    console.log('mid值:', this.mid);
    console.log('current值:', this.current);
    
    // 临时测试:如果mid为空,使用默认值
    if (!this.mid) {
      console.warn('警告:mid为空,使用测试数据');
      // 取消注释下面一行可以直接显示测试数据
      // this.loadTestData();
      // return;
    }
    
    this.getScanItems();
  },
 
  methods: {
    // 临时测试方法:加载测试数据
    loadTestData() {
      console.log('加载测试数据');
      this.scanItems = [
        { id: 1, scanItem: '产品SN', snNo: '' },
        { id: 2, scanItem: '机架号', snNo: '' },
        { id: 3, scanItem: '电机编号', snNo: '' },
      ];
      uni.showToast({ 
        title: '已加载测试数据', 
        icon: 'success' 
      });
    },
    
    // 从 MES_SJ_SCAN_ITEM_CK 获取数据
    getScanItems() {
      console.log('开始获取SN列表, mid:', this.mid);
      
      if (!this.mid) {
        console.error('mid为空,无法获取列表');
        uni.showToast({ 
          title: "缺少必要参数", 
          icon: "none" 
        });
        return;
      }
 
      uni.showLoading({ title: "加载中..." });
      
      console.log('准备调用接口:/SJ/GetList, 参数:', { mid: this.mid });
      
      this.$post({
        url: "/SJ/GetList",
        data: { mid: this.mid }
      }).then(res => {
        console.log('接口返回结果:', res);
        
        if (res.status === 0) {
          console.log('返回的数据:', res.data);
          
          this.scanItems = res.data.map(x => ({
            id: x.id,
            scanItem: x.scanItem,
            snNo: x.snNo || "",
          }));
          
          console.log('处理后的scanItems:', this.scanItems);
          
          if (this.scanItems.length === 0) {
            uni.showToast({ 
              title: "暂无扫码项目", 
              icon: "none" 
            });
          }
        } else {
          console.error('接口返回错误:', res.message);
          uni.showToast({ 
            title: res.message || "加载失败", 
            icon: "none" 
          });
        }
      }).catch(err => {
        console.error("加载扫码项目出错:", err);
        uni.showToast({ 
          title: "加载异常", 
          icon: "none" 
        });
      }).finally(() => {
        uni.hideLoading();
      });
    },
 
    // 扫码成功回调(由 nvue 扫码页面调用)
    onScanSuccess(scanResult) {
      console.log('扫码结果:', scanResult);
      
      if (this.currentScanIndex >= 0 && this.currentScanIndex < this.scanItems.length) {
        // 更新对应项目的SN码
        this.scanItems[this.currentScanIndex].snNo = scanResult.trim();
        
        uni.showToast({
          title: '扫码成功',
          icon: 'success'
        });
      }
    },
    
    // 打开扫码页面(使用识别率更高的方案)
    onScan(index) {
      this.currentScanIndex = index;
      const itemName = this.scanItems[index].scanItem;
      
      // 跳转到 nvue 扫码页面(使用官方 Barcode 组件,识别率 90%+)
      uni.navigateTo({
        url: `/pages/QC/SJ/BarcodeScan?title=${encodeURIComponent(itemName)}`
      });
    },
 
    // 保存 SN 数据到 MES_SJ_SCAN_ITEM_CK
    submit() {
      this.$post({
        url: "/SJ/SaveSn",
        data: {
          mid: this.mid,
          items: this.scanItems.map(x => ({
            id: x.id,
            snNo: x.snNo,
          })),
        }
      }).then(res => {
        if (res.status === 0) {
          uni.showToast({ 
            title: "保存成功", 
            icon: "success" 
          });
        } else {
          uni.showToast({ 
            title: res.message || "保存失败", 
            icon: "none" 
          });
        }
      }).catch(err => {
        console.error("保存出错:", err);
        uni.showToast({ 
          title: "保存异常", 
          icon: "none" 
        });
      });
    },
  },
};
</script>
 
<style lang="scss" scoped>
.sn-scan-page {
  padding: 20rpx;
  font-size: 28rpx;
  font-family: "Microsoft YaHei";
}
 
.title {
  font-size: 36rpx;
  font-weight: bold;
  margin-bottom: 20rpx;
  text-align: center;
}
 
.remark {
  color: #666;
  font-size: 22rpx;
  margin-bottom: 30rpx;
  background-color: #f9f9f9;
  border-left: 6rpx solid #007aff;
  padding: 16rpx 20rpx;
  border-radius: 8rpx;
  line-height: 1.6;
}
 
.scan-table {
  border: 1rpx solid #ccc;
  border-radius: 10rpx;
  overflow: hidden;
}
 
.table-header {
  display: flex;
  background-color: #f5f5f5;
  font-weight: bold;
  padding: 20rpx 10rpx;
}
 
.table-row {
  display: flex;
  align-items: center;
  border-top: 1rpx solid #eee;
  padding: 20rpx 10rpx;
}
 
.col {
  flex: 1;
  text-align: center;
}
 
.name {
  flex: 1;
}
 
.sn {
  flex: 2;
}
 
.action {
  flex: 1;
}
 
.sn-input {
  flex: 2;
  border: 1rpx solid #ccc;
  border-radius: 6rpx;
  padding: 10rpx;
}
 
.scan-btn {
  flex: 1;
  font-size: 26rpx;
  background-color: #007aff;
  color: #fff;
  border-radius: 6rpx;
  line-height: 60rpx;
  height: 60rpx;
}
 
.scan-btn:active {
  background-color: #005bb5;
}
 
.action-bar {
  margin-top: 40rpx;
  text-align: center;
}
</style>