request-example.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * 请求方法使用示例
  3. */
  4. import { userApi, contentApi, promiseApi } from '@/utils/api';
  5. // 示例1:使用回调方式调用登录接口
  6. export function loginExample() {
  7. // 准备登录数据
  8. const loginData = {
  9. username: 'test',
  10. password: '123456'
  11. };
  12. // 调用登录接口
  13. userApi.login(loginData, (res, err) => {
  14. if (err) {
  15. console.error('登录失败', err);
  16. uni.showToast({
  17. title: '登录失败',
  18. icon: 'none'
  19. });
  20. return;
  21. }
  22. if (res.data && res.data.code === 0) {
  23. console.log('登录成功', res.data);
  24. // 保存用户信息
  25. uni.setStorageSync('userInfo', JSON.stringify(res.data.data));
  26. uni.showToast({
  27. title: '登录成功',
  28. icon: 'success'
  29. });
  30. } else {
  31. console.error('登录失败', res.data.msg);
  32. uni.showToast({
  33. title: res.data.msg || '登录失败',
  34. icon: 'none'
  35. });
  36. }
  37. });
  38. }
  39. // 示例2:使用Promise方式调用登录接口
  40. export async function loginPromiseExample() {
  41. try {
  42. // 准备登录数据
  43. const loginData = {
  44. username: 'test',
  45. password: '123456'
  46. };
  47. // 调用登录接口
  48. const res = await promiseApi.login(loginData);
  49. if (res.data && res.data.code === 0) {
  50. console.log('登录成功', res.data);
  51. // 保存用户信息
  52. uni.setStorageSync('userInfo', JSON.stringify(res.data.data));
  53. uni.showToast({
  54. title: '登录成功',
  55. icon: 'success'
  56. });
  57. return res.data.data;
  58. } else {
  59. console.error('登录失败', res.data.msg);
  60. uni.showToast({
  61. title: res.data.msg || '登录失败',
  62. icon: 'none'
  63. });
  64. return null;
  65. }
  66. } catch (error) {
  67. console.error('登录请求异常', error);
  68. uni.showToast({
  69. title: '网络异常,请稍后重试',
  70. icon: 'none'
  71. });
  72. return null;
  73. }
  74. }
  75. // 示例3:在页面中使用
  76. // 在Vue组件中使用示例
  77. /*
  78. export default {
  79. data() {
  80. return {
  81. username: '',
  82. password: ''
  83. };
  84. },
  85. methods: {
  86. // 回调方式登录
  87. handleLogin() {
  88. const loginData = {
  89. username: this.username,
  90. password: this.password
  91. };
  92. userApi.login(loginData, (res, err) => {
  93. // 处理登录结果
  94. if (err) {
  95. this.$toast('登录失败');
  96. return;
  97. }
  98. if (res.data && res.data.code === 0) {
  99. this.$toast('登录成功');
  100. // 跳转到首页
  101. uni.switchTab({
  102. url: '/pages/index/index'
  103. });
  104. } else {
  105. this.$toast(res.data.msg || '登录失败');
  106. }
  107. });
  108. },
  109. // Promise方式登录
  110. async handleLoginPromise() {
  111. try {
  112. const loginData = {
  113. username: this.username,
  114. password: this.password
  115. };
  116. const res = await promiseApi.login(loginData);
  117. if (res.data && res.data.code === 0) {
  118. this.$toast('登录成功');
  119. // 跳转到首页
  120. uni.switchTab({
  121. url: '/pages/index/index'
  122. });
  123. } else {
  124. this.$toast(res.data.msg || '登录失败');
  125. }
  126. } catch (error) {
  127. this.$toast('网络异常,请稍后重试');
  128. }
  129. }
  130. }
  131. }
  132. */