MrShi
2025-03-12 69a1b3bf45738f048361ee4ccb6bdc64fce35720
h5/utils/service.js
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,69 @@
import { baseUrl, app_url } from "./config.js"
export const http = function (options) {
   {
      return new Promise((resolve, reject) => {
         let token = uni.getStorageSync('token') || ''
         // åœ¨ç™»å½•的时候需要储存 token uni.setStorageSync("authorization","这里是登录获取的token值")
         // uni.showLoading({
         //    title:"加载中..."
         // })
         uni.showLoading({
            title: '加载中',
            mask: true
         })
         uni.request({
            url: baseUrl + options.url,
            data: options.data || {},
            method: options.method || 'GET',
            header: options.header || {
               // æ ¹æ®å®žé™…接口设计 key å– token æˆ–者 authorization
               dm_user_token: token || '',
               "content-type": 'application/json'
            },
            success: (res) => {
               let data = res.data
               // æŽ§åˆ¶å°æ˜¾ç¤ºæ•°æ®ä¿¡æ¯
               uni.hideLoading()
               // ç™»å½•过期
               if (data.code !== 200) {
                  setTimeout(() => {
                     uni.showToast({
                        title: data.message,
                        icon: "none",
                        duration: 2000
                     })
                  },500)
                  if (data.code === 500) {
                     uni.showToast({
                        'title': '服务器异常',icon:'none'
                     })
                  }
                  if (data.code === 5112) {
                     uni.clearStorageSync()
                     window.location.href = app_url
                  }
                  return resolve(data)
               }
               resolve(data)
               // return response.data
            },
            fail: (err) => {
               // é¡µé¢ä¸­å¼¹æ¡†æ˜¾ç¤ºå¤±è´¥
               uni.showToast({
                  title: '请求接口失败',
                  icon:"none"
               })
               // è¿”回错误消息
               reject(err)
               uni.hideLoading()
               return Promise.reject(err)
            },
            catch: (e) => {
               console.log(e)
            }
         })
      })
   }
}