使用uniapp获取手机通讯录和短信

<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{ title }}</text>
		</view>
		<view class="contact"><button type="default" @click="getContact">通讯录</button></view>
		<!-- <view class="contact-list" v-for="(item, index) of contactList" :key="index">
			<view class="item">
				<view class="phone">
					<text>姓名:{{ item.displayName }}</text>
					<view class="phone-number">
						<view>
							<text>电话:</text>
							<view v-for="(sitem, sindex) of item.phoneNumbers" :key="sindex">
								<text>{{ sitem.value }}</text>
							</view>
						</view>
						<view>
							<text>emails:</text>
							<view v-for="(sitem, sindex) of item.emails" :key="sindex">
								<text>{{ sitem.value }}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view> -->
		<view class="message">
			<button class="get-message" @click="getMessage">获取短信</button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			title: 'Hello',
			contactList: []
		};
	},
	onShow() {
		this.getContact();
		this.getMessage()
	},
	methods: {
		getContact() {
			console.log('getContact');
			// 定义获取联系人的类型
			let type = plus.contacts.ADDRESSBOOK_PHONE;//当前是手机联系人
			plus.contacts.getAddressBook(
				type,
				success => {
					// 获取通讯录成功
					success.find(
					
						[],//[]或者"",表示全部获取
						res => {
							console.log(json.stringify(res));
							this.contactList = res;
						},
						err => {
							console.log(err);
						}
					);
				},
				error => {
					console.log(error);
				}
			);
		},
		getMessage() {
		//这分代码来自于https://www.cnblogs.com/jucheng/p/12176346.html
		//不是原创哈~~~~~
			console.log('getmessage');
			//不同机型可能获取不全
			//查看权限是否已允许
			var Context = plus.android.runtimeMainActivity();
			var res = plus.android.invoke('android.support.v4.app.ActivityCompat', 'checkSelfPermission', Context, 'android.permission.READ_SMS');
			var PERMISSIONS_STORAGE = new Array();
			PERMISSIONS_STORAGE.push('android.permission.READ_SMS');
			// res == -1 时为询问状态,询问时会走Show 和 Hidden
			if (res != '0') {
				plus.android.invoke('android.support.v4.app.ActivityCompat', 'requestPermissions', Context, PERMISSIONS_STORAGE, 1);
			} else {
				var main = plus.android.runtimeMainActivity();
				var Uri = plus.android.importClass('android.net.Uri');
				var ContactsContract = plus.android.importClass('android.provider.ContactsContract');
				var uri = Uri.parse('content://sms/');
				var cr = main.getContentResolver();
				plus.android.importClass(cr);
				var cur = cr.query(uri, null, null, null, null);
				plus.android.importClass(cur);
				cur.moveToFirst();
				while (cur.moveToNext()) {
					var index_Address = cur.getColumnIndex('address');
					var address = cur.getString(index_Address);
					//短信内容
					var index_Body = cur.getColumnIndex('body');
					var body = cur.getString(index_Body);
					//类型1接收 2发送
					var index_Type = cur.getColumnIndex('type');
					var type = cur.getString(index_Type);
					console.log(address, body, type);
					// 收件人
					// 短信内容
					// 短信的类型
				}
				cur.close();
			}
		}
	}
};
</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;
}
</style>

你可能感兴趣的:(工作笔记,javascript,uniapp,html5)