12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- let BASE_URL = 'https://ld.726p.com' // 请求地址 - 生产环境
- // let BASE_URL = 'http://ld-test.wenxingshuju.com' // ceshi
- // let BASE_URL = 'http://60.205.169.65:8006' // 请求地址 - 测试环境
- // #ifdef H5
- BASE_URL = '' //线上模式
- // BASE_URL = '/api' // 开发模式
- // #endif
- const LOGIN_ERR_CODES = [ // 登录状态失效错误码
- '2008',
- '2005',
- '4001',
- '4002',
- ]
- const handleToLogin = () => {
- uni.clearStorageSync()
- uni.reLaunch({
- url: '/pages/subPackages/login/loginPhone'
- });
- }
- const req = (url, method, data, selfHeader) => {
- const dataes = {
- // 'ttl': uni.getStorageSync('ttl'),
- }
- const header = {
- 'source': uni.getStorageSync('channel_source') || '',
- 'version': uni.getStorageSync('channel_version') || '',
- // #ifdef H5
- 'source': '3002',
- 'version': 'v1.0',
- // #endif
- 'token': uni.getStorageSync('token') || '',
- }
- return new Promise((resolve, reject) => {
- uni.request({
- url:`${BASE_URL}${url}`,
- data: data ? Object.assign(dataes, data) : dataes,
- method: method,
- header: selfHeader ? Object.assign(header, selfHeader) : header,
- success: function (res) {
- if (LOGIN_ERR_CODES.includes(String(res.data.errno))) {
- handleToLogin()
- }
- resolve(res)
- },
- fail: function (err) {
- reject(err)
- }
- })
- })
- }
- const get = (url, data, header = {}) => {
- return req(url, 'GET', data, {
- ...header
- })
- }
- const post = (url, data, header = {}) => {
- return req(url, 'POST', data, {
- 'Content-Type': 'application/x-www-form-urlencoded',
- ...header,
- })
- }
- const postJSON = (url, data, header = {}) => {
- return req(url, 'POST', data, {
- 'Content-Type': 'application/json',
- ...header,
- })
- }
- // 获取随机参数 h5使用
- const getUuid = () => {
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
- const r = (Math.random() * 16) | 0,
- v = c == 'x' ? r : (r & 0x3) | 0x8
- return v.toString(16)
- })
- }
- export {
- req,
- BASE_URL,
- get,
- post,
- postJSON,
- getUuid,
- }
|