tel.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. <!--本代码主要是拨号盘的功能用户可以实现按键拨打并且在拨号盘的后方有与用户拨号相匹配的联系人号码点击即可快速拨打。
  2. 内容:
  3. 1.匹配的联系人和通话记录列表,点击即可播出
  4. 2.显示号码输入框和删除按钮
  5. 3.数字键盘和拨打按钮区域
  6. 4.点击切换按钮即可收起数字键盘展示与拨号相匹配的列表。
  7. 更新人:张甜甜
  8. 最后更新时间:2024.9.23
  9. 更新内容:样式-->
  10. <template>
  11. <view class="container">
  12. <!-- 匹配的联系人和通话记录列表 -->
  13. <view v-if="matchedItems.length > 0" class="matched-contacts">
  14. <view
  15. v-for="(item, index) in matchedItems"
  16. :key="index"
  17. class="contact-item"
  18. @touchstart="startPressTimer(item.number)"
  19. @touchend="clearPressTimer"
  20. @click="selectContact(item.number)"
  21. >
  22. <text class="contact-name">{{ item.name }}</text>
  23. <div class="contact-number" v-html="highlightMatch(item.number, displayPhoneNumber)"></div>
  24. </view>
  25. </view>
  26. <!-- 显示号码输入框和删除按钮 -->
  27. <view :class="['display', { 'display-bottom': !showDialPad }]">
  28. <text class="phone-number">
  29. {{ displayPhoneNumber }}
  30. </text>
  31. <image
  32. v-if="phoneNumber.length > 0"
  33. src="../../static/pics/delete.png"
  34. @touchstart="startDelete"
  35. @touchend="stopDelete"
  36. @click="deleteNumber"
  37. class="delete"
  38. ></image>
  39. </view>
  40. <!-- 数字键盘和拨打按钮区域 -->
  41. <view class="dial-pad" :style="{ zIndex: showDialPad ? '3' : '1' }">
  42. <view v-show="showDialPad" class="row" v-for="(row, index) in dialPad" :key="index">
  43. <button v-for="digit in row" :key="digit" @click="appendNumber(digit)" class="digit-button">
  44. {{ digit }}
  45. </button>
  46. </view>
  47. </view>
  48. <!-- 切换按钮和拨打按钮区域 -->
  49. <view class="bottom-buttons">
  50. <!-- 切换按钮 -->
  51. <view class="toggle-button">
  52. <button class="scale" @click="toggleDialPad">
  53. <image v-show="showDialPad" src="../../static/pics/down.png" class="ScaleIcon"></image>
  54. <image v-show="!showDialPad" src="../../static/pics/up.png" class="ScaleIcon"></image>
  55. </button>
  56. </view>
  57. <view class="action">
  58. <!-- 只显示一个拨打按钮 -->
  59. <button class="call-button single-sim" @click="callNumber(phoneNumber)">
  60. <image src="../../static/pics/call.png" class="CallIcon"></image>
  61. </button>
  62. </view>
  63. </view>
  64. </view>
  65. </template>
  66. <script>
  67. import pinyin from 'pinyin';
  68. export default {
  69. data() {
  70. return {
  71. simNumber: 0,
  72. phoneNumber: '', // 输入的电话号码
  73. showDialPad: true, // 控制是否显示数字键盘
  74. dialPad: [
  75. ['1', '2', '3'],
  76. ['4', '5', '6'],
  77. ['7', '8', '9'],
  78. ['*', '0', '#']
  79. ],
  80. contacts: [], // 存储联系人信息
  81. matchedItems: [], // 匹配的联系人和通话记录
  82. telephoneLog: [], // 存储通话记录
  83. pressTimer: null, // 计时器用于长按操作
  84. deleteInterval: null // 计时器用于删除操作
  85. };
  86. },
  87. mounted() {
  88. this.getContacts(); // 组件挂载时获取联系人数据
  89. this.handleGetCallLogs(); // 获取通话记录
  90. },
  91. computed: {
  92. displayPhoneNumber() {
  93. // 根据输入的电话号码长度显示部分号码或省略号
  94. if (this.phoneNumber.length <= 11) {
  95. const part1 = this.phoneNumber.slice(0, 3);
  96. const part2 = this.phoneNumber.slice(3, 7);
  97. const part3 = this.phoneNumber.slice(7, 11);
  98. return `${part1} ${part2} ${part3}`.trim(); // 按 "3 4 4" 格式显示号码
  99. } else {
  100. return `...${this.phoneNumber.slice(-11)}`; // 若号码过长,则显示最后11位,前面用省略号表示
  101. }
  102. }
  103. },
  104. watch: {
  105. phoneNumber(newValue) {
  106. this.matchContactsAndCallLogs(newValue); // 当电话号码变化时调用匹配方法
  107. }
  108. },
  109. onLoad() {
  110. // 初始化联系人列表
  111. this.getContacts();
  112. // 监听联系人更新事件
  113. uni.$on('contactsUpdated', () => {
  114. // 重新加载联系人
  115. this.getContacts();
  116. });
  117. },
  118. onUnload() {
  119. // 页面卸载时移除事件监听
  120. uni.$off('contactsUpdated');
  121. },
  122. methods: {
  123. startPressTimer(number) {
  124. this.pressTimer = setTimeout(() => {
  125. // 执行长按操作,例如显示操作菜单
  126. console.log('长按号码:', number);
  127. // 这里可以添加长按的具体操作
  128. }, 2000); // 设置长按时间
  129. },
  130. clearPressTimer() {
  131. if (this.pressTimer) {
  132. clearTimeout(this.pressTimer);
  133. this.pressTimer = null;
  134. }
  135. },
  136. startDelete(event) {
  137. if (!event) return;
  138. event.stopPropagation();
  139. if (this.phoneNumber.length === 0) {
  140. return;
  141. }
  142. if (this.deleteInterval) {
  143. clearInterval(this.deleteInterval);
  144. }
  145. this.deleteInterval = setInterval(() => {
  146. if (this.phoneNumber.length > 0) {
  147. this.deleteNumber();
  148. } else {
  149. clearInterval(this.deleteInterval);
  150. }
  151. }, 100);
  152. },
  153. stopDelete(event) {
  154. if (event) {
  155. event.stopPropagation();
  156. }
  157. if (this.deleteInterval) {
  158. clearInterval(this.deleteInterval);
  159. this.deleteInterval = null;
  160. }
  161. },
  162. deleteNumber() {
  163. if (this.phoneNumber.length > 0) {
  164. this.phoneNumber = this.phoneNumber.slice(0, -1);
  165. }
  166. if (this.phoneNumber.length === 0) {
  167. this.stopDelete();
  168. }
  169. console.log('Current phoneNumber:', this.phoneNumber);
  170. },
  171. // 高亮匹配的文本部分
  172. highlightMatch(text, query) {
  173. if (!query) return text;
  174. // 格式化后的文本,带有空格,如 '136 2341 3568'
  175. const formattedText = this.formatPhoneNumber(text);
  176. // 移除查询字符串中的空格
  177. const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/\s+/g, '');
  178. // 构建正则表达式,允许匹配时中间有任意数量的空格
  179. const regexQuery = escapedQuery.split('').map(char => `${char}\\s*`).join('');
  180. const regex = new RegExp(`(${regexQuery})`, 'gi');
  181. // 执行替换,保持原始文本的空格并高亮匹配部分
  182. const result = formattedText.replace(regex, (match) => `<span class="highlight">${match}</span>`);
  183. return result;
  184. },
  185. // 获取联系人并格式化
  186. getContacts() {
  187. let self = this;
  188. plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, (addressbook) => {
  189. addressbook.find(['displayName', 'phoneNumbers'], (contacts) => {
  190. self.contacts = []; // 清空当前联系人列表
  191. contacts.forEach((contact) => {
  192. if (contact.phoneNumbers && contact.phoneNumbers.length > 0) {
  193. contact.phoneNumbers.forEach(phone => {
  194. let cleanNumber = phone.value.replace(/[^\d\s]/g, '');
  195. self.contacts.push({
  196. name: contact.displayName,
  197. number: cleanNumber
  198. });
  199. });
  200. }
  201. });
  202. // 确保事件在获取到联系人数据后触发
  203. uni.$emit('contactsUpdated');
  204. }, (e) => {
  205. console.log("获取联系人失败: " + e.message);
  206. });
  207. }, (e) => {
  208. console.log("打开通讯录失败: " + e.message);
  209. });
  210. },
  211. handleGetCallLogs() {
  212. //获取历史记录联系人
  213. if (plus.android) {
  214. try {
  215. const MainActivity = plus.android.runtimeMainActivity();
  216. const CallLogHelper = plus.android.importClass("com.example.mylibrary.CallLogHelper");
  217. if (CallLogHelper) {
  218. const logsJson = CallLogHelper.getCallLogs(MainActivity);
  219. console.log("Raw logsJson:", logsJson);
  220. const logsArray = JSON.parse(logsJson);
  221. console.log("Parsed logsArray:", logsArray);
  222. if (Array.isArray(logsArray)) {
  223. this.telephoneLog = logsArray;
  224. } else {
  225. console.error("解析后的数据不是数组");
  226. }
  227. } else {
  228. console.error("CallLogHelper 类未加载");
  229. }
  230. } catch (e) {
  231. console.error("获取通话记录失败:", e);
  232. }
  233. } else {
  234. console.error("Android 环境未准备好");
  235. }
  236. },
  237. appendNumber(digit) {
  238. if (this.phoneNumber.length >= 14) {
  239. return;
  240. }
  241. this.phoneNumber += digit;
  242. },
  243. getInitials(str) {
  244. const initialsArray = pinyin(str, {
  245. style: pinyin.STYLE_FIRST_LETTER
  246. });
  247. const firstLettersOnly = initialsArray.map(initial => initial[0]);
  248. return firstLettersOnly.join('');
  249. },
  250. // 匹配联系人
  251. matchContacts(query) {
  252. if (!this.contacts || this.contacts.length === 0) {
  253. return [];
  254. }
  255. const cleanQuery = query.replace(/\s+/g, '');
  256. let matchedContacts = [];
  257. this.contacts.forEach(contact => {
  258. const cleanNumber = contact.number.replace(/\s+/g, '');
  259. if (cleanNumber.includes(cleanQuery)) {
  260. matchedContacts.push({
  261. ...contact,
  262. number: cleanNumber
  263. });
  264. } else if (contact.notes) {
  265. contact.notes.forEach(note => {
  266. if (note.includes(cleanQuery)) {
  267. matchedContacts.push({
  268. ...contact,
  269. number: cleanNumber
  270. });
  271. }
  272. });
  273. }
  274. });
  275. // 按备注的第一个字的拼音首字母进行排序
  276. matchedContacts.sort((a, b) => {
  277. const noteA = a.notes ? a.notes[0] : '';
  278. const noteB = b.notes ? b.notes[0] : '';
  279. const initialsA = this.getInitials(noteA);
  280. const initialsB = this.getInitials(noteB);
  281. return initialsA.localeCompare(initialsB);
  282. });
  283. return matchedContacts;
  284. },
  285. // 匹配通话记录
  286. matchCallLogs(query) {
  287. // 如果 this.telephoneLog 未定义或为空,返回空数组
  288. if (!this.telephoneLog || this.telephoneLog.length === 0) {
  289. console.log('call logs is null');
  290. return [];
  291. }
  292. const cleanQuery = query.replace(/\s+/g, '');
  293. return this.telephoneLog.filter(log => {
  294. const cleanNumber = log.number.replace(/\s+/g, '');
  295. return cleanNumber.includes(cleanQuery);
  296. }).map(log => ({
  297. ...log,
  298. number: log.number.replace(/\s+/g, '')
  299. }));
  300. },
  301. // 匹配联系人和通话记录
  302. matchContactsAndCallLogs(query) {
  303. if (query.length === 0) {
  304. this.matchedItems = [];
  305. return;
  306. }
  307. const cleanQuery = query.replace(/\s+/g, '');
  308. const matchedContacts = this.matchContacts(cleanQuery);
  309. const matchedCallLogs = this.matchCallLogs(cleanQuery);
  310. console.log('Query:', cleanQuery);
  311. console.log('Matched Contacts:', matchedContacts);
  312. console.log('Matched Call Logs:', matchedCallLogs);
  313. const uniqueItems = new Map();
  314. // 先插入联系人信息
  315. matchedContacts.forEach(contact => {
  316. uniqueItems.set(contact.number, contact);
  317. });
  318. // 再插入通话记录,只有在号码未被匹配到联系人时才插入
  319. matchedCallLogs.forEach(log => {
  320. if (!uniqueItems.has(log.number)) {
  321. // 如果通话记录中的号码没有匹配到联系人,则标记为未知联系人
  322. uniqueItems.set(log.number, {
  323. name: '陌生号',
  324. number: log.number
  325. });
  326. }
  327. });
  328. // 获取所有条目并进行排序
  329. let items = Array.from(uniqueItems.values());
  330. // 将“陌生号”条目分开
  331. const unknownItems = items.filter(item => item.name === '陌生号');
  332. const knownItems = items.filter(item => item.name !== '陌生号');
  333. // 按拼音首字母排序已知条目
  334. knownItems.sort((a, b) => {
  335. const nameA = a.name || '';
  336. const nameB = b.name || '';
  337. const initialsA = this.getInitials(nameA);
  338. const initialsB = this.getInitials(nameB);
  339. return initialsA.localeCompare(initialsB);
  340. });
  341. // 合并已知条目和“陌生号”条目
  342. this.matchedItems = knownItems.concat(unknownItems);
  343. console.log('Final Matched Items:', this.matchedItems);
  344. },
  345. selectContact(number) {
  346. this.phoneNumber = number;
  347. this.matchedItems = [];
  348. // 显示号码后,延迟执行重置操作
  349. setTimeout(() => {
  350. this.callNumber(number);
  351. }, 500); // 延迟 500 毫秒后拨号并重置
  352. },
  353. callNumber(number) {
  354. if (!number || number.length === 0) {
  355. uni.showToast({
  356. title: '请输入电话号码',
  357. icon: 'none'
  358. });
  359. return;
  360. }
  361. // 在拨号前更新通话记录
  362. this.handleGetCallLogs();
  363. // this.getContacts();
  364. // 拨打电话
  365. plus.device.dial(number, false);
  366. // 拨打电话后重置拨号页面状态
  367. this.resetDialPad();
  368. },
  369. toggleDialPad() {
  370. this.showDialPad = !this.showDialPad;
  371. },
  372. // 重置拨号页面状态的方法
  373. resetDialPad() {
  374. this.phoneNumber = ''; // 清空输入的电话号码
  375. this.matchedItems = []; // 清空匹配的联系人和通话记录
  376. this.showDialPad = true; // 确保数字键盘显示
  377. },
  378. formatPhoneNumber(number) {
  379. const cleanedNumber = number.replace(/[^\d]/g, '');
  380. if (cleanedNumber.length === 11) {
  381. return cleanedNumber.replace(/^(\d{3})(\d{4})(\d{4})$/, '$1 $2 $3');
  382. } else if (cleanedNumber.length > 7) {
  383. return cleanedNumber.replace(/^(\d{3})(\d{4})(\d+)$/, '$1 $2 $3');
  384. } else {
  385. return cleanedNumber;
  386. }
  387. }
  388. }
  389. };
  390. </script>
  391. <style>
  392. .container {
  393. /* 整体容器,使用 flex 布局,垂直排列子元素并居中,容器高度占满整个视口,背景色为浅灰色 */
  394. display: flex;
  395. flex-direction: column;
  396. align-items: center;
  397. justify-content: flex-start;
  398. height: 100vh;
  399. background-color: #f7f7f7;
  400. padding-top: 50px;
  401. overflow: hidden;
  402. }
  403. .display, .display-bottom {
  404. /* 显示号码的输入框,宽度占满父容器,高度 80px,背景色为白色,带有灰色边框 */
  405. width: 100%;
  406. height: 80px;
  407. background-color: white;
  408. border: 1px solid #e0e0e0;
  409. display: flex;
  410. align-items: center;
  411. justify-content: space-between;
  412. padding:0 30px 0 10px; /* 调整左侧 padding,增加右侧 padding */
  413. margin-bottom: 0;
  414. transition: all 0.2s;
  415. position: fixed;
  416. z-index: 3;
  417. }
  418. .display {
  419. /* 控制输入框初始显示位置 */
  420. bottom: 420px;
  421. }
  422. .display-bottom {
  423. /* 切换后,输入框移动到更低位置 */
  424. bottom: 100px;
  425. z-index: 4;
  426. }
  427. .phone-number {
  428. /* 电话号码文本的样式,字体大小为 46px */
  429. font-size: 40px;
  430. transform: translateX(20px);
  431. }
  432. .delete {
  433. /* 删除按钮的样式,指定按钮的宽度和高度 */
  434. height: 100rpx;
  435. width: 80rpx;
  436. margin-left: -10rpx;
  437. }
  438. .dial-pad {
  439. /* 数字键盘区域的样式,宽度占满父容器,背景色为浅灰色,固定定位,覆盖在容器底部 */
  440. width: 100%;
  441. background-color: #f7f7f7;
  442. padding-bottom: 0;
  443. position: fixed;
  444. bottom: 100px;
  445. left: 0;
  446. right: 0;
  447. z-index: 3;
  448. }
  449. .row {
  450. /* 数字键盘每一行的样式,使用 flex 布局,使数字按钮在行内均匀分布 */
  451. display: flex;
  452. justify-content: space-around;
  453. transition: all 0.3s;
  454. }
  455. .digit-button {
  456. /* 数字按钮的样式 */
  457. width: 33%;
  458. height: 80px;
  459. background-color: #ecfff3;
  460. border-radius: 0; /* 去掉圆角 */
  461. color: black;
  462. font-size: 50px;
  463. border: 0px solid #d0dddd; /* 边框更细更浅 */
  464. display: flex;
  465. align-items: center;
  466. justify-content: center;
  467. z-index: 3;
  468. }
  469. .toggle-button {
  470. /* 切换按钮的样式,宽度占 30%,固定在底部,高度为 100px,靠右对齐 */
  471. width: 33.3%;
  472. height: 100px;
  473. display: flex;
  474. justify-content: center;
  475. align-items: center;
  476. transition: all 0.2s;
  477. position: fixed;
  478. bottom: 0px;
  479. right: 0; /* 右对齐 */
  480. z-index: 4;
  481. background-color: #ecfff3;
  482. }
  483. .action {
  484. display: flex;
  485. position: fixed;
  486. bottom: 0;
  487. width: 66.7%; /* 确保整个区域占满屏幕宽度 */
  488. left: 0;
  489. padding: 0;
  490. z-index: 4;
  491. }
  492. .CallIcon {
  493. /* 拨打图标的样式,指定图标的宽度和高度 */
  494. width: 60px;
  495. height: 60px;
  496. z-index: 4;
  497. position: absolute; /* 使用绝对定位使图标在按钮内居中 */
  498. top: 50%; /* 垂直居中 */
  499. left: 50%; /* 水平居中 */
  500. transform: translate(-50%, -50%); /* 将图标移动到按钮的中心位置 */
  501. }
  502. .single-sim {
  503. width: 100%; /* 占满整个 .action 容器的宽度 */
  504. height: 100px; /* 设置按钮高度 */
  505. background-color: #ecfff3; /* 按钮背景颜色 */
  506. color: #A9A9A9;
  507. font-size: 36px;
  508. display: flex;
  509. align-items: center;
  510. justify-content: center;
  511. z-index: 4;
  512. position: relative;
  513. }
  514. .scale {
  515. /* 切换按钮的样式,背景色为浅绿色,内容居中对齐 */
  516. width: 100%;
  517. height: 100px;
  518. display: flex;
  519. background-color: #ecfff3;
  520. align-items: center;
  521. justify-content: center;
  522. }
  523. .ScaleIcon {
  524. /* 切换图标的样式,指定图标的宽度和高度 */
  525. width: 60px;
  526. height: 60px;
  527. }
  528. .matched-contacts {
  529. /* 匹配联系人列表的样式,宽度占满父容器,背景色为白色,带有浅灰色边框,高度占视口的 70%,支持垂直滚动 */
  530. width: 100%;
  531. background-color: #ffffff;
  532. border: 1px solid #ccc;
  533. height: 73vh; /* 设置为视口高度的 70% */
  534. overflow-y: auto;
  535. z-index: 3;
  536. position: fixed; /* 固定定位 */
  537. top: 0px; /* 距离顶部的距离,根据需要调整 */
  538. left: 0; /* 确保在左侧对齐 */
  539. }
  540. .contact-item {
  541. display: flex;
  542. justify-content: space-between; /* 使两个部分分别靠左和靠右 */
  543. align-items: center;
  544. height: 80px;
  545. padding: 10px;
  546. border-bottom: 1px solid #ccc;
  547. }
  548. /* 匹配部分用红色渲染 */
  549. .highlight {
  550. color: red;
  551. font-weight: bold; /* Optional: Make the highlighted text bold */
  552. }
  553. .contact-name {
  554. font-size: 36px;
  555. margin-right: 10px;
  556. max-width: 40%; /* 左侧区域占 40% 宽度 */
  557. white-space: normal;
  558. word-break: break-word;
  559. overflow: hidden;
  560. text-overflow: ellipsis;
  561. display: -webkit-box;
  562. -webkit-box-orient: vertical;
  563. -webkit-line-clamp: 2; /* 限制为两行显示 */
  564. }
  565. /* .contact-name > span {
  566. display: inline-block;
  567. width: 100%;
  568. text-align: center; /* 第二行居中对齐 */
  569. .contact-number {
  570. font-size: 26px;
  571. max-width: 80%;
  572. min-width: 0;
  573. white-space: nowrap; /* 禁止换行 */
  574. overflow: hidden;
  575. text-overflow: ellipsis;
  576. display: inline; /* 确保在一行内 */
  577. }
  578. .contact-number span {
  579. display: inline; /* 确保高亮部分在一行内 */
  580. }
  581. .contact-number .highlight {
  582. color: red; /* 高亮颜色 */
  583. font-weight: bold; /* Optional: Make the highlighted text bold */
  584. }
  585. .contact-number .line-1, .contact-number .line-2 {
  586. display: inline-block;
  587. width: 100%;
  588. text-align: right; /* 保持右对齐 */
  589. overflow: hidden;
  590. text-overflow: ellipsis;
  591. }
  592. </style>