<template>
  <view class="content">
    <button @click="godoBle">添加设备</button>

    <button @click="chooseImage">上传图片</button>
  </view>
</template>

<script>
import { common } from "../../proto/bundle.js";

//const CryptoJS = require("crypto-js");
const HmacSHA1 = require("crypto-js/hmac-sha1");
const base64 = require("crypto-js/enc-base64");
const utf8 = require("crypto-js/enc-utf8");

const policy =
  '{"expiration": "2120-01-01T12:00:00.000Z","conditions": [["content-length-range", 0, 104857600]]}';

const OSSAccessKeyId = "LTAICUWe9r9tRcPB";

const accessKeySecret = "gCZSuqdztp6X1n2xMaCqzhnBSWVJv6";

let wordArray = utf8.parse(policy);
const policyBase64 = base64.stringify(wordArray);

//获取加密参数
const sha1 = HmacSHA1(policyBase64, accessKeySecret);
console.log(`sha1 = ${sha1}`);
const signature = base64.stringify(sha1);
console.log(`sign = ${signature}`);



export default {
  data() {
    return {};
  },
  onLoad() {},
  methods: {
    //选择图片
    chooseImage() {
      let that = this;

      uni.chooseImage({
        count: 1,
        success(res) {
          console.log(res);
          let file = res.tempFiles[0];
          console.log(file);
		  that.wxUploadImage(file)
        },
      });
    },
    //上传图片
    wxUploadImage(file) {
      uni.uploadFile({
        url: "https://airsmart-photo1.oss-cn-shanghai.aliyuncs.com",
        filePath: file.path,
        name: 'file',
        formData: {
          'key': "wx/test3.png",
		  'success_action_status':200,///如果该域的值设置为200或者204，OSS返回一个空文档和相应的状态码。
      	  'OSSAccessKeyId': OSSAccessKeyId,
      	  'policy': policyBase64,
          'Signature':signature,
          'Content-Type':'image/jpeg',
        },
        success: (uploadFileRes) => {
			console.log("上传成功");
          console.log(uploadFileRes);
        },
		fail(err){
			console.log(err);
		}
      });
    },
    
    goCompent3() {
      uni.navigateTo({ url: "../test/component3/component3" });
    },
    test1() {
      let errinfo = common.ErrorInfo.create({
        errorCode: 0,
        errorMessage: Buffer.from("成功"),
      });

      //把ErrorInfo对象 编码成Uint8Array (browser) or Buffer (node)
      let errBuffer = common.ErrorInfo.encode(errinfo).finish();

      //把Uint8Array (browser) 或者 Buffer (node) 解码成ErrorInfo对象
      let message = common.ErrorInfo.decode(errBuffer);

      //转化为一个对象
      let obj = common.ErrorInfo.toObject(message, {
        enums: String, // enums as string names
        longs: String, // longs as strings (requires long.js)
        //bytes: String,
      });
      console.log(obj);
      let buf = Buffer.from(obj.errorMessage);
      console.log(`errorMessage = ${buf.toString()}`);
      // let buf = Buffer.from(obj.errorMessage)
      // console.log(buf);
      // console.log(buf.toString());
    },
    godoBle() {
      //添加设备
      // #ifdef MP-WEIXIN||APP-PLUS
      uni.navigateTo({
        url: "../ble/ScanBleDevice",
      });
      // #endif
      // #ifdef H5
      uni.navigateTo({
        url: "../ble/ConnectBleDevice",
      });
      // uni.showToast({
      // 	title:'H5页面不支持扫描设备',
      // 	icon:'none'
      // })
      // #endif
    },
  },
};
</script>

<style>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin: 20rpx;
}
</style>
