/*
 * @Author: zhuangzhou zhuangzhou@btioe.com
 * @Date: 2022-05-04 17:53:53
 * @LastEditors: zhuangzhou zhuangzhou@btioe.com
 * @LastEditTime: 2022-05-04 17:55:27
 * @FilePath: /mlmt-mini/src/utils/common.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import store from '@/store'
import router from './router'

export default {
    isString(str) {
        return typeof str === 'string'
    },
    isArray(array) {
        return Object.prototype.toString.call(array) === '[object Array]'
    },
    // 深拷贝
    deepCopy(source, target) {
        target = target || {}
        for (var i in source) {
            if (typeof source[i] === 'object') {
                target[i] = source[i].constructor === Array ? [] : {}
                util.deepCopy(source[i], target[i])
            } else {
                target[i] = source[i]
            }
        }
        return target
    },
    // 生成uuid
    guid() {
        function S4() {
            return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
        }
        return S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4()
    },
    isLogin() {
        return !!store.state.token
    },
    checkLogin() {
        if (!store.state.token) {
            uni.navigateTo({
                url: '/pages/login/index'
            })
            return false
        }
        return true
    },
    currentPage() {
        const pages = getCurrentPages()
        return pages[pages.length - 1]
    },
    copy(text) {
        uni.setClipboardData({
            data: text,
            success: function () {
                uni.showToast({
                    title: '复制成功'
                })
            },
            fail: function (err) {
                console.log(err)
                uni.showToast({
                    title: '复制失败'
                })
            }
        })
    },
    scrollTop() {
        uni.pageScrollTo({ scrollTop: 0, duration: 200 })
    },
    showNext(item) {
        console.log(item)
        if (!item.path || !item.path.schema || item.path.schema === 'none') return
        if (item.path.schema === 'h5' && item.path.url) {
            router.web(item.path.url)
            return
        }
        if (item.path.schema === 'native' && item.path.url == 'Page') {
            if (item.path.params.url.startsWith('mini:')) {
                let array = item.path.params.url.split(':')
                wx.navigateToMiniProgram({
                    appId: array[1],
                    path: array[2]
                })
                return
            }
            this.showNextByParam(item.path.params.url)
            // router.pushCheck(item.path.params.url)
            return
        }
        if (item.path.schema === 'method' && item.path.url == 'method') {
            this[item.path.params.method]()
            return
        }
    },
    showNextByParam(url) {
        const path = url.split('?')[0]
        const params = this.queryStringToObject(url.split('?')[1])
        console.log(path)
        console.log(params)
        if (path) {
            router.pushCheck(path, params)
        } else {
            console.log('未找到参数')
        }
    },
    queryStringToObject(queryString) {
      if(!queryString){
        return {}
      }
        const params = {}
        const pairs = queryString.split('&')
        for (let i = 0; i < pairs.length; i++) {
            const pair = pairs[i].split('=')
            params[pair[0]] = decodeURIComponent(pair[1])
        }
        return params
    },
    toAward() {
        router.switchTab('award')
    },
    getQueryObj(url) {
        if (url.indexOf('?') != -1) {
            let obj = {}
            let arr = url.slice(url.indexOf('?') + 1).split('&')
            arr.forEach((item) => {
                let param = item.split('=')
                obj[param[0]] = param[1]
            })
            return obj
        } else {
            return null
        }
    },
    compareVersion(v1, v2) {
        v1 = v1.split('.')
        v2 = v2.split('.')
        const len = Math.max(v1.length, v2.length)

        while (v1.length < len) {
            v1.push('0')
        }
        while (v2.length < len) {
            v2.push('0')
        }

        for (let i = 0; i < len; i++) {
            const num1 = parseInt(v1[i])
            const num2 = parseInt(v2[i])

            if (num1 > num2) {
                return 1
            } else if (num1 < num2) {
                return -1
            }
        }

        return 0
    },
    showKefu(title) {
        wx.openCustomerServiceChat({
            extInfo: { url: 'https://work.weixin.qq.com/kfid/kfca8c314f2305c7903' },
            corpId: 'ww5fb8760e92b10064',
            showMessageCard: !!title,
            sendMessageTitle: title,
            success(res) {}
        })
    },

    shuffle(arr) {
        var length = arr.length,
            randomIndex,
            temp
        while (length) {
            randomIndex = Math.floor(Math.random() * length--)
            temp = arr[randomIndex]
            arr[randomIndex] = arr[length]
            arr[length] = temp
        }
        return arr
    },
    ignoreRatio0(str) {
        if (typeof str === 'number') {
            str += ''
        }
        if (str === '0') return str
        if (str.indexOf('.') < 0) return str
        str += ''
        let temp = str.replace(/(0+)$/g, '')
        if (temp[temp.length - 1] === '.') {
            return temp.substr(0, temp.length - 1)
        }
        return temp
    }
}
