user.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import Http from "../utils/http";
  2. import { host, isDebug } from "../utils/constant";
  3. const userHttp = new Http(host);
  4. export function fetchProfile() {
  5. return userHttp.get("/user/me").then((res) => {
  6. res.balance = (Number(res.balance) / 100).toFixed(2);
  7. res.discountAmount = (Number(res.discountAmount) / 100).toFixed(2);
  8. res.refundableAmount = (Number(res.refundableAmount) / 100).toFixed(2);
  9. // if (isDebug) {
  10. // res.userRechargeRightsList = [
  11. // {
  12. // discount: 90,
  13. // },
  14. // ];
  15. // }
  16. getApp<any>().globalData.user = res;
  17. return res;
  18. });
  19. }
  20. /**
  21. * 查询我的权益和优惠券
  22. */
  23. export function fetchRightsAndCoupons() {
  24. return userHttp.get("/account/listRightsAndCoupons").then((res) => {
  25. return res;
  26. });
  27. }
  28. export function fetchCollectList() {
  29. if (getApp<any>().globalData.collectIds) {
  30. return Promise.resolve(getApp<any>().globalData.collectIds);
  31. }
  32. return userHttp
  33. .get<
  34. {
  35. status: number;
  36. stationId: number;
  37. }[]
  38. >("/user/collectList?page=1&page_size=999")
  39. .then((res) => {
  40. getApp<any>().globalData.collectIds = res
  41. ? res
  42. .filter((item) => Number(item.status) === 1)
  43. .map((item) => {
  44. return Number(item.stationId);
  45. })
  46. : [];
  47. return getApp<any>().globalData.collectIds;
  48. });
  49. }
  50. export function addCollectList(sid: number) {
  51. let ids = getApp<any>().globalData.collectIds
  52. ? (getApp<any>().globalData.collectIds as number[])
  53. : [];
  54. const status = ids.includes(sid) ? 0 : 1;
  55. return userHttp
  56. .post("/user/collect", {
  57. data: {
  58. stationId: sid,
  59. status,
  60. },
  61. })
  62. .then(() => {
  63. if (status === 1) {
  64. ids.push(sid);
  65. } else {
  66. ids = ids.filter((id) => id !== sid);
  67. }
  68. getApp<any>().globalData.collectIds = ids;
  69. return status === 1;
  70. });
  71. }
  72. export function updateProfile(data: any) {
  73. return userHttp.put("/user", {
  74. data,
  75. });
  76. }
  77. declare const wx: any;
  78. export function insertMoney(amount: number) {
  79. if (isDebug) {
  80. return Promise.resolve();
  81. }
  82. return userHttp
  83. .post("/payment/wxPay", {
  84. data: {
  85. amount: parseInt(`${amount * 100}`),
  86. openid: getApp<any>().globalData.user.openid,
  87. },
  88. })
  89. .then((res: any) => {
  90. return new Promise((resolve, reject) => {
  91. // #ifdef MP-WEIXIN
  92. wx.requestPayment({
  93. timeStamp: `${res.timeStamp}`,
  94. nonceStr: res.nonceStr,
  95. package: res.packageVal,
  96. signType: res.signType,
  97. paySign: res.paySign,
  98. success(res: any) {
  99. resolve(res);
  100. },
  101. fail: reject,
  102. });
  103. // #endif
  104. // #ifndef MP-WEIXIN
  105. reject({
  106. errMsg: "目前仅支持微信支付",
  107. });
  108. // #endif
  109. });
  110. });
  111. }
  112. export function logout() {
  113. return userHttp.get("/user/logout");
  114. }
  115. export function fetchWallet(
  116. type: number,
  117. page: number,
  118. pageSize: number,
  119. query?: string
  120. ) {
  121. return userHttp.get(
  122. `/account/walletDetail?page=${page}&page_size=${pageSize}&type=${type}${
  123. query ? "&" + query : ""
  124. }`
  125. );
  126. }
  127. export function fetchOrder(orderid: string) {
  128. return userHttp.get(`/charge/orderDetail/${orderid}`);
  129. }
  130. export function fetchOrders(page: number, pageSize: number, query?: string) {
  131. return userHttp.get(
  132. `/charge/listUserChargeOrders?pageNum=${page}&pageSize=${pageSize}${
  133. query ? "&" + query : ""
  134. }`
  135. );
  136. }
  137. export function applyRefund(reason: string) {
  138. return userHttp.post(`/payment/wxApplyRefund`, {
  139. data: {
  140. reason,
  141. },
  142. });
  143. }
  144. export function listRefund(param: any) {
  145. return userHttp.post(`/account/listRefund`, {
  146. data: {
  147. ...param,
  148. },
  149. });
  150. }
  151. /**
  152. * 获取站点可以领取的优惠券
  153. * @param stationId
  154. */
  155. export function listAvailableCoupons(stationId: any) {
  156. return userHttp.get(`/coupon/listAvailableCoupons?stationId=${stationId}`, {
  157. });
  158. }
  159. /**
  160. * 领取优惠券
  161. * @param couponId
  162. */
  163. export function collectCoupon (couponId: number) {
  164. return userHttp.get(`/coupon/collectCoupon?couponId=${couponId}`, {
  165. });
  166. }