interaction-log.vue 17 KB

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