interaction-log.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <template>
  2. <view class="container">
  3. <view class="page-header">
  4. <text class="page-title">记录互动</text>
  5. </view>
  6. <!-- 家庭数据区 -->
  7. <!-- 选择互动对象(仅在未通过URL传入memberId时显示) -->
  8. <view class="form-section" v-if="!preselectedMemberId">
  9. <text class="section-label">互动对象</text>
  10. <view class="member-select">
  11. <view class="member-item" v-for="m in members" :key="m.id"
  12. :class="{selected: selectedMemberId === m.id}"
  13. @click="selectMember(m)">
  14. <text class="member-name">{{ m.nickname }}</text>
  15. <text class="member-rel">{{ m.relationship || m.relativeLabel || m.roleLabel }}</text>
  16. </view>
  17. </view>
  18. </view>
  19. <!-- 互动类型 -->
  20. <view class="form-section">
  21. <text class="section-label">互动类型</text>
  22. <view class="type-grid">
  23. <view class="type-item" v-for="(name, key) in interactionTypes" :key="key"
  24. :class="{selected: selectedType === key}"
  25. @click="selectedType = key">
  26. <text class="type-icon">{{ typeIcons[key] || '💬' }}</text>
  27. <text class="type-name">{{ name }}</text>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- 互动描述 -->
  32. <view class="form-section">
  33. <text class="section-label">描述(选填)</text>
  34. <textarea class="desc-input" v-model="description"
  35. placeholder="简单描述一下这次互动..." maxlength="256"></textarea>
  36. </view>
  37. <!-- 互动时间 -->
  38. <view class="form-section">
  39. <text class="section-label">互动时间</text>
  40. <view class="time-selector">
  41. <view class="time-option" :class="{selected: timeType === 'now'}" @click="timeType = 'now'">现在</view>
  42. <view class="time-option" :class="{selected: timeType === 'custom'}" @click="timeType = 'custom'">选择时间</view>
  43. </view>
  44. <view v-if="timeType === 'custom'" class="custom-time">
  45. <picker mode="dateTime" :value="customTime" @change="onTimeChange">
  46. <view class="time-display">{{ customTime || '选择时间' }}</view>
  47. </picker>
  48. </view>
  49. </view>
  50. <!-- 附件上传区域 -->
  51. <view class="form-section">
  52. <text class="section-label">添加附件(选填)</text>
  53. <!-- 照片区域 -->
  54. <view class="media-section">
  55. <text class="media-subtitle">📷 照片</text>
  56. <view class="media-grid">
  57. <view class="media-item" v-for="(photo, idx) in photos" :key="idx">
  58. <image class="media-preview" :src="photo" mode="aspectFill" @click="previewPhoto(idx)"></image>
  59. <view class="media-delete" @click.stop="removePhoto(idx)">×</view>
  60. </view>
  61. <view class="media-add" v-if="photos.length < 9" @click="choosePhotos">
  62. <text class="media-add-icon">+</text>
  63. <text class="media-add-text">{{ photos.length }}/9</text>
  64. </view>
  65. </view>
  66. </view>
  67. <!-- 视频区域 -->
  68. <view class="media-section" style="margin-top: 20rpx;">
  69. <text class="media-subtitle">🎬 视频</text>
  70. <view class="media-grid" v-if="video">
  71. <view class="media-item video-item">
  72. <view class="video-placeholder">
  73. <text class="video-icon">🎬</text>
  74. <text class="video-name">{{ videoName || '视频已选择' }}</text>
  75. </view>
  76. <view class="media-delete" @click.stop="removeVideo">×</view>
  77. </view>
  78. </view>
  79. <view class="media-add" v-if="!video" @click="chooseVideo">
  80. <text class="media-add-icon">+</text>
  81. <text class="media-add-text">选择视频</text>
  82. </view>
  83. </view>
  84. <!-- 音频区域 -->
  85. <view class="media-section" style="margin-top: 20rpx;">
  86. <text class="media-subtitle">🎵 音频</text>
  87. <view class="media-grid" v-if="audio">
  88. <view class="media-item audio-item">
  89. <view class="audio-placeholder">
  90. <text class="audio-icon">🎵</text>
  91. <text class="audio-name">{{ audioName || '音频已选择' }}</text>
  92. </view>
  93. <view class="media-delete" @click.stop="removeAudio">×</view>
  94. </view>
  95. </view>
  96. <view class="media-add" v-if="!audio" @click="chooseAudio">
  97. <text class="media-add-icon">+</text>
  98. <text class="media-add-text">选择音频</text>
  99. </view>
  100. </view>
  101. </view>
  102. <!-- 提交按钮 -->
  103. <view class="submit-bar">
  104. <button class="btn-submit" @click="onSubmit" :disabled="!canSubmit || submitting">
  105. {{ submitting ? '保存中...' : '保存记录' }}
  106. </button>
  107. </view>
  108. </view>
  109. </template>
  110. <script>
  111. import { getFamilyMemberList, addInteraction } from '../../utils/api.js'
  112. import config from '../../config.js'
  113. export default {
  114. data() {
  115. return {
  116. members: [],
  117. selectedMemberId: null,
  118. preselectedMemberId: null, // URL传入的memberId,不显示成员选择器
  119. currentMemberId: null, // 当前登录用户的家庭成员ID(fromMemberId)
  120. selectedType: '',
  121. description: '',
  122. timeType: 'now',
  123. customTime: '',
  124. submitting: false,
  125. // 附件相关
  126. photos: [], // 照片临时路径列表(最多9张)
  127. video: '', // 视频临时路径
  128. videoName: '',
  129. audio: '', // 音频临时路径
  130. audioName: '',
  131. uploading: false,
  132. interactionTypes: {
  133. dining: '一起用餐',
  134. outgoing: '外出活动',
  135. call: '电话联系',
  136. video: '视频通话',
  137. chat: '聊天交流',
  138. play: '一起游戏',
  139. gift: '赠送礼物'
  140. },
  141. typeIcons: {
  142. dining: '🍽️',
  143. outgoing: '🚗',
  144. call: '📞',
  145. video: '📹',
  146. chat: '💬',
  147. play: '🎮',
  148. gift: '🎁'
  149. }
  150. }
  151. },
  152. computed: {
  153. canSubmit() {
  154. return this.selectedMemberId && this.selectedType
  155. }
  156. },
  157. onLoad(options) {
  158. if (options.memberId) {
  159. this.preselectedMemberId = Number(options.memberId)
  160. this.selectedMemberId = Number(options.memberId)
  161. }
  162. this.loadMembers()
  163. },
  164. methods: {
  165. async loadMembers() {
  166. try {
  167. var res = await getFamilyMemberList()
  168. if (res.code === 200 && res.data) {
  169. var all = res.data || []
  170. // 确定"我"的成员ID:优先从成员列表中匹配当前登录用户
  171. var userInfo = uni.getStorageSync('userInfo')
  172. var uid = (userInfo && userInfo.id) || 0
  173. var self = this
  174. var found = null
  175. if (uid) {
  176. for (var i = 0; i < all.length; i++) {
  177. var m = all[i]
  178. if (m && m.userId && Number(m.userId) === Number(uid)) {
  179. found = m
  180. break
  181. }
  182. }
  183. }
  184. this.currentMemberId = found ? found.id : null
  185. // 互动对象列表过滤掉"我"(from==to 后端会拒绝)
  186. this.members = all.filter(function(m) {
  187. return !(m && m.userId && found && Number(m.userId) === Number(found.userId))
  188. })
  189. }
  190. } catch (e) {
  191. uni.showToast({ title: '加载成员失败', icon: 'none' })
  192. }
  193. },
  194. selectMember(member) {
  195. this.selectedMemberId = member.id
  196. },
  197. onTimeChange(e) {
  198. this.customTime = e.detail.value
  199. },
  200. // ===== 照片选择 =====
  201. choosePhotos() {
  202. var self = this
  203. var remaining = 9 - self.photos.length
  204. if (remaining <= 0) return
  205. uni.chooseImage({
  206. count: remaining,
  207. sizeType: ['compressed'],
  208. sourceType: ['album', 'camera'],
  209. success: function(res) {
  210. res.tempFilePaths.forEach(function(path) {
  211. if (self.photos.length < 9) {
  212. self.photos.push(path)
  213. }
  214. })
  215. }
  216. })
  217. },
  218. previewPhoto(idx) {
  219. uni.previewImage({
  220. current: idx,
  221. urls: this.photos
  222. })
  223. },
  224. removePhoto(idx) {
  225. this.photos.splice(idx, 1)
  226. },
  227. // ===== 视频选择 =====
  228. chooseVideo() {
  229. var self = this
  230. uni.chooseVideo({
  231. sourceType: ['album', 'camera'],
  232. maxDuration: 60,
  233. compressed: true,
  234. success: function(res) {
  235. self.video = res.tempFilePath
  236. self.videoName = res.name || '视频'
  237. }
  238. })
  239. },
  240. removeVideo() {
  241. this.video = ''
  242. this.videoName = ''
  243. },
  244. // ===== 音频选择 =====
  245. chooseAudio() {
  246. var self = this
  247. uni.chooseMessageFile({
  248. count: 1,
  249. type: 'file',
  250. extension: ['mp3', 'wav', 'm4a', 'aac', 'ogg'],
  251. success: function(res) {
  252. if (res.tempFiles && res.tempFiles.length > 0) {
  253. self.audio = res.tempFiles[0].path
  254. self.audioName = res.tempFiles[0].name || '音频'
  255. }
  256. },
  257. fail: function() {
  258. // 用户取消或不支持,尝试录音
  259. uni.chooseVideo({
  260. sourceType: ['album'],
  261. maxDuration: 60,
  262. compressed: true,
  263. success: function(res) {
  264. self.audio = res.tempFilePath
  265. self.audioName = res.name || '音频'
  266. }
  267. })
  268. }
  269. })
  270. },
  271. removeAudio() {
  272. this.audio = ''
  273. this.audioName = ''
  274. },
  275. // ===== 上传文件 =====
  276. uploadFile(filePath) {
  277. var self = this
  278. return new Promise(function(resolve, reject) {
  279. uni.uploadFile({
  280. url: config.API_BASE_URL + '/api/media/upload',
  281. filePath: filePath,
  282. name: 'file',
  283. header: { 'Authorization': 'Bearer ' + uni.getStorageSync('token') },
  284. success: function(res) {
  285. try {
  286. var data = JSON.parse(res.data)
  287. if (data && data.data && data.data.url) {
  288. resolve(data.data.url)
  289. } else if (data && data.url) {
  290. resolve(data.url)
  291. } else {
  292. resolve('')
  293. }
  294. } catch (e) {
  295. resolve(res.data || '')
  296. }
  297. },
  298. fail: function() { reject() }
  299. })
  300. })
  301. },
  302. // ===== 提交 =====
  303. async onSubmit() {
  304. if (!this.canSubmit) {
  305. uni.showToast({ title: '请选择对象和类型', icon: 'none' })
  306. return
  307. }
  308. if (this.submitting) return
  309. // fromMemberId 必须与 toMemberId 不同(后端校验"互动双方不能是同一人")
  310. var fromId = this.currentMemberId || this.selectedMemberId
  311. if (!fromId || Number(fromId) === Number(this.selectedMemberId)) {
  312. uni.showToast({ title: '无法确定当前成员,请返回后重试', icon: 'none' })
  313. return
  314. }
  315. this.submitting = true
  316. this.uploading = true
  317. try {
  318. var happenedAt = ''
  319. if (this.timeType === 'custom' && this.customTime) {
  320. happenedAt = this.customTime
  321. }
  322. // 上传附件
  323. var photoUrls = []
  324. if (this.photos && this.photos.length > 0) {
  325. for (var i = 0; i < this.photos.length; i++) {
  326. try {
  327. var url = await this.uploadFile(this.photos[i])
  328. if (url) photoUrls.push(url)
  329. } catch (e) {
  330. // 单个上传失败不阻塞整个流程
  331. }
  332. }
  333. }
  334. var videoUrl = ''
  335. if (this.video) {
  336. try {
  337. videoUrl = await this.uploadFile(this.video) || ''
  338. } catch (e) {
  339. videoUrl = ''
  340. }
  341. }
  342. var audioUrl = ''
  343. if (this.audio) {
  344. try {
  345. audioUrl = await this.uploadFile(this.audio) || ''
  346. } catch (e) {
  347. audioUrl = ''
  348. }
  349. }
  350. this.uploading = false
  351. // 保存互动记录
  352. var res = await addInteraction({
  353. fromMemberId: fromId,
  354. toMemberId: this.selectedMemberId,
  355. interactionType: this.selectedType,
  356. description: this.description,
  357. happenedAt: happenedAt,
  358. photoUrls: photoUrls.join(','),
  359. videoUrl: videoUrl,
  360. audioUrl: audioUrl
  361. })
  362. if (res.code === 200) {
  363. uni.showToast({ title: '记录成功', icon: 'success' })
  364. setTimeout(function() {
  365. uni.navigateBack()
  366. }, 1500)
  367. } else {
  368. uni.showToast({ title: res.message || '保存失败', icon: 'none' })
  369. }
  370. } catch (e) {
  371. uni.showToast({ title: '保存失败', icon: 'none' })
  372. } finally {
  373. this.submitting = false
  374. this.uploading = false
  375. }
  376. }
  377. }
  378. }
  379. </script>
  380. <style scoped>
  381. .container {
  382. min-height: 100vh;
  383. background: #f5f5f5;
  384. padding: 24rpx;
  385. }
  386. .page-header {
  387. padding: 20rpx 0 30rpx;
  388. }
  389. .page-title {
  390. font-size: 36rpx;
  391. font-weight: bold;
  392. color: #333;
  393. }
  394. .form-section {
  395. background: #fff;
  396. border-radius: 16rpx;
  397. padding: 24rpx;
  398. margin-bottom: 20rpx;
  399. }
  400. .section-label {
  401. font-size: 28rpx;
  402. color: #333;
  403. font-weight: 500;
  404. display: block;
  405. margin-bottom: 16rpx;
  406. }
  407. .member-select {
  408. display: flex;
  409. flex-wrap: wrap;
  410. gap: 16rpx;
  411. }
  412. .member-item {
  413. padding: 16rpx 24rpx;
  414. border: 2rpx solid #e0e0e0;
  415. border-radius: 12rpx;
  416. background: #fafafa;
  417. }
  418. .member-item.selected {
  419. border-color: #F97316;
  420. background: #FFF7ED;
  421. }
  422. .member-name {
  423. font-size: 28rpx;
  424. color: #333;
  425. display: block;
  426. }
  427. .member-rel {
  428. font-size: 22rpx;
  429. color: #999;
  430. }
  431. .type-grid {
  432. display: flex;
  433. flex-wrap: wrap;
  434. gap: 16rpx;
  435. }
  436. .type-item {
  437. width: calc(25% - 12rpx);
  438. padding: 20rpx 0;
  439. text-align: center;
  440. border: 2rpx solid #e0e0e0;
  441. border-radius: 12rpx;
  442. background: #fafafa;
  443. }
  444. .type-item.selected {
  445. border-color: #F97316;
  446. background: #FFF7ED;
  447. }
  448. .type-icon {
  449. font-size: 40rpx;
  450. display: block;
  451. margin-bottom: 8rpx;
  452. }
  453. .type-name {
  454. font-size: 22rpx;
  455. color: #666;
  456. }
  457. .desc-input {
  458. width: 100%;
  459. height: 160rpx;
  460. background: #fafafa;
  461. border-radius: 12rpx;
  462. padding: 20rpx;
  463. font-size: 28rpx;
  464. box-sizing: border-box;
  465. }
  466. .time-selector {
  467. display: flex;
  468. gap: 16rpx;
  469. margin-bottom: 16rpx;
  470. }
  471. .time-option {
  472. padding: 16rpx 32rpx;
  473. border: 2rpx solid #e0e0e0;
  474. border-radius: 12rpx;
  475. font-size: 28rpx;
  476. color: #666;
  477. }
  478. .time-option.selected {
  479. border-color: #F97316;
  480. color: #F97316;
  481. background: #FFF7ED;
  482. }
  483. .custom-time {
  484. margin-top: 12rpx;
  485. }
  486. .time-display {
  487. padding: 20rpx;
  488. background: #fafafa;
  489. border-radius: 12rpx;
  490. font-size: 28rpx;
  491. color: #333;
  492. }
  493. /* ===== 附件上传样式 ===== */
  494. .media-section {
  495. margin-bottom: 16rpx;
  496. }
  497. .media-subtitle {
  498. font-size: 24rpx;
  499. color: #666;
  500. display: block;
  501. margin-bottom: 12rpx;
  502. }
  503. .media-grid {
  504. display: flex;
  505. flex-wrap: wrap;
  506. gap: 12rpx;
  507. }
  508. .media-item {
  509. width: 150rpx;
  510. height: 150rpx;
  511. border-radius: 8rpx;
  512. position: relative;
  513. overflow: hidden;
  514. }
  515. .media-preview {
  516. width: 100%;
  517. height: 100%;
  518. }
  519. .media-delete {
  520. position: absolute;
  521. top: 0;
  522. right: 0;
  523. width: 40rpx;
  524. height: 40rpx;
  525. background: rgba(0, 0, 0, 0.6);
  526. color: #fff;
  527. font-size: 24rpx;
  528. display: flex;
  529. align-items: center;
  530. justify-content: center;
  531. border-radius: 0 8rpx 0 8rpx;
  532. }
  533. .video-item,
  534. .audio-item {
  535. width: 200rpx;
  536. height: 100rpx;
  537. background: #f0f0f0;
  538. border-radius: 8rpx;
  539. }
  540. .video-placeholder,
  541. .audio-placeholder {
  542. width: 100%;
  543. height: 100%;
  544. display: flex;
  545. flex-direction: column;
  546. align-items: center;
  547. justify-content: center;
  548. }
  549. .video-icon,
  550. .audio-icon {
  551. font-size: 32rpx;
  552. }
  553. .video-name,
  554. .audio-name {
  555. font-size: 20rpx;
  556. color: #666;
  557. margin-top: 8rpx;
  558. overflow: hidden;
  559. text-overflow: ellipsis;
  560. white-space: nowrap;
  561. max-width: 180rpx;
  562. }
  563. .media-add {
  564. width: 150rpx;
  565. height: 150rpx;
  566. background: #fafafa;
  567. border: 2rpx dashed #ddd;
  568. border-radius: 8rpx;
  569. display: flex;
  570. flex-direction: column;
  571. align-items: center;
  572. justify-content: center;
  573. }
  574. .media-add-icon {
  575. font-size: 48rpx;
  576. color: #999;
  577. }
  578. .media-add-text {
  579. font-size: 20rpx;
  580. color: #999;
  581. margin-top: 8rpx;
  582. }
  583. /* ===== 提交按钮 ===== */
  584. .submit-bar {
  585. margin-top: 40rpx;
  586. }
  587. .btn-submit {
  588. width: 100%;
  589. background: #F97316;
  590. color: #fff;
  591. border: none;
  592. border-radius: 12rpx;
  593. padding: 24rpx 0;
  594. font-size: 32rpx;
  595. }
  596. .btn-submit[disabled] {
  597. background: #ccc;
  598. }
  599. </style>