ContactCard.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <template>
  2. <view class="contact-card-root">
  3. <view class="contact-card" @click="$emit('click', contact)">
  4. <view class="card-avatar" :style="{ background: avatarColor }">
  5. <text class="avatar-text">{{ initial }}</text>
  6. </view>
  7. <text class="card-name">{{ contact.name }}</text>
  8. <view class="card-type-tag" :style="{ background: typeColor + '20', color: typeColor }">
  9. {{ typeLabel }}
  10. </view>
  11. <text class="card-known" v-if="contact.knownSince">
  12. 认识 {{ knownYears }} 年
  13. </text>
  14. <text class="card-phone" v-if="contact.phone">{{ contact.phone }}</text>
  15. <view class="card-intimacy">
  16. <text class="intimacy-heart">❤️</text>
  17. <text class="intimacy-value">{{ contact.intimacyLevel }}</text>
  18. </view>
  19. <text class="card-birthday" v-if="contact.birthdayCountdown">
  20. 🎂 {{ contact.birthdayLabel }} · {{ contact.birthdayCountdown }}
  21. </text>
  22. <view class="card-family-status" v-if="contact.familyMemberId">
  23. <text class="family-badge">已是家庭成员</text>
  24. </view>
  25. <view class="card-invite" v-else @click.stop="handleInvite">
  26. <text class="invite-text">邀请加入家庭</text>
  27. </view>
  28. </view>
  29. <view class="invite-modal" v-if="showInviteModal" @click.stop>
  30. <view class="modal-mask" @click="showInviteModal = false"></view>
  31. <view class="modal-content">
  32. <text class="modal-title">邀请加入家庭</text>
  33. <view class="form-row">
  34. <text class="form-label">关系类型</text>
  35. <picker :range="relationshipTypeList" range-key="typeName" @change="onRelationshipChange">
  36. <text class="form-value">{{ selectedRelationshipName || '请选择' }}</text>
  37. </picker>
  38. </view>
  39. <view class="form-row">
  40. <text class="form-label">辈分等级</text>
  41. <picker :range="generationLevelList" @change="onGenerationChange">
  42. <text class="form-value">{{ selectedGeneration || '请选择' }}</text>
  43. </picker>
  44. </view>
  45. <view class="modal-buttons">
  46. <view class="modal-btn cancel" @click="showInviteModal = false">取消</view>
  47. <view class="modal-btn confirm" @click="confirmInvite">确认邀请</view>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. import config from '@/config.js'
  55. export default {
  56. props: {
  57. contact: { type: Object, required: true }
  58. },
  59. data: function() {
  60. return {
  61. showInviteModal: false,
  62. relationshipTypeList: [
  63. { typeName: '家人', typeKey: 'family' },
  64. { typeName: '朋友', typeKey: 'friend' },
  65. { typeName: '伴侣', typeKey: 'partner' },
  66. { typeName: '同事', typeKey: 'colleague' },
  67. { typeName: '其他', typeKey: 'other' }
  68. ],
  69. selectedRelationshipName: '',
  70. selectedRelationshipKey: '',
  71. generationLevelList: ['父母(+1)', '子女(-1)', '配偶(0)', '兄弟姐妹(0)', '祖父母(+2)', '孙子女(-2)'],
  72. selectedGeneration: ''
  73. }
  74. },
  75. computed: {
  76. initial: function() {
  77. return this.contact.name ? this.contact.name[0] : '?'
  78. },
  79. typeLabel: function() {
  80. var map = { family: '家人', friend: '朋友', partner: '伴侣', colleague: '同事', other: '其他' }
  81. return map[this.contact.relationshipType] || '其他'
  82. },
  83. typeColor: function() {
  84. var map = { family: '#10B981', friend: '#F59E0B', partner: '#FF6B35', colleague: '#3B82F6', other: '#94A3B8' }
  85. return map[this.contact.relationshipType] || '#94A3B8'
  86. },
  87. avatarColor: function() {
  88. var colors = ['#FF6B35', '#667eea', '#10B981', '#F59E0B', '#8B5CF6', '#EC4899']
  89. var idx = (this.contact.id || 0) % colors.length
  90. return colors[idx]
  91. },
  92. knownYears: function() {
  93. if (!this.contact.knownSince) return 0
  94. var now = new Date()
  95. var known = new Date(this.contact.knownSince)
  96. return Math.floor((now - known) / (365.25 * 86400000))
  97. }
  98. },
  99. methods: {
  100. handleInvite: function() {
  101. this.showInviteModal = true
  102. // 预填联系人已有的关系类型
  103. var self = this
  104. this.selectedRelationshipKey = this.contact.relationshipType || 'other'
  105. for (var i = 0; i < this.relationshipTypeList.length; i++) {
  106. if (this.relationshipTypeList[i].typeKey === this.selectedRelationshipKey) {
  107. this.selectedRelationshipName = this.relationshipTypeList[i].typeName
  108. break
  109. }
  110. }
  111. },
  112. onRelationshipChange: function(e) {
  113. var idx = e.detail.value
  114. var item = this.relationshipTypeList[idx]
  115. if (item) {
  116. this.selectedRelationshipName = item.typeName
  117. this.selectedRelationshipKey = item.typeKey
  118. }
  119. },
  120. onGenerationChange: function(e) {
  121. this.selectedGeneration = this.generationLevelList[e.detail.value]
  122. },
  123. confirmInvite: function() {
  124. if (!this.selectedRelationshipName) {
  125. uni.showToast({ title: '请选择关系类型', icon: 'none' })
  126. return
  127. }
  128. var that = this
  129. var generationMap = {
  130. '父母(+1)': 'parent',
  131. '子女(-1)': 'child',
  132. '配偶(0)': 'spouse',
  133. '兄弟姐妹(0)': 'sibling',
  134. '祖父母(+2)': 'grandparent',
  135. '孙子女(-2)': 'grandchild'
  136. }
  137. var generationLevel = generationMap[this.selectedGeneration] || 'child'
  138. uni.request({
  139. url: config.API_BASE_URL + '/api/contact/invite-to-family',
  140. method: 'POST',
  141. header: { 'Authorization': 'Bearer ' + uni.getStorageSync('token') },
  142. data: {
  143. contactId: this.contact.id,
  144. relationshipType: this.selectedRelationshipKey || 'other',
  145. generationLevel: generationLevel
  146. },
  147. success: function(res) {
  148. if (res.data && res.data.code === 200) {
  149. uni.showToast({ title: '邀请成功', icon: 'success' })
  150. that.showInviteModal = false
  151. that.$emit('invited', that.contact.id)
  152. } else {
  153. uni.showToast({ title: res.data && res.data.message || '邀请失败', icon: 'none' })
  154. }
  155. },
  156. fail: function() {
  157. uni.showToast({ title: '网络错误', icon: 'none' })
  158. }
  159. })
  160. }
  161. }
  162. }
  163. </script>
  164. <style scoped>
  165. .contact-card {
  166. width: 240rpx;
  167. flex-shrink: 0;
  168. background: #fff;
  169. border-radius: 20rpx;
  170. padding: 24rpx;
  171. margin-right: 16rpx;
  172. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  173. display: flex;
  174. flex-direction: column;
  175. align-items: center;
  176. }
  177. .contact-card:active { opacity: 0.7; }
  178. .card-avatar {
  179. width: 80rpx;
  180. height: 80rpx;
  181. border-radius: 40rpx;
  182. display: flex;
  183. align-items: center;
  184. justify-content: center;
  185. margin-bottom: 12rpx;
  186. }
  187. .avatar-text {
  188. font-size: 32rpx;
  189. font-weight: bold;
  190. color: #fff;
  191. }
  192. .card-name {
  193. font-size: 28rpx;
  194. font-weight: bold;
  195. color: #333;
  196. margin-bottom: 8rpx;
  197. }
  198. .card-type-tag {
  199. font-size: 20rpx;
  200. padding: 2rpx 14rpx;
  201. border-radius: 10rpx;
  202. margin-bottom: 8rpx;
  203. }
  204. .card-known { font-size: 22rpx; color: #999; margin-bottom: 4rpx; }
  205. .card-phone { font-size: 22rpx; color: #bbb; margin-bottom: 8rpx; }
  206. .card-intimacy {
  207. display: flex;
  208. align-items: center;
  209. margin-bottom: 6rpx;
  210. }
  211. .intimacy-heart { font-size: 22rpx; margin-right: 4rpx; }
  212. .intimacy-value {
  213. font-size: 28rpx;
  214. font-weight: bold;
  215. color: #FF6B35;
  216. }
  217. .card-birthday {
  218. font-size: 20rpx;
  219. color: #EC4899;
  220. }
  221. .card-family-status {
  222. margin-top: 8rpx;
  223. }
  224. .family-badge {
  225. font-size: 20rpx;
  226. color: #10B981;
  227. background: rgba(16, 185, 129, 0.12);
  228. padding: 2rpx 12rpx;
  229. border-radius: 8rpx;
  230. }
  231. .card-invite {
  232. margin-top: 8rpx;
  233. padding: 4rpx 16rpx;
  234. background: #F97316;
  235. border-radius: 10rpx;
  236. }
  237. .invite-text {
  238. font-size: 20rpx;
  239. color: #fff;
  240. }
  241. .invite-modal {
  242. position: fixed;
  243. top: 0;
  244. left: 0;
  245. right: 0;
  246. bottom: 0;
  247. z-index: 999;
  248. display: flex;
  249. align-items: center;
  250. justify-content: center;
  251. }
  252. .modal-mask {
  253. position: absolute;
  254. top: 0;
  255. left: 0;
  256. right: 0;
  257. bottom: 0;
  258. background: rgba(0,0,0,0.5);
  259. }
  260. .modal-content {
  261. position: relative;
  262. width: 600rpx;
  263. background: #fff;
  264. border-radius: 20rpx;
  265. padding: 40rpx;
  266. }
  267. .modal-title {
  268. font-size: 32rpx;
  269. font-weight: bold;
  270. color: #333;
  271. text-align: center;
  272. margin-bottom: 30rpx;
  273. }
  274. .form-row {
  275. margin-bottom: 20rpx;
  276. }
  277. .form-label {
  278. font-size: 26rpx;
  279. color: #666;
  280. margin-bottom: 8rpx;
  281. }
  282. .form-value {
  283. font-size: 28rpx;
  284. color: #333;
  285. padding: 12rpx 16rpx;
  286. background: #f5f5f5;
  287. border-radius: 8rpx;
  288. }
  289. .modal-buttons {
  290. display: flex;
  291. justify-content: space-between;
  292. margin-top: 30rpx;
  293. }
  294. .modal-btn {
  295. flex: 1;
  296. text-align: center;
  297. padding: 16rpx 0;
  298. border-radius: 10rpx;
  299. font-size: 28rpx;
  300. }
  301. .modal-btn.cancel {
  302. background: #f5f5f5;
  303. color: #666;
  304. margin-right: 16rpx;
  305. }
  306. .modal-btn.confirm {
  307. background: #F97316;
  308. color: #fff;
  309. }
  310. </style>