import { url }  from './config'
import {signGen} from "@/utils"

function _request(options, showErrorModal = true) {
	return new Promise((resolve, reject)=> {
		uni.request(options).then(({data}) => {
			if(data.status === "OK"){
				resolve(data)
			}else{
				reject(data);
			}
		}).catch((err) => {
			reject(err);
		})
	})
}

export const apiService = {
	query(path, query, sign = false) {
		return _request({
			url: url + path,
			data: sign ? signGen(query) : query
		})
	},
	get(path, query, sign = true) {
		return _request({
			url: url + path,
			data: sign ? signGen(query) : query
		})
	},
	post(path, params, sign = true, showErrorModal = true) {
		return _request({
			url: url + path,
			data: sign ? signGen(params) : params,
			method: 'POST'
		}, showErrorModal)
	}
}