interaction-log.vue 17 KB

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