import { getCurrentTime } from '@/apis/modules/activity.js'

const files = [
	'https://huorantech.cn/%E7%94%A8%E6%88%B7%E6%9C%8D%E5%8A%A1%E5%8D%8F%E8%AE%AE.docx', // 用户服务协议
	'https://huorantech.cn/%E7%94%A8%E6%88%B7%E9%9A%90%E7%A7%81%E5%8D%8F%E8%AE%AE.docx', // 用户隐私协议
	'https://huorantech.cn/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD%E5%AE%9E%E9%AA%8C%E5%AE%A4%E5%BB%BA%E8%AE%BE%E6%96%B9%E6%A1%88-2020.1.docx', // 人工智能
	'https://huorantech.cn/%E5%A4%A7%E6%95%B0%E6%8D%AE%E7%AE%A1%E7%90%86%E4%B8%8E%E5%BA%94%E7%94%A8%E4%B8%93%E4%B8%9A%E5%BB%BA%E8%AE%BE%E6%96%B9%E6%A1%88.docx', // 大数据
	'https://huorantech.cn/%E9%87%91%E8%9E%8D%E7%A7%91%E6%8A%80%E5%AE%9E%E9%AA%8C%E5%AE%A4%E5%BB%BA%E8%AE%BE%E6%96%B9%E6%A1%88V2.0.docx', // 金融科技
]
const docExts = ['doc', 'xls', 'ppt', 'pdf', 'docx', 'xlsx', 'pptx']
export default {
	// 路由跳转
	to(url) {
		uni.navigateTo({
			url
		})
	},
	// 成功提示
	sucMsg(title, duration = 1500) {
		uni.showToast({
			title,
			duration
		})
	},
	// 错误提示
	errMsg(title, duration = 1500) {
		uni.showToast({
			title,
			icon: 'none',
			duration
		})
	},
	// 如果非数字,则返回0
	handleNaN(val) {
		return isNaN(val) || val == 0 ? '' : val
	},
	// 小于10,返回0+传入值
	preZero(val) {
		return val < 10 ? '0' + val : val
	},
	//返回格式化时间,传参例如:"yyyy-MM-dd hh:mm:ss"
	formatDate(date, fmt = 'yyyy-MM-dd hh:mm:ss') {
		var date = date ? date : new Date()
		var o = { 
			"M+" : date.getMonth()+1,                 //月份 
			"d+" : date.getDate(),                    //日 
			"h+" : date.getHours(),                   //小时 
			"m+" : date.getMinutes(),                 //分 
			"s+" : date.getSeconds(),                 //秒 
			"q+" : Math.floor((date.getMonth()+3)/3), //季度 
			"S"  : date.getMilliseconds()             //毫秒 
		}
		if(/(y+)/.test(fmt)) {
	        fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length))
		}
		for(var k in o) {
			if(new RegExp("("+ k +")").test(fmt)){
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)))
			}
		}
		return fmt
	},
	// 获取商务经理id
	getBmId(val) {
		return uni.getStorageSync('team').partnerId
	},
	// 获取商务经理名称
	getBmName(val) {
		return uni.getStorageSync('team').partnerClassificationName
	},
	// 判断文件类型是否能够通过uni.openDocument打开(doc, xls, ppt, pdf, docx, xlsx, pptx)
	isDoc(ext) {
		return docExts.includes(ext)
	},
	// 预览文档
	openFile(id) {
		uni.showLoading({
			title: '加载中',
			mask: true
		})
		// 下载文件资源到本地
		uni.downloadFile({
			url: files[id],
			success: function(res) {
				uni.hideLoading();
				uni.showLoading({
					title: '正在打开',
					mask: true
				})
				// 新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。
				uni.openDocument({
					filePath: res.tempFilePath,
					fileType: 'docx', // 文件类型,指定文件类型打开文件,有效值 doc, xls, ppt, pdf, docx, xlsx, pptx 
					showMenu: true, // 允许出现分享功能
					success: res => {
						uni.hideLoading()
					},
					fail: openError => {
						uni.hideLoading()
					}
				})
			},
			fail: function(err) {
				uni.hideLoading()
			}
		})
	},
	// 产品管理的产品分类(classificationId)有6个,订单管理的产品分类(authority)有5个,后者是由前者决定的,但是id不一样。把产品管理的分类id传入这个函数,即可返回订单的分类id
	getOrderType(id) {
		if (id == 1 || id == 2) return 1
		if (id == 3) return 2
		if (id == 4) return 3
		if (id == 5) return 0
		if (id == 6) return 4
	},
	// 去掉html标签
	removeTag(str) {
		return str.replace(/(<[^>]+>)|((&nbsp;)+)/g , '')
	},
    // 传入文件名获取文件后缀
    getFileExt(fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    },
    // 获取当前时间
    getNow () {
      return new Promise(async (resolve, reject) => {
        const res = await getCurrentTime()
        resolve(new Date(res.currentTime))
      })
    },
}