YTUserRequest.ets 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { IBestToast } from '@ibestservices/ibest-ui'
  2. import { AppStorageKeyCollect } from '../constants'
  3. import { ReqString, ResultCallBack } from '../models'
  4. import { LoginCollect } from '../models/LoginCollect'
  5. import { UserInfo, userInfo } from '../models/UserInfo'
  6. import { yTRouter } from '../utils/YTRouter'
  7. import { YTRequest } from './YTRequest'
  8. import { YTLog } from '../../../../Index'
  9. import { AxiosProgressEvent, FormData } from '@ohos/axios'
  10. import { YTDate } from '../utils/FormatDate'
  11. import { HuaweiAuthPlugin } from '../utils/HuaWeiAuthPlugin'
  12. //用户相关api
  13. export class YTUserRequest extends YTRequest {
  14. //获取验证码
  15. static getLoginCaptcha(phonenumber: string, result: ResultCallBack<LoginCollect>) {
  16. YTUserRequest.post<LoginCollect, LoginCollect>(
  17. `/warehouseApUser/sendSmsCode`,
  18. new LoginCollect('login')
  19. .setPhonenumber(phonenumber)
  20. )
  21. .then(res => {
  22. result(res)
  23. })
  24. .catch((err: Error) => {
  25. result(undefined, err)
  26. })
  27. }
  28. //验证码登录
  29. static phonenumberLogin(param: LoginCollect, result?: ResultCallBack<UserInfo>) {
  30. if (param.getUuid() !== undefined) {
  31. YTUserRequest.post<ReqString, LoginCollect>(`/warehouseApUser/phoneLogin`, param)
  32. .then(res => {
  33. userInfo.setToken(res[AppStorageKeyCollect.TOKEN])
  34. YTUserRequest.refreshUserInfo((res) => {
  35. IBestToast.show({ message: '登录成功' })
  36. yTRouter.routerBack()
  37. result?.(res)
  38. })
  39. })
  40. } else {
  41. IBestToast.show({ message: "请求异常,请重新发送验证码", type: "fail" })
  42. }
  43. }
  44. //注册
  45. static register(param: LoginCollect) {
  46. if (param.getUuid() !== undefined) {
  47. YTUserRequest.post<ReqString, LoginCollect>(`/warehouseApUser/phoneSave`, param)
  48. .then(res => {
  49. userInfo.setToken(res[AppStorageKeyCollect.TOKEN])
  50. YTUserRequest.refreshUserInfo(() => {
  51. IBestToast.show({ message: '注册成功' })
  52. yTRouter.routerBack()
  53. })
  54. })
  55. } else {
  56. IBestToast.show({ message: "请求异常,请重新发送验证码", type: "fail" })
  57. }
  58. }
  59. //密码登录
  60. static passwordLogin(param: LoginCollect, result?: ResultCallBack<UserInfo>) {
  61. YTUserRequest.post<ReqString, LoginCollect>(`/warehouseApUser/passwordLogin`, param)
  62. .then(res => {
  63. userInfo.setToken(res[AppStorageKeyCollect.TOKEN])
  64. YTUserRequest.refreshUserInfo((res) => {
  65. IBestToast.show({ message: '登录成功' })
  66. yTRouter.routerBack()
  67. result?.(res)
  68. })
  69. })
  70. }
  71. //重置密码
  72. static resetPassword(param: LoginCollect, result?: ResultCallBack<undefined>) {
  73. YTUserRequest.post<ReqString, LoginCollect>(`/warehouseApUser/resetPwd`, param)
  74. .then(() => {
  75. result?.()
  76. })
  77. }
  78. //华为登录
  79. static harmonyLogin(instance: LoginCollect) {
  80. try {
  81. IBestToast.showLoading()
  82. HuaweiAuthPlugin.requestAuth()
  83. .then(code => {
  84. instance.setCode(code!)
  85. YTUserRequest.post<ReqString, LoginCollect>(`/warehouseApUser/hmLogin`,
  86. instance)
  87. .then(data => {
  88. const token = data['token']
  89. userInfo.setToken(token)
  90. YTUserRequest.refreshUserInfo((userInfo) => {
  91. YTLog.info(userInfo)
  92. IBestToast.hide()
  93. setTimeout(() => {
  94. IBestToast.show({ message: '登录成功' })
  95. }, 100)
  96. yTRouter.routerBack()
  97. })
  98. })
  99. .catch((err: Error) => {
  100. IBestToast.hide()
  101. YTLog.error(err)
  102. })
  103. })
  104. .catch((e: Error) => {
  105. YTLog.error(e)
  106. IBestToast.hide()
  107. })
  108. } catch (e) {
  109. YTLog.error(e)
  110. IBestToast.hide()
  111. }
  112. }
  113. //刷新用户信息
  114. static refreshUserInfo(result?: ResultCallBack<UserInfo>) {
  115. YTUserRequest.post<UserInfo, null>(`/warehouseApUser/info`)
  116. .then(res => {
  117. userInfo.setUserInfoAndLogin(res)
  118. YTLog.info(userInfo)
  119. result?.(res)
  120. })
  121. .catch((err: Error) => {
  122. result?.(undefined, err)
  123. })
  124. }
  125. //上传文件
  126. static uploadFile(context: Context, fullpath: string, success: (url: string) => void) {
  127. const formData = new FormData()
  128. formData.append('file', fullpath)
  129. YTUserRequest.upPost<ReqString, FormData>('/common/upload', formData, {
  130. headers: { 'Content-Type': 'multipart/form-data' },
  131. context,
  132. onUploadProgress: (progressEvent: AxiosProgressEvent): void => {
  133. YTLog.info(progressEvent && progressEvent.loaded && progressEvent.total ?
  134. Math.ceil(progressEvent.loaded / progressEvent.total * 100) + '%' : '0%', 'uploadFile');
  135. }
  136. })
  137. .then(res => {
  138. const url = res['url']
  139. success(url)
  140. })
  141. }
  142. // 修改用户头像
  143. static uploadHeadImg(context: Context, fullpath: string, success: () => void) {
  144. const formData = new FormData()
  145. formData.append('file', fullpath)
  146. YTUserRequest.upPost<ReqString, FormData>('/common/upload', formData, {
  147. headers: { 'Content-Type': 'multipart/form-data' },
  148. context,
  149. onUploadProgress: (progressEvent: AxiosProgressEvent): void => {
  150. YTLog.info(progressEvent && progressEvent.loaded && progressEvent.total ?
  151. Math.ceil(progressEvent.loaded / progressEvent.total * 100) + '%' : '0%', 'uploadFile');
  152. }
  153. })
  154. .then(res => {
  155. const url = res['url']
  156. YTUserRequest.post<null, ReqString>(`/warehouseApUser/modifyMemberIcon`,
  157. { 'memberIcon': url })
  158. .then(() => {
  159. success()
  160. })
  161. .catch((e: Error) => {
  162. YTLog.error(e)
  163. IBestToast.show({ message: '头像上传失败', type: 'fail' })
  164. })
  165. })
  166. .catch((e: Error) => {
  167. YTLog.error(e)
  168. })
  169. }
  170. // 修改用户昵称
  171. static changeNickname(name: string, success: () => void) {
  172. YTUserRequest.post<null, ReqString>(`/warehouseApUser/modifyMemberName`,
  173. { 'memberName': name })
  174. .then(() => {
  175. YTUserRequest.refreshUserInfo(() => {
  176. success()
  177. })
  178. })
  179. }
  180. //问题反馈
  181. static questionBack(des: string, createBy: string) {
  182. YTUserRequest.post<null, ReqString>(`/warehouseBack/saveQuestion`, {
  183. 'backQuestion': des,
  184. 'createBy': createBy,
  185. 'createTime': new YTDate().formatDate(),
  186. })
  187. .then(() => {
  188. IBestToast.show("反馈成功")
  189. yTRouter.routerBack()
  190. })
  191. .catch((e: Error) => {
  192. YTLog.error(e)
  193. })
  194. }
  195. }