const app = getApp() let subDomain = '' // 子域名,没有就等于 // const API_BASE_URL = 'https://apartmentcloud.xiaozhisz.cn/' // 主域名 const API_BASE_URL = 'http://test.zhiweisz.cn/' // 主域名 // const API_BASE_URL = 'https://yktms-cs.utsz.edu.cn/' // 主域名 import JSEncrypt from '../js_sdk/jsencrypt-Rsa/jsencrypt/jsencrypt.min.js'; function setCode(str, text) { let pubKey = str; // ES6 模板字符串 引用 rsa 公钥 let encryptStr = new JSEncrypt(); encryptStr.setPublicKey(pubKey); // 设置 加密公钥 let data = encryptStr.encrypt(text.toString()); // 进行加密 return data; } let tenantIdEnter='100' // let tenantIdEnter='1200' function setTenantId(tenantId){ tenantIdEnter=tenantId wx.setStorageSync('tenantId',tenantId) } const request = (url, method, data,id='',no='') => { wx.showLoading({ title: '加载中' }); let _url = API_BASE_URL + subDomain + url.replace("{id}",id) var userToken = wx.getStorageSync('token') let tenantId=tenantIdEnter//wx.getStorageSync('tenantId') if (method == "GET") { return new Promise((resolve, reject) => { let header={} if(no=='0'){ header={ 'Content-Type': 'application/json', 'tenantId': tenantId } }else{ header={ 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + (userToken ? userToken.access_token : ''), 'tenantId': tenantId } } wx.request({ url: _url, method: method, data: data, dataType: 'json', header: header, success(request) { if (request.statusCode === 401) { wx.showToast({ title: 'token已过期,请重新登录', duration: 2000, icon: 'none' }) wx.reLaunch({ url: '/pages/login/login', success: (res) => { wx.removeStorageSync('token') }, fail: (res) => { console.log(res) } }) } resolve(request.data) wx.hideLoading() }, fail(error) { reject(error) wx.hideLoading() }, complete(aaa) { // 加载完成 } }) }) } else if (method == "POST") { return new Promise((resolve, reject) => { let header={} console.log(no,'nonon') if(no=='0'){ header={ 'tenantId': tenantId } }else{ header={ 'Authorization': 'Bearer ' + (userToken ? userToken.access_token : ''), 'tenantId': tenantId } } wx.request({ url: _url, method: method, data: data, header: header, dataType: 'json', success(request) { if (request.statusCode === 401) { wx.showToast({ title: 'token已过期,请重新登录', duration: 2000, icon: 'none' }) wx.reLaunch({ url: '/pages/login/login', success: (res) => { wx.removeStorageSync('token') }, fail: (res) => { console.log(res) } }) } resolve(request.data) wx.hideLoading() }, fail(error) { reject(error) wx.hideLoading() }, complete(aaa) { // 加载完成 } }) }) } } const requestOne = (url, method, data) => { wx.showLoading({ title: '加载中' }); let _url = API_BASE_URL + subDomain + url let tenantId=tenantIdEnter//wx.getStorageSync('tenantId') return new Promise((resolve, reject) => { wx.request({ url: _url, method: method, data: data, header: { 'content-type': 'application/json', 'tenantId': tenantId }, dataType: 'json', success(request) { if (request.statusCode === 401) { wx.showToast({ title: 'token已过期,请重新登录', duration: 2000, icon: 'none' }) wx.reLaunch({ url: '/pages/login/login', success: (res) => { wx.removeStorageSync('token') }, fail: (res) => { console.log(res) } }) }else if(request.statusCode === 400){ // wx.showToast({ // title: 'token已过期,请重新登录', // duration: 2000, // icon: 'none' // }) // wx.reLaunch({ // url: '/pages/login/login', // success: (res) => { // wx.removeStorageSync('signature') // wx.removeStorageSync('token') // }, // fail: (res) => { // console.log(res) // } // }) } resolve(request.data) wx.hideLoading() }, fail(error) { reject(error) wx.hideLoading() }, complete(aaa) { // 加载完成 } }) }) } const request1 = (url, method, data) => { wx.showLoading({ title: '加载中' }); let _url = API_BASE_URL + subDomain + url let tenantId=tenantIdEnter//wx.getStorageSync('tenantId') return new Promise((resolve, reject) => { wx.request({ url: _url, method: method, data: data, header: { 'content-type': 'application/x-www-form-urlencoded', 'tenantId': tenantId }, dataType: 'json', success(request) { if (request.statusCode === 401) { wx.showToast({ title: 'token已过期,请重新登录', duration: 2000, icon: 'none' }) wx.reLaunch({ url: '/pages/login/login', success: (res) => { wx.removeStorageSync('token') }, fail: (res) => { console.log(res) } }) } resolve(request.data) wx.hideLoading() }, fail(error) { reject(error) wx.hideLoading() }, complete(aaa) { // 加载完成 } }) }) } const request2 = (url, method, data) => { wx.showLoading({ title: '加载中' }); let _url = API_BASE_URL + subDomain + url var userToken = wx.getStorageSync('token') let tenantId=tenantIdEnter//wx.getStorageSync('tenantId') if (method == "GET") { return new Promise((resolve, reject) => { wx.request({ url: _url, method: method, data: data, dataType: 'json', header: { 'Content-Type': 'application/json', 'tenantId': tenantId }, success(request) { resolve(request.data) wx.hideLoading() }, fail(error) { reject(error) wx.hideLoading() }, complete(aaa) { // 加载完成 } }) }) } else if (method == "POST") { return new Promise((resolve, reject) => { wx.request({ url: _url, method: method, data: data, header: { 'Authorization': 'Bearer ' + (userToken ? userToken.access_token : ''), 'tenantId': tenantId }, dataType: 'json', success(request) { if (request.statusCode === 401) { wx.showToast({ title: 'token已过期,请重新登录', duration: 2000, icon: 'none' }) wx.reLaunch({ url: '/pages/login/login', success: (res) => { wx.removeStorageSync('token') }, fail: (res) => { console.log(res) } }) } resolve(request.data) wx.hideLoading() }, fail(error) { reject(error) wx.hideLoading() }, complete(aaa) { // 加载完成 } }) }) } } const request3 = (url, method, data) => { wx.showLoading({ title: '加载中' }); let _url = subDomain + url var userToken = wx.getStorageSync('token') let tenantId=tenantIdEnter//wx.getStorageSync('tenantId') if (method == "GET") { return new Promise((resolve, reject) => { wx.request({ url: _url, method: method, data: data, dataType: 'json', header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + (userToken ? userToken.access_token : ''), 'tenantId': tenantId }, success(request) { if (request.statusCode === 401) { wx.showToast({ title: 'token已过期,请重新登录', duration: 2000, icon: 'none' }) wx.reLaunch({ url: '/pages/login/login', success: (res) => { wx.removeStorageSync('token') }, fail: (res) => { console.log(res) } }) } resolve(request.data) wx.hideLoading() }, fail(error) { reject(error) wx.hideLoading() }, complete(aaa) { // 加载完成 } }) }) } else if (method == "POST") { return new Promise((resolve, reject) => { wx.request({ url: _url, method: method, data: data, header: { 'Authorization': 'Bearer ' + (userToken ? userToken.access_token : ''), 'tenantId': tenantId }, dataType: 'json', success(request) { if (request.statusCode === 401) { wx.showToast({ title: 'token已过期,请重新登录', duration: 2000, icon: 'none' }) wx.reLaunch({ url: '/pages/login/login', success: (res) => { wx.removeStorageSync('token') }, fail: (res) => { console.log(res) } }) } resolve(request.data) wx.hideLoading() }, fail(error) { reject(error) wx.hideLoading() }, complete(aaa) { // 加载完成 } }) }) } } /** * 小程序的promise没有finally方法,自己扩展下 */ Promise.prototype.finally = function (callback) { var Promise = this.constructor; return this.then( function (value) { Promise.resolve(callback()).then( function () { return value; } ); }, function (reason) { Promise.resolve(callback()).then( function () { throw reason; } ); } ); } const throttle = (fn,delay)=>{ let oldDate = Date.now(); return function(){ let args = arguments; let newDate = Date.now(); let that = this; if(newDate-oldDate>delay){ fn.apply(that,args); //倘若时间差大于延长时间 就更新一次旧时间 oldDate = Date.now(); } } } module.exports = { request, request1, request2, requestOne, setCode, setTenantId, throttle, login: data => request1('oauth/token', 'POST', data), // 登录 oauthKey: (data) => request2('oauth/key', 'GET', data), // 登录code getDeviceItems: (data) => request('v1/device/get/items', 'POST', data), //获取设备 getSiteList: (data) => request('v1/site/get/list', 'GET', data), //获取房间列表信息 getIntegration: (data) => request('v1/card/get/integration', 'GET', data), //获取首页列表 getuserInfo: (data) => request('v1/account/user/info', 'GET', data), //获取账号信息 getSiteTenant: (data) => request('v1/site/get/tenant', 'GET', data), //获取已入住人员信息 openLock: (data) => request('v1/device/isp/openLock', 'POST', data), //远程开门 addCertificates: (data) => request('v1/device/add/certificate', 'POST', data), //添加凭证 getOpenRecord: (data) => request('v1/device/get/open/record', 'POST', data), //获取开门数据 getDeviceOperateUserInfo: (data) => request('v1/device/isp/operateUserInfo', 'POST', data), //获取开门数据 bindDeviceSite: (data) => request('v1/device/bind/site', 'POST', data), //绑定房间 unbindDeviceSite: (data) => request('v1/device/unbind/site', 'POST', data), //解除绑定房间 unbindCard: (data) => request('v1/site/unbind/card', 'POST', data), //人员解绑房间 sendCommand: (data) => request('v1/device/send/command', 'POST', data), //通用接口 sendCommandOne: (data) => requestOne('v1/device/send/bleCommand', 'POST', data), //通用接口 sendCommandGet: (data) => request('v1/device/send/command', 'GET', data), //通用接口 ZGregisterDevice: (data) => request('v1/zg/registerDevice', 'POST', data), //ZG注册平台 addCard: (data) => request('v1/card/add', 'POST', data), //创建人员(卡) bindCard: (data) => request('v1/site/bind/card', 'POST', data), //绑定房间 deleteDevice: (data) => request('v1/device/delete/device', 'POST', data), //删除设备 deleteZGDevice: (data) => request('v1/zg/deleteDevice', 'POST', data), //删除ZG设备 getTree: (data) => request('v1/site/get/tree', 'GET', data), //获取房源tree getCertificates: (data) => request('v1/device/certificate/list', 'GET', data), //获取凭证列表 ZGduserOperation: (data) => request('v1/zg/userOperation', 'POST', data), //添加人员 getDeviceResult: (data) => request('v1/device/get/command/result', 'POST', data), //查询反馈日志列表 deleteCertificates: (data) => request('v1/device/delete/certificate', 'POST', data), //凭证列表删除 sendEmergence: (data) => request('v1/device/send/emergence', 'POST', data), //发送离线密码短信 updatePush: (data) => request('v1/push/paas/update/push', 'POST', data), //修改反馈日志状态 editPassword: (data) => request('v1/account/edit/password', 'POST', data), //修改登录账户 fingerprintTransfer: (data) => request('v1/device/isp/fingerprintTransfer', 'POST', data), //添加指纹 getDeviceRegisterManager: (data) => request('v1/device/isp/registerManager', 'POST', data), //重置添加管理员 orderDeviceData: (data) => request('v1/device/order/deviceData', 'GET', data),//获取指定锁所有非703凭证数据 updateCertificates: (data) => request('v1/device/update/certificate', 'POST', data),//凭证列表修改 editStatusBySerial: (data,url) => request(url, 'GET', data),//凭证列表修改 editPushBySerial: (data) => request('v1/device/editPush/bySerial', 'POST', data),//指纹ID记录传输 registerDeviceAll:(data)=>request('v1/device/all/registerDevice', 'POST', data),//注册设备集成接口 getlistNum:(data)=>request('v1/card/get/list', 'POST', data),//获取人员 onlyTenant:(data)=>request('v1/card/only/tenant', 'GET', data),//查询房间和人员信息 atCardUser: data=>requestOne('v1/account/at/cardUser', 'POST', data),//租客登录 getConfig:data=>request('v1/auto/get/config', 'GET', data), getOperateList:data=>request('v1/log/operate/list', 'POST', data), getDepartmentUsers:(data,id)=>request('work/wechat/department/users/{id}', 'GET', data,id),//获取企业id getDepartment:data=>request('work/wechat/get/department', 'GET', data),//获取企业用户列表 convertToUserid:data=>request('work/wechat/convert_to_userid', 'GET', data),//用openid换企业账号id createWXCardUser:data=>request('v1/card/create/WXCard/user', 'GET', data),//用openid换企业账号id getLeasesList:(data)=>request('v1/leases/get/list','POST',data),//获取合约 getOpenId:data=>requestOne('work/wechat/jscode/session', 'GET', data),//获取openid editCertificate: (data) => requestOne('v1/card/ts/editCertificate', 'POST', data),//凭证状态修改 tsdeleteCertificates: (data) => requestOne('v1/device/delete/certificate', 'POST', data), //凭证列表删除 tsGetCertificate:(data)=>requestOne('v1/card/ts/getCertificate', 'POST', data),//租客端获取凭证列表 tsAddCertificate:(data)=>requestOne('v1/card/ts/addCertificate', 'POST', data),//租客端添加凭证 toWXCorpUser:(data)=>requestOne('v1/account/to/WXCorp/user', 'POST', data),//租客端登录 getApartmentCardInfo:(data)=>requestOne('v1/account/get/apartmentCardInfo', 'POST', data),// 获取合同信息,交租订单 createOrderPay:(data)=>request('v1/pay/createOrder', 'POST', data),// 创建交租订单 miniPrepayOrder:(data)=>requestOne('v1/pay/miniPrepayOrder','POST',data),//租客端创建订单 miniPrepayOrderT:(data)=>request('v1/pay/miniPrepayOrder','POST',data),//租客端创建订单 minGetOpenLocks:(data)=>requestOne('v1/card/ts/getOpenLocks','POST',data),//租客端获取开锁记录 getBleUnlockRecord:(data)=>requestOne('v1/card/ts/getBleUnlockRecord','POST',data),//租客端获取开锁记录 updateCard:(data)=>requestOne('v1/account/edit/myKey','POST',data), getDeviceOperateUserInfo:(data)=>requestOne('v1/device/isp/operateUserInfo','POST',data), // bleUnlockRecord: (data) => request('v1/zg/bleUnlockRecord','POST', data),//上传蓝牙开锁记录 // bleRspRecord: (data) => request3('http://192.168.1.34:8900/iot/v1/ble/deviceReport','POST', data),//传输蓝牙返回数据包 getItemsPay:(data)=>request('v1/price/get/items','POST',data),//计算待支付金额,生成对应订单 getSiteOrder:(data)=>request('v1/price/get/siteOrder','POST',data),//获取计算待支付金额订单 }