const OSS = require('ali-oss')
// 创建oss实例用于向阿里云发送请求
// export const client = new OSS({
//   region: 'oss-cn-beijing', 
//   accessKeyId: 'LTAI4GJ5dbnpeCoHUn3Lvu8Y',
//   accessKeySecret: 'MuBbYB4GgpSSb8T3lhS4at9OqLMKLq',
//   bucket: 'narcosis'
// })
export function client() {
	var client = new OSS({
		region: 'oss-cn-beijing',
		accessKeyId: 'LTAI4GJ5dbnpeCoHUn3Lvu8Y',
		accessKeySecret: 'MuBbYB4GgpSSb8T3lhS4at9OqLMKLq',
		bucket: 'narcosis'
	})
	return client
}
// 存储目录
export const catelog = 'after-sale-img/'
// 生成uuid 用于生成图片名
export function uuid() {
	var s = []
	var hexDigits = '0123456789abcdef'
	for (var i = 0; i < 36; i++) {
		s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
	}
	s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
	s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
	s[8] = s[13] = s[18] = s[23] = '-'

	var uuid = s.join('')
	return uuid
}

export function uploadoss() {
	uni.chooseImage({
		count: 1, //默认9
		sizeType: ['compressed'],
		sourceType: ['album'],
		success: res => {
			let tiemr = new Date();
			let address = tiemr.getFullYear() + '' + (tiemr.getMonth() + 1) + '' + tiemr.getDate();
			address = 'zskp/userHead/' + address + '/';
			var imageSrc = res.tempFilePaths[0];
			let str = res.tempFilePaths[0].substr(res.tempFilePaths[0].lastIndexOf('.'));
			let nameStr = address + tiemr.getTime() + str;
			let ossUrl = 'http://oss-cn-beijing.aliyuncs.com';
			uni.uploadFile({
				url: ossUrl, //输入你的bucketname.endpoint
				filePath: imageSrc,
				fileType: 'image',
				name: 'file',
				formData: {
					name: nameStr,
					key: nameStr,
					// policy: 'MuBbYB4GgpSSb8T3lhS4at9OqLMKLq', // 输入你获取的的policy
					OSSAccessKeyId: 'LTAI4GJ5dbnpeCoHUn3Lvu8Y', // 输入你的AccessKeyId
					success_action_status: '200', // 让服务端返回200,不然，默认会返回204
					// signature: 'MuBbYB4GgpSSb8T3lhS4at9OqLMKLq' // 输入你获取的的signature
				},
				success: res => {
					if (res.statusCode == '200') {
						consonle.log(ossUrl + nameStr);
						alert(ossUrl + nameStr)
					} else {
						consonle.log(res);
						alert(res)
					}
				}
			});
		}
	});
}
