1
0

edit.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <!--
  2. 编辑联系人信息,添加联系人名称、电话号码,并保存。删除联系人。修改备注传“NULL”的情况
  3. 张志宏 调整删除提示框,使用自定义组件
  4. 24.9.6
  5. 完善了删除联系人时若姓名和号码已修改则无法删除的bug,完善了修改联系人信息后是新增联系人且原有信息不会删除的bug
  6. fxl
  7. 24.9.2
  8. 张甜甜
  9. 添加了触发联系人更新事件确保拨号页面联系人匹配列表实时更新。 uni.$emit('contactsUpdated');
  10. 24.9.8
  11. -->
  12. <template style="">
  13. <view class="contact-page">
  14. <view class="contact-form" style="font-size: 50px;">
  15. <view class="name-delete">
  16. <p class="p-contact-name">联系人</p>
  17. <image src="../../static/pics/contact-delete.png" mode="" class="delete-picture" @click="showTipBar()"></image>
  18. </view>
  19. <custom-modal
  20. :visible="showModal"
  21. title="确认删除"
  22. content="你确定要删除这个联系人吗?"
  23. confirmText="删除"
  24. cancelText="取消"
  25. @confirm="deleteContact()"
  26. @cancel="handCancel()"
  27. @update:visible="showModal = $event"
  28. />
  29. <view class="new-group">
  30. <input type="text" v-model="contact.name" placeholder="请输入备注" />
  31. </view>
  32. <view class="phone-list">
  33. <view class="new-group" v-for="(phone, index) in contact.phones" :key="index">
  34. <input type="text" v-model="phone.number" placeholder="请输入手机号" />
  35. <view class="remove-phone" @click="removePhone(index)" v-if="contact.phones.length > 1">
  36. <image src="../../static/pics/de.png" style="height: 100rpx;width: 100rpx;align-items: center;justify-content: center; margin: 5px;"></image>
  37. </view>
  38. </view>
  39. </view>
  40. <view class="add-phone">
  41. <button @click="addPhone" style="font-size: 50rpx;background-color: white;">添加手机号</button>
  42. </view>
  43. <view class="saveOut">
  44. <view class="out">
  45. <button @click="gotoindex()" style="font-size: 50rpx;background-color: white;">退出</button>
  46. </view>
  47. <view class="save-contact">
  48. <button @click="saveContact()" style="background-color: #ecfff3;">保存</button>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. import CustomModal from '@/components/customModal.vue';
  56. export default {
  57. components:{
  58. CustomModal
  59. },
  60. data() {
  61. return {
  62. contact: {
  63. name: '', // 联系人备注
  64. phones: [
  65. { number: '' } // 手机号数组,初始化一个空对象
  66. ]
  67. },
  68. oldContact:{
  69. name: '', // 联系人备注
  70. phones: [
  71. { number: '' } // 手机号数组,初始化一个空对象
  72. ]
  73. },
  74. showModal:false,
  75. };
  76. },
  77. onLoad(options) {
  78. console.error('传输的内容',options);
  79. this.contact.name=decodeURIComponent(options.name);
  80. if(this.contact.name=='null'){
  81. this.contact.name=null
  82. }
  83. this.oldContact.phones=JSON.parse(decodeURIComponent(options.phones));
  84. this.contact.phones=JSON.parse(decodeURIComponent(options.phones));
  85. //保存传入数据,使得删除时可以准确查找
  86. this.oldContact.name=decodeURIComponent(options.name);
  87. if(this.oldContact.name=='null'){
  88. this.oldContact.name=null
  89. }
  90. },
  91. methods: {
  92. //显示删除提示框
  93. showTipBar(){
  94. this.showModal=true;
  95. },
  96. //取消删除
  97. handCancel(){
  98. this.showModal=false
  99. uni.showToast({
  100. title:'已取消删除'
  101. })
  102. },
  103. gotoindex(){
  104. uni.navigateBack({
  105. delta: 2 // 返回上一页
  106. });
  107. },
  108. addPhone() {
  109. // 添加新的手机号输入框
  110. this.contact.phones.push({ number: '' });
  111. },
  112. removePhone(index) {
  113. // 移除指定的手机号输入框
  114. this.contact.phones.splice(index, 1);
  115. },
  116. deleteContact() {
  117. this.showModal = false;
  118. if (plus.os.name !== 'Android') {
  119. uni.showToast({
  120. title: '非Android平台无法访问通讯录',
  121. icon: 'none'
  122. });
  123. return;
  124. }
  125. console.log("old1元数据", this.oldContact);
  126. // 直接执行删除操作
  127. plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, addressbook => {
  128. // 根据联系人名称和电话号码查找联系人
  129. addressbook.find(
  130. ["displayName", "phoneNumbers"],
  131. contacts => {
  132. if (contacts.length > 0) {
  133. let contactFound = false;
  134. // 遍历找到的联系人列表
  135. contacts.forEach(contact => {
  136. console.log("contact", contact);
  137. console.log("old元数据", this.oldContact);
  138. console.log("old号码元数据", this.oldContact.phones[0].number);
  139. // 检查联系人名称是否匹配
  140. if (contact.displayName === this.oldContact.name) {
  141. // 遍历联系人的电话号码,确保至少有一个号码匹配
  142. contact.phoneNumbers.forEach(phone => {
  143. if (phone.value === this.oldContact.phones[0].number||this.oldContact.phones.number==="") { // 使用第一个电话号码进行匹配
  144. contactFound = true;
  145. contact.remove(() => {
  146. uni.showToast({
  147. title: '联系人删除成功'
  148. });
  149. uni.$emit('contactsUpdated');
  150. setTimeout(() => {
  151. uni.navigateBack({
  152. delta: 2 // 返回上两页
  153. });
  154. }, 800);
  155. }, e => {
  156. uni.showToast({
  157. title: '删除联系人失败',
  158. icon: 'none'
  159. });
  160. console.error('删除联系人失败:', e.message);
  161. });
  162. }
  163. });
  164. }
  165. });
  166. if (!contactFound) {
  167. uni.showToast({
  168. title: '未找到匹配的联系人',
  169. icon: 'none'
  170. });
  171. }
  172. } else {
  173. uni.showToast({
  174. title: '未找到联系人',
  175. icon: 'none'
  176. });
  177. }
  178. },
  179. e => {
  180. uni.showToast({
  181. title: '查找联系人失败',
  182. icon: 'none'
  183. });
  184. console.error('查找联系人失败:', e.message);
  185. },
  186. { filter: this.contact.name } // 初步过滤条件为联系人名称
  187. );
  188. }, e => {
  189. uni.showToast({
  190. title: '获取通讯录失败',
  191. icon: 'none'
  192. });
  193. console.error("获取通讯录失败", e.message);
  194. });
  195. },
  196. addDe(){
  197. plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, addressbook => {
  198. // 根据联系人名称和电话号码查找联系人
  199. addressbook.find(
  200. ["displayName", "phoneNumbers"],
  201. contacts => {
  202. if (contacts.length > 0) {
  203. let contactFound = false;
  204. // 遍历找到的联系人列表
  205. contacts.forEach(contact => {
  206. // 检查联系人名称是否匹配
  207. if (contact.displayName === this.oldContact.name) {
  208. // 遍历联系人的电话号码,确保至少有一个号码匹配
  209. contact.phoneNumbers.forEach(phone => {
  210. if (phone.value === this.oldContact.phones[0].number) { // 使用第一个电话号码进行匹配
  211. contactFound = true;
  212. contact.remove();
  213. }
  214. });
  215. }
  216. });
  217. if (!contactFound) {
  218. uni.showToast({
  219. title: '未找到匹配的联系人',
  220. icon: 'none'
  221. });
  222. }
  223. } else {
  224. uni.showToast({
  225. title: '未找到联系人',
  226. icon: 'none'
  227. });
  228. }
  229. },
  230. e => {
  231. uni.showToast({
  232. title: '查找联系人失败',
  233. icon: 'none'
  234. });
  235. console.error('查找联系人失败:', e.message);
  236. },
  237. { filter: this.contact.name } // 初步过滤条件为联系人名称
  238. );
  239. }, e => {
  240. uni.showToast({
  241. title: '获取通讯录失败',
  242. icon: 'none'
  243. });
  244. console.error("获取通讯录失败", e.message);
  245. });
  246. },
  247. saveContact() {
  248. uni.navigateTo({
  249. url: '/pages/index/index' // 返回主页
  250. });
  251. var that = this;
  252. if (this.contact.phones.length === 0) {
  253. uni.showToast({
  254. title: '请填写完整的联系人信息',
  255. icon: 'none'
  256. });
  257. return;
  258. }
  259. // 确保plusready事件触发后再调用API
  260. if (plus.os.name !== 'Android') {
  261. uni.showToast({
  262. title: '非Android平台无法访问通讯录',
  263. icon: 'none'
  264. });
  265. return;
  266. }
  267. this.addDe()
  268. plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, addressbook => {
  269. // 创建新的联系人
  270. const newContact = addressbook.create();
  271. newContact.displayName = this.contact.name;
  272. // 过滤并映射电话号码
  273. const validPhones = this.contact.phones
  274. .filter(phone => phone.number.trim() !== '')
  275. .map(phone => ({
  276. type: "手机",
  277. value: phone.number,
  278. preferred: this.contact.phones.length === 1 || phone.number === this.contact.phones[0].number
  279. }));
  280. // 保存所有有效的电话号码
  281. newContact.phoneNumbers = validPhones;
  282. newContact.save(() => {
  283. uni.showToast({
  284. title: '联系人保存成功'
  285. });
  286. setTimeout(()=>{
  287. uni.navigateBack({
  288. delta: 2 // 返回上一页
  289. });
  290. },800);
  291. }, e => {
  292. uni.showToast({
  293. title: '保存联系人失败',
  294. icon: 'none'
  295. });
  296. console.error('保存联系人失败:', e.message);
  297. });
  298. }, e => {
  299. uni.showToast({
  300. title: '获取通讯录失败',
  301. icon: 'none'
  302. });
  303. console.error('获取通讯录失败:', e.message);
  304. });
  305. },
  306. resetContact() {
  307. // 重置联系人信息
  308. this.newContact = { name: '', phones: [{ number: '' }] };
  309. }
  310. }
  311. }
  312. </script>
  313. <style>
  314. button{
  315. font-size: 50rpx;
  316. border: 0.5px solid #aeaeae;
  317. border-radius: 10px;
  318. }
  319. .contact-page {
  320. padding: 10px 20px 20px 20px;
  321. margin: 0px;
  322. min-height: 100vh;
  323. display: flex;
  324. flex-direction: column;
  325. align-items: center;
  326. background-color: #f0efef;
  327. }
  328. .title{
  329. display: flex;
  330. align-items: center;
  331. justify-content: center;
  332. width: 100vw;
  333. padding-top: 8%;
  334. font-size: 60rpx;
  335. font-weight: 650;
  336. background-color: #fafafa;
  337. box-shadow: 0 2rpx 3rpx 3rpx rgba(248, 248, 248, 0.2);
  338. }
  339. .name-delete{
  340. display: flex;
  341. align-items: center;
  342. justify-content: space-between;
  343. /* margin-top: 2%; */
  344. margin-bottom: 10rpx;
  345. /* background-color: aqua; */
  346. }
  347. .p-contact-name{
  348. margin-left: 4%;
  349. font-size: 80rpx;
  350. color: #aeaeae;
  351. }
  352. .delete-picture{
  353. width: 100rpx;
  354. height: 100rpx;
  355. margin-right: 4%;
  356. }
  357. .contact-form {
  358. margin-top: 10px;
  359. }
  360. .phone-list {
  361. border-bottom: 1px solid #aeaeae;
  362. padding-bottom: 20px;
  363. margin-bottom: 20px;
  364. }
  365. .add-phone {
  366. margin-top: 20px;
  367. }
  368. .add-phone button {
  369. padding: 10px;
  370. font-size: 16px;
  371. margin-bottom: 30rpx;
  372. }
  373. .new-group {
  374. background-color: white;
  375. margin-bottom: 80rpx;
  376. height: 150rpx;
  377. margin-bottom: 10px;
  378. display: flex;
  379. align-items: center;
  380. border: 1px solid #aeaeae;
  381. border-radius: 10px;
  382. }
  383. .new-group input {
  384. /* 根据需要调整输入框大小 */
  385. width: 100%; /* 例如90% */
  386. height: 85%; /* 例如80% */
  387. font-size: 50rpx;
  388. padding: 10px; /* 根据需要调整内边距 */
  389. }
  390. .remove-phone {
  391. height: 100%;
  392. width: 20%;
  393. margin-left: 10px;
  394. color: red;
  395. align-items: center;
  396. justify-content: center;
  397. cursor: pointer;
  398. display: flex;
  399. }
  400. .saveOut{
  401. margin-top: auto;
  402. margin-top: 50rpx;
  403. display: flex;
  404. align-items: flex-end;
  405. justify-content: space-between;
  406. }
  407. .out{
  408. width: 200rpx;
  409. height: 300rpx;
  410. }
  411. .save-contact {
  412. width: 200rpx;
  413. height: 300rpx;
  414. }
  415. </style>