user.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { WX_GET_PHONE, WX_GET_USER, WX_UPDATE_USER, WX_UPLOAD_URL } from '../utils/apiList'
  2. import { request, Response } from '../utils/http'
  3. const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
  4. // import { UPLOAD, transformUploadResponseOkData } from 'miniprogram-uploader';
  5. interface PhoneParams {
  6. code: string
  7. encryptedData: string
  8. iv: string
  9. }
  10. interface PhoneResType {
  11. countryCode: string
  12. phoneNumber: string
  13. purePhoneNumber: string
  14. }
  15. interface UserResType {
  16. }
  17. type UserRes = Response & {
  18. data: GlobalUserInfo
  19. }
  20. type PhoneRes = Response & {
  21. data: PhoneResType
  22. }
  23. export const getUserInfo = async (): Promise<UserResType> => {
  24. const res = await request.get<UserRes>(WX_GET_USER)
  25. const app = getApp<IAppOption>();
  26. app.globalData.userInfo = Object.assign({}, app.globalData.userInfo, res.data)
  27. wx.setStorageSync('wxUserId', res.data?.wxUserId || '')
  28. return res.data
  29. }
  30. export const updateUserInfo = async (params: Partial<GlobalUserInfo>): Promise<Response> => {
  31. const res = await request.post<Response>(WX_UPDATE_USER, params)
  32. return res
  33. }
  34. export const decrptPhone = async (params: PhoneParams): Promise<PhoneResType> => {
  35. const res = await request.get<PhoneRes>(WX_GET_PHONE, params)
  36. return res.data
  37. }
  38. export const updateAvatar = async (avatarUrl: string): Promise<string> => {
  39. let url = ''
  40. wx.uploadFile({
  41. filePath: avatarUrl,
  42. name: 'file',
  43. url: WX_UPLOAD_URL,
  44. success: async (res) => {
  45. const data = JSON.parse(res.data)
  46. url = data.data
  47. }
  48. })
  49. await sleep(500)
  50. return Promise.resolve(url)
  51. }