my-family.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <template>
  2. <CustomNav title="我的家人" leftType="back" />
  3. <view class="page-container">
  4. <view class="family-card" v-for="family in families" :key="family.id">
  5. <view class="family-header">
  6. <view class="avatar-section">
  7. <view class="avatar-frame">
  8. <image class="avatar-img" :src="familyAvatar(family)" mode="aspectFill" />
  9. </view>
  10. </view>
  11. <view class="family-info">
  12. <text class="family-name">{{ family.boundUserNickname }}</text>
  13. <text class="family-phone" v-if="family.boundUserPhone">联系电话: {{ family.boundUserPhone }}</text>
  14. <text class="family-phone" v-else>联系电话: 未提供</text>
  15. </view>
  16. </view>
  17. <view class="action-buttons">
  18. <button class="action-btn primary" @click="viewHealthData(family)">健康数据</button>
  19. <button class="action-btn danger" @click="unbindFamily(family)">解除绑定</button>
  20. </view>
  21. </view>
  22. <view class="empty-state" v-if="families.length === 0">
  23. <image class="empty-icon" src="/static/icons/remixicon/account-circle-line.svg" />
  24. <text class="empty-text">暂无绑定的家人</text>
  25. </view>
  26. </view>
  27. </template>
  28. <script setup lang="ts">
  29. import { ref } from 'vue'
  30. import { onLoad, onShow } from '@dcloudio/uni-app'
  31. import CustomNav from '@/components/custom-nav.vue'
  32. import { listUserBindingsByBoundUser, deleteUserBinding, type UserBindingResponse, type UserBindingPageResponse } from '@/api/userBinding'
  33. import { downloadAvatar } from '@/api/user'
  34. import { avatarCache } from '@/utils/avatarCache'
  35. interface FamilyInfo extends UserBindingResponse {
  36. avatar?: string
  37. }
  38. const families = ref<FamilyInfo[]>([])
  39. const pageData = ref({
  40. pageNum: 1,
  41. pageSize: 10,
  42. total: 0,
  43. pages: 0
  44. })
  45. const defaultAvatar = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  46. // 获取家人列表
  47. const fetchFamilies = async () => {
  48. uni.showLoading({ title: '加载中...' })
  49. try {
  50. const token = uni.getStorageSync('token')
  51. if (!token) {
  52. uni.hideLoading()
  53. uni.showToast({
  54. title: '未登录',
  55. icon: 'none'
  56. })
  57. return
  58. }
  59. // 获取当前用户ID(家属ID)
  60. const userInfo = uni.getStorageSync('user_info')
  61. const familyUserId = userInfo?.id
  62. if (!familyUserId) {
  63. uni.hideLoading()
  64. uni.showToast({
  65. title: '获取用户信息失败',
  66. icon: 'none'
  67. })
  68. return
  69. }
  70. // 查询家属绑定的家人列表(使用新的接口)
  71. const response = await listUserBindingsByBoundUser(
  72. familyUserId,
  73. 'FAMILY',
  74. {
  75. pageNum: pageData.value.pageNum,
  76. pageSize: pageData.value.pageSize
  77. }
  78. )
  79. uni.hideLoading()
  80. const resp = response.data as any
  81. if (resp && resp.code === 200 && resp.data) {
  82. const pageResult = resp.data as UserBindingPageResponse
  83. families.value = pageResult.records as FamilyInfo[]
  84. pageData.value.total = pageResult.total
  85. pageData.value.pages = pageResult.pages
  86. // 为每个家人尝试下载头像
  87. for (const family of families.value) {
  88. try {
  89. if (family.patientUserId) {
  90. // 检查是否有缓存的头像
  91. if (avatarCache.has(family.patientUserId)) {
  92. family.avatar = avatarCache.get(family.patientUserId)
  93. } else {
  94. const dlRes: any = await downloadAvatar(String(family.patientUserId))
  95. if (dlRes && dlRes.statusCode === 200 && dlRes.tempFilePath) {
  96. family.avatar = dlRes.tempFilePath
  97. // 缓存头像路径
  98. avatarCache.set(family.patientUserId, dlRes.tempFilePath)
  99. }
  100. }
  101. }
  102. } catch (err) {
  103. console.warn('下载家人头像失败:', err)
  104. }
  105. }
  106. } else {
  107. uni.showToast({
  108. title: '获取家人信息失败',
  109. icon: 'none'
  110. })
  111. }
  112. } catch (error) {
  113. uni.hideLoading()
  114. console.error('获取家人信息失败:', error)
  115. uni.showToast({
  116. title: '获取家人信息失败',
  117. icon: 'none'
  118. })
  119. }
  120. }
  121. const familyAvatar = (family: FamilyInfo) => {
  122. if (family.avatar) {
  123. return family.avatar
  124. }
  125. return defaultAvatar
  126. }
  127. const viewHealthData = (family: FamilyInfo) => {
  128. // 跳转到公共健康数据查看页面,传递患者ID和绑定类型参数
  129. uni.navigateTo({
  130. url: `/pages/public/health/index?patientId=${family.patientUserId}&bindingType=FAMILY`
  131. })
  132. }
  133. const unbindFamily = (family: FamilyInfo) => {
  134. uni.showModal({
  135. title: '确认解除绑定',
  136. content: `确定要解除与 ${family.boundUserNickname} 的绑定关系吗?`,
  137. success: async (res) => {
  138. if (res.confirm) {
  139. try {
  140. uni.showLoading({ title: '正在解除绑定...' })
  141. // 调用解除绑定接口
  142. const response = await deleteUserBinding({
  143. patientUserId: family.patientUserId,
  144. boundUserId: family.boundUserId
  145. })
  146. uni.hideLoading()
  147. const resp = response.data as any
  148. if (resp && resp.code === 200) {
  149. uni.showToast({
  150. title: '解除绑定成功',
  151. icon: 'success'
  152. })
  153. // 重新加载家人列表
  154. fetchFamilies()
  155. } else {
  156. uni.showToast({
  157. title: resp?.message || '解除绑定失败',
  158. icon: 'none'
  159. })
  160. }
  161. } catch (error) {
  162. uni.hideLoading()
  163. console.error('解除绑定失败:', error)
  164. uni.showToast({
  165. title: '解除绑定失败',
  166. icon: 'none'
  167. })
  168. }
  169. }
  170. }
  171. })
  172. }
  173. onLoad(() => {
  174. fetchFamilies()
  175. })
  176. // 如果在微信小程序端且未登录,自动跳转到登录页
  177. onShow(() => {
  178. const token = uni.getStorageSync('token')
  179. if (!token) {
  180. uni.reLaunch({ url: '/pages/public/login/index' })
  181. }
  182. })
  183. </script>
  184. <style scoped>
  185. .page-container {
  186. min-height: 100vh;
  187. background-color: #f5f5f5;
  188. padding-top: calc(var(--status-bar-height) + 44px);
  189. padding-bottom: 40rpx;
  190. }
  191. .family-card {
  192. background-color: #fff;
  193. margin: 20rpx;
  194. border-radius: 20rpx;
  195. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  196. overflow: hidden;
  197. }
  198. .family-header {
  199. display: flex;
  200. padding: 40rpx;
  201. border-bottom: 1rpx solid #eee;
  202. }
  203. .avatar-section {
  204. margin-right: 30rpx;
  205. }
  206. .avatar-frame {
  207. width: 120rpx;
  208. height: 120rpx;
  209. border-radius: 50%;
  210. overflow: hidden;
  211. border: 1px solid rgba(128, 128, 128, 0.5);
  212. }
  213. .avatar-img {
  214. width: 100%;
  215. height: 100%;
  216. object-fit: cover;
  217. }
  218. .family-info {
  219. flex: 1;
  220. display: flex;
  221. flex-direction: column;
  222. justify-content: center;
  223. }
  224. .family-name {
  225. font-size: 36rpx;
  226. font-weight: bold;
  227. color: #333;
  228. margin-bottom: 10rpx;
  229. }
  230. .family-phone {
  231. font-size: 28rpx;
  232. color: #666;
  233. }
  234. .action-buttons {
  235. display: flex;
  236. padding: 30rpx 40rpx;
  237. gap: 20rpx;
  238. flex-wrap: wrap;
  239. }
  240. .action-btn {
  241. flex: 1;
  242. border-radius: 10rpx;
  243. font-size: 28rpx;
  244. line-height: 70rpx;
  245. min-width: 40%;
  246. }
  247. .primary {
  248. background-color: #3742fa;
  249. color: #fff;
  250. }
  251. .secondary {
  252. background-color: #f0f0f0;
  253. color: #333;
  254. }
  255. .danger {
  256. background-color: #ff4757;
  257. color: #fff;
  258. }
  259. .empty-state {
  260. display: flex;
  261. flex-direction: column;
  262. align-items: center;
  263. justify-content: center;
  264. padding: 100rpx 40rpx;
  265. }
  266. .empty-icon {
  267. width: 120rpx;
  268. height: 120rpx;
  269. margin-bottom: 30rpx;
  270. opacity: 0.5;
  271. }
  272. .empty-text {
  273. font-size: 32rpx;
  274. color: #666;
  275. margin-bottom: 40rpx;
  276. }
  277. </style>