interaction-log.vue 17 KB

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