index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. <template>
  2. <view class="today-recommend-page">
  3. <!-- 自定义导航栏 -->
  4. <view class="custom-navbar">
  5. <view class="navbar-left" @click="goBack">
  6. <text class="back-icon">←</text>
  7. </view>
  8. <view class="navbar-title">💕 今日推荐</view>
  9. <view class="navbar-right"></view>
  10. </view>
  11. <!-- 页面内容 -->
  12. <view class="page-content">
  13. <!-- 推荐用户卡片 -->
  14. <view class="recommend-list">
  15. <view class="recommend-card" v-for="(user, index) in recommendUsers" :key="index">
  16. <view class="card-header">
  17. <image class="user-avatar" :src="user.avatar" mode="aspectFill"></image>
  18. <view class="user-basic-info">
  19. <view class="user-name-row">
  20. <text class="user-name">{{ user.nickname }}</text>
  21. <view class="vip-badge" v-if="user.isVip">VIP</view>
  22. </view>
  23. <view class="user-details">
  24. <text class="user-detail" v-if="user.age && user.age !== '未知'">{{ user.age }}岁</text>
  25. <text class="user-detail" v-if="user.height">{{ user.height }}cm</text>
  26. <text class="user-detail" v-if="user.location && user.location !== '未填写'">{{ user.location }}</text>
  27. </view>
  28. </view>
  29. <view class="online-status" :class="{ online: user.isOnline }">
  30. <text class="status-dot"></text>
  31. <text class="status-text">{{ user.isOnline ? '在线' : '离线' }}</text>
  32. </view>
  33. </view>
  34. <view class="card-content">
  35. <view class="user-info-grid">
  36. <view class="info-item" v-if="user.job && user.job !== '未填写'">
  37. <text class="info-label">职业</text>
  38. <text class="info-value">{{ user.job }}</text>
  39. </view>
  40. <view class="info-item" v-if="user.education && user.education !== '未填写'">
  41. <text class="info-label">学历</text>
  42. <text class="info-value">{{ user.education }}</text>
  43. </view>
  44. <view class="info-item" v-if="user.constellation && user.constellation !== '未填写'">
  45. <text class="info-label">星座</text>
  46. <text class="info-value">{{ user.constellation }}</text>
  47. </view>
  48. <view class="info-item" v-if="user.salary && user.salary !== '未填写'">
  49. <text class="info-label">月收入</text>
  50. <text class="info-value">{{ user.salary }}</text>
  51. </view>
  52. </view>
  53. <view class="user-tags">
  54. <text class="tag" v-for="(tag, tagIndex) in user.hobbies" :key="tagIndex">{{ tag }}</text>
  55. </view>
  56. <view class="personal-intro" v-if="user.introduction">
  57. <text class="intro-label">个人简介:</text>
  58. <text class="intro-text">{{ user.introduction }}</text>
  59. </view>
  60. </view>
  61. <view class="card-actions">
  62. <view class="action-btn pass-btn" @click="handlePass(user, index)">
  63. <text class="action-icon">👎</text>
  64. <text class="action-text">不感兴趣</text>
  65. </view>
  66. <view class="action-btn like-btn" @click="handleLike(user, index)">
  67. <text class="action-icon">❤️</text>
  68. <text class="action-text">喜欢</text>
  69. </view>
  70. <view class="action-btn chat-btn" @click="handleChat(user)">
  71. <text class="action-icon">💬</text>
  72. <text class="action-text">打招呼</text>
  73. </view>
  74. </view>
  75. </view>
  76. </view>
  77. <!-- 暂无推荐 -->
  78. <view class="no-recommend" v-if="recommendUsers.length === 0">
  79. <text class="no-recommend-icon">💭</text>
  80. <text class="no-recommend-title">今日暂无新推荐</text>
  81. <text class="no-recommend-tip">明天再来看看吧~</text>
  82. <view class="complete-profile-btn" @click="goToProfile">
  83. <text class="btn-text">完善资料获得更多推荐</text>
  84. </view>
  85. </view>
  86. <!-- 底部占位 -->
  87. <view class="bottom-placeholder"></view>
  88. </view>
  89. </view>
  90. </template>
  91. <script>
  92. import userAuth from '@/utils/userAuth.js'
  93. import api from '@/utils/api.js'
  94. import { DEFAULT_IMAGES, EDUCATION_TEXT, SALARY_RANGE_TEXT } from '@/config/index.js'
  95. export default {
  96. data() {
  97. return {
  98. currentUserId: null,
  99. currentUserGender: null,
  100. // 推荐用户数据
  101. recommendUsers: []
  102. }
  103. },
  104. onLoad() {
  105. this.currentUserId = userAuth.getUserId()
  106. // 加载今日推荐
  107. this.loadTodayRecommend()
  108. },
  109. methods: {
  110. // 加载今日推荐
  111. async loadTodayRecommend() {
  112. try {
  113. uni.showLoading({
  114. title: '加载推荐中...'
  115. })
  116. const userId = this.currentUserId || parseInt(uni.getStorageSync('userId'))
  117. if (!userId) {
  118. uni.hideLoading()
  119. uni.showToast({
  120. title: '请先登录',
  121. icon: 'none'
  122. })
  123. return
  124. }
  125. await this.loadCurrentUserInfo()
  126. const data = await api.recommend.getUsers({
  127. userId,
  128. oppoOnly: 1,
  129. limit: 50
  130. })
  131. uni.hideLoading()
  132. if (data && data.length > 0) {
  133. const sortedUsers = data.filter(u => u.userId !== userId && u.compatibilityScore > 0 && (!this.currentUserGender || u.gender !== this.currentUserGender)).sort((a, b) => (b.compatibilityScore || 0) - (a.compatibilityScore || 0)).slice(0, 10)
  134. this.recommendUsers = sortedUsers.map(u => this.formatUserData(u))
  135. if (this.recommendUsers.length > 0) uni.showToast({
  136. title: `为您推荐了${this.recommendUsers.length}位高匹配度会员`,
  137. icon: 'success'
  138. })
  139. else uni.showToast({
  140. title: '暂无推荐用户',
  141. icon: 'none'
  142. })
  143. } else uni.showToast({
  144. title: '暂无推荐用户',
  145. icon: 'none'
  146. })
  147. } catch (e) {
  148. uni.hideLoading()
  149. console.error('获取推荐用户失败:', e)
  150. uni.showToast({
  151. title: '加载失败,请重试',
  152. icon: 'none'
  153. })
  154. }
  155. },
  156. // 加载当前用户信息
  157. async loadCurrentUserInfo() {
  158. try {
  159. const s = uni.getStorageSync('userInfo')
  160. if (s && s.gender !== undefined) this.currentUserGender = parseInt(s.gender)
  161. if (this.currentUserId) {
  162. const u = await api.user.getDetailInfo(this.currentUserId)
  163. if (u && u.gender !== undefined) this.currentUserGender = parseInt(u.gender)
  164. }
  165. } catch (e) {
  166. console.error('加载失败:', e)
  167. }
  168. },
  169. // 格式化用户数据
  170. formatUserData(u) {
  171. return {
  172. ...u,
  173. id: u.userId || u.id,
  174. avatar: this.validateImageUrl(u.avatarUrl || u.avatar),
  175. nickname: u.nickname || '用户',
  176. age: this.calculateAge(u.birthDate || u.birth_date) || u.age || '未知',
  177. height: u.height || null,
  178. location: this.formatLocation(u),
  179. job: u.jobTitle || u.job || '未填写',
  180. education: this.formatEducation(u.education || u.education_lev || u.educationLevel),
  181. constellation: u.star || u.constellation || '未填写',
  182. salary: this.formatSalary(u.salaryRange),
  183. hobbies: this.parseHobbies(u.hobby),
  184. introduction: u.introduction || u.selfIntro || '',
  185. isVip: u.isVip || false,
  186. isOnline: u.isOnline || false,
  187. compatibilityScore: u.compatibilityScore || 0
  188. }
  189. },
  190. // 计算年龄
  191. calculateAge(d) {
  192. if (!d) return null
  193. try {
  194. const b = new Date(d),
  195. t = new Date()
  196. let a = t.getFullYear() - b.getFullYear()
  197. const m = t.getMonth() - b.getMonth()
  198. if (m < 0 || (m === 0 && t.getDate() < b.getDate())) a--
  199. return a > 0 && a < 120 ? a : null
  200. } catch (e) {
  201. return null
  202. }
  203. },
  204. // 格式化位置
  205. formatLocation(u) {
  206. const p = []
  207. if (u.provinceName) p.push(u.provinceName)
  208. if (u.cityName) p.push(u.cityName)
  209. if (p.length === 0 && u.location) return u.location
  210. if (p.length === 0 && u.city) return u.city
  211. return p.join(' ') || '未填写'
  212. },
  213. // 格式化学历
  214. formatEducation(e) {
  215. if (!e) return '未填写'
  216. return EDUCATION_TEXT[e] || e
  217. },
  218. // 格式化月收入
  219. formatSalary(s) {
  220. if (!s) return '未填写'
  221. return SALARY_RANGE_TEXT[s] || s
  222. },
  223. // 解析兴趣爱好
  224. parseHobbies(h) {
  225. if (!h) return []
  226. try {
  227. const hs = typeof h === 'string' ? JSON.parse(h) : h
  228. return Array.isArray(hs) ? hs.slice(0, 5) : []
  229. } catch (e) {
  230. return []
  231. }
  232. },
  233. // 验证图片地址
  234. validateImageUrl(url) {
  235. if (!url || url === 'null' || url === 'undefined') return DEFAULT_IMAGES.avatar
  236. let c = String(url).trim()
  237. const i = c.indexOf('http')
  238. if (i > 0) c = c.slice(i)
  239. if (!c) return DEFAULT_IMAGES.avatar
  240. if (c.startsWith('/')) return `http://115.190.125.125:9000${c}`
  241. if (c.startsWith('http://') || c.startsWith('https://')) return c
  242. return DEFAULT_IMAGES.avatar
  243. },
  244. // 格式化匹配度
  245. formatMatchScore(s) {
  246. if (!s) return '0.0'
  247. return Number(s).toFixed(1)
  248. },
  249. // 处理不感兴趣
  250. async handlePass(user, index) {
  251. uni.showModal({
  252. title: '确认操作',
  253. content: `确定对${user.nickname}不感兴趣吗?`,
  254. success: async (res) => {
  255. if (res.confirm) {
  256. try {
  257. await api.recommend.feedback({
  258. userId: this.currentUserId,
  259. targetUserId: user.userId || user.id,
  260. type: 'dislike'
  261. })
  262. uni.showToast({
  263. title: '已记录您的偏好',
  264. icon: 'success'
  265. })
  266. this.recommendUsers.splice(index, 1)
  267. } catch (e) {
  268. console.error('失败:', e)
  269. uni.showToast({
  270. title: '操作失败,请重试',
  271. icon: 'none'
  272. })
  273. }
  274. }
  275. }
  276. })
  277. },
  278. // 处理喜欢
  279. async handleLike(user, index) {
  280. try {
  281. await api.recommend.feedback({
  282. userId: this.currentUserId,
  283. targetUserId: user.userId || user.id,
  284. type: 'like'
  285. })
  286. } catch (e) {
  287. console.error('失败:', e)
  288. }
  289. uni.showToast({
  290. title: `已喜欢${user.nickname} ❤️`,
  291. icon: 'success'
  292. })
  293. setTimeout(() => {
  294. this.recommendUsers.splice(index, 1)
  295. // 检查是否匹配成功(模拟)
  296. const isMatch = Math.random() > 0.7 // 30%几率匹配成功
  297. if (isMatch) {
  298. setTimeout(() => {
  299. uni.showModal({
  300. title: '🎉 匹配成功!',
  301. content: `恭喜您与${user.nickname}互相喜欢,现在可以开始聊天了!`,
  302. showCancel: false,
  303. confirmText: '开始聊天',
  304. success: (modalRes) => {
  305. if (modalRes.confirm) {
  306. // 跳转到聊天页面
  307. uni.navigateTo({
  308. url: `/pages/message/chat?targetUserId=${user.userId || user.id}&targetUserName=${encodeURIComponent(user.nickname)}&targetUserAvatar=${encodeURIComponent(user.avatar || '')}`
  309. })
  310. }
  311. }
  312. })
  313. }, 1000)
  314. }
  315. }, 800)
  316. },
  317. // 处理打招呼
  318. handleChat(user) {
  319. const greetings = [
  320. '你好,很高兴认识你!',
  321. 'Hi,看了你的资料很不错呢~',
  322. '你好,我们聊聊天吧!',
  323. 'Hello,可以交个朋友吗?'
  324. ]
  325. const randomGreeting = greetings[Math.floor(Math.random() * greetings.length)]
  326. uni.showModal({
  327. title: `向${user.nickname}打招呼`,
  328. content: `发送消息:"${randomGreeting}"`,
  329. showCancel: true,
  330. cancelText: '自定义',
  331. confirmText: '发送',
  332. success: (res) => {
  333. if (res.confirm) {
  334. uni.navigateTo({
  335. url: `/pages/message/chat?targetUserId=${user.userId||user.id}&targetUserName=${encodeURIComponent(user.nickname)}&message=${encodeURIComponent(randomGreeting)}`
  336. })
  337. } else {
  338. uni.navigateTo({
  339. url: `/pages/message/chat?targetUserId=${user.userId||user.id}&targetUserName=${encodeURIComponent(user.nickname)}`
  340. })
  341. }
  342. }
  343. })
  344. },
  345. // 跳转到个人资料
  346. goToProfile() {
  347. uni.navigateTo({
  348. url: '/pages/profile/index'
  349. })
  350. },
  351. // 返回
  352. goBack() {
  353. uni.navigateBack({
  354. fail: () => {
  355. // 返回失败时跳转首页
  356. uni.navigateTo({
  357. url: '/pages/index/index'
  358. })
  359. }
  360. })
  361. }
  362. }
  363. }
  364. </script>
  365. <style lang="scss" scoped>
  366. .today-recommend-page {
  367. min-height: 100vh;
  368. background: linear-gradient(180deg, #FFE5EE 0%, #FFF9F9 100%);
  369. padding-top: 90rpx;
  370. padding-bottom: 40rpx;
  371. }
  372. /* 自定义导航栏 */
  373. .custom-navbar {
  374. position: fixed;
  375. top: 0;
  376. left: 0;
  377. right: 0;
  378. height: 90rpx;
  379. display: flex;
  380. align-items: center;
  381. justify-content: space-between;
  382. padding: 0 20rpx;
  383. background: linear-gradient(135deg, #FF6B9D 0%, #FF8FAB 100%);
  384. z-index: 999;
  385. .navbar-left,
  386. .navbar-right {
  387. width: 80rpx;
  388. }
  389. .back-icon {
  390. font-size: 40rpx;
  391. color: #FFFFFF;
  392. font-weight: bold;
  393. }
  394. .navbar-title {
  395. flex: 1;
  396. text-align: center;
  397. font-size: 32rpx;
  398. font-weight: bold;
  399. color: #FFFFFF;
  400. }
  401. }
  402. /* 页面内容 */
  403. .page-content {
  404. padding: 20rpx 30rpx;
  405. }
  406. /* 推荐列表 */
  407. .recommend-list {
  408. .recommend-card {
  409. background: #FFFFFF;
  410. border-radius: 25rpx;
  411. margin-bottom: 30rpx;
  412. overflow: hidden;
  413. box-shadow: 0 8rpx 25rpx rgba(255, 107, 157, 0.15);
  414. border: 2rpx solid rgba(255, 107, 157, 0.1);
  415. .card-header {
  416. padding: 30rpx;
  417. display: flex;
  418. align-items: center;
  419. position: relative;
  420. background: linear-gradient(135deg, #FFF9FC 0%, #FFFFFF 100%);
  421. .user-avatar {
  422. width: 120rpx;
  423. height: 120rpx;
  424. border-radius: 60rpx;
  425. margin-right: 25rpx;
  426. border: 4rpx solid #FFE5EE;
  427. box-shadow: 0 4rpx 12rpx rgba(255, 107, 157, 0.2);
  428. }
  429. .user-basic-info {
  430. flex: 1;
  431. .user-name-row {
  432. display: flex;
  433. align-items: center;
  434. margin-bottom: 15rpx;
  435. .user-name {
  436. font-size: 34rpx;
  437. font-weight: bold;
  438. color: #333333;
  439. margin-right: 15rpx;
  440. }
  441. .vip-badge {
  442. background: linear-gradient(135deg, #FFD700 0%, #FFA500 100%);
  443. color: #FFFFFF;
  444. font-size: 20rpx;
  445. font-weight: bold;
  446. padding: 6rpx 15rpx;
  447. border-radius: 20rpx;
  448. box-shadow: 0 2rpx 8rpx rgba(255, 165, 0, 0.3);
  449. }
  450. }
  451. .user-details {
  452. display: flex;
  453. gap: 20rpx;
  454. .user-detail {
  455. font-size: 26rpx;
  456. color: #666666;
  457. background: rgba(255, 107, 157, 0.1);
  458. padding: 8rpx 16rpx;
  459. border-radius: 15rpx;
  460. }
  461. }
  462. }
  463. .online-status {
  464. position: absolute;
  465. top: 20rpx;
  466. right: 20rpx;
  467. display: flex;
  468. align-items: center;
  469. gap: 8rpx;
  470. .status-dot {
  471. width: 16rpx;
  472. height: 16rpx;
  473. border-radius: 50%;
  474. background: #CCCCCC;
  475. }
  476. .status-text {
  477. font-size: 22rpx;
  478. color: #999999;
  479. }
  480. &.online {
  481. .status-dot {
  482. background: #4CAF50;
  483. animation: blink 2s infinite;
  484. }
  485. .status-text {
  486. color: #4CAF50;
  487. }
  488. }
  489. }
  490. }
  491. .card-content {
  492. padding: 0 30rpx 20rpx;
  493. .user-info-grid {
  494. display: grid;
  495. grid-template-columns: 1fr 1fr;
  496. gap: 20rpx;
  497. margin-bottom: 25rpx;
  498. .info-item {
  499. background: #F8F9FA;
  500. padding: 20rpx;
  501. border-radius: 15rpx;
  502. .info-label {
  503. font-size: 22rpx;
  504. color: #999999;
  505. display: block;
  506. margin-bottom: 8rpx;
  507. }
  508. .info-value {
  509. font-size: 26rpx;
  510. color: #333333;
  511. font-weight: 500;
  512. }
  513. }
  514. }
  515. .user-tags {
  516. display: flex;
  517. flex-wrap: wrap;
  518. gap: 12rpx;
  519. margin-bottom: 25rpx;
  520. .tag {
  521. background: linear-gradient(135deg, #FFE5EE 0%, #FFD0DC 100%);
  522. color: #FF6B8A;
  523. font-size: 22rpx;
  524. padding: 10rpx 18rpx;
  525. border-radius: 20rpx;
  526. font-weight: 500;
  527. }
  528. }
  529. .personal-intro {
  530. background: rgba(255, 107, 157, 0.05);
  531. padding: 20rpx;
  532. border-radius: 15rpx;
  533. border-left: 4rpx solid #FF6B9D;
  534. .intro-label {
  535. font-size: 24rpx;
  536. color: #FF6B9D;
  537. font-weight: bold;
  538. display: block;
  539. margin-bottom: 10rpx;
  540. }
  541. .intro-text {
  542. font-size: 26rpx;
  543. color: #666666;
  544. line-height: 1.6;
  545. }
  546. }
  547. }
  548. .card-actions {
  549. display: flex;
  550. padding: 25rpx 30rpx 30rpx;
  551. gap: 15rpx;
  552. background: linear-gradient(135deg, #FAFAFA 0%, #F5F5F5 100%);
  553. .action-btn {
  554. flex: 1;
  555. display: flex;
  556. flex-direction: column;
  557. align-items: center;
  558. justify-content: center;
  559. padding: 25rpx 0;
  560. border-radius: 20rpx;
  561. font-weight: 500;
  562. transition: all 0.3s;
  563. .action-icon {
  564. font-size: 36rpx;
  565. margin-bottom: 8rpx;
  566. }
  567. .action-text {
  568. font-size: 24rpx;
  569. }
  570. &.pass-btn {
  571. background: #FFFFFF;
  572. color: #666666;
  573. border: 2rpx solid #E9ECEF;
  574. &:active {
  575. background: #F8F9FA;
  576. transform: scale(0.95);
  577. }
  578. }
  579. &.like-btn {
  580. background: linear-gradient(135deg, #FF6B9D 0%, #FF8FAB 100%);
  581. color: #FFFFFF;
  582. box-shadow: 0 6rpx 20rpx rgba(255, 107, 157, 0.3);
  583. &:active {
  584. transform: scale(0.95);
  585. }
  586. }
  587. &.chat-btn {
  588. background: linear-gradient(135deg, #4CAF50 0%, #66BB6A 100%);
  589. color: #FFFFFF;
  590. box-shadow: 0 6rpx 20rpx rgba(76, 175, 80, 0.3);
  591. &:active {
  592. transform: scale(0.95);
  593. }
  594. }
  595. }
  596. }
  597. }
  598. }
  599. @keyframes blink {
  600. 0%, 100% { opacity: 1; }
  601. 50% { opacity: 0.5; }
  602. }
  603. /* 暂无推荐 */
  604. .no-recommend {
  605. text-align: center;
  606. padding: 100rpx 40rpx;
  607. .no-recommend-icon {
  608. font-size: 120rpx;
  609. display: block;
  610. margin-bottom: 30rpx;
  611. }
  612. .no-recommend-title {
  613. font-size: 36rpx;
  614. color: #333333;
  615. font-weight: bold;
  616. display: block;
  617. margin-bottom: 15rpx;
  618. }
  619. .no-recommend-tip {
  620. font-size: 28rpx;
  621. color: #666666;
  622. display: block;
  623. margin-bottom: 50rpx;
  624. }
  625. .complete-profile-btn {
  626. background: linear-gradient(135deg, #FF6B9D 0%, #FF8FAB 100%);
  627. color: #FFFFFF;
  628. padding: 25rpx 45rpx;
  629. border-radius: 35rpx;
  630. box-shadow: 0 8rpx 20rpx rgba(255, 107, 157, 0.3);
  631. display: inline-block;
  632. .btn-text {
  633. font-size: 28rpx;
  634. font-weight: bold;
  635. }
  636. &:active {
  637. transform: scale(0.95);
  638. }
  639. }
  640. }
  641. .bottom-placeholder {
  642. height: 60rpx;
  643. }
  644. </style>