// 凡云ai,接口配置文件
// if (process.env.NODE_ENV === 'development') {
// 	console.log('开发环境---------------------');
// 	var siteConfig = {
// 		uniacid: uni.getStorageSync('uniacid1') || 5059,
// 		siteroot: "https://meta.swdz.com/index.php/",
// 		root: "https://meta.swdz.com/",
// 	};
// }

// if (process.env.NODE_ENV === 'production') {
// 	console.log('生产环境------------------');
// 	var siteConfig = {
// 		uniacid: uni.getStorageSync('uniacid1') || 5059,
// 		siteroot: "https://nywhcm.com/addons/wike_aging/public/index.php/",
// 		root: "https://nywhcm.com/addons/wike_aging/public/",
// 	};
// }




// let siteInfo = siteConfig;

if (process.env.NODE_ENV === 'development') {
	var siteInfo = {
		uniacid: uni.getStorageSync('uniacid') || 5059,
		// siteroot: "https://meta.swdz.com/index.php/",
		siteroot: "https://t.scxxq.cn/index.php/",
		root: "https://t.scxxq.cn/",
	};
} else {
	// var siteInfo = {
	// 	uniacid: uni.getStorageSync('uniacid') || 5059,
	// 	// siteroot: "https://meta.swdz.com/index.php/",
	// 	siteroot: "https://t.scxxq.cn/index.php/",
	// 	root: "https://t.scxxq.cn/",
	// };
	var siteInfo = {
		uniacid: uni.getStorageSync('uniacid') || 5059,
		siteroot: "https://nywhcm.com/addons/wike_aging/public/index.php/",
		root: "https://nywhcm.com/addons/wike_aging/public/",
	};
}


// require('../siteinfo.js')

// W7 Miniapp
// let a,b,c;
// a = siteInfo.siteroot;
// b = a.split("/app/index.php")
// c = b[0]
// siteInfo.siteroot = `${c}/addons/wike_aging/public/index.php/`
// siteInfo.root = `${c}/addons/wike_aging/public/`

// W7 3.0 Miniapp
// let a,b,c;
// a = siteInfo.siteroot;
// b = a.split("/app/index.php")
// c = b[0]
// siteInfo.siteroot = `${c}/addons/wike_chatgpt/public/index.php`
// siteInfo.root = `${c}/addons/wike_chatgpt/public/`

let uniacid = siteInfo.uniacid;


export const API_STEROOT = `${siteInfo.siteroot}`; //chat接口域名
export const API_ROOT = `${siteInfo.root}`; //后台接口域名
export const API_URL = `${siteInfo.root}mobile.php/index/index.html?uniacid=${uniacid}`; //后台接口域名
export const UNIACID = `${uniacid}`; //
export const IMG_URL = `${siteInfo.root}static/mobile/images`;

export function apiurl(action) {
	// return siteInfo.siteroot + 'api.' + action + '?uniacid=' + uniacid + "&" + uniacid;
	console.log('请求封装传递的uniacid', uni.getStorageSync('uniacid'));
	return siteInfo.siteroot + 'api.' + action + '?uniacid=' + (uni.getStorageSync('uniacid') ? uni.getStorageSync(
		'uniacid') : '5059') + "&";
}

import platform from '@/common/platform/index';
export default class Request {
	constructor() {
		// console.log('uniacid:'+uniacid);
		// 默认配置
		this.config = {
			baseUrl: siteInfo.siteroot,
			header: {
				'content-type': 'application/json',
				'platform': platform.get(),
				'uniacid': uniacid
			},
			url: '',
			data: {},
			params: {},
			method: 'GET',
			dataType: 'json',
			// #ifndef MP-ALIPAY || APP-PLUS
			responseType: 'text',
			// #endif
			custom: {},
			// #ifdef APP-PLUS
			sslVerify: false
			// #endif
		}

		/* 拦截器 */
		this.interceptor = {
			request: cb => {
				if (cb) {
					this.requestBefore = cb
				} else {
					this.requestBefore = request => request
				}
			},
			response: (cb) => {
				if (cb) {
					this.requestAfter = cb
				} else {
					this.requestAfter = response => response
				}
			}
		}
	}

	/* 判断url是否完整 */
	static isUrl(url) {
		return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
	}

	static addQueryString(params) {
		let paramsData = ''
		Object.keys(params).forEach(key => {
			paramsData += key + '=' + encodeURIComponent(params[key]) + '&'
		})
		return paramsData.substring(0, paramsData.length - 1)
	}

	/* 请求前 */
	static requestBefore(config) {
		return config
	}

	/* 请求后 */
	static requestAfter(response) {
		return response
	}

	/*设置全局配置*/
	setConfig(func) {
		return func(this.config)
	}

	/**
	 * @Function
	 * @param {Object} options - 请求配置项
	 * @prop {String} options.url - 请求路径
	 * @prop {Object} options.data - 请求参数
	 * @prop {Object} [options.responseType = config.responseType] [text|arraybuffer] - 响应的数据类型
	 * @prop {Object} [options.dataType = config.dataType] - 如果设为 json，会尝试对返回的数据做一次 JSON.parse
	 * @prop {Object} [options.header = config.header] - 请求header
	 * @prop {Object} [options.method = config.method] - 请求方法
	 * @returns {Promise<unknown>}
	 */
	async request(options = {}) {
		options = {
			...options,
			...this.config,
			...this.requestBefore(options)
		}
		return new Promise((resolve, reject) => {
			let mergeUrl = apiurl(options.url);
			if (JSON.stringify(options.params) !== '{}') {
				let query = Request.addQueryString(options.params);
				mergeUrl += mergeUrl.indexOf('?') === -1 ? `?${query}` : `&${query}`
			}
			options.url = mergeUrl
			options.success = res => {
				resolve(this.requestAfter(res.data))
			}
			options.fail = err => {
				reject(this.requestAfter(err))
			}
			uni.request(options)
		})
	}

	get(url, options = {}) {
		return this.request({
			url,
			method: 'GET',
			...options
		})
	}

	post(url, data, options = {}) {
		return this.request({
			url,
			data,
			method: 'POST',
			...options
		})
	}

}