api.js 32 KB

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