index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. <template>
  2. <view class="match-container">
  3. <!-- 顶部导航栏 -->
  4. <view class="custom-navbar">
  5. <view class="navbar-back" @click="goBack">
  6. <text class="back-icon">←</text>
  7. <text class="back-text">返回</text>
  8. </view>
  9. <view class="navbar-title">智能匹配</view>
  10. <view class="navbar-placeholder"></view>
  11. </view>
  12. <!-- 顶部提示 -->
  13. <view class="tip-box">
  14. <text class="tip-icon">⚡</text>
  15. <text class="tip-text">智能匹配系统正在为您寻找最合适的TA</text>
  16. </view>
  17. <!-- 匹配动画区域 -->
  18. <view class="match-animation">
  19. <view class="heart-container" v-if="isMatching">
  20. <text class="heart">❤️</text>
  21. <text class="heart">❤️</text>
  22. </view>
  23. <text class="match-status">{{ matchStatus }}</text>
  24. </view>
  25. <!-- 匹配方式选择 -->
  26. <view class="match-modes">
  27. <view class="mode-item" @click="selectMode('smart')">
  28. <view class="mode-icon">🎯</view>
  29. <view class="mode-title">智能算法</view>
  30. <view class="mode-desc">基于兴趣爱好、性格特征智能匹配</view>
  31. </view>
  32. <view class="mode-item" @click="selectMode('online')">
  33. <view class="mode-icon">⚡</view>
  34. <view class="mode-title">实时在线</view>
  35. <view class="mode-desc">只匹配当前在线的优质用户</view>
  36. </view>
  37. <view class="mode-item" @click="selectMode('precise')">
  38. <view class="mode-icon">💎</view>
  39. <view class="mode-title">精准推荐</view>
  40. <view class="mode-desc">根据您的偏好推荐最合适的对象</view>
  41. </view>
  42. </view>
  43. <!-- 开始匹配按钮 -->
  44. <view class="match-button-container" v-if="!isMatching">
  45. <button class="start-match-btn" @click="startMatch">
  46. <text class="btn-text">开始匹配</text>
  47. </button>
  48. </view>
  49. <!-- 取消匹配按钮 -->
  50. <view class="match-button-container" v-else>
  51. <button class="cancel-match-btn" @click="cancelMatch">
  52. <text class="btn-text">取消匹配</text>
  53. </button>
  54. </view>
  55. <!-- 匹配成功弹窗 -->
  56. <uni-popup ref="matchSuccessPopup" type="center">
  57. <view class="success-popup">
  58. <view class="success-icon">🎉</view>
  59. <view class="success-title">匹配成功!</view>
  60. <view class="success-score">匹配度:{{ matchScore }}%</view>
  61. <view class="matched-user-info">
  62. <image class="user-avatar" :src="matchedUser.avatarUrl || '/static/default-avatar.svg'" mode="aspectFill"></image>
  63. <view class="user-name">{{ matchedUser.nickname }}</view>
  64. <view class="user-detail">{{ matchedUser.age }}岁 | {{ matchedUser.city }}</view>
  65. <view class="user-interests">
  66. <text class="interest-tag" v-for="(interest, index) in matchedUser.interests" :key="index">
  67. {{ interest }}
  68. </text>
  69. </view>
  70. </view>
  71. <view class="popup-buttons">
  72. <button class="btn-chat" @click="goToChat">开始聊天</button>
  73. <button class="btn-continue" @click="continueMatch">继续匹配</button>
  74. </view>
  75. </view>
  76. </uni-popup>
  77. </view>
  78. </template>
  79. <script>
  80. export default {
  81. data() {
  82. return {
  83. isMatching: false,
  84. matchStatus: '点击下方按钮开始匹配',
  85. selectedMode: 'smart',
  86. matchScore: 0,
  87. matchedUser: {},
  88. userInfo: {},
  89. pollingTimer: null
  90. }
  91. },
  92. onLoad() {
  93. this.loadUserInfo();
  94. },
  95. onUnload() {
  96. // 页面卸载时停止匹配
  97. if (this.isMatching) {
  98. this.cancelMatch();
  99. }
  100. if (this.pollingTimer) {
  101. clearInterval(this.pollingTimer);
  102. }
  103. },
  104. methods: {
  105. // 加载用户信息
  106. loadUserInfo() {
  107. // 从本地存储获取用户信息
  108. const userInfo = uni.getStorageSync('userInfo');
  109. if (userInfo) {
  110. this.userInfo = userInfo;
  111. } else {
  112. uni.showToast({
  113. title: '请先登录',
  114. icon: 'none'
  115. });
  116. setTimeout(() => {
  117. uni.navigateBack();
  118. }, 1500);
  119. }
  120. },
  121. // 选择匹配模式
  122. selectMode(mode) {
  123. this.selectedMode = mode;
  124. uni.showToast({
  125. title: mode === 'smart' ? '智能算法' : mode === 'online' ? '实时在线' : '精准推荐',
  126. icon: 'none'
  127. });
  128. },
  129. // 开始匹配
  130. async startMatch() {
  131. if (!this.userInfo.userId) {
  132. uni.showToast({
  133. title: '用户信息不完整',
  134. icon: 'none'
  135. });
  136. return;
  137. }
  138. this.isMatching = true;
  139. this.matchStatus = '正在匹配中...';
  140. try {
  141. // 构建匹配数据
  142. const matchData = {
  143. userId: String(this.userInfo.userId),
  144. nickname: this.userInfo.nickname || '用户',
  145. gender: this.userInfo.gender || 1,
  146. age: this.userInfo.age || 25,
  147. avatarUrl: this.userInfo.avatarUrl || '',
  148. interests: this.userInfo.interests || ['旅游', '美食'],
  149. city: this.userInfo.city || '未知',
  150. introduction: this.userInfo.introduction || '',
  151. latitude: this.userInfo.latitude || null,
  152. longitude: this.userInfo.longitude || null
  153. };
  154. console.log('发送匹配请求:', matchData);
  155. // 调用匹配接口
  156. // 开发环境使用本地地址,生产环境使用服务器地址
  157. const baseUrl = 'http://localhost:8083'; // 本地开发
  158. // const baseUrl = 'http://115.190.125.125:8083'; // 生产环境
  159. const [error, res] = await uni.request({
  160. url: `${baseUrl}/match/start`,
  161. method: 'POST',
  162. header: {
  163. 'Content-Type': 'application/json'
  164. },
  165. data: matchData
  166. });
  167. console.log('匹配响应:', res);
  168. console.log('匹配错误:', error);
  169. // 检查是否有错误或响应无效
  170. if (error || !res || !res.data) {
  171. this.isMatching = false;
  172. this.matchStatus = '点击下方按钮开始匹配';
  173. uni.showToast({
  174. title: '服务器连接失败,请确保后端服务已启动',
  175. icon: 'none',
  176. duration: 3000
  177. });
  178. return;
  179. }
  180. // 检查 HTTP 状态码
  181. if (res.statusCode !== 200) {
  182. this.isMatching = false;
  183. this.matchStatus = '点击下方按钮开始匹配';
  184. uni.showToast({
  185. title: `服务器错误 (${res.statusCode}),请联系管理员`,
  186. icon: 'none',
  187. duration: 3000
  188. });
  189. console.error('HTTP错误:', res.statusCode, res.data);
  190. return;
  191. }
  192. if (res.data.code === 200) {
  193. console.log('=== 匹配成功,解析数据 ===');
  194. console.log('status:', res.data.status);
  195. console.log('完整data:', res.data.data);
  196. if (res.data.status === 'success') {
  197. // 立即匹配成功
  198. this.handleMatchSuccess(res.data.data);
  199. } else if (res.data.status === 'waiting') {
  200. // 等待匹配,开始轮询
  201. this.matchStatus = '正在寻找合适的对象...';
  202. this.startPolling();
  203. } else {
  204. this.isMatching = false;
  205. this.matchStatus = '点击下方按钮开始匹配';
  206. uni.showToast({
  207. title: res.data.msg || '匹配失败',
  208. icon: 'none'
  209. });
  210. }
  211. } else {
  212. this.isMatching = false;
  213. this.matchStatus = '点击下方按钮开始匹配';
  214. uni.showToast({
  215. title: res.data.msg || '匹配失败',
  216. icon: 'none'
  217. });
  218. }
  219. } catch (error) {
  220. console.error('匹配请求失败:', error);
  221. this.isMatching = false;
  222. this.matchStatus = '点击下方按钮开始匹配';
  223. uni.showToast({
  224. title: '网络错误,请重试',
  225. icon: 'none'
  226. });
  227. }
  228. },
  229. // 开始轮询匹配状态
  230. startPolling() {
  231. // 每2秒检查一次匹配状态(实际项目中可以用WebSocket)
  232. this.pollingTimer = setInterval(() => {
  233. // 这里可以调用一个查询匹配状态的接口
  234. // 暂时使用定时器模拟
  235. console.log('轮询匹配状态...');
  236. }, 2000);
  237. // 30秒后停止匹配
  238. setTimeout(() => {
  239. if (this.isMatching) {
  240. this.cancelMatch();
  241. uni.showToast({
  242. title: '暂无合适的匹配对象,请稍后再试',
  243. icon: 'none'
  244. });
  245. }
  246. }, 30000);
  247. },
  248. // 取消匹配
  249. async cancelMatch() {
  250. try {
  251. const baseUrl = 'http://localhost:8083'; // 本地开发
  252. // const baseUrl = 'http://115.190.125.125:8083'; // 生产环境
  253. const userId = String(this.userInfo.userId);
  254. const res = await uni.request({
  255. url: `${baseUrl}/match/cancel?userId=${userId}`,
  256. method: 'POST'
  257. });
  258. console.log('取消匹配响应:', res.data);
  259. uni.showToast({
  260. title: '已取消匹配',
  261. icon: 'none'
  262. });
  263. } catch (error) {
  264. console.error('取消匹配失败:', error);
  265. }
  266. this.isMatching = false;
  267. this.matchStatus = '点击下方按钮开始匹配';
  268. if (this.pollingTimer) {
  269. clearInterval(this.pollingTimer);
  270. this.pollingTimer = null;
  271. }
  272. },
  273. // 处理匹配成功
  274. handleMatchSuccess(data) {
  275. console.log('=== 处理匹配成功数据 ===');
  276. console.log('匹配数据:', data);
  277. this.isMatching = false;
  278. this.matchStatus = '匹配成功!';
  279. if (this.pollingTimer) {
  280. clearInterval(this.pollingTimer);
  281. this.pollingTimer = null;
  282. }
  283. // 解析匹配数据 - 兼容不同的数据结构
  284. this.matchScore = Math.round(data.matchScore || data.roomId ? 50 : 0);
  285. // 尝试多种可能的数据结构
  286. let matchedUserInfo = null;
  287. // 方式1: data.user2Info
  288. if (data.user2Info && typeof data.user2Info === 'object') {
  289. matchedUserInfo = data.user2Info;
  290. }
  291. // 方式2: data.matchedUser
  292. else if (data.matchedUser && typeof data.matchedUser === 'object') {
  293. matchedUserInfo = data.matchedUser;
  294. }
  295. // 方式3: data直接包含用户信息
  296. else if (data.userId || data.nickname) {
  297. matchedUserInfo = data;
  298. }
  299. // 方式4: 嵌套在matchPair中
  300. else if (data.matchPair) {
  301. matchedUserInfo = data.matchPair.matchedUser || data.matchPair.user2Info || data.matchPair;
  302. }
  303. console.log('提取的匹配对象信息:', matchedUserInfo);
  304. // 设置匹配用户数据
  305. if (matchedUserInfo) {
  306. this.matchedUser = {
  307. userId: matchedUserInfo.userId || matchedUserInfo.user_id || '',
  308. nickname: matchedUserInfo.nickname || matchedUserInfo.name || '匹配用户',
  309. age: matchedUserInfo.age || 0,
  310. city: matchedUserInfo.city || '未知',
  311. avatarUrl: matchedUserInfo.avatarUrl || matchedUserInfo.avatar_url || '/static/default-avatar.svg',
  312. interests: matchedUserInfo.interests || []
  313. };
  314. } else {
  315. // 如果完全无法提取,使用默认值
  316. console.warn('无法提取匹配用户信息,使用默认值');
  317. this.matchedUser = {
  318. userId: '',
  319. nickname: '匹配用户',
  320. age: 0,
  321. city: '未知',
  322. avatarUrl: '/static/default-avatar.svg',
  323. interests: []
  324. };
  325. }
  326. console.log('最终匹配用户数据:', this.matchedUser);
  327. // 显示匹配成功弹窗
  328. this.$refs.matchSuccessPopup.open();
  329. },
  330. // 前往聊天
  331. goToChat() {
  332. this.$refs.matchSuccessPopup.close();
  333. // 跳转到聊天页面(修正参数名称)
  334. const targetUserId = this.matchedUser.userId;
  335. const targetUserName = encodeURIComponent(this.matchedUser.nickname || '用户');
  336. const targetUserAvatar = encodeURIComponent(this.matchedUser.avatarUrl || '/static/default-avatar.svg');
  337. uni.navigateTo({
  338. url: `/pages/message/chat?targetUserId=${targetUserId}&targetUserName=${targetUserName}&targetUserAvatar=${targetUserAvatar}`
  339. });
  340. },
  341. // 继续匹配
  342. continueMatch() {
  343. this.$refs.matchSuccessPopup.close();
  344. this.matchStatus = '点击下方按钮开始匹配';
  345. this.matchedUser = {};
  346. },
  347. // 返回上一页
  348. goBack() {
  349. // 如果正在匹配中,先取消匹配
  350. if (this.isMatching) {
  351. uni.showModal({
  352. title: '提示',
  353. content: '正在匹配中,确定要退出吗?',
  354. success: (res) => {
  355. if (res.confirm) {
  356. this.cancelMatch();
  357. setTimeout(() => {
  358. uni.navigateBack();
  359. }, 500);
  360. }
  361. }
  362. });
  363. } else {
  364. uni.navigateBack();
  365. }
  366. }
  367. }
  368. }
  369. </script>
  370. <style scoped>
  371. .match-container {
  372. min-height: 100vh;
  373. background: linear-gradient(180deg, #FFF5F7 0%, #FFFFFF 100%);
  374. padding: 0;
  375. }
  376. /* 自定义导航栏 */
  377. .custom-navbar {
  378. height: 88rpx;
  379. background: white;
  380. display: flex;
  381. align-items: center;
  382. justify-content: space-between;
  383. padding: 0 20rpx;
  384. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  385. position: sticky;
  386. top: 0;
  387. z-index: 999;
  388. }
  389. .navbar-back {
  390. display: flex;
  391. align-items: center;
  392. padding: 10rpx 20rpx;
  393. cursor: pointer;
  394. }
  395. .navbar-back:active {
  396. opacity: 0.7;
  397. }
  398. .back-icon {
  399. font-size: 40rpx;
  400. color: #E91E63;
  401. font-weight: bold;
  402. margin-right: 8rpx;
  403. }
  404. .back-text {
  405. font-size: 28rpx;
  406. color: #333;
  407. }
  408. .navbar-title {
  409. font-size: 32rpx;
  410. font-weight: bold;
  411. color: #333;
  412. position: absolute;
  413. left: 50%;
  414. transform: translateX(-50%);
  415. }
  416. .navbar-placeholder {
  417. width: 120rpx;
  418. }
  419. .tip-box {
  420. background: #E3F2FD;
  421. border-left: 6rpx solid #2196F3;
  422. padding: 24rpx;
  423. margin: 20rpx;
  424. border-radius: 12rpx;
  425. display: flex;
  426. align-items: center;
  427. }
  428. .tip-icon {
  429. font-size: 36rpx;
  430. margin-right: 16rpx;
  431. }
  432. .tip-text {
  433. font-size: 28rpx;
  434. color: #1976D2;
  435. flex: 1;
  436. }
  437. .match-animation {
  438. height: 500rpx;
  439. display: flex;
  440. flex-direction: column;
  441. justify-content: center;
  442. align-items: center;
  443. margin: 40rpx 0;
  444. }
  445. .heart-container {
  446. position: relative;
  447. width: 200rpx;
  448. height: 200rpx;
  449. animation: pulse 1.5s ease-in-out infinite;
  450. }
  451. .heart {
  452. font-size: 100rpx;
  453. position: absolute;
  454. top: 50%;
  455. left: 50%;
  456. transform: translate(-50%, -50%);
  457. }
  458. .heart:nth-child(1) {
  459. animation: heart-beat 1.5s ease-in-out infinite;
  460. }
  461. .heart:nth-child(2) {
  462. animation: heart-beat 1.5s ease-in-out 0.75s infinite;
  463. opacity: 0.6;
  464. }
  465. .match-status {
  466. margin-top: 60rpx;
  467. font-size: 32rpx;
  468. color: #E91E63;
  469. font-weight: bold;
  470. }
  471. .match-modes {
  472. margin: 40rpx 20rpx;
  473. }
  474. .mode-item {
  475. background: white;
  476. border-radius: 16rpx;
  477. padding: 32rpx;
  478. margin-bottom: 24rpx;
  479. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  480. transition: all 0.3s;
  481. }
  482. .mode-item:active {
  483. transform: scale(0.98);
  484. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  485. }
  486. .mode-icon {
  487. font-size: 60rpx;
  488. text-align: center;
  489. margin-bottom: 16rpx;
  490. }
  491. .mode-title {
  492. font-size: 32rpx;
  493. font-weight: bold;
  494. color: #333;
  495. text-align: center;
  496. margin-bottom: 12rpx;
  497. }
  498. .mode-desc {
  499. font-size: 24rpx;
  500. color: #999;
  501. text-align: center;
  502. line-height: 36rpx;
  503. }
  504. .match-button-container {
  505. position: fixed;
  506. bottom: 60rpx;
  507. left: 40rpx;
  508. right: 40rpx;
  509. }
  510. .start-match-btn {
  511. background: linear-gradient(135deg, #E91E63 0%, #C2185B 100%);
  512. color: white;
  513. border: none;
  514. border-radius: 50rpx;
  515. height: 100rpx;
  516. line-height: 100rpx;
  517. font-size: 36rpx;
  518. font-weight: bold;
  519. box-shadow: 0 8rpx 24rpx rgba(233, 30, 99, 0.4);
  520. }
  521. .start-match-btn:active {
  522. background: linear-gradient(135deg, #C2185B 0%, #AD1457 100%);
  523. }
  524. .cancel-match-btn {
  525. background: #FF9800;
  526. color: white;
  527. border: none;
  528. border-radius: 50rpx;
  529. height: 100rpx;
  530. line-height: 100rpx;
  531. font-size: 36rpx;
  532. font-weight: bold;
  533. box-shadow: 0 8rpx 24rpx rgba(255, 152, 0, 0.4);
  534. }
  535. .btn-text {
  536. color: white;
  537. }
  538. /* 匹配成功弹窗 */
  539. .success-popup {
  540. width: 600rpx;
  541. background: white;
  542. border-radius: 24rpx;
  543. padding: 60rpx 40rpx;
  544. text-align: center;
  545. }
  546. .success-icon {
  547. font-size: 120rpx;
  548. margin-bottom: 20rpx;
  549. }
  550. .success-title {
  551. font-size: 40rpx;
  552. font-weight: bold;
  553. color: #E91E63;
  554. margin-bottom: 16rpx;
  555. }
  556. .success-score {
  557. font-size: 28rpx;
  558. color: #666;
  559. margin-bottom: 40rpx;
  560. }
  561. .matched-user-info {
  562. background: #F5F5F5;
  563. border-radius: 16rpx;
  564. padding: 32rpx;
  565. margin-bottom: 40rpx;
  566. }
  567. .user-avatar {
  568. width: 120rpx;
  569. height: 120rpx;
  570. border-radius: 60rpx;
  571. margin: 0 auto 20rpx;
  572. display: block;
  573. }
  574. .user-name {
  575. font-size: 36rpx;
  576. font-weight: bold;
  577. color: #333;
  578. margin-bottom: 12rpx;
  579. }
  580. .user-detail {
  581. font-size: 28rpx;
  582. color: #999;
  583. margin-bottom: 20rpx;
  584. }
  585. .user-interests {
  586. display: flex;
  587. flex-wrap: wrap;
  588. justify-content: center;
  589. gap: 12rpx;
  590. }
  591. .interest-tag {
  592. background: #E91E63;
  593. color: white;
  594. padding: 8rpx 20rpx;
  595. border-radius: 20rpx;
  596. font-size: 24rpx;
  597. }
  598. .popup-buttons {
  599. display: flex;
  600. gap: 20rpx;
  601. }
  602. .btn-chat,
  603. .btn-continue {
  604. flex: 1;
  605. height: 80rpx;
  606. line-height: 80rpx;
  607. border-radius: 40rpx;
  608. font-size: 28rpx;
  609. border: none;
  610. }
  611. .btn-chat {
  612. background: #E91E63;
  613. color: white;
  614. }
  615. .btn-continue {
  616. background: white;
  617. color: #E91E63;
  618. border: 2rpx solid #E91E63 !important;
  619. }
  620. /* 动画 */
  621. @keyframes pulse {
  622. 0%, 100% {
  623. transform: scale(1);
  624. }
  625. 50% {
  626. transform: scale(1.1);
  627. }
  628. }
  629. @keyframes heart-beat {
  630. 0%, 100% {
  631. transform: translate(-50%, -50%) scale(1);
  632. opacity: 1;
  633. }
  634. 50% {
  635. transform: translate(-50%, -50%) scale(1.2);
  636. opacity: 0.8;
  637. }
  638. }
  639. </style>