当前位置: 首页 > news >正文

广州建站方法wordpress搭建多个购物网站

广州建站方法,wordpress搭建多个购物网站,微信网页版二维码失效,wordpress没有链接在 UniApp 中封装 uni.request 实现全局使用,可通过以下方案实现 统一配置、拦截器管理、错误处理和全局调用。以下是完整实现步骤:一、基础封装方案(推荐)1. 创建 http.js 请求封装模块// utils/http.js// 基础配置 const BASE_U…

在 UniApp 中封装 uni.request 实现全局使用,可通过以下方案实现 统一配置、拦截器管理、错误处理和全局调用。以下是完整实现步骤:


一、基础封装方案(推荐)

1. 创建 http.js 请求封装模块
// utils/http.js// 基础配置
const BASE_URL = 'https://api.example.com'
const TIMEOUT = 15000// 请求队列(用于防止重复提交)
const pendingRequests = new Map()// 生成请求唯一标识
const generateReqKey = (config) => {return `${config.url}&${config.method}`
}// 核心请求函数
const request = (options) => {// 合并配置const config = {url: options.url,data: options.data || {},method: options.method || 'GET',header: {'Content-Type': 'application/json','X-Token': uni.getStorageSync('token') || '', // 自动携带token...(options.header || {}),},timeout: TIMEOUT,...options,}// 处理完整URLif (!config.url.startsWith('http')) {config.url = BASE_URL + config.url}// 防止重复请求拦截const reqKey = generateReqKey(config)if (pendingRequests.has(reqKey)) {return Promise.reject({ errMsg: '重复请求已取消' })}pendingRequests.set(reqKey, config)return new Promise((resolve, reject) => {uni.request({...config,success: (res) => {// 请求完成后移除队列pendingRequests.delete(reqKey)// 全局响应拦截器if (res.statusCode === 200) {// 业务状态码处理(示例)if (res.data.code === 401) {uni.navigateTo({ url: '/pages/login/login' })return reject(res.data)}resolve(res.data)} else {// HTTP状态码错误处理reject({ code: res.statusCode,message: `请求失败: ${res.errMsg}`})}},fail: (err) => {pendingRequests.delete(reqKey)// 网络错误统一处理reject({code: -1,message: '网络连接失败,请检查网络'})},complete: () => {// 可添加全局Loading关闭逻辑uni.hideLoading()}})})
}// 封装常用方法
const http = {get: (url, params = {}, options = {}) => {return request({url,data: params,method: 'GET',...options})},post: (url, data = {}, options = {}) => {return request({url,data,method: 'POST',...options})},put: (url, data = {}, options = {}) => {// 其他方法类似封装}
}export default http
2. 全局挂载(main.js)
// main.js
import Vue from 'vue'
import App from './App'
import http from '@/utils/http'// 挂载到Vue原型
Vue.prototype.$http = http// 挂载到全局uni对象
uni.$http = http// 如需使用this.$http调用
Vue.use({install(Vue) {Vue.config.globalProperties.$http = http}
})
3. 页面中使用
// 在.vue文件中
export default {methods: {async fetchData() {try {// GET请求const res1 = await this.$http.get('/user', { id: 123 })// POST请求const res2 = await uni.$http.post('/login', {username: 'admin',password: '123456'})} catch (err) {uni.showToast({ title: err.message, icon: 'none' })}}}
}

二、高级功能扩展

1. 添加拦截器系统
// 在http.js中添加
const interceptors = {request: [],response: []
}// 注册拦截器
http.addRequestInterceptor = (interceptor) => {interceptors.request.push(interceptor)
}http.addResponseInterceptor = (interceptor) => {interceptors.response.push(interceptor)
}// 修改request函数
const request = async (options) => {// ...原有代码...// 执行请求拦截器for (const interceptor of interceptors.request) {config = await interceptor(config) || config}return new Promise((resolve, reject) => {uni.request({...config,success: async (res) => {// 执行响应拦截器let response = resfor (const interceptor of interceptors.response) {response = await interceptor(response) || response}// ...后续处理...}})})
}
2. 使用示例(Token刷新)
// 在main.js中注册拦截器
http.addRequestInterceptor(config => {if (!config.url.includes('/refresh-token')) {const token = uni.getStorageSync('token')if (token) config.header['X-Token'] = token}return config
})http.addResponseInterceptor(async res => {if (res.data.code === 401) {// Token过期自动刷新const newToken = await http.post('/refresh-token', {refreshToken: uni.getStorageSync('refreshToken')})uni.setStorageSync('token', newToken)// 重新发送原请求return http(res.config)}return res
})

三、多端适配要点

  1. H5跨域处理

    // 开发环境代理配置 (vue.config.js)
    devServer: {proxy: {'/api': {target: 'https://api.example.com',changeOrigin: true,pathRewrite: { '^/api': '' }}}
    }
  2. 小程序域名白名单

    • 需在 manifest.json 配置合法域名

    "mp-weixin": {"appid": "xxx","setting": {"urlCheck": false},"permission": {"scope.userLocation": {"desc": "需要获取您的位置信息"}},"requiredPrivateInfos": ["getLocation"]
    }
  3. App端证书校验

    // 仅iOS需要处理
    if (uni.getSystemInfoSync().platform === 'ios') {config.sslVerify = false // 关闭SSL验证(仅测试环境)
    }

四、最佳实践建议

  1. 全局Loading控制

    // 请求计数
    let requestCount = 0http.addRequestInterceptor(config => {if (!config.hideLoading) {requestCount === 0 && uni.showLoading({ title: '加载中', mask: true })requestCount++}return config
    })http.addResponseInterceptor(response => {if (!response.config.hideLoading) {requestCount--requestCount === 0 && uni.hideLoading()}return response
    })
  2. 请求重试机制

    const retryRequest = (config, retry = 3) => {return request(config).catch(err => {return retry > 0 ? retryRequest(config, retry - 1) : Promise.reject(err)})
    }
  3. TypeScript支持

    // http.d.ts
    declare module '@vue/runtime-core' {interface ComponentCustomProperties {$http: {get<T = any>(url: string, params?: object): Promise<T>;post<T = any>(url: string, data?: object): Promise<T>;}}
    }

封装优势

  • 统一管理所有网络请求逻辑

  • 自动处理 token 和权限认证

  • 支持多端差异化配置

  • 提供拦截器实现业务解耦

  • 内置防重复提交和错误重试机制

http://www.dtcms.com/a/405681.html

相关文章:

  • 营销网站费用你理解的网络营销是什么
  • 做网站项目需要多少钱网站经营性备案流程
  • 域名备案 没有网站吗环境设计案例网站
  • 如何创建一个免费的网站免费做字体的网站好
  • 网站建设实战集团网站建设多少钱
  • wordpress 网站播放器插件珠海移动网站设计
  • 网站建设需要的技术wordpress数据表文档
  • 如何做网站页面免费的服装网站建设的规模和类别
  • 怎样才能做网站百度竞价排名系统
  • 东莞做网站有哪些北京网站设计精选刻
  • 自助建设网站平台中山市企业网站seo哪家好
  • 章丘做网站优化阿里云服务器免费一年
  • 玄圭做网站怎么样网站建设招聘信息
  • 网站开发新闻管理系统的背景河北做it的网站
  • 网站vpsseo是做什么的
  • 宁波网站推广排名永州市规划建设局网站
  • 网站上面的彩票快3怎么做下载的asp网站怎么打开
  • 网站信任 用户转化html指令代码大全
  • 云谷系统网站开发seo的概念是什么
  • 网站开发工具报告网站空间控制面板
  • 教师在哪些网站可以做兼职wordpress移除仪表盘
  • 青岛建设监理协会网站亚马逊雨林地图
  • 英语可以做推广的亲子类网站seo专员是什么职业
  • 网站建设中布局网盘资源共享群吧
  • 网站建设的工作视频人的吗龙岩app设计
  • 建设部人事司网站做百度推广的网络公司
  • 公司网站建设审批流程wordpress文章多密码
  • seo站长工具推广平台淘宝客如何做淘宝客网站
  • 小型网站开发需要什么步骤网站搭建关键词排名
  • 网站建设费长期待摊费用校园网站建设的作用