

import Storage from '@/common/utils/storage'
import { hostname, apiPrefix } from '@/common/hostname'
import EXEC from '../exec';



export const baseUrl =  `https://${hostname}${apiPrefix}`; 

export const http = <any>{
    // get 
    get: (url:string, params:any, options:any = {}) => request({ url, params, method:'GET', }, options),
    // post 
    post: (url:string, params:any, options:any = {}) => request({ url, params, method:'POST', }, options),
    // put 
    put: (url:string, params:any, options:any = {}) => request({ url, params, method:'PUT', }, options),
    // patch 
    patch: (url:string, params:any, options:any = {}) => request({ url, params, method:'PATCH', }, options),
    // post 
    delete: (url:string, params:any, options:any = {}) => request({ url, params, method:'DELETE', }, options),
}

const request = ({url, params, method='GET',}:any, options={}) => {
    return new Promise<void>((resolve:any, reject:any) => {
        let JSON = {
            url: baseUrl + url, //仅为示例，并非真实的接口地址
            header: method == 'GET' ? {'X-Requested-With': 'XMLHttpRequest',"Accept": "application/json","Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"} : {'X-Requested-With': 'XMLHttpRequest','Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
            method: method,
            data: params,
        }

        // 全局添加参数，并检查有效参数
        let globalParams = { 
            user_token: Storage.get('userToken') || '',
            exec_system_id: EXEC.system_id,
            exec_comp_id: EXEC.comp_id || 'IA',
            exec_db_code: EXEC.db_code,
            exec_identity_role: EXEC.identity_role,
            exec_device_type: 'WEAPP',
            exec_menu_id: EXEC.active_menu_id,
        }
        Object.assign(JSON.data, globalParams)
        JSON.data = checkData(JSON.data)

        uni.request({
            ...JSON,
            success: (result:any)=> {
                let res = result.data
                if(res['success']){
                    resolve(res)
                }else{
                    let msg:any = '接口出错了！'
                    if(typeof(res.result) == 'string'){
                        msg = res.result
                    }else if(typeof(res.result)  == 'object'){
                        msg = res.result.errorMsg
                    }
                    uni.showToast({
                        icon: 'error',
                        title: res.result,
                    })
                    if(res['stateCode']=='0209'){
                        uni.reLaunch({
                            url: '/pages/basic/sign/index',
                        })
                    }
                    reject(res)
                }
            },
            fail: (err:any)=>{
                uni.showToast({
                    icon: 'error',
                    title: '请求被拦截',
                })
                reject(err)
            },
        })
    })
}



// 检查参数是否有效
const checkData = (data:any) => {
    if (data == null || data == undefined ) return {};
    for (let key in data) {
      if ( 
        data[key] === null 
        || data[key] === undefined 
        // || data[key] === "" 
        || JSON.stringify(data[key]) == '{}' 
        || JSON.stringify(data[key]) == '[]'
      ){
        delete data[key];
      }
    }
    return data;
  }

