zjh
2025-11-10 6ec5099ef65c0e0dc4b1fae5f69eb70dc54dd2b0
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
<template>
  <view class="sn-scan-page">
    <view class="title">SN 扫码页面</view>
 
    <view class="remark">
      备注:点击"扫码"按钮扫描SN码,再次点击可覆盖上次记录
    </view>
 
    <view class="scan-table">
      <view class="table-header">
        <text class="col name">项目名称</text>
        <text class="col sn">SN码</text>
        <text 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
          class="col sn-input"
          type="text"
          v-model="item.snNo"
          placeholder="请输入或扫码"
        />
 
        <button
          class="col scan-btn"
          type="primary"
          @click="onScan(index)"
        >扫码</button>
      </view>
    </view>
 
    <view class="action-bar">
      <button type="success" @click="submit">保存</button>
    </view>
  </view>
</template>
 
<script>
export default {
  data() {
    return {
      mid: null, // MES主表ID
      scanItems: [], // 从 MES_SJ_SCAN_ITEM_CK 获取
    };
  },
 
  onLoad(options) {
    // 假设从上一页传入 mid
    this.mid = options.id || null;
    this.getScanItems();
  },
 
  methods: {
    // 从 MES_SJ_SCAN_ITEM_CK 获取数据
    getScanItems() {
      if (!this.mid) return;
 
      uni.showLoading({ title: "加载中..." });
      
      this.$post({
        url: "/SJ/GetList",
        data: { mid: this.mid }
      }).then(res => {
        if (res.status === 0) {
          this.scanItems = res.data.map(x => ({
            id: x.id,
            scanItem: x.scanItem,
            snNo: x.snNo || "",
          }));
        } else {
          uni.showToast({ 
            title: res.message || "加载失败", 
            icon: "none" 
          });
        }
      }).catch(err => {
        console.error("加载扫码项目出错:", err);
        uni.showToast({ 
          title: "加载异常", 
          icon: "none" 
        });
      }).finally(() => {
        uni.hideLoading();
      });
    },
 
    // 扫码功能
    onScan(index) {
      const self = this;
 
      // #ifdef MP-WEIXIN
      uni.scanCode({
        success(res) {
          self.scanItems[index].snNo = res.result;
        },
        fail(err) {
          console.log("扫码失败", err);
        },
      });
      // #endif
 
      // #ifdef H5
      uni.showToast({
        title: "请使用扫码枪输入到SN输入框",
        icon: "none",
      });
      // #endif
 
      // #ifdef APP-PLUS
      uni.scanCode({
        success(res) {
          self.scanItems[index].snNo = res.result;
        },
      });
      // #endif
    },
 
    // 保存 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>