1
0

add.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <!--
  2. 添加联系人
  3. fxl 24.08.14
  4. 美化界面
  5. 张甜甜
  6. 添加了触发联系人更新事件确保拨号页面联系人匹配列表实时更新。 uni.$emit('contactsUpdated');
  7. 24.9.8
  8. -->
  9. <template style="">
  10. <view class="contact-page">
  11. <view class="contact-form" style="font-size: 50px;">
  12. <view class="new-group">
  13. <input type="text" v-model="contact.name" placeholder="请输入姓名" />
  14. </view>
  15. <view class="phone-list">
  16. <view class="new-group" v-for="(phone, index) in contact.phones" :key="index">
  17. <input type="number" v-model="phone.number" placeholder="请输入手机号"
  18. pattern="[0-9]*"
  19. @input="preventNonNumeric(index, $event)"/>
  20. <view class="remove-phone" @click="removePhone(index)" v-if="contact.phones.length > 1">
  21. <image src="../../static/pics/de.png" style="height: 100rpx;width: 100rpx;align-items: center;justify-content: center; margin-right: 13px;"></image>
  22. </view>
  23. </view>
  24. </view>
  25. <view class="add-phone">
  26. <button @click="addPhone" style="font-size: 50rpx;background-color: white;">添加手机号</button>
  27. </view>
  28. <view class="saveOut">
  29. <view class="out">
  30. <button @click="gotoindex()" style="font-size: 50rpx;background-color: white;">退出</button>
  31. </view>
  32. <view class="save-contact">
  33. <button @click="saveContact()" style="background-color: #ecfff3;">保存</button>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import Hint from '@/components/hint.vue'
  41. export default {
  42. data() {
  43. return {
  44. contact: {
  45. name: '', // 联系人备注
  46. phones: [
  47. { number: '' } // 手机号数组,初始化一个空对象
  48. ]
  49. }
  50. };
  51. },
  52. components: {
  53. Hint
  54. },
  55. methods: {
  56. showHint() {
  57. const toast = this.$refs.Hint
  58. toast.title = '请填写完整的联系人信息';
  59. toast.icon = 'none';
  60. toast.show();
  61. },
  62. preventNonNumeric(index, event) {
  63. const value = event.detail.value;
  64. // 保留数字,不允许非数字字符
  65. const numericValue = value.replace(/\D/g, '');
  66. if (numericValue !== value) {
  67. // 如果用户输入了非数字字符,直接阻止更新输入框的值
  68. this.contact.phones[index].number = numericValue;
  69. }
  70. },
  71. addPhone() {
  72. // 添加新的手机号输入框
  73. this.contact.phones.push({ number: '' });
  74. },
  75. removePhone(index) {
  76. // 移除指定的手机号输入框
  77. this.contact.phones.splice(index, 1);
  78. },
  79. saveContact() {
  80. uni.navigateTo({
  81. url: '/pages/index/index' // 返回主页
  82. });
  83. var that = this;
  84. if (that.contact.phones[0].number.length === 0) {
  85. this.showHint()
  86. return;
  87. }
  88. // 确保plusready事件触发后再调用API
  89. if (plus.os.name !== 'Android') {
  90. uni.showToast({
  91. title: '非Android平台无法访问通讯录',
  92. icon: 'none'
  93. });
  94. return;
  95. }
  96. plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, addressbook => {
  97. // 创建新的联系人
  98. const newContact = addressbook.create();
  99. newContact.displayName = this.contact.name;
  100. // 过滤并映射电话号码
  101. const validPhones = this.contact.phones
  102. .filter(phone => phone.number.trim() !== '')
  103. .map(phone => ({
  104. type: "手机",
  105. value: phone.number,
  106. preferred: this.contact.phones.length === 1 || phone.number === this.contact.phones[0].number
  107. }));
  108. // 保存所有有效的电话号码
  109. newContact.phoneNumbers = validPhones;
  110. newContact.save(() => {
  111. uni.showToast({
  112. title: '联系人保存成功'
  113. });
  114. // 触发事件,通知拨号页面刷新联系人
  115. uni.$emit('contactsUpdated');
  116. setTimeout(()=>{
  117. uni.navigateBack({
  118. delta: 1 // 返回上一页
  119. });
  120. },800);
  121. }, e => {
  122. uni.showToast({
  123. title: '保存联系人失败',
  124. icon: 'none'
  125. });
  126. console.error('保存联系人失败:', e.message);
  127. });
  128. }, e => {
  129. uni.showToast({
  130. title: '获取通讯录失败',
  131. icon: 'none'
  132. });
  133. console.error('获取通讯录失败:', e.message);
  134. });
  135. },
  136. resetContact() {
  137. // 重置联系人信息
  138. this.newContact = { name: '', phones: [{ number: '' }] };
  139. }
  140. }
  141. };
  142. </script>
  143. <style>
  144. button{
  145. font-size: 50rpx;
  146. border: 0.5px solid #aeaeae;
  147. border-radius: 10px;
  148. }
  149. .contact-page {
  150. padding: 20px;
  151. margin: 0px;
  152. min-height: 100vh;
  153. display: flex;
  154. flex-direction: column;
  155. align-items: center;
  156. background-color: #f0efef;
  157. }
  158. .contact-form {
  159. margin-top: 20px;
  160. }
  161. .phone-list {
  162. border-bottom: 1px solid #aeaeae;
  163. padding-bottom: 20px;
  164. margin-bottom: 20px;
  165. }
  166. .add-phone {
  167. margin-top: 20px;
  168. }
  169. .add-phone button {
  170. padding: 10px;
  171. font-size: 16px;
  172. margin-bottom: 30rpx;
  173. }
  174. .new-group {
  175. background-color: white;
  176. margin-bottom: 80rpx;
  177. height: 150rpx;
  178. margin-bottom: 10px;
  179. display: flex;
  180. align-items: center;
  181. border: 1px solid #aeaeae;
  182. border-radius: 10px;
  183. }
  184. .new-group input {
  185. /* 根据需要调整输入框大小 */
  186. width: 100%; /* 例如90% */
  187. height: 85%; /* 例如80% */
  188. font-size: 50rpx;
  189. padding: 10px; /* 根据需要调整内边距 */
  190. }
  191. .remove-phone {
  192. height: 100%;
  193. width: 20%;
  194. margin-left: 10px;
  195. color: red;
  196. align-items: center;
  197. justify-content: center;
  198. cursor: pointer;
  199. display: flex;
  200. }
  201. .saveOut{
  202. margin-top: auto;
  203. margin-top: 50rpx;
  204. display: flex;
  205. align-items: flex-end;
  206. justify-content: space-between;
  207. }
  208. .out{
  209. width: 200rpx;
  210. height: 300rpx;
  211. }
  212. .save-contact {
  213. width: 200rpx;
  214. height: 300rpx;
  215. }
  216. </style>