小程序封装接口服务请求
小程序封装服务请求
接口使用(main.js)
const urlConfig = require('./urlConfig.js'); const utilHttp= require('./utilHttp.js'); getWxOpenId: function (params, callback, errback) { //获取微信openId utilHttps.requestGetFunction(urlConfig.reconsitution,"/ncwxmp/getWxOpenId", params, callback, errback); },
接口服务配置(urlConfig.js)
/*后台接口服务配置*/ var service = { sType: 'testB', //环境类型:exploit-开发环境,testB-测试环境,uat-uat环境,prod-生产环境 } const serverInfo = getServeContext(service.sType); function getServeContext(v) { // 判断环境服务 ; v - 环境类型 var service = { customerAccess: '', // h5品播项目服务地址 reconsitution: '', //移动接入服务 } switch (v) { case 'exploit': // 开发环境 service.customerAccess = 'http://192.168.233.51:8080/hsxt-access-point'; //测试服务 service.reconsitution = 'https://dc.aadv.net:10443/mobile/reconsitution'; //测试服务 break; case 'testB': // 测试环境 service.customerAccess = 'https://dc.aadv.net:10443/cn/company/pointAccess'; service.reconsitution = 'https://dc.aadv.net:10443/mobile/reconsitution'; break; case 'uat': // uat环境 service.customerAccess = 'https://dc.aadv.net:11443/cn/company/pointAccess'; service.reconsitution = 'https://dc.aadv.net:11443/mobile/reconsitution'; break; case 'prod': // 生产环境 service.customerAccess = 'https://www.hsxt.net:18092/hsxt-access-point'; service.reconsitution = 'https://mobi.hsxt.cn:9446/refactor'; break; } return service; } module.exports = { sType: service.sType, customerAccess: serverInfo.customerAccess, reconsitution: serverInfo.reconsitution }
服务封装(utilHttp.js)
/** * 非固定服务,访问ajax,get方法请求-formdata格式传参 * @param domainUrl 接口名称 * @param params 接口请求参数:{} * @param callback 接口请求成功回调 * @param errback 接口请求失败回调 * @returns {string} */ requestGetFunction: function (serverUrl, domainUrl, params, callback, errback) { wx.request({ url: utilHttps.subUrlFunc(serverUrl, domainUrl), data: params, method: 'GET', enableCache: false, header: utilHttps.getIsUserLogin(true, 'fromdata'), success: function (res) { utilHttps.getResultData(res, function (ress) { callback(ress); }, function (ress) { if (errback) { errback(ress); } else { utilHttps.getToast(ress.msg); } }); }, fail(res) { //wx.hideLoading(); utilHttps.getToast('服务器繁忙,请稍后重试!'); if (errback) { errback(res); } }, }) }, /** * 非固定服务,访问ajax,post方法请求-formdata格式传参 * @param domainUrl 接口名称 * @param params 接口请求参数:{} * @param callback 接口请求成功回调 * @param errback 接口请求失败回调 * @returns {string} */ requestPostFunction: function (serverUrl, domainUrl, params, callback, errback) { wx.request({ url: utilHttps.subUrlFunc(serverUrl, domainUrl), data: params, method: 'POST', header: utilHttps.getIsUserLogin(true), success: function (res) { utilHttps.getResultData(res, function (ress) { callback(ress); }, function (ress) { if (errback) { errback(ress); } else { wx.showToast({ title: ress.msg, icon: 'none', duration: 2000 //持续的时间 }) } }); }, fail(res) { //wx.hideLoading(); utilHttps.getToast('服务器繁忙,请稍后重试!'); if (errback) { errback(res); } }, }) }, /** * 非固定服务,访问ajax,get方法请求-json格式传参 * @param domainUrl 接口名称 * @param params 接口请求参数:{} * @param callback 接口请求成功回调 * @param errback 接口请求失败回调 * @returns {string} */ requestGetJsonFunction: function (serverUrl, domainUrl, params, callback, errback) { wx.request({ url: utilHttps.subUrlFunc(serverUrl, domainUrl), data: params, method: 'GET', dataType: "JSON", enableCache: false, header: utilHttps.getIsUserLogin(true), success: function (res) { utilHttps.getResultData(res, function (ress) { callback(ress); }, function (ress) { if (errback) { errback(ress); } else { wx.showToast({ title: ress.msg, icon: 'none', duration: 2000 }) } }); }, fail(res) { //wx.hideLoading(); utilHttps.getToast('服务器繁忙,请稍后重试!'); if (errback) { errback(res); } }, }) }, /** * 非固定服务,访问ajax,post方法请求-json格式传参 * @param domainUrl 接口名称 * @param params 接口请求参数:{} * @param callback 接口请求成功回调 * @param errback 接口请求失败回调 * @returns {string} */ requestPostJsonFunction: function (serverUrl, domainUrl, params, callback, errback) { wx.request({ url: utilHttps.subUrlFunc(serverUrl, domainUrl), data: JSON.stringify(params), method: 'POST', dataType: "JSON", header: utilHttps.getIsUserLogin(true, 'json'), success: function (res) { utilHttps.getResultData(res, function (ress) { callback(ress); }, function (ress) { if (errback) { errback(ress); } else { utilHttps.getToast(ress.msg); } }); }, fail(res) { //wx.hideLoading(); utilHttps.getToast('服务器繁忙,请稍后重试!'); if (errback) { errback(res); } }, }) }, getIsUserLogin(flag, format) { //获取登录信息写进请求头:flag-是否加入登录信息 // channelType: 1-网页接入,2-POS接入,3- 刷卡器接入,4- 移动app接入,5- 互生平板接入,6- 通用平板接入,7- 语音接入,8- 第三方接入, 9- 互生POS接入,30- 刷卡器 andorid接入,31- 刷卡器IOS接入,40- 移动App andorid接入,41- 移动App IOS接入,60- 通用平板 andorid,61- 通用平板 ios var header = { 'content-Type': 'application/json' } if (format == 'json') { header["content-Type"] = 'application/json'; } else if (format == 'fromdata') { header["content-Type"] = 'application/x-www-form-urlencoded;charset=UTF-8'; } if (flag) { if (!isEmpty(wx.getStorageSync('chat_percustid'))) { return header; } var entUserInfo = wx.getStorageSync('entUserInfo') if (entUserInfo) { entUserInfo = JSON.parse(entUserInfo); header.token = entUserInfo.GToken; header.GToken = entUserInfo.GToken; header.custId = entUserInfo.operCustId; header.entCustId = entUserInfo.entCustId; header.channelType = "1"; header.mac = ""; header.version = "1.0.0"; header.appName = "miniNCHelper"; } } return header; },