ContactCard.vue 8.8 KB

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