mobile.js
8.32 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
var baseURL = 'http://dsc-b.vchangyi.com/party/phpapi';
// var baseURL = 'https://yq.vchangyi.com/party/phpapi';
// var baseURL = 'http://t.dev/party/phpapi'
// 页面数据绑定操作
var action = {
// 是否开始签到
isRun : 0,
init : function() {
var WECHAT_JS_SIGN_URL = '/api.WechatJsSign.php';
var USER_DETAIL_URL = '/api.UserDetail.php';
// 获取微信jsapi签名
baseRquest.get(WECHAT_JS_SIGN_URL, {'url' : location.href}, action.sign);
// 用户信息
baseRquest.get(USER_DETAIL_URL, {}, action.userDetail);
},
// 配置微信jsapi config,回调触发
sign : function(data) {
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: data.appId, // 必填,公众号的唯一标识
timestamp: data.timestamp, // 必填,生成签名的时间戳
nonceStr: data.nonceStr, // 必填,生成签名的随机串
signature: data.signature,// 必填,签名
jsApiList: ['chooseImage','uploadImage','getLocalImgData','downloadImage','getLocation'] // 必填,需要使用的JS接口列表
});
},
// 回调触发,获取用户信息
userDetail : function(data) {
// openid不存在
if (!data) {
alert('数据异常,不要搞事情');
return;
}
// 如果数据中有isLogin,有登录流程
if (data.isLogin !== undefined) {
location.href = baseURL + '/api.WechatAuth.php';
}
action.isRun = 1;
// 未提交过资料,展示开场
if (data.realname === undefined || data.realname == '') {
$('#show').show();
$('#index').hide();
$('#detail').hide();
} else {
// 提交过资料,展示个人信息
$('.detail-realname').html(data.realname);
$('.detail-lottery-num').html('NO.' + data.id);
$('.detail-head-img').attr('src', data.headimg);
$('#show').hide();
$('#index').hide();
$('#detail').show();
}
},
// 打开摄像头
openCamera : function(){
// if (action.isRun == 0) {
// alert('尚未开启,请稍后尝试');
// return;
// }
wx.chooseImage({
count: 1, // 默认9
sizeType: ['compressed'], // 'original', 'compressed'指定是原图还是压缩图,默认都有
sourceType: ['camera','album'], // 'album', 'camera'指定来源是相册还是相机,默认都有
success: function (res) {
var localIds = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
wx.uploadImage({
localId: localIds.toString(), // 需要上传的图片的ID,由chooseImage接口获得
isShowProgressTips: 1, // 进度提示
success: function (res) {
var serverId = res.serverId; // 返回图片的服务器端ID,即mediaId
$(".data-img").val(serverId);
console.log(serverId);
wx.downloadImage({
serverId: serverId, // 需要下载的图片的服务器端ID,由uploadImage接口获得
isShowProgressTips: 1, // 默认为1,显示进度提示
success: function (res) {
var localId = res.localId; // 返回图片下载后的本地ID
$('#show').hide();
$('#index').show();
$('#detail').hide();
action.wxgetLocalImgData(localId, 1);
}
});
},
fail: function (res) {
alert('图片上传失败,请重试');
}
});
}
});
},
// 展示选中的图片
wxgetLocalImgData : function(e,num){
var headimg = $(".head-img-show");
if(window.__wxjs_is_wkwebview){
wx.getLocalImgData({
localId: e, // 图片的localID
success: function (res) {
var localData = res.localData; // localData是图片的base64数据,可以用img标签显示
localData = localData.replace('jgp', 'jpeg');//iOS 系统里面得到的数据,类型为 image/jgp,因此需要替换一下
$(headimg).attr("src", localData);
},fail:function(res){
alert("显示失败");
}
});
}else{
$(headimg).attr("src", e);
}
},
// 提交签到申请
submit : function(){
var mediaId = $(".data-img").val();
var realname = $(".data-realname").val().replace( /^\s*/, '');
if (realname == '' || mediaId == '' || realname == '姓名:陈独秀') {
alert('提交的资料数据不完整');
return;
}
wx.getLocation({
type: 'gcj02',
success: (res) => {
console.log(res);
let latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
let longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
let data = {
mediaId : mediaId,
realname : realname,
latitude : latitude,
longitude : longitude
}
// 提交签到
let ADD_USER_URL = '/api.AddUser.php';
baseRquest.post(ADD_USER_URL, data, this.addUserResult);
}
});
},
// 增加成功,重新加载页面,进入用户详情页
addUserResult : function(data) {
console.log(data);
let url = window.location.href; //获取当前url
let key = '?t=';
if (url.indexOf("?") > 0) {
key = '&t=';
}
location.href = url + key + (new Date()).valueOf();
},
// 投票
vote : function(){
let VOTE_URL = '/api.Vote.php';
baseRquest.get(VOTE_URL, {a:'a'}, (data)=>{
console.log(data);
location.href = data.url;
});
}
}
// api接口数据请求,数据交互引用的axios
let baseRquest = {
// get请求
get: function(url, params, callback) {
axios.defaults.baseURL = baseURL;
axios.get(url, {
params : params
}).then(function (response) {
if (response.status == 200)
{
data = response.data;
if (!!data) {
// 过滤错误信息,统一抛出
if (data.errcode != 0 && data.errcode != 200) {
alert(data.errmsg);
} else {
callback(data.result);
}
}
}else {
alert('get无法访问接口');
}
})
.catch(function (error) {
});
},
// post请求
post: function(url, params, callback) {
axios.defaults.baseURL = baseURL;
let data = new URLSearchParams();
Object.keys(params).forEach(function(key){
data.append(key, params[key]);
});
axios.post(url, data,{headers: { 'Content-Type': 'application/x-www-form-urlencoded','Authorization':'123'}})
.then(function (response) {
if (response.status == 200)
{
data = response.data;
if (!!data) {
// 过滤错误信息,统一抛出
if (data.errcode != 0 && data.errcode != 200) {
alert(data.errmsg);
} else {
callback(data.result);
}
}
}else {
alert('post无法访问接口');
}
})
.catch(function (error) {
console.log(error);
});
}
}