-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
184 lines (178 loc) · 5.07 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//app.js
const util = require('/utils/util.js')
const http = require("/utils/http.js")
const common = require("/utils/common.js")
App({
globalData: {
app_id:'wx658570b5cb789b91',
secret:'5cad1c056ffa74452aa4c43e057d1fc0',
public_open_id: null,
user_info: null,
token: null,
mobile:null,
base: "https://api.qibu131.cn/",
share_img: ['/images/share_img/1.jpg','/images/share_img/2.jpg'],
share: {},
deviceInfo: {}
},
onLaunch: function (options) {
this.getDeviceInfo();
let token = wx.getStorageSync('token')
this.globalData.token = token;
let open_id = wx.getStorageSync('open_id');
if (open_id) {
this.getUserInfo();
}
this.setShare();
},
//获取设备初始化宽高
getDeviceInfo() {
var res = wx.getSystemInfoSync();
res.rpxR = 750 / res.windowWidth;
res.rpxWidth = res.rpxR * res.windowWidth;
res.rpxHeight = res.rpxR * res.windowHeight;
this.globalData.deviceInfo = res;
},
wxLogin(obj) {
let open_id = wx.getStorageSync('open_id');
if(open_id){
return new Promise((resolve)=>{
this.getUserInfo().then((res)=>{
resolve(res);
});
});
}else{
return new Promise((resolve) => {
common.wechatLogin().then((res) => {
obj.code = res.code;
return this.register(obj);
}).then((res)=>{
resolve(res);
});
});
}
},
getUserInfo() {
return new Promise((resolve) => {
let open_id = wx.getStorageSync('open_id')
let url = this.globalData.base + 'Public/getUserInfo'
let data = { open_id: open_id }
http.Post({ url: url, params: data }).then((res) => {
if (res.code == 'success' && res.data.token) {
wx.setStorageSync('user_info', res.data);
wx.setStorageSync('token', res.data.token);
this.globalData.token = res.data.token;
this.globalData.public_open_id = res.data.public_open_id;
this.globalData.user_info = res.data;
this.globalData.mobile = res.data.mobile;
resolve({ code: 'success' });
}else{
resolve({ code: 'fail' });
}
})
});
},
register(obj){
return new Promise((resolve) => {
let url = this.globalData.base + 'Public/login';
let login_data = { data: obj.encryptedData,iv:obj.iv,code:obj.code}
return http.Post({
url: url, params: login_data}).then((res) => {
if (res.code == 'success') {
wx.setStorageSync('open_id', res.data.open_id);
wx.setStorageSync('user_info', res.data);
wx.setStorageSync('token', res.data.token);
this.globalData.token = res.data.token;
this.globalData.public_open_id = res.data.public_open_id;
this.globalData.user_info = res.data;
this.globalData.mobile = res.data.mobile;
resolve({ code: 'success' });
} else {
resolve({ code: 'fail' });
}
})
});
},
modal(obj){
return new Promise((resolve, reject) => {
wx.showModal({
title: obj.title ? obj.title : '提示',
content: obj.content ? obj.content : '哈哈',
showCancel: true,
cancelText: obj.cancelText ? obj.cancelText : '取消',
cancelColor: "#000",
confirmText: obj.confirmText ? obj.confirmText : '确定',
confirmColor: "#0f0",
success: (res) => {
resolve(res);
},
fail: (res) => {
reject(res);
}
});
});
},
alert(obj){
wx.showToast({
title: obj.title ? obj.title : 'ok',
icon: 'none',
duration: obj.time ? obj.time : 1000,
mask: true
})
},
setTabBarMsg(){
let token = this.globalData.token;
if(token){
let url = this.globalData.base + 'SystemInfo/getSystemInfoCount'
let data = { token: token }
http.Get({ url: url, params: data }).then((res) => {
if (res.code == 'success') {
let numbers = res.data.count;
if (numbers>0){
wx.setTabBarBadge({
index: 2,
text: numbers
})
}else{
wx.removeTabBarBadge({
index: 2
})
}
}
})
}
},
redirectIndex() {
wx.switchTab({
url: '/pages/index/index',
})
},
redirectUser() {
wx.switchTab({
url: '/pages/my/my',
})
},
setShare(){
let share_img = this.globalData.share_img;
let num = new Date().getSeconds() % share_img.length;
let img = share_img[num];
this.globalData.share = {
title: '富余捐助贫困',
path: '/pages/index/index',
imageUrl: img
}
},
getToken(){
return new Promise((resolve) => {
resolve(this.globalData.token);
});
},
checkLogin(){
let token = this.globalData.token;
if (!token) {
wx.switchTab({
url: '/pages/my/my',
})
}
}
})