user.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import Taro from '@tarojs/taro'
  2. import { API_OAUTH_LOGIN, API_REGISTER, API_LOGIN, API_LOGOUT, API_CLEAR_USER, API_USER_INFO, API_CHECK_UNIQUE } from './http/api'
  3. import { request } from './http/request'
  4. import { clearSuccess, getInfoSuccess, loginSuccess, logoutSuccess, registerSuccess, updateSuccess } from '@/store/user'
  5. export const checkUnique = (username?: string, email?: string) => {
  6. return new Promise((resolve, reject) => {
  7. request({
  8. url: API_CHECK_UNIQUE, method: 'GET', data: { username: username, email: email }
  9. }).then(res => {
  10. resolve(res);
  11. console.log(res);
  12. // dispatch(loginSuccess(res));
  13. })
  14. })
  15. }
  16. export const login = (username: string, password: string) => {
  17. return new Promise((resolve, reject) => {
  18. request({
  19. url: API_LOGIN, method: 'POST', data: { username, password }
  20. }).then(res => {
  21. resolve(res)
  22. // dispatch(loginSuccess(res));
  23. }).catch(e => {
  24. reject(e)
  25. })
  26. })
  27. }
  28. export const wxLogin = (code: string/*, encryptedData: string, iv: string*/) => {
  29. return new Promise((resolve, reject) => {
  30. request({
  31. url: API_OAUTH_LOGIN, method: 'POST',
  32. data: {
  33. code: code,
  34. type: 'WX_MP',
  35. app_version: '1',
  36. client_version: '1',
  37. client_type: 'WX_MP',
  38. // extra: {
  39. // encryptedData: encryptedData,
  40. // iv: iv
  41. // }
  42. }
  43. }).then(res => {
  44. resolve(res)
  45. // dispatch(loginSuccess(res));
  46. }).catch(e => {
  47. reject(e)
  48. })
  49. })
  50. };
  51. /*
  52. export const wxLogin = (code: string) => (dispatch: any) => {
  53. request({
  54. url: API_OAUTH_LOGIN, method: 'POST', data: { code,type:'WX_MP',app_version:'1',client_version:'1',client_type:'WXP_MP' }
  55. }).then(res => {
  56. console.log(res);
  57. dispatch(loginSuccess(res));
  58. })
  59. }*/
  60. export const register = (name: string, email: string, password: string) => {
  61. return new Promise((resolve, reject) => {
  62. request({
  63. url: API_REGISTER, method: 'POST', data: { client_type: 'IOS', username: name, email: email, password: password }
  64. }).then(res => {
  65. resolve(res);
  66. // dispatch(loginSuccess(res));
  67. }).catch(e => {
  68. reject(e)
  69. })
  70. })
  71. }
  72. export const logout = () => (dispatch: any) => {
  73. request({
  74. url: API_LOGOUT, method: 'GET', data: {}
  75. }).then(_ => {
  76. dispatch(logoutSuccess());
  77. Taro.navigateBack();
  78. })
  79. }
  80. export const clear = () => (dispatch: any) => {
  81. request({
  82. url: API_CLEAR_USER, method: 'DELETE', data: {}
  83. }).then(_ => {
  84. dispatch(clearSuccess());
  85. Taro.navigateBack();
  86. })
  87. }
  88. export const getInfo = () => (dispatch: any) => {
  89. request({
  90. url: API_USER_INFO, method: 'GET', data: {}
  91. }).then(res => {
  92. dispatch(getInfoSuccess(res));
  93. })
  94. }
  95. export const update = (params: any) => {
  96. return new Promise((resolve, reject) => {
  97. request({
  98. url: API_USER_INFO, method: 'POST', data: params
  99. }).then(res => {
  100. // dispatch(updateSuccess(params));
  101. resolve(res)
  102. }).catch(e=>{
  103. reject(e)
  104. })
  105. })
  106. }