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