User.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import i18n from '../i18n';
  2. import request from '../Utils/RequestUtils';
  3. import {
  4. addAsyncStorage,
  5. removeAsyncStorage,
  6. } from '../Utils/AsyncStorageUtils';
  7. import submitPhone from '../Utils/FormUtils';
  8. import Toast from './Toast';
  9. import { alert } from '../Utils/TotastUtils';
  10. const app = (now) => ({
  11. id: null,
  12. userInfo: {},
  13. getUser() {
  14. return request
  15. .get('/user/my')
  16. .then((res) => {
  17. now({
  18. id: res.id,
  19. userInfo: res,
  20. });
  21. })
  22. .catch((e) => {
  23. now({
  24. id: 0,
  25. });
  26. return Promise.reject(e);
  27. });
  28. },
  29. loginByPsd(phone, password) {
  30. const { loading, warnning, success } = now(Toast);
  31. loading();
  32. return request
  33. .post('/auth/loginByRegister', {
  34. data: {
  35. phone: submitPhone(phone),
  36. password,
  37. identity: 'USER',
  38. },
  39. requestType: 'form',
  40. })
  41. .then((res) => {
  42. return addAsyncStorage('token', res);
  43. })
  44. .then(() => {
  45. const { getUser } = now();
  46. return getUser();
  47. })
  48. .then(() => {
  49. success(i18n.t('deng-lu-cheng-gong'));
  50. })
  51. .catch((e) => {
  52. warnning(e.error);
  53. });
  54. },
  55. loginByCode(phone, code) {
  56. const { loading, warnning, success } = now(Toast);
  57. loading();
  58. return request
  59. .post('/auth/phoneLogin', {
  60. data: {
  61. phone: `+86${submitPhone(phone)}`,
  62. code,
  63. identity: 'USER',
  64. },
  65. requestType: 'form',
  66. })
  67. .then((res) => {
  68. return addAsyncStorage('token', res);
  69. })
  70. .then(() => {
  71. const { getUser } = now();
  72. return getUser();
  73. })
  74. .then(() => {
  75. success(i18n.t('deng-lu-cheng-gong'));
  76. })
  77. .catch((e) => {
  78. warnning(e.error);
  79. });
  80. },
  81. logout() {
  82. alert('', `${i18n.t('que-ding-yao-tui-chu-gai-zhang-hao-ma')}?`, () => {
  83. const { loading, success } = now(Toast);
  84. loading();
  85. // 移除 token
  86. removeAsyncStorage('token').then(() => {
  87. // 清除用户信息
  88. now({ id: 0, userInfo: {} });
  89. success(i18n.t('tui-chu-cheng-gong'));
  90. });
  91. });
  92. },
  93. uploadInfo({ ...info }) {
  94. const { id, getUser } = now();
  95. const { loading, warnning, success } = now(Toast);
  96. loading();
  97. return request
  98. .post('/user/save', {
  99. data: {
  100. ...info,
  101. id,
  102. },
  103. })
  104. .then(() => {
  105. return getUser();
  106. })
  107. .then(() => {
  108. success(i18n.t('geng-xin-cheng-gong'));
  109. return Promise.resolve();
  110. })
  111. .catch((e) => {
  112. warnning(e.error);
  113. });
  114. },
  115. loginByRegister(phone, password) {
  116. const { loading, warnning, success } = now(Toast);
  117. loading();
  118. return request
  119. .post('/auth/loginByRegister', {
  120. data: {
  121. phone: submitPhone(phone),
  122. password,
  123. },
  124. requestType: 'form',
  125. })
  126. .then(() => {
  127. return request.post('/auth/login', {
  128. data: {
  129. username: submitPhone(phone),
  130. password,
  131. },
  132. requestType: 'form',
  133. });
  134. })
  135. .then((res) => {
  136. return addAsyncStorage('token', res);
  137. })
  138. .then(() => {
  139. const { getUser } = now();
  140. return getUser();
  141. })
  142. .then(() => {
  143. success(i18n.t('zhu-ce-cheng-gong'));
  144. })
  145. .catch((e) => {
  146. warnning(e.error);
  147. });
  148. },
  149. });
  150. export default app;