import {ref } from 'vue';
import Aes from './Aes.js';
import Common from '../utils/common.js';
import { httpconfig } from './httpconfig.js';

export const $http = {
  post: (type, Nothis, Objson, callBack) => {
    // 使用 ref 存储可能需要响应式的数据（根据实际需求调整）
    const appUrlAddressYDHL = ref(uni.getStorageSync('appUrlAddress') + '/bdhealth/').value;
    let formData = Object.assign({}, Objson.data);
    formData.code = Objson.code;
    
    let sessionData = uni.getStorageSync('userData');
    let session = sessionData ? [sessionData] : [{}];
    formData.session = session;
    
    let bodyData = JSON.stringify(formData);
    if (appUrlAddressYDHL.indexOf('Encrypt') > -1) {
      bodyData = Aes.encrypt(JSON.stringify(formData));
    }
    
    uni.request({
      url: appUrlAddressYDHL,
      data: bodyData,
      method: 'post',
      header: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        Authorization: httpconfig.authorizationToken,
        'Access-Control-Allow-Origin': '218.17.212.34'
      },
      success: (res) => {
        if (+res.statusCode === 200) {
          let data = res.data;
          if (appUrlAddressYDHL.indexOf('Encrypt') > -1) {
            data = JSON.parse(Aes.decrypt(data));
          }
          
          if (+data.errorCode === 0) {
            Objson.success.call(Nothis, data);
          } else {
            const timeoutCodes = ['01040053', '01040054', '01040055'];
            if (timeoutCodes.includes(data.errorCode)) {
              uni.showToast({
                title: '网络超时，请重新操作.',
                icon: 'none',
                success: () => {}
              });
              Objson.success.call(Nothis, data);
            } else {
              if (data.errorCode !== '01040052') {
                if (Objson.fail) {
                  Objson.fail.call(Nothis, data);
                }
                if (callBack?.error === 'Y') {
                  Objson.success.call(Nothis, data);
                  return;
                }
                uni.showToast({
                  title: data.errorMessage,
                  icon: 'none'
                });
              } else {
                uni.showToast({
                  title: `${data.errorCode} - ${data.errorMessage}`,
                  icon: 'none',
                  success: () => {}
                });
              }
            }
          }
        } else {
          const urlCode = formData.code || '';
          uni.showModal({
            title: '异常提示',
            content: `${urlCode}网络环境异常-${res.statusCode}`,
            showCancel: false,
            confirmText: '知道了',
            success: (resModal) => {
              if (resModal.confirm && callBack?.error === 'Y') {
                const data = {
                  errorCode: '-1',
                  errorMessage: '接口异常'
                };
                Objson.success.call(Nothis, data);
              }
            }
          });
        }
      },
      fail: (error) => {
        const data = {
          errorCode: '-1',
          errorMessage: '网络异常，请检查网络IP/端口地址'
        };
        Objson.success.call(Nothis, data);
      }
    });
  }
};