storge.js
5.1 KB
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
const UPLOAD_STATUS = {
// 上传状态
success: 1,
fail: 2,
};
let wv, getResult;
import { uploadFile, getOssToken } from "@/utils/ossUploadFile/uploadFile.js";
import config from "@/config.js";
export default {
onLoad() {},
mounted() {
// console.log(JSON.stringify(this))
var currentWebview = this.$parent.$scope.$getAppWebview(); //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效
setTimeout(function () {
wv = currentWebview.children()[0];
wv.setStyle({ top: "100%", height: 300 });
}, 1000); //如果是页面初始化调用时,需要延时一下
// console.log(88);
// // this.checkRecord();
// setTimeout(() => {
// this.checkRecord();
// }, 1000 * 6);
},
methods: {
handlerMessage(result) {
console.log(result);
getResult = result;
},
checkRecord() {
uni.$isNetWork().then(async (res) => {
let recordList = JSON.parse(uni.$storage.get("recordList") || "[]");
for (let i = 0; i < recordList.length; i++) {
try {
await this.handleRerord(recordList[i]);
} catch (error) {
continue;
}
}
});
},
getFilePath() {
return new Promise((resolve) => {
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function (fs) {
resolve(fs.root.fullPath);
});
});
},
setTimeoutResult() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(getResult);
}, 1000);
});
},
/**上传记录*/
async handleRerord(val) {
let recordUrlList = val.recordUrlList;
// 上传失败的记录
for (let i of recordUrlList) {
console.log(i.status == UPLOAD_STATUS.fail);
if (i.status == UPLOAD_STATUS.fail) {
const filePath = i.file.split("/").pop();
const { lsaccessKeySecret, securityToken, lsaccessKeyId } =
await getOssToken();
console.log(lsaccessKeySecret, securityToken, lsaccessKeyId);
const rootPath = await this.getFilePath();
wv.evalJS(
`msgFromUniapp('${lsaccessKeyId}','${lsaccessKeySecret}','${securityToken}','${filePath}','file://${rootPath}uniapp_save/')`
);
for (let k = 0; k < 100; k++) {
const result = await this.setTimeoutResult();
if (result) {
if (result.detail.data[0].code == 0) {
uni.removeSavedFile({
filePath: i.file,
complete: function (res) {
console.log(res);
},
});
i.status = UPLOAD_STATUS.success;
i.file = config.OSS_DOMAIN + "/" + config.FOLDER + filePath;
} else {
// this.sendSentry(result.detail.data[0].error);
throw "上传失败";
}
getResult = "";
break;
}
}
// const result = await this.checkFileUpload(filePath);
// if (!(result instanceof Error)) {
// i.status = UPLOAD_STATUS.success;
// i.file = result;
// }
}
}
//目前处于保存中
if (uni.$lock) {
return;
}
// 判断是否全部上传成功
if (recordUrlList.every((i) => i.status == UPLOAD_STATUS.success)) {
val.recordUrlList = val.recordUrlList.map((i) => i.file);
try {
await uni.$api.commonApi.saveRecCustomer(val).then((res) => {
console.log(res);
});
let recordList = JSON.parse(uni.$storage.get("recordList") || "[]");
let idx = recordList.findIndex(
(item) => item.receptionStartTime == val.receptionStartTime
);
recordList.splice(idx, 1);
uni.$storage.set("recordList", JSON.stringify(recordList));
} catch (err) {
Promise.reject(error);
}
}
},
// sendSentry(msg) {
// const { customerName, phoneNumber, carModel, staffName } =
// this.$store.state.user.customerInfo;
// const { name } = uni.$storage.get("userInfo");
// this.$sendSentry(
// `上传失败:手机号-${phoneNumber} 姓名-${customerName} 销售-${staffName} 车型-${carModel} 员工-${name},失败原因-${msg}`
// );
// },
async checkFileUpload(tempFilePath) {
console.log(tempFilePath);
let attempt = 0;
let errorMsg = "";
while (attempt < 2) {
try {
const result = await uploadFile(tempFilePath);
// 上传成功
return result;
} catch (error) {
errorMsg = error;
attempt++;
// 等待一段时间后重试
await new Promise((resolve) => setTimeout(resolve, 3000)); // 3秒后重试
}
}
// 达到最大尝试次数后,如果上传仍然失败,可以进行相应的处理
console.error("上传失败,达到最大尝试次数");
// 返回一个错误或者进行其他操作
return new Error(errorMsg);
},
},
};