uni-app的一些方法

function request({
	url,
	data,
	method
}) {
	uni.showLoading({
		title: '加载中...'
	});
	return new Promise((resolve, reject) => {
		// alert('token',getLocalStorage('token'))
		// const token = uni.getStorageSync('token')
		const token = localStorage.getItem("token");
		uni.request({
			// url: hosts,
			// url: process.env.NODE_ENV == 'development' ? dev.BASE_API + url : dev.Serve_url + url,
			url: process.env.NODE_ENV == 'development' ? dev.BASE_API + url : window.location.origin + url,
			data: data,
			method: method,
			header: {
				// "content-type": "application/json",
				// "content-type":"application/x-www-form-urlencoded",
				// "Content-Type": (method == 'GET' ? "application/json" :
				// 	"application/x-www-form-urlencoded"),
				//"Content-Type": (method == 'POST' ? "application/json" :
					"application/x-www-form-urlencoded" || "application/json"),
                    
                     'Content-Type': 'application/json; charset=UTF-8',
				// "Authorization": token
				"token": token
			},
			success: (res) => {
				uni.hideLoading();
				if (res.data.success === false) {
					// if (res.data.status === 401) {
					// 	uni.setStorageSync({
					// 		key: "TOKEN",
					// 		data: ""
					// 	})
					// 	mpvue.navigateTo({
					// 		url: '/pages/login/main'
					// 	})
					// } else {
					// 	uni.showToast({
					// 		title: res.data.data,
					// 		icon: 'none',
					// 		duration: 2000
					// 	})
					// 	reject(res);
					// }
					if (res.data.code == 401) {
						uni.showToast({
							title: '签名认证失败!',
							icon: 'none',
							duration: 2000
						})
						setTimeout(() => {
							uni.navigateTo({
								url: `/pages/login/index`
							});
						}, 1000)
						removeLocalStorage('token')
					} else {
						uni.showToast({
							title: res.data.data,
							icon: 'none',
							duration: 2000
						})
						reject(res);
					}
				} else {
					resolve(res.data)
				}
			},
			fail: (res) => {
				uni.hideLoading();
				reject(res);
			},
			complete: (res) => {
				uni.hideLoading();
			}
		});
	});
}
//toast提示信息
function showToast(title, icon = 'none') {
	uni.showToast({
		title,
		icon
	});
}

// function get(url, data, resbook){
//     return request(url, 'GET', data, resbook);
// }

// function post(url, data, resbook){
//     return request(url, 'POST', data, resbook);
// }


//根据key获取storage
function getLocalStorage(key) {
	return uni.getStorageSync(key);
}

//根据key删除storage
function removeLocalStorage(key) {
	return uni.removeStorageSync(key);
}

//根据key value设置storage
function setLocalStorage(key, value) {
	uni.setStorageSync(key, value);
}
//获取url参数
function getUrlParams(key) {
	let url = window.location.href;
	const arr = url.split("?");
	const newArr = arr[1] ? arr[1].split("&") : [];
	for (var i = 0; i < newArr.length; i++) {
		let temp = newArr[i].split("=");
		if (temp[0] === key) {
			let search = temp[1];
			if (search.indexOf("#") > 0) {
				search = search.substring(0, search.indexOf("#"));
			}
			return search;
		}
	}
}

  

import { request } from "@/common/util.js"
 
export function expireList(data) {
  return request({
    url: '/merchant/room/reservation/expire',
    method: 'POST',
    data
  })
}
posted @ 2021-12-15 17:09  abcByme  阅读(132)  评论(0编辑  收藏  举报