interaction-log.vue 18 KB

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