restapi.js 7.67 KB
// /**
//  * 
// 0本地 
// 1开发 
// 2测试
// 3生产环境
// 4预发布环境
// */
// const accountInfo = wx.getAccountInfoSync()
// const systemInfo = wx.getSystemInfoSync()
// // console.log(systemInfo);
// // console.log(accountInfo.miniProgram.envVersion)
// //手动控制体验版环境的请求 true就依据下面index false就默认为2
// const manual = false
// const release = false

// let index =1

// // 判断小程序环境
// // develop:开发版(正常开发工具,测试时)
// // trial:体验版
// // release:正式版
// // console.log( systemInfo.platform);
// // console.log(accountInfo.miniProgram.envVersion);
// if (systemInfo.platform != 'devtools') {
//   switch (accountInfo.miniProgram.envVersion) {
//     case 'develop':
//       // this.globalData.url = 'http://192.168.0.252:3000/api'; //
//       break
//     case 'trial':
//       index = manual ? index : 2
//       break
//     case 'release':
//       index = release ? index : 3
//       break
//     default:
//       index = 3
//       break
//   }
// }
import { envIndex } from '~/config'
let index = envIndex
const domain_items = {
    ip: 'http://192.168.3.181:9182',
    dev: 'https://dj.dev.glsx.com.cn',
    uat: 'https://dj.test.glsx.com.cn',
    prod: 'https://dj.glsx.com.cn',
    preRelease: 'https://dj.beta.glsx.com.cn'
  }
  const target = ['ip', 'dev', 'uat', 'prod','preRelease']

const domain= domain_items[target[index]]


/**
 * 通用请求
 * url: 请求路径
 * data: 数据参数
 * header: 请求头
 * method: 请求类型,默认GET,需大写
 * dataType: 返回数据格式,默认json
 * responseType: 响应数据类型,默认text
 * success: 响应成功回调函数
 */
function request(url, data, header, method, dataType, responseType, success, fail) {
  //埋点
  var requestStartTime = Date.now()
  wx.showNavigationBarLoading()
  // 请求超时时间默认60秒
  var timeout = 60000
  if (header && header['request-timeout']) {
    timeout = header['request-timeout']
    delete header['request-timeout']
  }
  // 设置请求头,携带Cookie信息
  setHeader(header)
  var that = this
  wx.request({
    url: domain + url,
    data: data,
    header: header,
    timeout: timeout,
    method: method,
    dataType: dataType,
    responseType: responseType,
    success: function (response) {
      // 未登录
      setHeader(response.header)
      if (response.statusCode == 401) {
        // 跳转到正在登录页面
        wx.navigateTo({
          url: '/pages/auth/login/login'
        })
      } else if (response.statusCode == 200) {
        success(response.data)
      } else {
        if (fail && 'function' === typeof fail) {
          fail()
        }
      }
    },
    fail: function (error) {
      console.log('error:' + JSON.stringify(error))
      if (fail && 'function' === typeof fail) {
        fail()
      }
    },
    complete: function (data) {
      wx.hideNavigationBarLoading()
    }
  })
}

/**
 * 设置请求头Cookie信息
 * header 源请求头信息
 * return 包含Cookie信息header
 */
function setHeader(header) {
  var app = getApp()
  if (app && app.globalData.extConfig.merchantId) {
    header['MerchantId'] = app.globalData.extConfig.merchantId
  }
  var sessionKey = 'DJ_S_'
  // 本地获取存储的sessionId
  var sessionId = wx.getStorageSync(sessionKey)
  if (sessionId) {
    // 优先从本地获取sessionId
    header['Cookie'] = [sessionKey, '=', sessionId].join('')
    return header
  }
  // 本地没有,则从服务器获取
  var cookies = header['Set-Cookie']
  if (cookies) {
    var re = new RegExp('' + sessionKey + '=([^;]*)', 'ig')
    sessionId = cookies.match(re) ? cookies.match(re)[0].substr(sessionKey.length + 1) : null
    if (sessionId) {
      // 从cookie中获取sessionId存储到本地
      wx.setStorageSync(sessionKey, sessionId)
    }
  }
  if (sessionId) {
    header['Cookie'] = [sessionKey, '=', sessionId].join('')
  }
  return header
}

function relogin() {}

module.exports = {
  /**
   * 域名
   */
  domain: domain,

  /**
   * 请求接口封装
   */
  req: request,

  /**
   * 获取会话ID
   */
  getSessionId: function () {
    return wx.getStorageSync(djSessionId)
  },

  /**
   * 利用jscode登录
   * code: 登录凭证
   * appId: 应用ID
   * success: 成功返回处理
   */
  jscodeLogin: function (code, appId, success) {
    request(
      '/web-dj-weapp/jscode/login',
      {
        code: code,
        appId: appId
      },
      {},
      'GET',
      'json',
      'text',
      success
    )
  },

  /**
   * 获取门店信息
   */
  getMerchantBussiness(merchantId, success, fail) {
    request(
      '/web-dj-weapp/servicer/getMerchantBussiness',
      {
        merchantId: merchantId
      },
      {},
      'GET',
      'json',
      'text',
      success,
      fail
    )
  },

  /**
   * 授权登录
   * wechatAuthSetting:微信授权信息
   */
  authLogin: function (wechatAuthSetting, success, fail) {
    request(
      '/web-dj-weapp/auth/login',
      JSON.stringify(wechatAuthSetting),
      {
        'content-type': 'application/json'
      },
      'POST',
      'json',
      'text',
      success,
      fail
    )
  },

  /**
   * 授权手机号
   * wechatAuthSetting:微信授权信息
   */
  authPhone: function (wechatAuthSetting, success) {
    request(
      '/web-dj-weapp/auth/phone',
      JSON.stringify(wechatAuthSetting),
      {
        'content-type': 'application/json'
      },
      'POST',
      'json',
      'text',
      success
    )
  },

  /**
   * 获取小程序头像
   * appId: 应用ID
   */
  getHeadImage: function (appId, success) {
    request('/web-dj-weapp/' + appId + '/headimage/get', null, {}, 'GET', 'json', 'text', success)
  },

  /**
   * 获取小程序应用信息
   * merchantId:商户号
   */
  getAppInfo: function (merchantId, success) {
    request(
      '/web-dj-weapp/appinfo/get',
      {
        merchantId: merchantId
      },
      {
        MerchantId: merchantId
      },
      'GET',
      'json',
      'text',
      success
    )
  },
  /** 我的消息 - 获取最新未读客服信息内容 */
  getCustomerMessageNew: function (data, success, fail) {
    request(
      '/web-dj-weapp/remindServiceInfo/getCustomerMessageNew',
      data,
      {
        'content-type': 'application/json'
      },
      'POST',
      'json',
      'text',
      success,
      fail
    )
  },
  /**
   * 分享二维码
   */
  createQrcode: function (vo, success, fail) {
    request('/web-dj-weapp/servicer/createQrcode', vo, {}, 'GET', 'json', 'text', success, fail)
  },
  /**
   * 根据场景id获取场景值
   */
  qrCodeScene: function (qrcodeScene, success, fail) {
    request('/web-dj-weapp/home/qrCodeScene/getSceneById', qrcodeScene, {}, 'GET', 'json', 'text', success, fail)
  },
  /**
   * 退出登录
   * @param {成功回调方法} success
   * @param {失败回调方法} fail
   */
  logout: function (success, fail) {
    request('/web-dj-weapp/logout', {}, {}, 'GET', 'json', 'text', success, fail)
  },
  /**
   * 页面访问数据收集
   * @param {收集数据} data
   * @param {成功回调方法} success
   * @param {失败回调方法} fail
   */
  pageVisitCollect: function (data, success, fail) {
    request(
      '/web-dj-weapp/page/visit/collect',
      data,
      {
        'content-type': 'application/json'
      },
      'POST',
      'json',
      'text',
      success,
      fail
    )
  }
}