user.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import Http from "../utils/http";
  2. import { host } from "../utils/constant";
  3. const userHttp = new Http(host);
  4. export function fetchProfile() {
  5. return userHttp.get("/user/me").then((res) => {
  6. getApp().globalData.user = res;
  7. return res;
  8. });
  9. }
  10. export function fetchCollectList() {
  11. if (getApp().globalData.collectIds) {
  12. return Promise.resolve(getApp().globalData.collectIds);
  13. }
  14. return userHttp
  15. .get<{
  16. data: {
  17. status: number;
  18. station_id: number;
  19. }[];
  20. }>("/user/collectList?page=1&page_size=999")
  21. .then((res) => {
  22. getApp().globalData.collectIds = res.data
  23. ? res.data
  24. .filter((item) => Number(item.status) === 1)
  25. .map((item) => {
  26. return Number(item.station_id);
  27. })
  28. : [];
  29. return getApp().globalData.collectIds;
  30. });
  31. }
  32. export function addCollectList(sid: number) {
  33. let ids = getApp().globalData.collectIds
  34. ? (getApp().globalData.collectIds as number[])
  35. : [];
  36. const status = ids.includes(sid) ? 2 : 1;
  37. return userHttp
  38. .post("/user/addCollect", {
  39. data: {
  40. station_id: sid,
  41. status,
  42. },
  43. })
  44. .then(() => {
  45. if (status === 1) {
  46. ids.push(sid);
  47. } else {
  48. ids = ids.filter((id) => id !== sid);
  49. }
  50. getApp().globalData.collectIds = ids;
  51. return status === 1;
  52. });
  53. }
  54. export function updateProfile(data: {
  55. nick_name?: string;
  56. avatar?: string;
  57. vin?: string;
  58. license_plate?: string;
  59. card_no?: string;
  60. }) {
  61. return userHttp.post("/user/update", {
  62. data,
  63. });
  64. }
  65. export function insertMoney(amount: number) {
  66. return userHttp
  67. .post<{
  68. appId: string;
  69. nonceStr: string;
  70. package: string;
  71. paySign: string;
  72. signType: string;
  73. timestamp: string;
  74. }>("/payment/pay", {
  75. data: {
  76. amount: parseInt(`${amount * 100}`),
  77. openid: getApp().globalData.user?.openid,
  78. },
  79. })
  80. .then((res: any) => {
  81. return new Promise((resolve, reject) => {
  82. uni.requestPayment({
  83. provider: "wxpay",
  84. orderInfo: {
  85. timeStamp: res.timestamp,
  86. nonceStr: res.nonceStr,
  87. package: res.package,
  88. signType: "MD5",
  89. paySign: res.paySign,
  90. },
  91. success(res: any) {
  92. resolve(res);
  93. },
  94. fail: reject,
  95. });
  96. });
  97. });
  98. }
  99. export function logout() {
  100. return userHttp.get("/user/logout");
  101. }
  102. export function fetchWallet(type: number, page: number, pageSize: number) {
  103. return userHttp
  104. .get<{
  105. data: any[];
  106. }>(`/account/wallet?page=${page}&page_size=${pageSize}&type=${type}`)
  107. .then((res) => {
  108. return res.data;
  109. });
  110. }
  111. export function fetchOrder(orderid: string) {
  112. return userHttp.get(`/charge/orderInfo?order_id=${orderid}`);
  113. }