小程序API管理(promise)

创建HTTP文件夹,并创建request和api两个js文件

创建request.js用来封装小程序的request请求

const app = getApp();
const host = app.globalData.URL
// get请求使用 json对象转字符串 (formatParams )
const formatParams = (data) => {
  let arr = []
  for (let name in data) {
    arr.push(encodeURIComponent(name) + '=' + encodeURIComponent(data[name]))
  }
  return arr.join('&')
}
// 创建统一调用函数

const httpService = (url, params, method, loading = true) => {
  let header = {
    "content-type": "application/x-www-form-urlencoded"
  }
  let defaultData = {
    token:app.globalData.token,
    source:'wxxcx'
  }
  return new Promise((resolve, reject) => {
    if (method === 'post') {
      wx.request({
        url: host + url,
        data: Object.assign(defaultData, params),
        method: method,
        header: header,
        timeout: 15000,
        complete: (res) => {
          resolve(res)
        }
      })
    } else if (method === 'get') {
      wx.request({
        url: host + url + '?' + formatParams(Object.assign(defaultData, params)),
        method: method,
        header: header,
        timeout: 15000,
        complete: (res) => {
          resolve(res)
        }
      })
    }

  })
}

module.exports = {
  httpService
}
复制代码

创建api.js用来统一管理api接口

//引入封装好的request文件
const { httpService } = require('../http/request')
const app = getApp()
// 职场相关接口

/**
 * 获取职位列表
 */
const getPositionList = (params) => {
  return httpService('/position/jobs', params, 'get')
}
/**
 * 获取公司列表
 */
const getCompanyList = (params) => {
  return httpService('/position/company2', params, 'get')
}
/**
 * 获取轮播数据
 */
const getBanner = (params) => {
  return httpService('/position/fairbanner', params, 'get')
}

/**
 * 获取简历详情
 */
const getCvDetails = (params) => {
  return httpService('/cv/detail', params, 'get')
}
/**
 * 投递简历
 */
const deliveryCv = (params) => {
  return httpService('/cv/deliver', params, 'get')
}
 /**
  * 获取职位详情
  */
 const getJobDetails = (params) => {
  return httpService('/position/jobdetail', params, 'post')
}
 /**
  * 收藏职位
  */
 const collectjob = (params) => {
   return httpService('/position/collectjob',params,'get')
 }

 /**
  * 获取职能类表
  */
 const getFormresource = (params) => {
  return httpService('/position/formresource',params,'get')
}

/**
 * 获取公司详情页中职位列表数据
 */
const getcompanyJob = (params) => {
  return httpService('/position/jobfeed',params,'post')
}

/**
 * 获取公司信息
 */
const getCompanyInfo = (params) => {
  return httpService('/position/moreposition',params,'post')
}

// 登录相关接口

 /**
  * 微信登录
  */
 const wxLogin = (params) => {
  return httpService('/login/xcxlogin', params, 'post')
}
/**
 * 职场状态确认与更新接口
 */
const wxgroupconfirm = (params) => {
  return httpService('/position/groupconfirm', params, 'post')
}
/**
 * 获取验证码
 */
const getCode = (params) => {
  return httpService('/mobile/phonecode', params, 'post')
}

// 会议相关接口

/**
 * 获取我的会议
 */
const getMyMetting = (params) => {
  return httpService('/user/usermeeting', params, 'post')
}
//导出接口
module.exports = {
  getPositionList,
  getBanner,
  getCompanyList,
  getCvDetails,
  wxLogin,
  wxgroupconfirm,
  getCode,
  deliveryCv,
  getJobDetails,
  collectjob,
  getFormresource,
  getcompanyJob,
  getCompanyInfo,
  getMyMetting
}

复制代码

页面使用

//引入封装的api文件
import {导出的接口对象} from ‘封装的api文件相对路径’

import {
  getPositionList,
  getBanner,
  getCompanyList
} from "../../../http/api"

//使用方式

getPositionList({
    参数
})
.then(res =>{
    成功回调
})
.catch(err=>{
    失败回调
})
复制代码

这种API管理方式对于api数量多的功能不太友好,后期会继续优化

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享