<script>
export default {
globalData: {
width: 300,
max_time : 1200,
is_socket:false,
socketTask : null,
xt_id : null,
socket_id : null,
xt_socket_id : null,
showLoading : false
},
onLaunch: function() {
try{
let machine_number = uni.getStorageSync('machine_number');
if(!machine_number){
let Os = plus.android.importClass('android.os.SystemProperties');
let os = new Os();
machine_number = plus.android.invoke(os, 'get', 'ro.serialno');
uni.setStorageSync('machine_number', machine_number);
plus.android.autoCollection(os);
}
if(!this.globalData.socketTask){
this.websoker();
}
let system = uni.getSystemInfoSync();
this.globalData.width = system.screenWidth;
if(system.brand && system.brand=='Allwinner'){
const serialPort = uni.requireNativePlugin('Fvv-UniSerialPort')
serialPort.setPath('/dev/ttyS4')
serialPort.setBaudRate(9600)
serialPort.setParity(0)
serialPort.setStopBits(1)
let isOpen = serialPort.isOpen()
if(!isOpen){
serialPort.open(res => {
if(res.status){
serialPort.onMessage(rec=>{
if(rec[0] != -86){
let card_arr = [rec[13],rec[14],rec[15],rec[16]]
let card = this.getCardNumber(card_arr)
uni.$emit('update',{type:99,card:card})
}
});
}else{
console.log(res)
}
})
}
}else{
let main = plus.android.runtimeMainActivity();
let Context = plus.android.importClass("android.content.Context");
let mStatusBarManager = main.getSystemService(Context.STATUS_BAR_SERVICE);
let StatusBarManager = plus.android.importClass(mStatusBarManager);
mStatusBarManager.disable(StatusBarManager.DISABLE_EXPAND);
plus.android.autoCollection(mStatusBarManager);
let _this = this;
let Intent = plus.android.importClass('android.content.Intent');
let PendingIntent = plus.android.importClass('android.app.PendingIntent');
let IntentFilter = plus.android.importClass('android.content.IntentFilter');
let NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
let nfcAdapter = NfcAdapter.getDefaultAdapter(main);
if(nfcAdapter){
let intent = new Intent(main, main.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
let pendingIntent = PendingIntent.getActivity(main, 0, intent, 0);
let ndef = new IntentFilter("android.nfc.action.TECH_DISCOVERED");
ndef.addDataType("*/*");
let intentFiltersArray = [ndef];
let techListsArray = [["android.nfc.tech.IsoDep"], ["android.nfc.tech.NfcA"], ["android.nfc.tech.NfcB"], ["android.nfc.tech.NfcF"], ["android.nfc.tech.Nfcf"], ["android.nfc.tech.NfcV"], ["android.nfc.tech.NdefFormatable"], ["android.nfc.tech.MifareClassi"], ["android.nfc.tech.MifareUltralight"]];
plus.globalEvent.addEventListener("newintent",
function() {
setTimeout(_this.handle_nfc_data,100);
}, false);
plus.globalEvent.addEventListener("pause", function(e) {
if (nfcAdapter) {
nfcAdapter.disableForegroundDispatch(main);
}
}, false);
plus.globalEvent.addEventListener("resume", function(e) {
if (nfcAdapter) {
nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
}
}, false);
plus.android.autoCollection(intent);
plus.android.autoCollection(ndef);
}
}
}catch(e){
console.log(e)
let machine_number = uni.getStorageSync('machine_number')
let msg = machine_number + '错误:' + e;
this.$api.post('/v1/log/add',{msg:msg}).then((res) => {
}).catch((err) => {
uni.showToast({
title: '错误日志记录失败',
icon : 'none',
mask: true,
duration: 3000
});
})
}
},
onShow: function() {
},
onHide: function() {
//let _this = this;
/* if(_this.globalData.socketTask){
_this.globalData.socketTask.close();
_this.globalData.socketTask= null;
_this.globalData.is_socket = false;
}
if(_this.globalData.socket_id){
clearInterval(_this.globalData.socket_id);
_this.globalData.socket_id = null;
}
if(_this.globalData.xt_socket_id){
clearInterval(_this.globalData.xt_socket_id);
_this.globalData.xt_socket_id = null;
} */
},
onError:function(){
let obj = {
a: '全局异常',
}
let content = JSON.stringify(obj);
uni.request({
url: 'https://jwfw.weizyxy.com/api/banpai/savelog', //仅为示例,并非真实接口地址。
method: 'post',
data: {
content
},
success: (res) => {
}
});
},
methods:{
websoker: function() {
let _this = this;
if(_this.globalData.is_socket){
return ;
}
_this.globalData.socketTask = uni.connectSocket({
url: 'ws://118.25.110.228:8282',
//url: 'ws://192.168.0.120:8282',
success(data) {
//console.log('开始连接')
},
complete: () => {}
});
_this.globalData.socketTask.onOpen(function(res) {
//console.log('打开连接')
if(_this.globalData.socket_id){
clearInterval(_this.globalData.socket_id)
_this.globalData.socket_id = null;
}
if(!_this.globalData.xt_socket_id){
_this.globalData.xt_socket_id = setInterval(function(){
_this.globalData.socketTask.send({
data : '1'
})
},40000);
}
})
_this.globalData.socketTask.onMessage(function(res) {
let data = JSON.parse(res.data)
if(data.type == 1){
let machine_number = uni.getStorageSync('machine_number');
if(!machine_number){
let Os = plus.android.importClass('android.os.SystemProperties');
let os = new Os();
machine_number = plus.android.invoke(os, 'get', 'ro.serialno');
uni.setStorageSync('machine_number', machine_number);
plus.android.autoCollection(os);
}
uni.$emit('update',{type:666})
plus.runtime.getProperty(plus.runtime.appid,(wgtinfo)=>{
let msg = {type:1,machine_number:machine_number,client_id:data.data,versionCode:wgtinfo.versionCode};
_this.globalData.socketTask.send({
data : JSON.stringify(msg)
})
})
}else if(data.type == 4){
plus.runtime.restart();
}else if(data.type == 777){
uni.$emit('update',{type:data.type})
uni.$emit('lv_u',{type:data.type})
}else{
uni.$emit('update',{type:data.type})
}
})
_this.globalData.socketTask.onClose(function() {
_this.showLoading({
title : '网络未连接',
mask : true
})
if(!_this.globalData.socket_id){
_this.globalData.socket_id = setInterval(_this.websoker,8000);
}
})
_this.globalData.socketTask.onError(function(errMsg) {
_this.showLoading({
title : '网络未连接',
mask : true
})
if(!_this.globalData.socket_id){
_this.globalData.socket_id = setInterval(_this.websoker,8000);
}
})
},
handle_nfc_data: function () {
let _this = this;
let main = plus.android.runtimeMainActivity();
let runtimeIntent = main.getIntent();
let b = runtimeIntent.getExtras();
plus.android.importClass(b);
let set = b.keySet();
plus.android.importClass(set);
let i = set.iterator();
plus.android.importClass(i);
let hasNest = i.hasNext();
while (hasNest) {
let key = i.next();
let v = b.get(key);
if(key == 'android.nfc.extra.ID'){
let card = _this.getCardNumber(v);
uni.$emit('update',{type:99,card:card})
}
hasNest = i.hasNext();
}
},
getCardNumber:function(bytes){//获取高四位
let byteArr = [
"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"
];
let out = "";
let pos = 0;
let _this = this;
bytes = bytes.reverse();
bytes.forEach(function(v){
pos = (v&0xF0)>>4;
out += byteArr[pos];
pos = v&0x0F;
out+= byteArr[pos];
});
return parseInt(out,2);
},
showLoading : function(object){
if(this.globalData.showLoading == false){
uni.showLoading({
title : '网络未连接',
mask:true
});
this.globalData.showLoading = true;
}
},
hideLoading : function(){
if(this.globalData.showLoading){
uni.hideLoading()
this.globalData.showLoading = false;
}
}
}
};
</script>
<style>
/ 解决头条小程序组件内引入字体不生效的问题 /
/ #ifdef MP-TOUTIAO /
@font-face {
font-family: uniicons;
src: url('/static/uni.ttf');
}
/* #endif */
</style>
1 个回复
3***@qq.com
解决了吗?怎么解决的?