-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.js
109 lines (108 loc) · 3.02 KB
/
app.js
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
import { doRequest, sidPrefix } from "./config";
App({
onShow: function () {},
onLaunch: function () {
var that = this;
wx.getStorage({
key: "selectedChatType",
success: (res) => {
that.globalData.chatType = res.data;
},
});
if (that.globalData.sid) {
that.upload_cache_conversation(that.globalData.sid);
doRequest("/channel", "GET", { sid: that.globalData.sid }).then((res) => {
that.globalData.channel = res.data.data;
});
} else {
wx.getStorage({
key: "sid1",
success: (res) => {
that.upload_cache_conversation(res.data);
doRequest("/channel", "GET", { sid: res.data }).then((res) => {
that.globalData.channel = res.data.data;
});
},
});
}
},
onHide: function () {
// this.upload_conversation()
},
globalData: {
chatType: "bing",
channel: [{ name: "New Bing", value: "bing" }],
},
upload_cache_conversation: function (sid) {
var that = this;
wx.getStorage({
key: "chatList",
success: function (res) {
var data = res.data || [];
data = data.slice(-5);
doRequest("/save", "POST", {
sid: sidPrefix + sid,
conversations: data,
}).then((res) => {
that.globalData["channel"] = res.data["channel"];
console.log("upload " + data.length + " conversations success!");
});
},
});
},
upload_conversation: function (conversations = []) {
var that = this;
if (conversations.length == 0) {
that.getSid((sid) => {
that.upload_cache_conversation(sid);
});
} else {
that.getSid((sid) => {
doRequest("/save", "POST", {
sid: sidPrefix + sid,
conversations: conversations,
}).then((res) => {
that.globalData["channel"] = res.data["channel"];
console.log(
"upload " + conversations.length + " conversations success!"
);
});
});
}
},
getSid: function (callback) {
var that = this;
if (!this.globalData.sid) {
var sid = wx.getStorageSync("sid1");
if (!sid) {
wx.login({
success: (res) => {
doRequest("/openid", "GET", {
code: res.code,
})
.then((data) => {
if (data.statusCode != 200) {
callback("anonymous");
return;
}
that.globalData.sid = data.data.data.openid;
that.globalData.channel = data.data.data.channel;
wx.setStorageSync("sid1", that.globalData.sid);
callback(data.data.data.openid);
})
.catch((err) => {
console.log(err);
callback("");
});
},
});
} else {
this.globalData.sid = sid;
wx.setStorageSync("sid1", this.globalData.sid);
callback(sid);
}
} else {
callback(this.globalData.sid);
}
},
});