var vm = new Vue({ el: '#app', data: function () { return { isLoading: false, XBar: '', // 扫到的条码 Weight: '', // 显示的重量 userInfo: { loginGuid: '', loginAccount: '' } // 如需传 userName } }, mounted() { try { var info = this.GetLoginInfor(); if (info) { this.userInfo.loginGuid = info.loginGuid || ''; this.userInfo.loginAccount = info.loginAccount || ''; } // 初始将焦点放到条码输入 this.$nextTick(() => this.$refs.XBar && this.$refs.XBar.focus()); } catch (e) { // ignore } }, methods: { // 扫码后调用后端获取重量 getWeight() { var that = this; if (!that.XBar || that.XBar.toString().trim().length === 0) { that.$playSound && that.$playSound('error'); that.$toast && that.$toast.fail("条码不能为空"); that.$refs.XBar && that.$refs.XBar.focus(); return; } that.isLoading = true; that.AxiosHttp("post", 'Womdaa/GetWeightByXt_new', { LsBar: that.XBar, userName: that.userInfo.loginAccount || '' }, false) .then(function (res) { var json = res; if (json && json.status == 0) { // 后端返回的重量字段(沿用现有代码习惯) var weight = (json.data && json.data.tbBillList && json.data.tbBillList.weight) || ''; that.Weight = weight; that.$playSound && that.$playSound('success'); that.$notify && that.$notify({ type: 'success', message: "称重成功" }); } else { that.Weight = ''; that.$playSound && that.$playSound('error'); var msg = (json && json.message) ? json.message : "称重失败"; that.$dialog && that.$dialog.alert({ message: msg, theme: 'round-button' }); } that.isLoading = false; // 清空并聚焦准备下一个扫码 that.XBar = ''; that.$nextTick(() => that.$refs.XBar && that.$refs.XBar.focus()); }) .catch(function () { that.isLoading = false; that.Weight = ''; that.$playSound && that.$playSound('error'); that.$dialog && that.$dialog.alert({ message: "网络错误,请重试!", theme: 'round-button' }); that.XBar = ''; that.$nextTick(() => that.$refs.XBar && that.$refs.XBar.focus()); }); } } });