后台对接公共接口调用

//在utils里面写公共方法  比如说这个页面为path.js 页面

const prefix = 'https://dev.cdsoft.work/ct';
const getDepartmentList = prefix + '/mobile/getDepartmentList';
//员工注册 
const employeeRegister=prefix + "/mobile/employeeRegister";
//伯爵订单--登录 1刷新 2.重新申请  审核通过
const getExamineStatusById = prefix +'/mobile/getExamineStatusById'
//暴露接口,抛出
export {
  login,
  getDepartmentList,
  employeeRegister,
  getExamineStatusById
  // getUserInfo
}
//需要请求的方式  比如这个写在util.js 里面
  /*get 请求 post 请求 */
 
/**发送GET请求**/
function getRequest(url, data, callback) {
  request(url, data, 'get', null, true, callback);
}
/**发送POST请求**/
function postRequest(url, data, callback) {
  request(url, data, 'post', null, true, callback);
}
/**
    url:请求地址
    data:请求内容,对象格式
    get /post 请求方式
    header 自定义请求
   * needAuth 是否带上token 默认为true
 * callback 回调
   

**/
function request(url, data, type, header, needAuth, callback) {
  const app = getApp();
  if (!header) {
    header = {
      'content-type': 'application/json',
    };
  }
  if (needAuth != false) {
    const token = app.globalData.token;
    header.token = token;
  }
  wx.request({
    url: url,
    method: type,
    data: data,
    header: header,
    success: function (res) {
      if (!res.data.success && res.data.message == 'token error') {
        // wx.reLaunch({
        //   url: `/pages/login/login?isTokenError=true`,
        // })
      }
      callback && callback(res)
    }
  })
}

//暴露接口
module.exports = {
  formatTime: formatTime,
  postRequest: postRequest,
  getRequest: getRequest
}
/*这个页面所需要对接数据 **/
/**需要引入以上两个页面----> 后端***/
import {getDepartmentList,employeeRegister} from '../../utils/path.js'  //大括号里面代表 这个页面与后台对接参数
import{postRequest} form '../../utils/utils.js' //这个postRequest代表请求数据的方式
 formsubmit:function(e){
    console.log(e);
    const phone = this.data.phoneValue;
    const password = this.data.passValue;
    let bool = apiname.rePhone(phone);
    if(!bool){
      wx.showToast({
        title: '手机号输入错误',
        icon: 'none'
      })
      return false;
    }
    postRequest(login, {
      "phone": phone,
       "password": password
       },res => {
        
        if (res.data.success){
          //登录成功 -- 跳转下个页面
          getApp().globalData.token = res.data.data.token

        }else{
          //登录失败
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
       });

  },
每个你讨厌的现在,都有一个不努力的曾经
原文地址:https://www.cnblogs.com/yuanxiangguang/p/11102978.html