App.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <script>
  2. export default {
  3. globalData: {
  4. url: 'https://h5.bike.woqichuxing.cn/api',//域名
  5. },
  6. onLaunch: function() {
  7. console.log('App Launch')
  8. let token = uni.getStorageSync('token')
  9. if (!token) {
  10. uni.reLaunch({
  11. url: '/pages/login',
  12. })
  13. }
  14. },
  15. onShow: function() {
  16. console.log('App Show')
  17. },
  18. onHide: function() {
  19. console.log('App Hide')
  20. },
  21. methods:{
  22. request(api, params, method) {
  23. var that = this;
  24. return new Promise((resolve, reject) => {
  25. uni.request({
  26. url: that.globalData.url + api,
  27. data: params,
  28. header: {
  29. 'content-type': 'application/json',
  30. 'Accept': 'application/json',
  31. 'Cache-Control': 'no-cache',
  32. 'Authorization': uni.getStorageSync('token'),
  33. },
  34. method: method,
  35. success: (res) => {
  36. resolve(res)
  37. if (res.statusCode !== 200) {
  38. if (res.statusCode == 401) {
  39. uni.showToast({
  40. title: '登录过期,请重新登录',
  41. icon:'none'
  42. })
  43. uni.reLaunch({
  44. url: '/pages/login',
  45. })
  46. } else {
  47. uni.showModal({
  48. title: '提示',
  49. content: res.data.message,
  50. showCancel: false
  51. })
  52. return
  53. }
  54. }
  55. },
  56. fail: (err) => {
  57. reject(err, '请求失败')
  58. },
  59. complete: () => {
  60. that.globalData.req = true
  61. }
  62. })
  63. })
  64. }
  65. }
  66. }
  67. </script>
  68. <style>
  69. /*每个页面公共css */
  70. page{
  71. background-color: #efefef;
  72. }
  73. </style>