api.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /**
  2. * API 接口配置文件
  3. */
  4. // 开发环境和生产环境的 API 基础地址
  5. // 所有请求通过网关转发
  6. const BASE_URL = process.env.NODE_ENV === 'development'
  7. ? 'http://localhost:8083/api' // 开发环境 - 通过网关
  8. : 'https://your-domain.com/api' // 生产环境
  9. /**
  10. * 封装 uni.request
  11. */
  12. const request = (options) => {
  13. return new Promise((resolve, reject) => {
  14. const token = uni.getStorageSync('token')
  15. const headers = {
  16. 'Content-Type': 'application/json'
  17. }
  18. if (token) {
  19. headers['Authorization'] = token
  20. }
  21. uni.request({
  22. url: (/^https?:\/\//.test(options.url) ? options.url : (BASE_URL + options.url)),
  23. method: options.method || 'GET',
  24. data: options.data || {},
  25. header: headers,
  26. dataType: 'json',
  27. success: (res) => {
  28. console.log('API请求成功:', options.url, '状态码:', res.statusCode, '返回数据:', res.data)
  29. if (res.statusCode === 200) {
  30. // 根据后端约定的数据格式处理
  31. if (res.data.code === 200 || res.data.code === 0 || res.data.success) {
  32. resolve(res.data.data || res.data)
  33. } else if (res.data.code === 403) {
  34. // 业务层面的403错误,清除token并跳转登录
  35. uni.removeStorageSync('token')
  36. uni.removeStorageSync('userInfo')
  37. uni.showToast({
  38. title: res.data.message || res.data.msg || '拒绝访问,请重新登录',
  39. icon: 'none'
  40. })
  41. uni.navigateTo({
  42. url: '/pages/page3/page3'
  43. })
  44. reject(res.data)
  45. } else {
  46. uni.showToast({
  47. title: res.data.message || res.data.msg || '请求失败',
  48. icon: 'none'
  49. })
  50. reject(res.data)
  51. }
  52. } else if (res.statusCode === 401) {
  53. // 未授权,跳转登录
  54. uni.removeStorageSync('token')
  55. uni.removeStorageSync('userInfo')
  56. uni.showToast({
  57. title: '未授权,请重新登录',
  58. icon: 'none'
  59. })
  60. uni.navigateTo({
  61. url: '/pages/page3/page3'
  62. })
  63. reject(res)
  64. } else if (res.statusCode === 403) {
  65. // 拒绝访问,清除token并跳转登录
  66. uni.removeStorageSync('token')
  67. uni.removeStorageSync('userInfo')
  68. uni.showToast({
  69. title: '拒绝访问,请重新登录',
  70. icon: 'none'
  71. })
  72. uni.navigateTo({
  73. url: '/pages/page3/page3'
  74. })
  75. reject(res)
  76. } else {
  77. uni.showToast({
  78. title: '网络请求失败',
  79. icon: 'none'
  80. })
  81. reject(res)
  82. }
  83. },
  84. fail: (err) => {
  85. console.error('API请求失败:', options.url, '错误信息:', err)
  86. uni.showToast({
  87. title: (err && err.errMsg) ? err.errMsg.replace('request:','') : '网络连接失败',
  88. icon: 'none'
  89. })
  90. reject(err)
  91. }
  92. })
  93. })
  94. }
  95. /**
  96. * API 接口列表
  97. */
  98. export default {
  99. // 地区
  100. area: {
  101. // 获取省份列表
  102. getProvinces: () => request({ url: '/recommend/area/provinces' }),
  103. // 获取城市列表(可选省份ID)
  104. getCities: (provinceId) => {
  105. const url = provinceId ? `/recommend/area/cities?provinceId=${provinceId}` : '/recommend/area/cities'
  106. return request({ url })
  107. },
  108. // 获取区域列表(根据城市ID)
  109. getAreas: (cityId) => request({ url: `/recommend/area/areas?cityId=${cityId}` })
  110. },
  111. // 用户相关
  112. user: {
  113. // 获取用户信息
  114. getInfo: () => request({ url: '/user/info' }),
  115. // 获取指定用户的详细信息(包含简介和照片)
  116. getDetailInfo: (userId) => request({
  117. url: `/user/info?userId=${userId}`
  118. }),
  119. // 查询用户是否为红娘
  120. getMatchmakerStatus: (userId) => request({
  121. url: `/user/matchmaker-status?userId=${userId}`
  122. }),
  123. // 获取今日匹配数
  124. getMatchCount: () => request({ url: '/user/match-count' }),
  125. // 更新用户基本信息(昵称、头像等)
  126. updateInfo: (data) => request({
  127. url: '/user/basic',
  128. method: 'PUT',
  129. data
  130. }),
  131. // 更新单个字段
  132. updateField: (userId, fieldName, fieldValue) => request({
  133. url: `/user/basic/field?userId=${userId}&fieldName=${fieldName}&fieldValue=${encodeURIComponent(fieldValue)}`,
  134. method: 'PUT'
  135. })
  136. },
  137. // 认证相关
  138. auth: {
  139. // 密码登录(手机号+密码)
  140. loginByPassword: (phone, password) => request({
  141. // 直连 login 服务(开发环境),避免网关未配置导致未路由
  142. url: 'http://localhost:8087/api/login/password',
  143. method: 'POST',
  144. data: { phone, password }
  145. }),
  146. // 发送登录验证码(走网关 -> 登录服务)
  147. sendCode: (phone) => request({
  148. url: '/login/send-code',
  149. method: 'POST',
  150. data: { phone }
  151. }),
  152. // 验证码登录(走网关 -> 登录服务)
  153. smsLogin: (phone, code) => request({
  154. url: '/login/sms-login',
  155. method: 'POST',
  156. data: { phone, code }
  157. }),
  158. // 微信登录(直连 login 服务)
  159. wechatLogin: (data) => request({
  160. url: 'http://localhost:8087/api/login/wechat/login',
  161. method: 'POST',
  162. data: data // ✅ 传递完整的登录数据对象(包含code, nickname, avatarUrl, phoneCode)
  163. }),
  164. // 获取微信手机号(直连 login 服务)
  165. wechatPhone: (code) => request({
  166. url: 'http://localhost:8087/api/login/wechat/phone',
  167. method: 'POST',
  168. data: { code }
  169. })
  170. },
  171. // 首页相关
  172. home: {
  173. // 获取轮播图
  174. getBanners: () => request({ url: '/home/banners' }),
  175. // 获取公告列表
  176. getNotices: () => request({ url: '/announcement/active' }),
  177. // 获取首页金刚区功能列表
  178. getFunctionGrid: () => request({ url: '/home/function-grid' }),
  179. // 根据类型获取金刚区功能列表
  180. getFunctionGridByType: (type) => request({ url: `/home/function-grid/type?type=${type}` }),
  181. // 获取未读消息数
  182. getUnreadCount: () => request({ url: '/home/unread-count' })
  183. },
  184. // 成功案例
  185. successCase: {
  186. // 获取成功案例列表
  187. getList: (params) => request({
  188. url: '/success-case/list',
  189. method: 'GET',
  190. data: params
  191. }),
  192. // 获取成功案例详情
  193. getDetail: (caseNo) => request({
  194. url: `/success-case/detail/${caseNo}`,
  195. method: 'GET'
  196. }),
  197. // 获取案例时间线
  198. getTimeline: (caseNo) => request({
  199. url: `/success-case/timeline/${caseNo}`,
  200. method: 'GET'
  201. })
  202. },
  203. // 活动相关
  204. activity: {
  205. // 获取活动列表
  206. getList: (params) => request({
  207. url: '/activity/list',
  208. method: 'GET',
  209. data: params
  210. }),
  211. // 获取活动详情
  212. getDetail: (id) => request({
  213. url: `/activity/detail/${id}`,
  214. method: 'GET'
  215. }),
  216. // 报名活动
  217. register: (activityId, userId) => request({
  218. url: `/activity/register/${activityId}?userId=${userId}`,
  219. method: 'POST'
  220. }),
  221. // 取消报名
  222. cancelRegister: (activityId) => request({
  223. url: `/activity/cancel/${activityId}`,
  224. method: 'POST'
  225. }),
  226. // 获取我的活动列表
  227. getMyActivities: (params) => request({
  228. url: '/activity/my',
  229. method: 'GET',
  230. data: params
  231. }),
  232. // 创建活动订单并获取支付参数
  233. createOrder: (userId, activityId, activityName, price) => request({
  234. url: '/activity-order/create',
  235. method: 'POST',
  236. data: {
  237. userId,
  238. activityId,
  239. activityName,
  240. price
  241. }
  242. })
  243. },
  244. // 课程相关
  245. course: {
  246. // 获取课程列表
  247. getList: (params) => request({
  248. url: '/course/list',
  249. method: 'GET',
  250. data: params
  251. }),
  252. // 获取课程详情(带学习进度)
  253. getDetail: (courseId, makerId) => request({
  254. url: `/course/detail/${courseId}${makerId ? '?makerId=' + makerId : ''}`,
  255. method: 'GET'
  256. }),
  257. // 更新学习进度
  258. updateProgress: (makerId, courseId, progress) => request({
  259. url: '/course/progress',
  260. method: 'POST',
  261. data: { makerId, courseId, progress }
  262. }),
  263. // 完成课程(领取积分)
  264. complete: (makerId, courseId) => request({
  265. url: '/course/complete',
  266. method: 'POST',
  267. data: { makerId, courseId }
  268. }),
  269. // 获取我的学习记录
  270. getMyProgress: (makerId) => request({
  271. url: `/course/my-progress?makerId=${makerId}`,
  272. method: 'GET'
  273. }),
  274. // 购买课程(旧接口-模拟)
  275. purchase: (courseId, data) => request({
  276. url: `/course/purchase/${courseId}`,
  277. method: 'POST',
  278. data
  279. }),
  280. // 积分兑换课程(红娘端 - 旧接口,已废弃)
  281. exchange: (data) => request({
  282. url: '/course/exchange',
  283. method: 'POST',
  284. data
  285. }),
  286. // 获取已兑换的课程列表(红娘端 - 旧接口,已废弃)
  287. getPurchasedList: (makerId) => request({
  288. url: `/course/purchased?makerId=${makerId}`,
  289. method: 'GET'
  290. })
  291. },
  292. // 红娘课程相关(独立于用户课程)
  293. matchmakerCourse: {
  294. // 获取红娘课程列表
  295. getList: (params = {}) => request({
  296. url: '/matchmaker-course/list',
  297. method: 'GET',
  298. data: params
  299. }),
  300. // 根据分类获取红娘课程列表
  301. getListByCategory: (categoryName) => request({
  302. url: `/matchmaker-course/list/category?categoryName=${encodeURIComponent(categoryName)}`,
  303. method: 'GET'
  304. }),
  305. // 获取所有课程分类
  306. getCategories: () => request({
  307. url: '/matchmaker-course/categories',
  308. method: 'GET'
  309. }),
  310. // 获取红娘课程详情
  311. getDetail: (id) => request({
  312. url: `/matchmaker-course/detail/${id}`,
  313. method: 'GET'
  314. }),
  315. // 检查是否已兑换
  316. checkExchanged: (makerId, courseId) => request({
  317. url: `/matchmaker-course/check-exchanged?makerId=${makerId}&courseId=${courseId}`,
  318. method: 'GET'
  319. }),
  320. // 积分兑换课程
  321. exchange: (data) => request({
  322. url: '/matchmaker-course/exchange',
  323. method: 'POST',
  324. data
  325. }),
  326. // 获取已兑换的课程列表
  327. getPurchasedList: (makerId) => request({
  328. url: `/matchmaker-course/purchased?makerId=${makerId}`,
  329. method: 'GET'
  330. })
  331. },
  332. // 课程订单相关(微信支付)
  333. courseOrder: {
  334. // 购买课程(获取微信支付参数)
  335. purchase: (data) => request({
  336. url: '/course-order/purchase',
  337. method: 'POST',
  338. data
  339. }),
  340. // 检查是否已购买课程
  341. checkPurchased: (userId, courseId) => request({
  342. url: `/course-order/check?userId=${userId}&courseId=${courseId}`,
  343. method: 'GET'
  344. }),
  345. // 获取已购买的课程列表
  346. getPurchasedCourses: (userId) => request({
  347. url: `/course-order/purchased?userId=${userId}`,
  348. method: 'GET'
  349. })
  350. },
  351. // 红娘相关
  352. matchmaker: {
  353. // 获取红娘列表
  354. getList: (params) => request({
  355. url: '/matchmaker/list',
  356. method: 'POST',
  357. data: params
  358. }),
  359. // 获取全职红娘列表
  360. getFormalList: (pageNum, pageSize) => request({
  361. url: `/matchmaker/formal?pageNum=${pageNum}&pageSize=${pageSize}`
  362. }),
  363. // 获取红娘详情
  364. getDetail: (id) => request({
  365. url: `/matchmaker/detail/${id}`
  366. }),
  367. // 根据userId查询红娘信息
  368. getByUserId: (userId) => request({
  369. url: `/matchmaker/by-user/${userId}`
  370. }),
  371. // 预约红娘
  372. book: (matchmakerId, data) => request({
  373. url: `/matchmaker/book/${matchmakerId}`,
  374. method: 'POST',
  375. data
  376. }),
  377. // 提交红娘申请
  378. submitApply: (data) => request({
  379. url: '/matchmaker-apply/submit',
  380. method: 'POST',
  381. data
  382. }),
  383. // 查询红娘申请状态
  384. getApplyStatus: (userId) => request({
  385. url: `/matchmaker-apply/status?userId=${userId}`,
  386. method: 'GET'
  387. }),
  388. // 工作台相关
  389. getWorkbenchData: () => request({ url: '/matchmaker/workbench/data' }),
  390. // 获取我的资源
  391. getMyResources: (params) => request({
  392. url: '/matchmaker/resources',
  393. method: 'GET',
  394. data: params
  395. }),
  396. // 获取排行榜数据(总排行榜)
  397. getRankingData: (params) => request({
  398. url: '/matchmaker/ranking',
  399. method: 'GET',
  400. data: params
  401. }),
  402. // 获取本周排行榜(按点赞数和成功人数平均数排名)
  403. getWeeklyRanking: (params) => request({
  404. url: '/matchmaker/weekly-ranking',
  405. method: 'GET',
  406. data: params
  407. }),
  408. // 给红娘点赞(一周只能给同一红娘点赞一次)
  409. likeMatchmaker: (userId, matchmakerId) => request({
  410. url: `/matchmaker/like?userId=${userId}&matchmakerId=${matchmakerId}`,
  411. method: 'POST'
  412. }),
  413. // 检查是否已点赞
  414. checkLikeStatus: (userId, matchmakerId) => request({
  415. url: `/matchmaker/check-like?userId=${userId}&matchmakerId=${matchmakerId}`,
  416. method: 'GET'
  417. }),
  418. // 签到相关
  419. checkinStatus: (makerId) => request({
  420. url: `/matchmaker/checkin/status?makerId=${makerId}`
  421. }),
  422. checkinStats: (makerId) => request({
  423. url: `/matchmaker/checkin/stats?makerId=${makerId}`
  424. }),
  425. doCheckin: (makerId) => request({
  426. url: `/matchmaker/checkin/do?makerId=${makerId}`,
  427. method: 'POST'
  428. }),
  429. // 更新红娘资料(编辑资料页使用)
  430. updateProfile: (matchmakerId, data) => request({
  431. url: `/matchmaker/update/${matchmakerId}`,
  432. method: 'PUT',
  433. data
  434. }),
  435. // 获取本月签到记录
  436. checkinList: (makerId, year, month) => request({
  437. url: `/matchmaker/checkin/list?makerId=${makerId}&year=${year}&month=${month}`
  438. })
  439. },
  440. // 推荐相关
  441. recommend: {
  442. // 获取推荐用户列表(网关转发到推荐服务)
  443. getUsers: ({ userId, oppoOnly = 1, limit = 20, excludeIds }) => {
  444. let url = `/recommend/users?userId=${userId}&oppoOnly=${oppoOnly}&limit=${limit}`;
  445. if (excludeIds) {
  446. url += `&excludeIds=${excludeIds}`;
  447. }
  448. return request({ url });
  449. },
  450. // 行为反馈:like/dislike
  451. feedback: ({ userId, targetUserId, type }) => request({
  452. url: `/recommend/feedback?userId=${userId}&targetUserId=${targetUserId}&type=${type}`
  453. }),
  454. // 曝光上报
  455. exposure: ({ userId, shownUserIds }) => request({
  456. url: `/recommend/exposure?userId=${userId}&shownUserIds=${encodeURIComponent(shownUserIds)}`
  457. }),
  458. // 规则检索
  459. search: (query) => request({
  460. url: '/recommend/search',
  461. method: 'POST',
  462. data: query
  463. }),
  464. // 获取今日推荐
  465. getTodayRecommend: () => request({
  466. url: '/recommend/today'
  467. })
  468. },
  469. // 消息相关
  470. message: {
  471. // 获取消息列表
  472. getList: (params) => request({
  473. url: '/message/list',
  474. data: params
  475. }),
  476. // 获取会话列表
  477. getConversations: () => request({
  478. url: '/message/conversations'
  479. }),
  480. // 发送消息
  481. send: (data) => request({
  482. url: '/message/send',
  483. method: 'POST',
  484. data
  485. }),
  486. // ===== 系统消息 =====
  487. getSystemList: async (userId, pageNum = 1, pageSize = 20) => {
  488. const res = await request({ url: `/message/system/list?userId=${userId}&pageNum=${pageNum}&pageSize=${pageSize}` })
  489. // 后端Result包装:{ code, data:{ list,total,page,pageSize } }
  490. return res.data || res
  491. },
  492. getSystemUnreadCount: async (userId) => {
  493. const res = await request({ url: `/message/system/unread-count?userId=${userId}` })
  494. return (typeof res.data === 'number') ? res.data : (res.data?.count || 0)
  495. },
  496. markSystemRead: (id) => request({
  497. url: `/message/system/read/${id}`,
  498. method: 'POST'
  499. }),
  500. getSystemDetail: async (id) => {
  501. const res = await request({ url: `/message/system/detail/${id}` })
  502. return res.data || res
  503. },
  504. markAllSystemRead: (userId) => request({
  505. url: `/message/system/read-all?userId=${userId}`,
  506. method: 'POST'
  507. })
  508. },
  509. // 动态相关
  510. dynamic: {
  511. // 获取推荐动态列表(广场)
  512. getRecommendList: (params) => request({
  513. url: '/dynamic/recommend',
  514. data: params
  515. }),
  516. // 获取动态列表
  517. getList: (params) => request({
  518. url: '/dynamic/list',
  519. data: params
  520. }),
  521. // 获取动态详情
  522. getDetail: (dynamicId, userId) => request({
  523. url: `/dynamic/detail/${dynamicId}`,
  524. data: { userId }
  525. }),
  526. // 发表评论
  527. addComment: (dynamicId, content, images, parentCommentId = 0) => request({
  528. url: `/dynamic/comment`,
  529. method: 'POST',
  530. data: { dynamicId, content, images, parentCommentId, userId: 1 },
  531. header: { 'Content-Type': 'application/json' }
  532. }),
  533. // 评论列表
  534. getComments: (dynamicId, pageNum = 1, pageSize = 10) => request({
  535. url: `/dynamic/comment/list/${dynamicId}`,
  536. data: { pageNum, pageSize }
  537. }),
  538. // 评论点赞/取消
  539. likeComment: (commentId, userId) => {
  540. // 如果没有传入userId,从本地存储获取
  541. if (!userId) {
  542. const userInfo = uni.getStorageSync('userInfo');
  543. userId = userInfo ? (userInfo.userId || userInfo.id) : null;
  544. }
  545. if (!userId) {
  546. return Promise.reject(new Error('用户未登录'));
  547. }
  548. return request({
  549. url: `/dynamic/comment/like?commentId=${commentId}&userId=${userId}`,
  550. method: 'POST'
  551. });
  552. },
  553. unlikeComment: (commentId, userId) => {
  554. // 如果没有传入userId,从本地存储获取
  555. if (!userId) {
  556. const userInfo = uni.getStorageSync('userInfo');
  557. userId = userInfo ? (userInfo.userId || userInfo.id) : null;
  558. }
  559. if (!userId) {
  560. return Promise.reject(new Error('用户未登录'));
  561. }
  562. return request({
  563. url: `/dynamic/comment/like/${commentId}?userId=${userId}`,
  564. method: 'DELETE'
  565. });
  566. },
  567. // 获取用户动态列表
  568. getUserDynamics: (userId, params) => {
  569. const { pageNum = 1, pageSize = 10, currentUserId = null } = params || {}
  570. let url = `/dynamic/user/${userId}?pageNum=${pageNum}&pageSize=${pageSize}`
  571. if (currentUserId) {
  572. url += `&currentUserId=${currentUserId}`
  573. }
  574. return request({
  575. url: url,
  576. method: 'GET'
  577. })
  578. },
  579. // 点赞动态
  580. like: (dynamicId, userId) => {
  581. // 如果没有传入userId,从本地存储获取
  582. if (!userId) {
  583. const userInfo = uni.getStorageSync('userInfo');
  584. userId = userInfo ? (userInfo.userId || userInfo.id) : null;
  585. }
  586. if (!userId) {
  587. return Promise.reject(new Error('用户未登录'));
  588. }
  589. return request({
  590. url: `/dynamic/like?dynamicId=${dynamicId}&userId=${userId}`,
  591. method: 'POST'
  592. });
  593. },
  594. // 取消点赞
  595. unlike: (dynamicId, userId) => {
  596. // 如果没有传入userId,从本地存储获取
  597. if (!userId) {
  598. const userInfo = uni.getStorageSync('userInfo');
  599. userId = userInfo ? (userInfo.userId || userInfo.id) : null;
  600. }
  601. if (!userId) {
  602. return Promise.reject(new Error('用户未登录'));
  603. }
  604. return request({
  605. url: `/dynamic/like/${dynamicId}?userId=${userId}`,
  606. method: 'DELETE'
  607. });
  608. },
  609. // 收藏动态
  610. favorite: (dynamicId, userId) => {
  611. // 如果没有传入userId,从本地存储获取
  612. if (!userId) {
  613. const userInfo = uni.getStorageSync('userInfo');
  614. userId = userInfo ? (userInfo.userId || userInfo.id) : null;
  615. }
  616. if (!userId) {
  617. return Promise.reject(new Error('用户未登录'));
  618. }
  619. return request({
  620. url: `/dynamic/favorite?dynamicId=${dynamicId}&userId=${userId}`,
  621. method: 'POST'
  622. });
  623. },
  624. // 取消收藏
  625. unfavorite: (dynamicId, userId) => {
  626. // 如果没有传入userId,从本地存储获取
  627. if (!userId) {
  628. const userInfo = uni.getStorageSync('userInfo');
  629. userId = userInfo ? (userInfo.userId || userInfo.id) : null;
  630. }
  631. if (!userId) {
  632. return Promise.reject(new Error('用户未登录'));
  633. }
  634. return request({
  635. url: `/dynamic/favorite/${dynamicId}?userId=${userId}`,
  636. method: 'DELETE'
  637. });
  638. },
  639. // 创建个人动态
  640. createUserDynamic: (payload) => request({
  641. url: '/dynamic/user',
  642. method: 'POST',
  643. data: payload,
  644. header: { 'Content-Type': 'application/json' }
  645. }),
  646. // 更新个人动态
  647. updateUserDynamic: (dynamicId, userId, payload) => request({
  648. url: `/dynamic/user/${dynamicId}?userId=${userId}`,
  649. method: 'PUT',
  650. data: payload,
  651. header: { 'Content-Type': 'application/json' }
  652. }),
  653. // 删除个人动态
  654. deleteUserDynamic: (dynamicId, userId) => request({
  655. url: `/dynamic/user/${dynamicId}?userId=${userId}`,
  656. method: 'DELETE'
  657. }),
  658. // 删除动态(旧接口,保留兼容)
  659. delete: (dynamicId) => request({
  660. url: `/dynamic/${dynamicId}`,
  661. method: 'DELETE'
  662. }),
  663. // 发布动态(文本或已存在的媒体URL列表)
  664. publish: (payload) => request({
  665. url: '/dynamic/publish',
  666. method: 'POST',
  667. data: payload,
  668. header: { 'Content-Type': 'application/json' }
  669. }),
  670. // 单个文件上传方法
  671. uploadSingle: (filePath) => {
  672. return new Promise((resolve, reject) => {
  673. console.log('开始上传文件:', filePath)
  674. console.log('上传URL:', BASE_URL + '/dynamic/publish/upload')
  675. uni.uploadFile({
  676. url: BASE_URL + '/dynamic/publish/upload',
  677. filePath: filePath,
  678. name: 'file',
  679. success: (res) => {
  680. console.log('上传响应:', res)
  681. try {
  682. const data = JSON.parse(res.data)
  683. console.log('解析后的响应数据:', data)
  684. if (data.code === 200 || data.code === 0 || data.success) {
  685. console.log('上传成功,返回URL:', data.data)
  686. resolve(data.data)
  687. } else {
  688. console.error('上传失败,服务器返回错误:', data)
  689. reject(new Error(data.message || '上传失败'))
  690. }
  691. } catch (e) {
  692. console.error('解析响应数据失败:', e, '原始响应:', res.data)
  693. reject(new Error('解析响应数据失败'))
  694. }
  695. },
  696. fail: (error) => {
  697. console.error('上传请求失败:', error)
  698. reject(new Error('上传请求失败: ' + (error.errMsg || '未知错误')))
  699. }
  700. })
  701. })
  702. },
  703. // 批量上传多个文件(遍历调用单个上传)
  704. uploadBatch: (filePaths) => {
  705. return new Promise(async (resolve, reject) => {
  706. const urls = []
  707. try {
  708. for (let filePath of filePaths) {
  709. const url = await new Promise((resolveUpload, rejectUpload) => {
  710. uni.uploadFile({
  711. url: BASE_URL + '/dynamic/publish/upload',
  712. filePath: filePath,
  713. name: 'file',
  714. success: (res) => {
  715. try {
  716. const data = JSON.parse(res.data)
  717. if (data.code === 200 || data.code === 0 || data.success) {
  718. resolveUpload(data.data)
  719. } else {
  720. rejectUpload(data)
  721. }
  722. } catch (e) {
  723. rejectUpload(e)
  724. }
  725. },
  726. fail: rejectUpload
  727. })
  728. })
  729. urls.push(url)
  730. }
  731. resolve(urls)
  732. } catch (e) {
  733. reject(new Error(`批量上传失败: ${e.message || '未知错误'}`))
  734. }
  735. })
  736. },
  737. // 提交举报
  738. submitReport: (data) => request({
  739. url: '/dynamic/report',
  740. method: 'POST',
  741. data
  742. }),
  743. // 获取用户收藏列表
  744. getFavoritesList: (userId, pageNum = 1, pageSize = 10) => request({
  745. url: `/dynamic/favorites?userId=${userId}&pageNum=${pageNum}&pageSize=${pageSize}`
  746. }),
  747. // 获取用户点赞列表
  748. getLikedList: (userId, pageNum = 1, pageSize = 10) => request({
  749. url: `/dynamic/likes?userId=${userId}&pageNum=${pageNum}&pageSize=${pageSize}`
  750. }),
  751. // 获取用户浏览记录列表
  752. getBrowseHistoryList: (userId, pageNum = 1, pageSize = 10) => request({
  753. url: `/dynamic/browse-history?userId=${userId}&pageNum=${pageNum}&pageSize=${pageSize}`
  754. }),
  755. // 清空用户浏览记录
  756. clearBrowseHistory: (userId) => request({
  757. url: `/dynamic/browse-history?userId=${userId}`,
  758. method: 'DELETE'
  759. })
  760. },
  761. // VIP相关
  762. vip: {
  763. // 获取VIP信息(状态、套餐等)
  764. getInfo: (userId) => request({
  765. url: `/vip/info?userId=${userId}`
  766. }),
  767. // 获取VIP套餐列表
  768. getPackages: () => request({
  769. url: '/vip/packages'
  770. }),
  771. // 购买VIP套餐(获取支付参数)
  772. purchase: (userId, packageId) => request({
  773. url: '/vip/purchase',
  774. method: 'POST',
  775. data: { userId, packageId }
  776. }),
  777. // 查询订单状态
  778. getOrderStatus: (orderNo) => request({
  779. url: `/vip/order/status?orderNo=${orderNo}`
  780. }),
  781. // 新增:查询支付状态(userId + packageId)
  782. checkPayStatus: (userId, packageId) => request({
  783. url: '/vip/checkPayStatus',
  784. method: 'GET',
  785. data: { userId, packageId }
  786. })
  787. },
  788. // 用户反馈
  789. feedback: {
  790. // 提交用户反馈
  791. submit: (data) => request({
  792. url: '/feedback/submit',
  793. method: 'POST',
  794. data
  795. }),
  796. // 上传反馈图片
  797. uploadImage: (filePath) => {
  798. return new Promise((resolve, reject) => {
  799. uni.uploadFile({
  800. url: BASE_URL + '/feedback/upload',
  801. filePath: filePath,
  802. name: 'file',
  803. success: (res) => {
  804. try {
  805. const data = JSON.parse(res.data)
  806. if (data.code === 200 || data.code === 0 || data.success) {
  807. resolve(data.data)
  808. } else {
  809. reject(new Error(data.message || '上传失败'))
  810. }
  811. } catch (e) {
  812. reject(new Error('解析响应数据失败'))
  813. }
  814. },
  815. fail: (error) => {
  816. reject(new Error('上传请求失败: ' + (error.errMsg || '未知错误')))
  817. }
  818. })
  819. })
  820. }
  821. },
  822. // 积分商城相关
  823. pointsMall: {
  824. // 获取商品列表
  825. getProducts: (params) => request({
  826. url: '/points/products',
  827. method: 'GET',
  828. data: params
  829. }),
  830. // 获取推荐商品
  831. getRecommendProducts: (limit = 10) => request({
  832. url: `/points/products/recommend?limit=${limit}`,
  833. method: 'GET'
  834. }),
  835. // 获取商品详情
  836. getProductDetail: (id) => request({
  837. url: `/points/products/${id}`,
  838. method: 'GET'
  839. }),
  840. // 获取积分余额
  841. getBalance: (makerId) => request({
  842. url: `/points/balance?makerId=${makerId}`,
  843. method: 'GET'
  844. }),
  845. // 获取积分明细
  846. getRecords: (makerId, pageNum = 1, pageSize = 20) => request({
  847. url: `/points/records?makerId=${makerId}&pageNum=${pageNum}&pageSize=${pageSize}`,
  848. method: 'GET'
  849. }),
  850. // 获取积分规则
  851. getRules: () => request({
  852. url: '/points/rules',
  853. method: 'GET'
  854. }),
  855. // 兑换商品
  856. exchange: (data) => request({
  857. url: '/points/exchange',
  858. method: 'POST',
  859. data
  860. }),
  861. // 获取订单列表
  862. getOrders: (makerId, status, pageNum = 1, pageSize = 10) => {
  863. let url = `/points/orders?makerId=${makerId}&pageNum=${pageNum}&pageSize=${pageSize}`
  864. if (status !== undefined && status !== null) {
  865. url += `&status=${status}`
  866. }
  867. return request({ url, method: 'GET' })
  868. },
  869. // 获取订单详情
  870. getOrderDetail: (orderNo) => request({
  871. url: `/points/orders/${orderNo}`,
  872. method: 'GET'
  873. }),
  874. // 增加积分(签到等)
  875. addPoints: (makerId, ruleType, reason) => request({
  876. url: '/points/add',
  877. method: 'POST',
  878. data: { makerId, ruleType, reason }
  879. })
  880. },
  881. // 我的资源相关(通过网关访问8081服务)
  882. myResource: {
  883. // 获取资源列表
  884. getList: (matchmakerId, keyword, pageNum = 1, pageSize = 10) => {
  885. let url = `/my-resource/list?matchmakerId=${matchmakerId}&pageNum=${pageNum}&pageSize=${pageSize}`
  886. if (keyword) {
  887. url += `&keyword=${encodeURIComponent(keyword)}`
  888. }
  889. return request({ url })
  890. },
  891. // 搜索资源(按姓名或手机号)
  892. search: (matchmakerId, keyword, gender) => {
  893. let url = `/my-resource/search?matchmakerId=${matchmakerId}`
  894. if (keyword) {
  895. url += `&keyword=${encodeURIComponent(keyword)}`
  896. }
  897. if (gender) {
  898. url += `&gender=${gender}`
  899. }
  900. return request({ url })
  901. },
  902. // 获取资源下拉列表
  903. getDropdown: (matchmakerId, gender) => {
  904. let url = `/my-resource/dropdown?matchmakerId=${matchmakerId}`
  905. if (gender) {
  906. url += `&gender=${gender}`
  907. }
  908. return request({ url })
  909. },
  910. // 获取已注册用户的资源下拉列表(user_id不为空)
  911. getRegisteredDropdown: (matchmakerId, gender) => {
  912. let url = `/my-resource/registered-dropdown?matchmakerId=${matchmakerId}`
  913. if (gender) {
  914. url += `&gender=${gender}`
  915. }
  916. return request({ url })
  917. },
  918. // 搜索已注册用户的资源(user_id不为空)
  919. searchRegistered: (matchmakerId, keyword, gender) => {
  920. let url = `/my-resource/registered-search?matchmakerId=${matchmakerId}`
  921. if (keyword) {
  922. url += `&keyword=${encodeURIComponent(keyword)}`
  923. }
  924. if (gender) {
  925. url += `&gender=${gender}`
  926. }
  927. return request({ url })
  928. }
  929. },
  930. // 撮合成功案例上传相关(通过网关访问1004服务)
  931. successCaseUpload: {
  932. // 提交成功案例
  933. submit: (data) => request({
  934. url: '/success-case-upload/submit',
  935. method: 'POST',
  936. data
  937. }),
  938. // 获取成功案例列表
  939. getList: (matchmakerId, pageNum = 1, pageSize = 10) => request({
  940. url: `/success-case-upload/list?matchmakerId=${matchmakerId}&pageNum=${pageNum}&pageSize=${pageSize}`
  941. }),
  942. // 获取审核记录列表
  943. getAuditRecords: (matchmakerId, auditStatus, pageNum = 1, pageSize = 20) => {
  944. let url = `/success-case-upload/audit-records?matchmakerId=${matchmakerId}&pageNum=${pageNum}&pageSize=${pageSize}`
  945. if (auditStatus !== null && auditStatus !== undefined) {
  946. url += `&auditStatus=${auditStatus}`
  947. }
  948. return request({ url })
  949. },
  950. // 获取审核记录详情
  951. getAuditRecordDetail: (id) => request({
  952. url: `/success-case-upload/audit-records/${id}`
  953. }),
  954. // 标记审核记录为已读
  955. markAsRead: (id) => request({
  956. url: `/success-case-upload/audit-records/${id}/read`,
  957. method: 'POST'
  958. }),
  959. // 获取未读审核记录数量
  960. getUnreadCount: (matchmakerId) => request({
  961. url: `/success-case-upload/audit-records/unread-count?matchmakerId=${matchmakerId}`
  962. })
  963. }
  964. }
  965. // 导出 request 函数供其他模块使用
  966. export { request }