index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. }, 800)
  296. },
  297. // 处理打招呼
  298. handleChat(user) {
  299. const greetings = [
  300. '你好,很高兴认识你!',
  301. 'Hi,看了你的资料很不错呢~',
  302. '你好,我们聊聊天吧!',
  303. 'Hello,可以交个朋友吗?'
  304. ]
  305. const randomGreeting = greetings[Math.floor(Math.random() * greetings.length)]
  306. uni.showModal({
  307. title: `向${user.nickname}打招呼`,
  308. content: `发送消息:"${randomGreeting}"`,
  309. showCancel: true,
  310. cancelText: '自定义',
  311. confirmText: '发送',
  312. success: (res) => {
  313. if (res.confirm) {
  314. uni.navigateTo({
  315. url: `/pages/message/chat?targetUserId=${user.userId||user.id}&targetUserName=${encodeURIComponent(user.nickname)}&message=${encodeURIComponent(randomGreeting)}`
  316. })
  317. } else {
  318. uni.navigateTo({
  319. url: `/pages/message/chat?targetUserId=${user.userId||user.id}&targetUserName=${encodeURIComponent(user.nickname)}`
  320. })
  321. }
  322. }
  323. })
  324. },
  325. // 跳转到个人资料
  326. goToProfile() {
  327. uni.navigateTo({
  328. url: '/pages/profile/index'
  329. })
  330. },
  331. // 返回
  332. goBack() {
  333. uni.navigateBack({
  334. fail: () => {
  335. // 返回失败时跳转首页
  336. uni.navigateTo({
  337. url: '/pages/index/index'
  338. })
  339. }
  340. })
  341. }
  342. }
  343. }
  344. </script>
  345. <style lang="scss" scoped>
  346. .today-recommend-page {
  347. min-height: 100vh;
  348. background: linear-gradient(180deg, #FFE5EE 0%, #FFF9F9 100%);
  349. padding-top: 90rpx;
  350. padding-bottom: 40rpx;
  351. }
  352. /* 自定义导航栏 */
  353. .custom-navbar {
  354. position: fixed;
  355. top: 0;
  356. left: 0;
  357. right: 0;
  358. height: 90rpx;
  359. display: flex;
  360. align-items: center;
  361. justify-content: space-between;
  362. padding: 0 20rpx;
  363. background: linear-gradient(135deg, #FF6B9D 0%, #FF8FAB 100%);
  364. z-index: 999;
  365. .navbar-left,
  366. .navbar-right {
  367. width: 80rpx;
  368. }
  369. .back-icon {
  370. font-size: 40rpx;
  371. color: #FFFFFF;
  372. font-weight: bold;
  373. }
  374. .navbar-title {
  375. flex: 1;
  376. text-align: center;
  377. font-size: 32rpx;
  378. font-weight: bold;
  379. color: #FFFFFF;
  380. }
  381. }
  382. /* 页面内容 */
  383. .page-content {
  384. padding: 20rpx 30rpx;
  385. }
  386. /* 推荐列表 */
  387. .recommend-list {
  388. .recommend-card {
  389. background: #FFFFFF;
  390. border-radius: 25rpx;
  391. margin-bottom: 30rpx;
  392. overflow: hidden;
  393. box-shadow: 0 8rpx 25rpx rgba(255, 107, 157, 0.15);
  394. border: 2rpx solid rgba(255, 107, 157, 0.1);
  395. .card-header {
  396. padding: 30rpx;
  397. display: flex;
  398. align-items: center;
  399. position: relative;
  400. background: linear-gradient(135deg, #FFF9FC 0%, #FFFFFF 100%);
  401. .user-avatar {
  402. width: 120rpx;
  403. height: 120rpx;
  404. border-radius: 60rpx;
  405. margin-right: 25rpx;
  406. border: 4rpx solid #FFE5EE;
  407. box-shadow: 0 4rpx 12rpx rgba(255, 107, 157, 0.2);
  408. }
  409. .user-basic-info {
  410. flex: 1;
  411. .user-name-row {
  412. display: flex;
  413. align-items: center;
  414. margin-bottom: 15rpx;
  415. .user-name {
  416. font-size: 34rpx;
  417. font-weight: bold;
  418. color: #333333;
  419. margin-right: 15rpx;
  420. }
  421. .vip-badge {
  422. background: linear-gradient(135deg, #FFD700 0%, #FFA500 100%);
  423. color: #FFFFFF;
  424. font-size: 20rpx;
  425. font-weight: bold;
  426. padding: 6rpx 15rpx;
  427. border-radius: 20rpx;
  428. box-shadow: 0 2rpx 8rpx rgba(255, 165, 0, 0.3);
  429. }
  430. }
  431. .user-details {
  432. display: flex;
  433. gap: 20rpx;
  434. .user-detail {
  435. font-size: 26rpx;
  436. color: #666666;
  437. background: rgba(255, 107, 157, 0.1);
  438. padding: 8rpx 16rpx;
  439. border-radius: 15rpx;
  440. }
  441. }
  442. }
  443. .online-status {
  444. position: absolute;
  445. top: 20rpx;
  446. right: 20rpx;
  447. display: flex;
  448. align-items: center;
  449. gap: 8rpx;
  450. .status-dot {
  451. width: 16rpx;
  452. height: 16rpx;
  453. border-radius: 50%;
  454. background: #CCCCCC;
  455. }
  456. .status-text {
  457. font-size: 22rpx;
  458. color: #999999;
  459. }
  460. &.online {
  461. .status-dot {
  462. background: #4CAF50;
  463. animation: blink 2s infinite;
  464. }
  465. .status-text {
  466. color: #4CAF50;
  467. }
  468. }
  469. }
  470. }
  471. .card-content {
  472. padding: 0 30rpx 20rpx;
  473. .user-info-grid {
  474. display: grid;
  475. grid-template-columns: 1fr 1fr;
  476. gap: 20rpx;
  477. margin-bottom: 25rpx;
  478. .info-item {
  479. background: #F8F9FA;
  480. padding: 20rpx;
  481. border-radius: 15rpx;
  482. .info-label {
  483. font-size: 22rpx;
  484. color: #999999;
  485. display: block;
  486. margin-bottom: 8rpx;
  487. }
  488. .info-value {
  489. font-size: 26rpx;
  490. color: #333333;
  491. font-weight: 500;
  492. }
  493. }
  494. }
  495. .user-tags {
  496. display: flex;
  497. flex-wrap: wrap;
  498. gap: 12rpx;
  499. margin-bottom: 25rpx;
  500. .tag {
  501. background: linear-gradient(135deg, #FFE5EE 0%, #FFD0DC 100%);
  502. color: #FF6B8A;
  503. font-size: 22rpx;
  504. padding: 10rpx 18rpx;
  505. border-radius: 20rpx;
  506. font-weight: 500;
  507. }
  508. }
  509. .personal-intro {
  510. background: rgba(255, 107, 157, 0.05);
  511. padding: 20rpx;
  512. border-radius: 15rpx;
  513. border-left: 4rpx solid #FF6B9D;
  514. .intro-label {
  515. font-size: 24rpx;
  516. color: #FF6B9D;
  517. font-weight: bold;
  518. display: block;
  519. margin-bottom: 10rpx;
  520. }
  521. .intro-text {
  522. font-size: 26rpx;
  523. color: #666666;
  524. line-height: 1.6;
  525. }
  526. }
  527. }
  528. .card-actions {
  529. display: flex;
  530. padding: 25rpx 30rpx 30rpx;
  531. gap: 15rpx;
  532. background: linear-gradient(135deg, #FAFAFA 0%, #F5F5F5 100%);
  533. .action-btn {
  534. flex: 1;
  535. display: flex;
  536. flex-direction: column;
  537. align-items: center;
  538. justify-content: center;
  539. padding: 25rpx 0;
  540. border-radius: 20rpx;
  541. font-weight: 500;
  542. transition: all 0.3s;
  543. .action-icon {
  544. font-size: 36rpx;
  545. margin-bottom: 8rpx;
  546. }
  547. .action-text {
  548. font-size: 24rpx;
  549. }
  550. &.pass-btn {
  551. background: #FFFFFF;
  552. color: #666666;
  553. border: 2rpx solid #E9ECEF;
  554. &:active {
  555. background: #F8F9FA;
  556. transform: scale(0.95);
  557. }
  558. }
  559. &.like-btn {
  560. background: linear-gradient(135deg, #FF6B9D 0%, #FF8FAB 100%);
  561. color: #FFFFFF;
  562. box-shadow: 0 6rpx 20rpx rgba(255, 107, 157, 0.3);
  563. &:active {
  564. transform: scale(0.95);
  565. }
  566. }
  567. &.chat-btn {
  568. background: linear-gradient(135deg, #4CAF50 0%, #66BB6A 100%);
  569. color: #FFFFFF;
  570. box-shadow: 0 6rpx 20rpx rgba(76, 175, 80, 0.3);
  571. &:active {
  572. transform: scale(0.95);
  573. }
  574. }
  575. }
  576. }
  577. }
  578. }
  579. @keyframes blink {
  580. 0%, 100% { opacity: 1; }
  581. 50% { opacity: 0.5; }
  582. }
  583. /* 暂无推荐 */
  584. .no-recommend {
  585. text-align: center;
  586. padding: 100rpx 40rpx;
  587. .no-recommend-icon {
  588. font-size: 120rpx;
  589. display: block;
  590. margin-bottom: 30rpx;
  591. }
  592. .no-recommend-title {
  593. font-size: 36rpx;
  594. color: #333333;
  595. font-weight: bold;
  596. display: block;
  597. margin-bottom: 15rpx;
  598. }
  599. .no-recommend-tip {
  600. font-size: 28rpx;
  601. color: #666666;
  602. display: block;
  603. margin-bottom: 50rpx;
  604. }
  605. .complete-profile-btn {
  606. background: linear-gradient(135deg, #FF6B9D 0%, #FF8FAB 100%);
  607. color: #FFFFFF;
  608. padding: 25rpx 45rpx;
  609. border-radius: 35rpx;
  610. box-shadow: 0 8rpx 20rpx rgba(255, 107, 157, 0.3);
  611. display: inline-block;
  612. .btn-text {
  613. font-size: 28rpx;
  614. font-weight: bold;
  615. }
  616. &:active {
  617. transform: scale(0.95);
  618. }
  619. }
  620. }
  621. .bottom-placeholder {
  622. height: 60rpx;
  623. }
  624. </style>