// import WindVane from '@ali/lib-windvane' import WindVane from '../../../../lib/windvane.common.js' // import Mtop from '@ali/lib-mtop/build/mtop.common' import Mtop from '../../../../lib/mtop.common.js' import ERROR_CODE from './errorCode' import { get } from 'jsonuri' interface ResultData { msg?: string code?: string type: string model?: any } interface RequestOption { api: string v?: string data?: any } let isInChrome = false export function setDebug() { isInChrome = true } export const windvaneIsAvailable = !!(WindVane && WindVane.isAvailable) /** * @name H5 请求 * @description 仅在开发环境下可使用 * @param {RequestOption} option * @param {Function} validChecker * @returns {Promise} */ const mtopH5Request = (option: RequestOption, validChecker: Function = defValidChecker) => { if (windvaneIsAvailable) { alert('App容器中禁止使用 mtopH5Request!') } let params = Object.assign({ // 通用参数 // api: '', // 必须 v: '1.0', // 必须 // data: {}, // 必须(注意1) // appKey: '12574478', // 非必须,默认不需要传 ecode: 0, // 必须(注意2) type: 'GET', // 非必须。请求类型(GET/POST),默认是GET dataType: 'jsonp', // 非必须。数据类型(jsonp/originaljsonp/json),默认jsonp timeout: 20000 // 非必须。接口超时设置,默认为20000ms }, option) // 获取用户模拟authInfo let authInfo = __getLocalStorageItem('__authInfo') if (authInfo) { params.data.authInfo = authInfo } // Mtop环境切换 let mtopEnv = __getLocalStorageItem('__mtopEnv') if (mtopEnv) { params.api = params.api + mtopEnv const lib = window['lib'] if (lib && /pre/.test(mtopEnv)) { lib.mtop.config.subDomain = 'wapa' lib.mtop.config.mainDomain = 'taobao.com' lib.mtop.config.prefix = 'h5api' } } let subDomain = __getLocalStorageItem('__subDomain') if (subDomain) { const lib = window['lib'] lib.mtop.config.subDomain = subDomain } let request = Mtop.request(params) return new Promise((resolve: (res: any) => void, reject) => { request.then((e) => { let valid = validChecker(e) if (valid.type === 'succeed') { resolve(valid.model) } else { reject(valid) } }).catch((d) => { console.error('MTOP ERROR', d) if (d && d.retType === 2) { __goLogin() } reject(resultDataFormat(ERROR_CODE.NET_ERR)) }) }) } export const mtopNativeRequest = (option: RequestOption, validChecker: Function = defValidChecker) => { let params: any = Object.assign({ version: '1.0', method: 'get' }, option) delete params.data params.bizParams = option.data params.version = option.v || params.version return new Promise((resolve, reject) => { WindVane.call('Mtop', 'request', params, function (re) { let valid = validChecker({ data: re }) if (valid.type === 'succeed' && valid.model) { resolve(valid.model) } else { reject(valid) } }, function (re) { // Native Android 位于后台中需要正常返回数据 if (re && re.msg === 'BACKGROUND') { resolve(resultDataFormat({ type: 'succeed', msg: 'BACKGROUND', model: {} })) } // 接口异常 console.error('WV Mtop调用失败:', params, re) reject(resultDataFormat(ERROR_CODE.NET_ERR)) }) }) } export const mtopRequest = (option: RequestOption, validChecker: Function = defValidChecker) => { let isDebug = __isDebug() // 开发模式下请求H5 if (isDebug && !windvaneIsAvailable) { return mtopH5Request(option, validChecker) } return mtopNativeRequest(option, validChecker) } function __goLogin() { // 统一处理未登录 window.location.href = `https://login.m.taobao.com/login.htm?redirectURL=${encodeURIComponent(window.location.href)}` // console.info('点击登录: https://login.m.taobao.com/login.htm?redirectURL=' + encodeURIComponent(window.location.href)) } function defValidChecker (re): ResultData { let model = get(re, 'data/model') if (model) { return resultDataFormat({ type: 'succeed', model: model }) } return resultDataFormat(ERROR_CODE.NET_ERR) } function __getLocalStorageItem(key) { return get(window, `/localStorage/${key}`) } function __isDebug() { return __getLocalStorageItem('__isDebug') === 'true' } export function resultDataFormat ({ msg, code, type, model }: ResultData): ResultData { let re: ResultData if (type === 'succeed') { return { type: 'succeed', model } } re = { msg, code: code !== undefined ? code : '', type: type || 'error' } if (model) { re.model = model } return re }