/**
 * @Time 2019-12-12
 * @Author Zhengjingyi
 * @Action 全局公共方法
 */
import requestUrl from '@/services/config.env.js'

export function getComStorage(key) { // 获取本地Storage
    return new Promise(function(resolve, reject) {
        uni.getStorage({
            key: key,
            success: function(res) {
                resolve(res.data)
            },
            fail: function(res) {
                reject(false)
            }
        })
    })
}

export function setStorage(key, data) { // 存储本地Storage
    return new Promise(function(resolve, reject) {
        uni.setStorage({
            key: key,
            data: data,
            success: function(res) {}
        })
    })
}

export function getStorage() { // 获取本地userInfo
    return new Promise(function(resolve, reject) {
        uni.getStorage({
            key: 'userInfo',
            success: function(res) {
                resolve(res.data)
            },
            fail: function(res) {
                reject(false)
            }
        })
    })
}

export function getStorageAddressKey() { // 获取本地地址信息
    return new Promise(function(resolve, reject) {
        uni.getStorage({
            key: 'address_key',
            success: function(res) {
                resolve(res.data)
            }
        })
    })
}

export function navigateTo(url) {
    //路由跳转:页面之间路由跳转
    uni.navigateTo({
        url: url
    })
}

export function redirectTo(url) {
    //路由跳转:关闭当前页跳转到新页面	
    uni.redirectTo({
        url: url
    })
}

export function switchTabTo(url) {
    //路由跳转:底部 tab页
    uni.switchTab({
        url: url
    })
}

export function getWindowHeight() {
    // 获取窗口高度
    const { windowHeight, pixelRatio } = wx.getSystemInfoSync()
    return windowHeight
}

export function adaptRichTextImg(res) {
    /**
     *@富文本实现图片自适应
     *@style再添加自适应样式
     */
    const html = res.replace(/<img[^>]*>/gi, function(match, capture) {
        let match1 = match.replace(/<img*/gi,
                '<img style="width:100% !important;height:auto !important;float:left !important;"'),
            results = match1.replace(/style=/gi, 'style="width:100%;height:auto;float:left;"')
        return results
    })
    return html
}

export function formatDate() {
    //获取当前时间
    let date = new Date()
    let y = date.getFullYear()
    let MM = date.getMonth() + 1
    MM = MM < 10 ? ('0' + MM) : MM
    let d = date.getDate()
    d = d < 10 ? ('0' + d) : d
    let h = date.getHours()
    h = h < 10 ? ('0' + h) : h
    let m = date.getMinutes()
    m = m < 10 ? ('0' + m) : m
    let s = date.getSeconds()
    s = s < 10 ? ('0' + s) : s
    return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
}

export function isNumber(value) { //验证是否为数字
    var patrn = /^(-)?\d+(\.\d+)?$/
    if (patrn.exec(value) == null || value == '') {
        return false
    } else {
        return true
    }
}

export function FormatMoney(num) {
    // 金额千分位
    return num.toString().replace(/\d+/, function(n) { // 先提取整数部分
        return n.replace(/(\d)(?=(\d{3})+$)/g, function($1) { // 对整数部分添加分隔符
            return $1 + ','
        })
    })
}

const install = Vue => {
    console.log('初始化挂载($api)工具方法 utilsTools.js')
    Vue.prototype.$api = {
        FormatMoney,
        formatDate,
        navigateTo,
        redirectTo,
        switchTabTo,
        isNumber,
        setStorage,
        getStorage,
        getComStorage,
        getWindowHeight,
        adaptRichTextImg,
        getStorageAddressKey,
    }
}

export default install
