import App from './App'

import Vue from 'vue'
import './uni.promisify.adaptor'

import uView from "uview-ui";
Vue.use(uView);
Vue.config.productionTip = false

import './css/style.css'

import mySwiper from './components/my-swiper/my-swiper.vue';
Vue.component('my-swiper', mySwiper);
import useEmpty from './components/use-empty/use-empty.vue';
Vue.component('use-empty', useEmpty);
import useLoadmore from './components/use-loadmore/use-loadmore.vue';
Vue.component('use-loadmore', useLoadmore);
import topSearch from './components/top-search/top-search.vue';
Vue.component('top-search', topSearch);
import myGap from './components/my-gap/my-gap.vue';
Vue.component('my-gap', myGap);
import myGoods from './components/my-goods/my-goods.vue';
Vue.component('my-goods', myGoods);
import sendType from './components/send-type/send-type.vue';
Vue.component('send-type', sendType);
import useListTitle from './components/use-list-title/use-list-title.vue';
Vue.component('use-list-title', useListTitle);

// 配置项
const config = process.env.ENV_PATH ? require(process.env.ENV_PATH) : require('./env/dev.js')
Vue.prototype.serverPath = config.serverPath // 请求路径
Vue.prototype.serverFilePath = config.serverFilePath // 文件路径
Vue.prototype.appName = config.name // 应用名称
Vue.prototype.appVersion = config.appVersion // 小程序版本
Vue.prototype.webSocketURL = config.webSocketURL // socket 链接
Vue.prototype.logo = config.logo //
Vue.prototype.appId = config.appId // 微信小程序 appid
Vue.prototype.$isIphoneX = false

Vue.prototype.$request = function(method, url, data, isJSON, hideLoading, showErrMsg) {
	return new Promise((resolve) => {
		if (!hideLoading) {
			uni.showLoading({
				title: '请稍后...'
			})
		}

		// 过滤 null，undefind，'' 的参数
		for (let key in data) {
			if (data[key] === null || data[key] === undefined || data[key] === '') {
				delete data[key]
			}
		}

		const token = uni.getStorageSync('token');
		const tenantId = uni.getStorageSync('tenantId');
		// if (tenantId) {
		// 	data.tenantId = tenantId;
		// }
		url = url.indexOf('http') === 0 ? url : `${Vue.prototype.serverPath}${url}`;
		uni.request({
			url,
			method,
			data,
			header: {
				'Content-Type': isJSON ? 'application/json' : 'application/x-www-form-urlencoded',
				'Authorization': 'Bearer ' + token,
				'clientId': 'e5cd7e4891bf95d1d19206ce24a7b32e',
				// 'tenantId': Vue.prototype.tenantId 或者 'tenantId': `${tenantId}`
				// application/x-www-form-urlencoded application/json
			},
			success: (res) => {
				uni.hideLoading();
				if (res.statusCode === 200) {
					resolve(res.data);
				} else {
					if (showErrMsg) {
						resolve({
							result: false,
							msg: res.data.data.msg
						})
					} else {
						resolve(false);
						uni.showToast({
							title: res.data.data.msg,
							icon: 'error',
							duration: 2000
						});
						// if (this.$refs && this.$refs.uNotify) {
						// 	this.$refs.uNotify.show({
						// 		type: 'error',
						// 		top: 0,
						// 		message: res.data.msg,
						// 		icon: 'error-circle'
						// 	})
						// }
					}
				}
			},
			fail: (err) => {
				uni.hideLoading()
				resolve(false)
			}
		})
	})
}

Vue.prototype.autoLogin = async () => { // 小程序自动登录
	return new Promise(resolve => {
		uni.login({
			success: async (res) => {
				const result = await Vue.prototype.$request('post', '/auth/miniLogin', {
					code: res.code,
					tenantId: '000000'
					//appid: Vue.prototype.appId
				})
				if (result) {
					uni.setStorageSync("token", result.data.access_token);
					uni.navigateTo({
						url: `/pages/diningList/diningList`
					})
					//await Vue.prototype.getUser()
					resolve(true)
				} else {
					uni.showModal({
						title: '温馨提示',
						content: '您尚未注册，是否前往注册？',
						showCancel: true,
						confirmText: '去注册',
						success: (response) => {
							if (response.confirm) {
								// 用户点击确定
								uni.navigateTo({
									url: '/pages/bindPhone/bindPhone'
								})
								resolve(true)
							} else if (response.cancel) {
								// 用户点击取消
								resolve(false)
							}
						},
						fail: () => {
							resolve(false)
						}
					})
				}
			},
			fail: (e) => {
				resolve(false)
			}
		})
	})
}

// 拼接文件完整地址
Vue.prototype.getFilePath = (path) => {
	return `${Vue.prototype.serverFilePath}${path}`
}

App.mpType = 'app'
const app = new Vue({
	...App
})
app.$mount()


// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
export function createApp() {
	const app = createSSRApp(App)
	return {
		app
	}
}
// #endif