All files / src/core/dom useNativeAPI.ts

15.87% Statements 10/63
0% Branches 0/56
0% Functions 0/11
16.12% Lines 10/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175  1x 1x 1x 1x                                                                                                                                       1x                             1x                 1x             1x                                                                                             1x                       1x                        
import { miniprogramThis } from './global'
 
const fakeMediaResult = (request, response) => {
    if ('type' in response) {
        return response
    }
    if (request.mediaType.includes('video')) {
        return {
            tempFiles: [{
                tempFilePath: response.tempFilePath,
                size: response.size,
                duration: response.duration,
                height: response.height,
                width: response.width,
                thumbTempFilePath: response.tempFilePath,
                fileType: 'video',
            }],
            type: 'video',
        }
    }
    const { tempFilePaths = [], tempFiles = [] } = response
    return {
        tempFiles: tempFilePaths.map((tempFilePath, index) => ({
            tempFilePath: tempFiles[index].path || tempFilePath,
            size: tempFiles[index].size,
            fileType: 'image',
        })),
        type: 'image',
    }
}
 
/**
 * ## 拍摄或从手机相册中选择图片或视频。
 *
 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/video/wx.chooseMedia.html
 * @export
 * @param {*} options
 * @return {*} 
 */
export function chooseMedia(options) {
    const {
        count = 9,
        mediaType = ['image', 'video'],
        sourceType = ['album', 'camera'],
        maxDuration = 10,
        sizeType = ['original', 'compressed'],
        camera = 'back',
        // @deprecated
        compressed = true,
        ...resetCbs
    } = options
    const success = (res) => {
        if (resetCbs.success) {
            resetCbs.success(fakeMediaResult(options, res))
        }
    }
    if (typeof miniprogramThis.chooseMedia === 'function') {
        return miniprogramThis.chooseMedia({ ...options, success })
    }
    if (mediaType.includes('video')) {
        const videoOptions = {
            sourceType,
            compressed,
            maxDuration: options.maxDuration === undefined ? 60 : maxDuration,
            camera,
            ...resetCbs,
            success,
        }
        return miniprogramThis.chooseVideo(videoOptions)
    }
    const imageOptions = {
        count,
        sizeType,
        sourceType,
        ...resetCbs,
        success,
    }
    return miniprogramThis.chooseImage(imageOptions)
}
 
export function uploadFile(options) {
    const {
        url,
        filePath,
        name = 'file',
        header = {},
        formData = {},
        timeout = 20,
        enableProfile = true,
        enableHttp2 = false,
        ...resetCbs
    } = options
    return miniprogramThis.uploadFile({
        url,
        filePath,
        name,
        header,
        formData,
        timeout,
        enableProfile,
        enableHttp2,
        ...resetCbs,
    })
}

export function getSystemInfoSync(keys = ['window', 'device', 'appBase']) {
    return typeof miniprogramThis.getWindowInfo === 'function'
        ? keys.reduce((acc, key) => ({
            ...acc,
            ...miniprogramThis[`get${key.charAt(0).toUpperCase() + key.substring(1)}Info`](),
        }), {})
        : miniprogramThis.getSystemInfoSync()
}

export function vibrateShort(options) {
    if (getSystemInfoSync(['window', 'device']).platform === 'devtools') {
        return
    }
    return miniprogramThis.vibrateShort(options)
}

export function getMenuButtonBoundingClientRectSync() {
    let menuRect
    try {
        menuRect = miniprogramThis.getMenuButtonBoundingClientRect ? miniprogramThis.getMenuButtonBoundingClientRect() : null
        if (menuRect === null) {
            throw 'getMenuButtonBoundingClientRect error'
        }
        // 取值为 0 的情况  有可能 width 不为 0, top 为 0 的情况
        if (!menuRect.width || !menuRect.top || !menuRect.left || !menuRect.height) {
            throw 'getMenuButtonBoundingClientRect error'
        }
    } catch (e) {
        const windowInfo = getSystemInfoSync(['window', 'device'])
        const isIOS = !!(windowInfo.system.toLowerCase().search('ios') + 1)
        const height = 32 // 胶囊的高度
        let width = 88 // 胶囊的宽度
        let gap = 4 // 胶囊按钮上下间距 使导航内容居中
        if (windowInfo.platform === 'android') {
            gap = 8
            width = 96
        } else if (windowInfo.platform === 'devtools') {
            if (isIOS) {
                gap = 5.5 // 开发工具中 ios 手机
            } else {
                gap = 7.5 // 开发工具中 android 和其他手机
            }
        }
        // 开启 wifi 的情况下修复 statusBarHeight 值获取不到
        if (!windowInfo.statusBarHeight) {
            windowInfo.statusBarHeight = windowInfo.screenHeight - windowInfo.windowHeight - 20
        }
        // 获取不到胶囊信息就自定义重置一个
        menuRect = {
            bottom: windowInfo.statusBarHeight + gap + height,
            height,
            left: windowInfo.windowWidth - width - 10,
            right: windowInfo.windowWidth - 10,
            top: windowInfo.statusBarHeight + gap,
            width,
        }
    }
    return menuRect
}
 
export function nextTick(cb) {
    if (typeof miniprogramThis.nextTick === 'function') {
        return miniprogramThis.nextTick(cb)
    } else if (typeof Promise !== 'undefined') {
        return Promise.resolve().then(cb)
    } else {
        setTimeout(() => cb(), 0)
    }
}