// 存放接口处
import http from './request.js';

// #ifdef MP-WEIXIN
import {
	TextDecoder
} from '@/utils/text-encoding-0.6.3/index.js'
// #endif


// 获取聊天记录
export const getMessageList = (data) => http.request({
	method: "GET",
	url: '/chat/history'
});

/**
 * 微信code登录
 * @param{platform:string ,code: 微信code} data
 */
export const thirdLogin = (data) => http.request({
	method: "POST",
	url: '/user/third',
	data
})

// 发送消息
// export const sendMessageApi = (data) => http.request({
// 	method: "POST",
// 	url: '/chat/random',
// 	data
// })


// 发送消息
// export const sendMessageApi = (data) => {
// const requestTask = uni.request({
// 	url: "https://jx.xiaolinghou.com/api/chat/random",
// 	timeout: 150000,
// 	responseType: "text",
// 	method: "POST",
// 	header: {
// 		token: 'ccef1fb8-c85c-4144-9e69-00a4f279f9ab'
// 	},
// 	enableChunked: true, //配置这里
// 	data,
// 	success: response => {
// 		console.log(response)
// 	},
// 	fail: error => {}
// })
// requestTask.onChunkReceived(function(res) {
// 	const uint8Array = new Uint8Array(res.data);
// 	// 尝试使用多个字符编码进行解码
// 	const encodings = ['utf-8', 'gbk', 'big5']; // 按照你的需求添加更多的编码方式
// 	let text = '';
// 	for (const encoding of encodings) {
// 		try {
// 			const textDecoder = new TextDecoder(encoding);
// 			text = textDecoder.decode(uint8Array);
// 			break;
// 		} catch (error) {
// 			console.log(`Decoding failed with ${encoding}: ${error}`);
// 		}
// 	}

// 	console.log(text);
// })
// }

// 发送消息
export const sendMessageApi = async (data, that) => {
	let token = uni.getStorageSync('token')
	if (!token) {
		uni.navigateTo({
			url: '/pages/login/login'
		})
		return
	}
	// #ifdef H5
	const response = await fetch('https://jx.xiaolinghou.com/api/chat/random', {
		method: "POST",
		headers: {
			"Content-Type": "application/json",
			token,
		},
		body: JSON.stringify(data)
	}, );
	const reader = response.body.getReader();
	const decoder = new TextDecoder("utf-8");

	let read = () => {
		return reader.read().then(({
			done,
			value
		}) => {
			if (done) {
				that.isSnedMsg = false
				return;
			}
			// 将已下载部分的内容展示出来
			let text = decoder.decode(value, {
				stream: true
			});
			const match = text.match(/"(.*?)"/);
			if (match) {
				text = match[1];
			}
			console.log(text);
			that.messageList[that.messageList.length - 1].message += text
			// 继续读取下一部分
			return read();
		});
	}

	read()
	// #endif



	// #ifdef MP-WEIXIN
	const requestTask = uni.request({
		url: "https://jx.xiaolinghou.com/api/chat/random",
		timeout: 150000,
		responseType: "text",
		method: "POST",
		header: {
			token
		},
		enableChunked: true, //配置这里
		data,
		success: response => {
			console.log(response)
		},
		fail: error => {}
	})
	const decoder = new TextDecoder("utf8");
	requestTask.onChunkReceived(function(response) {
		const uint8Array = new Uint8Array(response.data);
		let text = String.fromCharCode.apply(null, uint8Array);
		text = decodeURIComponent(escape(text));
		const match = text.match(/"(.*?)"/);
		if (match) {
			text = match[1]
		}
		console.log(text);
		that.messageList[that.messageList.length - 1].message += text
	})
	// #endif
}




// 获取问题列表
export const getQuestionApi = () => http.request({
	method: "GET",
	url: '/index/article'
})

// 获取价格区间
export const getPhotoPriceApi = () => http.request({
	method: "GET",
	url: '/index/photo'
})

// 支付
export const toPay = (data) => http.request({
	method: "POST",
	url: '/order/pay',
	data
})
// 快递方式列表
export const getPostList = () => http.request({
	method: "GET",
	url: '/order/express'
})


// 代收信件信息
export const getAgentBusinessInfoApi = () => http.request({
	method: "GET",
	url: '/user/write_back'
})


// 保存代收信息
export const setAgentBusinessInfoApi = (data) => http.request({
	method: "POST",
	url: '/user/set_back',
	data
})

// 查询订单详情

export const getOrderInfo = (params) => http.request({
	method: "GET",
	url: '/order/info',
	params
})