/**
 *@des 公共接口
 *@author zhengjinyi
 *@date 2020/03/19 14:56:57
 *@param registerByPass
 */
import requestUrl from '@/services/config.env.js'
import request from '@/common/config/caimeiApi.js'
import $reg from '@/common/config/common.js'

/**
 * @新分类下的商品列表
 * @param:tinyTypeID 三级分类ID
 * @param:pageNum	页码
 * @param:pageSize	每页显示条数
 */
export function searchQueryTinyType(url, params) {
    return new Promise(function(resolve, reject) {
        request.lodingGet(url, params, res => {
            if (res.code == 0) {
                resolve(res)
            } else {
                reject(res)
            }
        })
    })
}
/**
 *上传图片
 */
export function uploadFileImage() {
    return new Promise(function(resolve, reject) {
        uni.chooseImage({
            count: 1, //默认1
            sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album'], //从相册选择
            success: (res) => {
                const tempFilePaths = res.tempFilePaths
                wx.showLoading({ title: '上传中~' })
                const uploadTask = uni.uploadFile({
                    url: requestUrl + '/tools/image/upload/multi',
                    filePath: tempFilePaths[0],
                    name: 'file',
                    header: {
                        'Content-Type': 'multipart/form-data',
                    },
                    formData: {
                        'user': 'test'
                    },
                    success: function(res) {
                        wx.hideLoading()
                        resolve(res)
                    },
                    error: function(e) {
                        wx.hideLoading()
                        reject(res)
                    }
                })
            }
        })
    })
}
/**
 *上传文件
 * 限制pdf,doc,docx
 */
export function uploadFilePdfDocDocx() {
    return new Promise(function(resolve, reject) {
        wx.chooseMessageFile({
            count: 1,
            type: 'file',
            success(res) {
                // tempFilePath可以作为img标签的src属性显示图片
                const tempFilePaths = res.tempFiles
                const size = tempFilePaths[0].size //获取图片的大小，单位B
                const filename = res.tempFiles[0].name
                const newfilename = filename + ''
                const url = requestUrl + '/tools/file/upload/oss'
                uni.setStorageSync('fileName', filename)
                console.log('filename', filename)
                //截取
                let fixFile = newfilename.substr(newfilename.lastIndexOf('.'))
                console.log(fixFile)
                //统一转成小写  
                let lowFixFile = fixFile.toLowerCase()
                if (lowFixFile != '.pdf' && lowFixFile != '.doc' && lowFixFile != '.docx') { //限制了文件类型
                    uni.showToast({
                        title: '文件格式不正确!',
                        icon: 'none',
                        mask: true,
                        duration: 3000
                    })
                    return
                }
                if (size > 51200000) { //限制了文件的大小50M
                    uni.showToast({
                        title: '文件大小不能超过50M',
                        icon: 'none',
                        mask: true,
                        duration: 3000
                    })
                    return
                }
                wx.showLoading({ title: '上传中~' })
                wx.uploadFile({
                    url: url,
                    filePath: tempFilePaths[0].path,
                    name: 'file',
                    header: {
                        'Content-Type': 'multipart/form-data',
                    },
                    formData: {
                        'user': 'test'
                    },
                    success: function(res) {
                        wx.hideLoading()
                        resolve(res)
                    },
                    error: function(err) {
                        wx.hideLoading()
                        reject(err)
                    }
                })
            }
        })
    })
}

/**
 *上传文件,仅限制PDF文件格式
 */
export function uploadFilePdf() {
    return new Promise(function(resolve, reject) {
        wx.chooseMessageFile({
            count: 1,
            type: 'file',
            success(res) {
                // tempFilePath可以作为img标签的src属性显示图片
                const tempFilePaths = res.tempFiles
                const size = tempFilePaths[0].size //获取图片的大小，单位B
                const filename = res.tempFiles[0].name
                const newfilename = filename + ''
                const url = requestUrl + '/tools/file/upload/oss'
                uni.setStorageSync('fileName', filename)
                console.log('filename', filename)
                //截取
                let fixFile = newfilename.substr(newfilename.lastIndexOf('.'))
                console.log(fixFile)
                //统一转成小写  
                let lowFixFile = fixFile.toLowerCase()
                if (lowFixFile != '.pdf') { //限制了文件类型
                    uni.showToast({
                        title: '文件必须为".pdf"格式!',
                        icon: 'none',
                        mask: true,
                        duration: 3000
                    })
                    return
                }
                if (size > 20480000) { //限制了文件的大小20MB
                    uni.showToast({
                        title: '文件大小不能超过20M',
                        icon: 'none',
                        mask: true,
                        duration: 3000
                    })
                    return
                }
                wx.showLoading({ title: '上传中~' })
                wx.uploadFile({
                    url: url,
                    filePath: tempFilePaths[0].path,
                    name: 'file',
                    header: {
                        'Content-Type': 'multipart/form-data',
                    },
                    formData: {
                        'user': 'test'
                    },
                    success: function(res) {
                        wx.hideLoading()
                        resolve(res)
                    },
                    error: function(err) {
                        wx.hideLoading()
                        reject(err)
                    }
                })
            }
        })
    })
}
/**
 * 协销机构资料上传文件
 * 限制pdf,doc,docx
 */
export function uploadFilePdfDocDocxXlsx() {
    debugger
    return new Promise(function(resolve, reject) {
        wx.chooseMessageFile({
            count: 1,
            type: 'file',
            success(res) {
                // tempFilePath可以作为img标签的src属性显示图片
                const tempFilePaths = res.tempFiles
                const size = tempFilePaths[0].size //获取图片的大小，单位B
                const filename = res.tempFiles[0].name
                const newfilename = filename + ''
                const url = requestUrl + '/tools/file/upload/oss'
                uni.setStorageSync('fileName', filename)
                console.log('filename', filename)
                //截取
                let fixFile = newfilename.substr(newfilename.lastIndexOf('.'))
                console.log(fixFile)
                //统一转成小写  
                let lowFixFile = fixFile.toLowerCase()
                if (
                    lowFixFile != '.pdf' &&
					lowFixFile != '.doc' &&
					lowFixFile != '.docx' &&
					lowFixFile != '.ppt' &&
					lowFixFile != '.pptx' &&
					lowFixFile != '.xlsx' &&
					lowFixFile != '.xls'
                ) { //限制了文件类型
                    uni.showToast({
                        title: '文件格式不正确!',
                        icon: 'none',
                        mask: true,
                        duration: 3000
                    })
                    return
                }
                if (size > 51200000) { //限制了文件的大小50M
                    uni.showToast({
                        title: '文件大小不能超过50M',
                        icon: 'none',
                        mask: true,
                        duration: 3000
                    })
                    return
                }
                wx.showLoading({ title: '上传中~' })
                wx.uploadFile({
                    url: url,
                    filePath: tempFilePaths[0].path,
                    name: 'file',
                    header: {
                        'Content-Type': 'multipart/form-data',
                    },
                    formData: {
                        'user': 'test'
                    },
                    success: function(res) {
                        wx.hideLoading()
                        resolve(res)
                    },
                    error: function(err) {
                        wx.hideLoading()
                        reject(err)
                    }
                })
            }
        })
    })
}
