import { useStore } from "vuex";
// import { getLogin } from "@/utils/auth.js";
const dict = {
	'1':'成功',
	'2':'账户已存在',
	'3': '验证码错误',
	'4': '手机号码重复',
	'5':'邮箱重复',
	'6': '用户账户被锁',
	'7': '密码错误',
	'8':'账号不存在',
	'9': '监督员状态未审核',
	'10': '监督员状态未通过',
	'101':'登陆注册成功',
	'102':'监督员删除',
	'103':'验证码超时',
}
function fetch(method) {
  return function (url, data, otherParams = {}, headers = {}) {
    // if (method === 'POST' || method === 'PUT') {
    //   data = {
    //     param: data,
    //   };
    // }
    const sessionId = uni.getStorageSync("sessionId");
    //定时器
    return new Promise((resolve, reject) => {
      uni.request({
        url,
        method,
        data,
        header: {
          token: sessionId,
		  ...headers,
        },
        success(data) {
          if (data.statusCode === 200) {
            if (data.data.status === 1 || data.data.code === 200 || data.data.status ==='1') {
							if(data.data.message === '网址未重复') {
								wx.setStorageSync('isRepeat', 'false')
							}
              if (data.data.status === 1) {
				  resolve(data.data.data ? data.data.data : data.data);
			  } else {
				   resolve(data.data.data);
			  }
            } else if(
						data.data.status === 103 || data.data.status === 3 || 
						data.data.status === 2 ||data.data.status === 4 ||
						data.data.status === 5 ||data.data.status === 6 ||
						data.data.status === 7 || data.data.status === 8 ||
						data.data.status === 9 ||data.data.status === 10 ||
						data.data.status === 102 ) { 
							const status = data.data.status + '';
							const message = dict[status]
							uni.showToast({
							  // title: "系统异常，请稍后再试！",
							  title: `${message}`,
							  icon: "none",
							  duration: 3000,
							});
							reject(data.data.status);
						} else {
							if(data.data.message === '网址重复') {
								uni.showToast({
								  // title: "系统异常，请稍后再试！",
								  title: `网址重复，请换一个网址`,
								  icon: "none",
								  duration: 3000,
								});
								wx.setStorageSync('isRepeat', 'true')
							} else {
								uni.showToast({
								  // title: "系统异常，请稍后再试！",
								  title: `${data.data.message}`,
								  icon: "none",
								  duration: 2000,
								});
							}
             reject(data);
            }
          } else {
            reject(data);
          }
        },
        fail(err) {
					console.log('fail', err)
          reject(err);
        },
        ...otherParams,
      });
    });
  };
}

export default {
  get: fetch("GET"),
  post: fetch("POST"),
  put: fetch("PUT"),
  delete: fetch("DELETE"),
  connect: fetch("CONNECT"),
  head: fetch("HEAD"),
  options: fetch("OPTIONS"),
  trace: fetch("TRACE"),
};
