addemergencycontact.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <view class="container">
  3. <form @submit="handleSubmit">
  4. <u-input label="姓名" v-model.trim="tempContact.name" placeholder="请输入姓名"></u-input>
  5. <u-input label="关系" v-model.trim="tempContact.relationship" placeholder="请输入关系(如:父亲/母亲)"></u-input>
  6. <u-input label="手机号" v-model.trim="tempContact.phone" placeholder="请输入11位手机号" keyboard-type="number"></u-input>
  7. <button type="primary" class="submit-btn" :disabled="!isFormValid" form-type="submit">
  8. {{ currentType === 'add' ? '确认添加' : '确认修改' }}
  9. </button>
  10. </form>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. data() {
  16. return {
  17. tempContact: {
  18. name: '',
  19. relationship: '',
  20. phone: '',
  21. remark: '',
  22. // userId: null,
  23. },
  24. currentType: 'add',
  25. };
  26. },
  27. onLoad(options) {
  28. this.currentType = options.type || 'add';
  29. if (this.currentType === 'edit') {
  30. const contactId = options.id;
  31. const pages = getCurrentPages();
  32. const prevPage = pages[pages.length - 2]; // 上一个页面
  33. const parentList = prevPage?.data?.personList || [];
  34. const found = parentList.find(item => item.id == contactId);
  35. if (found) {
  36. this.tempContact = { ...found };
  37. } else {
  38. uni.navigateBack(); // 未找到数据,返回
  39. }
  40. }
  41. },
  42. computed: {
  43. isFormValid() {
  44. const { name, relationship, phone } = this.tempContact;
  45. return name && relationship && /^1[3-9]\d{9}$/.test(phone);
  46. }
  47. },
  48. methods: {
  49. async handleSubmit() {
  50. if (!this.isFormValid) return;
  51. const url = this.currentType === 'add'
  52. ? '/app/emergency/insertEmergency'
  53. : '/app/emergency/updateEmergency';
  54. try {
  55. const res = await this.$Request.postT(url, this.tempContact);
  56. if (res.code === 0) {
  57. // 成功后回传联系人信息(带ID)
  58. const contact = {
  59. name: this.tempContact.name,
  60. relationship: this.tempContact.relationship,
  61. phone: this.tempContact.phone,
  62. id: res.data?.id || this.tempContact.id // 以接口返回ID为准
  63. };
  64. uni.$emit('updateContact', {
  65. ...contact,
  66. type: this.currentType
  67. });
  68. uni.showToast({ title: '操作成功', icon: 'success' });
  69. uni.navigateBack();
  70. } else {
  71. uni.showToast({ title: res.msg || '操作失败', icon: 'none' });
  72. }
  73. } catch (err) {
  74. uni.showToast({ title: '网络错误', icon: 'none' });
  75. console.error('提交失败:', err);
  76. }
  77. }
  78. }
  79. };
  80. </script>
  81. <style scoped lang="scss">
  82. .container {
  83. padding: 40rpx 30rpx;
  84. background-color: #fff;
  85. }
  86. .page-title {
  87. font-size: 36rpx;
  88. font-weight: 600;
  89. color: #333;
  90. text-align: center;
  91. margin: 40rpx 0;
  92. }
  93. .upload-avatar {
  94. display: flex;
  95. flex-direction: column;
  96. align-items: center;
  97. margin-bottom: 40rpx;
  98. .avatar-preview {
  99. width: 200rpx;
  100. height: 200rpx;
  101. border-radius: 50%;
  102. overflow: hidden;
  103. position: relative;
  104. image {
  105. width: 100%;
  106. height: 100%;
  107. }
  108. .delete-btn {
  109. position: absolute;
  110. top: -15rpx;
  111. right: -15rpx;
  112. width: 30rpx;
  113. height: 30rpx;
  114. background-color: #ff4949;
  115. color: #fff;
  116. border-radius: 50%;
  117. text-align: center;
  118. line-height: 30rpx;
  119. font-size: 24rpx;
  120. z-index: 1;
  121. }
  122. }
  123. .upload-btn {
  124. display: flex;
  125. flex-direction: column;
  126. align-items: center;
  127. margin-top: 20rpx;
  128. color: #999;
  129. font-size: 28rpx;
  130. image {
  131. width: 120rpx;
  132. height: 120rpx;
  133. margin-bottom: 10rpx;
  134. }
  135. }
  136. }
  137. .submit-btn {
  138. margin-top: 60rpx;
  139. background-color: #00c18a !important;
  140. border-radius: 10rpx;
  141. font-size: 32rpx;
  142. height: 80rpx;
  143. border: none;
  144. }
  145. </style>