import Vue from 'vue'
import App from './App'
import {appConfig} from './config'
import {weixin} from './utils/weixin.js'

Vue.config.productionTip = false
Vue.prototype.baseUrl = appConfig.baseUrl
Vue.prototype.openidInfoKey = appConfig.openidInfoKey

Vue.prototype.arrayCol = (arr, column) => {
	const res = []
	arr.forEach(i => {
		res.push(i[column])
	})
	return res
}
Vue.prototype.alert = msg => {
	uni.showToast({title: msg, duration: 2000, icon: 'none'})
}
Vue.prototype.checkLogin = () => {
	const phone_token = uni.getStorageSync('phone_token')
	if (!phone_token || !phone_token.phone) {
		uni.navigateTo({
			url: '/pages/login/index'
		})
		return false
	}
	return true
}
Vue.prototype.getToken = () => {
	const phone_token = uni.getStorageSync('phone_token')
	return phone_token ? phone_token.token : ''
}
Vue.prototype.sendReq = (url, params, cb, method) => {
	method = method || 'POST'
	weixin.getOpenidInfo(info => {
		params.openid = info.openid
		uni.request({
			url: appConfig.baseUrl + url,
			method: method,
			data: params,
			success: r => {
				if (r.data.status === 0) {
					uni.showToast({title: r.data.msg, duration: 2000, icon: 'none'})
				} else {
					cb(r.data.data)
				}
			}
		})
	})
}
Vue.prototype.reqByToken = (url, params, cb, method) => {
	method = method || 'POST'
	const phone_token = uni.getStorageSync('phone_token')
	params.token = phone_token.token
	uni.request({
		url: appConfig.baseUrl + url,
		method: method,
		data: params,
		success: r => {
			if (r.data.status === 1) {
				cb(r.data.data)
			} else if (r.data.status === 2) {
				uni.navigateTo({
					url: '/package/pages/login/index'
				})
				return false
			} else {
				uni.showToast({title: r.data.msg, duration: 2000, icon: 'none'})
			}
		}
	})
}

App.mpType = 'app'

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