CocosCreator釋出華為快遊戲——上傳日誌檔案到伺服器、截圖儲存
阿新 • • 發佈:2021-01-21
技術標籤:CocosCreator
因為華為快遊戲好像是沒有像微信一樣提供uploadFile的介面(快應用有。。。),所以要自己重新寫一個xhr發的過程,用HelloWorld寫了個小demo做記錄阿巴阿巴;
另一個是截圖儲存的,因為canvas.toTempFilePath也不支援,so。。。
cc.Class({ extends: cc.Component, properties: { label: { default: null, type: cc.Label }, // defaults, set visually when attaching this script to the Canvas text: 'Hello, World!' }, // use this for initialization onLoad: function () { this.label.string = this.text; var url = "https://xyx.zonst.com/update/wx/user/txt"; function FormData(data, rs) { return ((function (data, rs) { let data_string = '\r\n' for (let [k, v] of Object.entries(data)) { if (k === "file") { //data_string += '------WebKitFormBoundary'+rs+'--' data_string += '------WebKitFormBoundary' + rs + '\r\nContent-Disposition: form-data; name="' + k + '"; filename="hwlog.txt"\r\nContent-Type: "text/plain"\r\n\r\n' + v + '\r\n'; } else if (({}).toString.call(v) === '[object Array]') { for (let el of v) { data_string += '------WebKitFormBoundary' + rs + '\r\nContent-Disposition: form-data; name="' + k + '"\r\n\r\n' + el + '\r\n'; } } else { data_string += '------WebKitFormBoundary' + rs + '\r\nContent-Disposition: form-data; name="' + k + '"\r\n\r\n' + v + '\r\n'; } } data_string += '------WebKitFormBoundary' + rs + '--' return data_string; })(data, rs)); } function randomString32(len) { const loopn = len || 32; const c = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; let res = ''; for (let i = 0; i < loopn; i++) { res += c.charAt(Math.floor(Math.random() * c.length)); } return res; } var logs = "測試123"; try { console.log("嘗試上報錯誤日誌1.3"); var rs = randomString32(16); var fd = FormData({ file: logs, }, rs, logs); var xhr = new XMLHttpRequest(); xhr.open("POST", url, true); xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=----WebKitFormBoundary' + rs); xhr.onreadystatechange = function () { console.log("狀態變更", xhr.readyState) if (xhr.readyState == 4) { console.log("狀態碼", xhr.status) if (xhr.status >= 200 && xhr.status < 400) { var response = xhr.responseText; console.log("httpPost[" + url + "] status:", xhr.status, "回覆訊息:", response); } else { console.warn("httpPost[" + url + "] error->status:" + xhr.status); } } }; //超時回撥記錄 xhr.ontimeout = function () { console.log("httpPost timeout url:" + url); } xhr.send(fd); console.log("嘗試上報錯誤日誌2"); } catch (e) { console.log(e) } console.log("嘗試上報錯誤日誌3"); }, // called every frame update: function (dt) { //this.label.string = dt; }, captureScreenToPhoto: function (callback) { var camera = cc.Camera.main; cc.log("獲取主相機") var texture = new cc.RenderTexture(); texture.initWithSize(Math.ceil(cc.winSize.width), Math.ceil(cc.winSize.height)); camera.targetTexture = texture; camera.render(); camera.targetTexture = null; cc.log("渲染到相機") var data = texture.readPixels(); cc.log("資料", data.length); var udata = new Uint8Array(data); try { hbs.saveImageTemp({ data: udata, width: Math.ceil(cc.winSize.width), height: Math.ceil(cc.winSize.height), fileType: "jpg", reverse: true, success: (res) => { cc.log("暫存圖片", JSON.stringify(res)); hbs.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: (res) => { cc.log("儲存圖片到本地", res.tempFilePath); }, fail: (err) => { cc.error("儲存失敗", JSON.stringify(err)); }, }); }, fail: (err) => { cc.error("失敗", JSON.stringify(err)); }, complete: (res) => { cc.log("執行完成") } }); } catch (err) { cc.error(JSON.stringify(err)); } cc.log("完成") }, savePhoto: function (callback) { cc.log("嘗試儲存圖片") if (!hbs.saveImageToPhotosAlbum) { cc.log("不能儲存圖片到本地") hbs.showModal({ title: '提示', content: '當前應用版本過低,無法使用該功能,請升級到最新版本後重試。' }) if (callback) { callback(); } return; } cc.log("嘗試儲存圖片1") var that = this; hbs.getSetting({ success(res) { cc.log(JSON.stringify(res)); if (res.authSetting["writePhotosAlbum"]) { that.captureScreenToPhoto(callback); } else { that.authorize( "writePhotosAlbum", "請前往設定介面,\n授權相簿訪問許可權", () => { that.captureScreenToPhoto(callback); } ); } } }) }, authorize: function (scope, message, callback, errorMessage) { cc.log("嘗試授權儲存圖片到相簿", scope); var params = {}; if (scope == "userInfo") { params = { appid: APP_ID, type: "token", scope: "userInfo", } } hbs.authorize({ scope: scope, params: params, success() { cc.log("申請許可權成功"); callback(); }, fail(res) { cc.log("======>hbs.authorize fail", res); hbs.getSetting({ success(res) { cc.log("hbs.getSetting-->", res); if (res.authSetting[scope] != null) { hbs.openSetting({ success(res) { if (!res.authSetting[scope]) { return; } if (msgbox && msgbox.isValid) { msgbox.node.destroy(); } callback() } }) } } }) }, }) }, });