user.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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, API_CLIENT_ID, API_USER_PERMS, API_USER_LOCATION } from './http/api'
  3. import { request } from './http/request'
  4. import { clearSuccess, getInfoSuccess, loginSuccess, logoutSuccess, registerSuccess, updateSuccess } from '@/store/user'
  5. export const checkUnique = (params: any) => {
  6. return new Promise((resolve, reject) => {
  7. request({
  8. url: API_CHECK_UNIQUE, method: 'GET', data: params
  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 uploadPerm = (params: any) => {
  29. return new Promise((resolve, reject) => {
  30. request({
  31. url: API_USER_PERMS, method: 'POST', data: { ...params }//{ push_enabled: push_enable }
  32. }).then(res => {
  33. resolve(res)
  34. // dispatch(loginSuccess(res));
  35. }).catch(e => {
  36. reject(e)
  37. })
  38. })
  39. }
  40. export const getPerm = (params: any) => {
  41. return new Promise((resolve, reject) => {
  42. request({
  43. url: API_USER_PERMS, method: 'GET', data: { ...params }//{ push_enabled: push_enable }
  44. }).then(res => {
  45. resolve(res)
  46. // dispatch(loginSuccess(res));
  47. }).catch(e => {
  48. reject(e)
  49. })
  50. })
  51. }
  52. export const wxLogin = (code: string/*, encryptedData: string, iv: string*/) => {
  53. return new Promise((resolve, reject) => {
  54. request({
  55. url: API_OAUTH_LOGIN, method: 'POST',
  56. data: {
  57. code: code,
  58. type: 'WX_MP',
  59. app_version: '1',
  60. client_version: '1',
  61. client_type: 'WX_MP',
  62. // extra: {
  63. // encryptedData: encryptedData,
  64. // iv: iv
  65. // }
  66. }
  67. }).then(res => {
  68. resolve(res)
  69. // dispatch(loginSuccess(res));
  70. }).catch(e => {
  71. reject(e)
  72. })
  73. })
  74. };
  75. /*
  76. export const wxLogin = (code: string) => (dispatch: any) => {
  77. request({
  78. url: API_OAUTH_LOGIN, method: 'POST', data: { code,type:'WX_MP',app_version:'1',client_version:'1',client_type:'WXP_MP' }
  79. }).then(res => {
  80. console.log(res);
  81. dispatch(loginSuccess(res));
  82. })
  83. }*/
  84. export const register = (name: string, email: string, password: string) => {
  85. return new Promise((resolve, reject) => {
  86. request({
  87. url: API_REGISTER, method: 'POST', data: { client_type: 'IOS', username: name, email: email, password: password }
  88. }).then(res => {
  89. resolve(res);
  90. // dispatch(loginSuccess(res));
  91. }).catch(e => {
  92. reject(e)
  93. })
  94. })
  95. }
  96. export const logout = () => {
  97. return new Promise((resolve, reject) => {
  98. request({
  99. url: API_LOGOUT, method: 'GET', data: {}
  100. }).then(res => {
  101. return resolve(res)
  102. // dispatch(getInfoSuccess(res));
  103. }).catch(e => {
  104. return reject(e)
  105. })
  106. })
  107. // request({
  108. // url: API_LOGOUT, method: 'GET', data: {}
  109. // }).then(_ => {
  110. // dispatch(logoutSuccess());
  111. // Taro.navigateBack();
  112. // })
  113. }
  114. export const clear = () => (dispatch: any) => {
  115. request({
  116. url: API_CLEAR_USER, method: 'DELETE', data: {}
  117. }).then(_ => {
  118. dispatch(clearSuccess());
  119. Taro.navigateBack();
  120. })
  121. }
  122. export const latestLocation = () => {
  123. return new Promise((resolve, reject) => {
  124. request({
  125. url: API_USER_LOCATION, method: 'GET', data: {}
  126. }).then(res => {
  127. return resolve(res)
  128. // dispatch(getInfoSuccess(res));
  129. }).catch(e => {
  130. return reject(e)
  131. })
  132. })
  133. }
  134. export const clearLocation = () => {
  135. return new Promise((resolve, reject) => {
  136. request({
  137. url: API_USER_LOCATION, method: 'DELETE', data: {}
  138. }).then(res => {
  139. return resolve(res)
  140. // dispatch(getInfoSuccess(res));
  141. }).catch(e => {
  142. return reject(e)
  143. })
  144. })
  145. }
  146. export const getInfo = () => {
  147. return new Promise((resolve, reject) => {
  148. request({
  149. url: API_USER_INFO, method: 'GET', data: {}
  150. }).then(res => {
  151. return resolve(res)
  152. // dispatch(getInfoSuccess(res));
  153. }).catch(e => {
  154. return reject(e)
  155. })
  156. })
  157. }
  158. export const update = (params: any) => {
  159. return new Promise((resolve, reject) => {
  160. request({
  161. url: API_USER_INFO, method: 'POST', data: params
  162. }).then(res => {
  163. // dispatch(updateSuccess(params));
  164. resolve(res)
  165. }).catch(e => {
  166. reject(e)
  167. })
  168. })
  169. }
  170. export const clientId = () => {
  171. if (global.registerID) {
  172. request({
  173. url: API_CLIENT_ID, method: 'POST', data: {
  174. provider: 'JIGUANG',
  175. client_type: 'IOS',
  176. client_id: global.registerID
  177. }
  178. })
  179. }
  180. }