Ver Código Fonte

微信登录

jack 8 anos atrás
pai
commit
a7e362736b
5 arquivos alterados com 499 adições e 10 exclusões
  1. 4 3
      app/html/backBindOn.html
  2. 9 7
      app/html/backBindUn.html
  3. 157 0
      app/js/backBindOn.js
  4. 236 0
      app/js/backBindUn.js
  5. 93 0
      app/js/weChatlogin.js

+ 4 - 3
app/html/backBindOn.html

@ -19,20 +19,21 @@
19 19
			<div class="bindtip"><img src="../images/login_bg_tishi_2_nor@3x.png" width="100%" /></div>
20 20
		    <form class="mui-input-group frmboxNew">
21 21
		    	<div class="mui-input-row frm-input">
22
		            <input type="text" class="mui-input-clear frmtype" placeholder="请输入账号">
22
		            <input type="text" class="mui-input-clear frmtype" placeholder="请输入账号" id="accountNumber">
23 23
		        </div>
24 24
				<div class="mui-input-row frm-input">
25
					<input type="password" class="frmtype" placeholder="请输入密码">
25
					<input type="password" class="frmtype" placeholder="请输入密码" id="password">
26 26
				</div>
27 27
		    </form>
28 28
		    
29 29
		    <div class="mui-content-padded frmboxNew">
30
				<button class="mui-btn mui-btn-block frmbtn frmactive" disabled>绑&nbsp;定</button>
30
				<button class="mui-btn mui-btn-block frmbtn frmactive" id="building" disabled>绑&nbsp;定</button>
31 31
			</div>
32 32
			
33 33
		</div>
34 34
		<script src="../js/public/mui.min.js"></script>
35 35
		<script src="../js/public/base.js"></script>
36
		<script src="../js/backBindOn.js"></script>
36 37
	</body>
37 38
38 39
</html>

+ 9 - 7
app/html/backBindUn.html

@ -19,21 +19,22 @@
19 19
			<div class="bindtip"><img src="../images/login_bg_tishi_1_nor@3x.png" width="100%" /></div>
20 20
		    <form class="mui-input-group frmboxNew">
21 21
		    	<div class="mui-input-row frm-input">
22
		            <input type="text" class="mui-input-clear frmtype" value="此处回填微信名">
22
		            <input type="text" class="mui-input-clear frmtype" value="此处回填微信名" id="weiChatName">
23 23
		        </div>
24 24
		        <div class="mui-input-row frm-input">
25
		            <input type="tel" class="mui-input-clear frmtype" placeholder="请输入手机号码">
25
		            <input type="tel" class="mui-input-clear frmtype" placeholder="请输入手机号码" id="phone">
26 26
		        </div>
27 27
		        <div class="mui-row mui-input-row frm-input">
28 28
		        	<div class="mui-col-xs-8">
29
		        		<input type="password" class="frmtype" placeholder="请输入验证码">
29
		        		<input type="number" class="frmtype" placeholder="请输入验证码" id="phoneCode">
30 30
		        	</div>
31 31
					<div class="mui-col-xs-4">
32
						<input type="button" class="frmtype getCode" disabled value="获取验证码">
32
						<input type="button" class="frmtype getCode" disabled value="获取验证码" id="getPhoneCode">
33
						<input type="button" class="frmtype getCode" value="" style="display: none;" id="getcodeoff">
33 34
					</div>
34 35
				</div>
35 36
				<div class="mui-input-row frm-input">
36
					<input id='password' type="password" class="frmtype" placeholder="请设置登录密码">
37
					<input type="password" class="frmtype" placeholder="请设置登录密码" id="setpassword">
37 38
				</div>
38 39
		    </form>
39 40
		    
@ -44,15 +45,16 @@
44 45
	                    <input type="checkbox" name="checkbox" checked="true" style="display:none;">我已阅读并同意<a class="protocollink" id="protocollink">科袖用户协议</a>
45 46
	               </label>
46 47
		    	</div>
47
				<button class="mui-btn mui-btn-block frmbtn frmactive" disabled>绑&nbsp;定</button>
48
				<button class="mui-btn mui-btn-block frmbtn frmactive" id="binding" disabled>绑&nbsp;定</button>
48 49
				<p class="otherWordNew">
49
					<span class="mui-pull-right">已有账号?</span>
50
					<span class="mui-pull-right" id="alreadyAccount">已有账号?</span>
50 51
				</p>
51 52
			</div>
52 53
			
53 54
		</div>
54 55
		<script src="../js/public/mui.min.js"></script>
55 56
		<script src="../js/public/base.js"></script>
57
		<script src="../js/backBindUn.js"></script>
56 58
	</body>
57 59
58 60
</html>

+ 157 - 0
app/js/backBindOn.js

@ -0,0 +1,157 @@
1
mui.plusReady(function() {
2
	var accountNumber=document.getElementById("accountNumber");
3
	var oPassword=document.getElementById("password");
4
	var oBuilding=document.getElementById("building");
5
	var ws = plus.webview.currentWebview();
6
	console.log(ws.openid)
7
	mui('.frmboxNew').on('keyup', "#accountNumber,#password", function() {
8
			if(accountNumber.value == "" || oPassword.value == "" ) {
9
				oBuilding.setAttribute("disabled","disabled");
10
			} else {
11
				if(oBuilding.getAttribute("disabled")==null) {
12
					return;
13
				}
14
				oBuilding.removeAttribute("disabled");
15
			}
16
		});
17
	/*绑定按钮*/
18
		oBuilding.addEventListener('tap', function() {
19
			userVal();
20
		})
21
		/*校验用户账号*/
22
		function userVal() {
23
			var gunf = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
24
			var hunPhone = /^1[3|4|5|7|8]\d{9}$/;
25
			if(hunPhone.test(accountNumber.value)) {
26
				userRegisterOk();
27
			} else if(gunf.test(accountNumber.value)) {
28
				userRegisterOk();
29
			} else {
30
				plus.nativeUI.toast("请输入正确的手机或邮箱", toastStyle)
31
				return;
32
			}
33
		}
34
		/*判断账号是否注册*/
35
		function userRegisterOk() {
36
			mui.ajax(baseUrl + '/ajax/isReg?key=' + accountNumber.value, {
37
				dataType: 'json', //数据格式类型
38
				type: 'GET', //http请求类型
39
				timeout: 10000, //超时设置
40
				success: function(data) {
41
					console.log(data.data)
42
					if(data.data == true) {
43
						plus.nativeUI.toast("用户不存在请注册用户", toastStyle);
44
						return;
45
					} else {
46
						passwordVal()
47
					}
48
				},
49
				error: function() {
50
					plus.nativeUI.toast("服务器链接超时", toastStyle);
51
					return;
52
				}
53
			});
54
		}
55
		/*校验登录密码*/
56
		function passwordVal() {
57
			if(oPassword.value.length < 6) {
58
				plus.nativeUI.toast("密码不少于6位", toastStyle);
59
				return;
60
			} else {
61
				loginBut();
62
			}
63
		}
64
		/*提交登录*/
65
		function loginBut() {
66
			mui.ajax(baseUrl + '/ajax/login', {
67
				data: {
68
					"pw": oPassword.value, 
69
					"lk": accountNumber.value
70
				},
71
				dataType: 'json', //数据格式类型
72
				type: 'POST', //http请求类型
73
				timeout: 10000, //超时设置
74
				success: function(data) {
75
					console.log(data.data)
76
					if(data.data != "null" && data.data != null) {
77
						var userId = data.data.id;
78
						plus.storage.setItem('userid', userId);
79
						alert(333)
80
						buildingWeiChat(userId);
81
						firstLogin();
82
					} else {
83
						plus.nativeUI.toast("帐号和密码不匹配,请检查后重试", toastStyle);
84
						return;
85
					}
86
				},
87
				error: function() {
88
					plus.nativeUI.toast("服务器链接超时", toastStyle);
89
					return;
90
				}
91
			});
92
		}
93
		function buildingWeiChat(id){
94
			console.log(ws.openid);
95
			console.log(id);
96
			mui.ajax(baseUrl + '/ajax/oauth/associate', {
97
				data: {
98
					oauthType:"weixin", 
99
					openid   :ws.openid,
100
					userid   :id
101
				},
102
				dataType: 'json', //数据格式类型
103
				type: 'POST', //http请求类型
104
				timeout: 10000, //超时设置
105
				success: function(data) {
106
					console.log(JSON.stringify(data));
107
					if(data.success) {
108
						alert(444);
109
					}else{
110
						if(data.code==2) {
111
							
112
						}
113
					}
114
				},
115
				error: function(x) {
116
					console.log(JSON.stringify(x));
117
					plus.nativeUI.toast("服务器链接超时", toastStyle);
118
					return;
119
				}
120
			});
121
		}
122
		/*判断用户第一次登录,是否填写了个人信息*/
123
		function firstLogin() {
124
			var professorId = plus.storage.getItem('userid');
125
			mui.ajax(baseUrl + "/ajax/professor/" + professorId, {
126
				dataType: 'json', //数据格式类型
127
				type: 'GET', //http请求类型
128
				async: false,
129
				timeout: 10000, //超时设置
130
				success: function(data) {
131
					console.log(JSON.stringify(data))
132
					if(data.success) {
133
						alert(555)
134
						if(data.data.authentication < 0){
135
							var productView = mui.preload({
136
								url: '../html/fillinfo.html',
137
								id: '../html/fillinfo.html',
138
								show: {
139
									aniShow: "slide-in-right"
140
								},
141
								extras: {
142
									userid: professorId
143
								}
144
							});
145
							productView.show();
146
						}else{
147
							 goHome();
148
						}
149
					}
150
				},
151
				error: function() {
152
					plus.nativeUI.toast("服务器链接超时", toastStyle);
153
					return;
154
				}
155
			});
156
		}
157
})

+ 236 - 0
app/js/backBindUn.js

@ -0,0 +1,236 @@
1
mui.plusReady(function() {
2
var ws = plus.webview.currentWebview();
3
var phone=document.getElementById("phone");
4
var getPhoneCode=document.getElementById("getPhoneCode");
5
var oPhoneCode=document.getElementById("phoneCode");
6
var weiChatName=document.getElementById("weiChatName");
7
var setpassword=document.getElementById("setpassword");
8
var binding=document.getElementById("binding");
9
var state = 0;
10
var phoneCode = false;
11
document.getElementById("weiChatName").value=ws.name;
12
phone.addEventListener("keyup",function(){
13
	if(phone.value==""){
14
		getPhoneCode.setAttribute("disabled","disabled");
15
	}else{
16
		if(getPhoneCode.getAttribute("disabled")==null){
17
			return;
18
		}
19
		getPhoneCode.removeAttribute("disabled")
20
	}
21
})
22
/*校验提交按钮显示状态*/
23
		mui('.frmboxNew').on('keyup', "#weiChatName,#phone,#phoneCode,#setpassword", function() {
24
			if(weiChatName.value == "" || phone.value == "" || oPhoneCode.value == "" || setpassword.value == "") {
25
				binding.setAttribute("disabled","disabled");
26
			} else {
27
				if(binding.getAttribute("disabled")==null) {
28
					return;
29
				}
30
				binding.removeAttribute("disabled");
31
			}
32
		});
33
		
34
		/*校验手机号*/
35
		getPhoneCode.addEventListener("tap",function(){
36
			phoneVal();
37
		})
38
		function phoneVal() {
39
			var hunPhone = /^1[3|4|5|7|8]\d{9}$/;
40
			if(hunPhone.test(phone.value)) {
41
				isReg();
42
			} else {
43
				plus.nativeUI.toast("请输入正确的手机号码", toastStyle);
44
				return;
45
			}
46
		}
47

48
		/*校验用户名是否注册*/
49
		function isReg() {
50
			mui.ajax(baseUrl + '/ajax/isReg?key=' + phone.value, {
51
				dataType: 'json', //数据格式类型
52
				type: 'GET', //http请求类型
53
				timeout: 10000, //超时设置
54
				success: function(data) {
55
					if(data.data == false) {
56
						plus.nativeUI.toast("该账号已存在,请直接登录", toastStyle);
57
						return;
58
					} else {
59
						phoneCode = true;
60
						if(phoneCode){
61
							sendAuthentication();
62
						}
63
					}
64
				},
65
				error: function() {
66
					plus.nativeUI.toast("服务器链接超时", toastStyle);
67
					return;
68
				}
69
			});
70
		}
71
		/*手机发送验证码*/
72
		function sendAuthentication() {
73
			console.log("send code")
74
			mui.ajax(baseUrl + '/ajax/regmobilephone', {
75
				data: {
76
					mobilePhone: phone.value
77
				},
78
				dataType: 'json', //数据格式类型
79
				type: 'GET', //http请求类型
80
				async: false,
81
				timeout: 10000, //超时设置
82
				success: function(data) {
83
					console.log(JSON.stringify(data))
84
					if(data.success) {
85
						state = data.data;
86
						console.log(state);
87
						doClick();
88
					}
89
				},
90
				error: function() {
91
					plus.nativeUI.toast("服务器链接超时", toastStyle);
92
					return;
93
				}
94
			})
95
		}
96
	/*30s后重新获取验证码*/
97
		function doClick() {
98
			var getCodeOff = document.getElementById("getcodeoff");
99
			getPhoneCode.style.display = "none";
100
			getCodeOff.style.display = "block";
101
			getCodeOff.value = "60s后重新获取";
102
			var clickTime = new Date().getTime();
103
			var Timer = setInterval(function() {
104
				var nowTime = new Date().getTime();
105
				var second = Math.ceil(60 - (nowTime - clickTime) / 1000);
106
				if(second > 0) {
107
					getCodeOff.value = second + "s后重新获取";
108
				} else {
109
					clearInterval(Timer);
110
					getPhoneCode.style.display = "block";
111
					getCodeOff.style.display = "none";
112
					getPhoneCode.value = "获取验证码";
113
				}
114
			}, 1000);
115
		}
116
		/*校验验证码*/
117
		function codeVal() {
118
			console.log(state);
119
			console.log(oPhoneCode.value)
120
			mui.ajax(baseUrl + '/ajax/validCode', {
121
				data: {
122
					"state": state,
123
					"vc": oPhoneCode.value
124
				},
125
				dataType: 'json', //数据格式类型
126
				async: false,
127
				type: 'POST', //http请求类型
128
				timeout: 10000, //超时设置
129
				success: function(data) {
130
					console.log(JSON.stringify(data));
131
					if(data.success) {
132
						if(data.data) {
133
							valOld();
134
						}else{
135
							plus.nativeUI.toast("验证码错误,请检查后重试", toastStyle);
136
							return;
137
						}
138
					}else{
139
						console.log(data.msg);
140
						if(data.msg=="验证超时"){
141
							plus.nativeUI.toast("验证码已过期,请重新获取", toastStyle);
142
							return;
143
						}else{
144
							plus.nativeUI.toast("请填写正确的手机号,验证码", toastStyle);
145
							return;
146
						}
147
						
148
					}
149
				},
150
				error: function() {
151
					plus.nativeUI.toast("服务器链接超时", toastStyle);
152
					return;
153
				}
154
			})
155
		}
156
		/*校验真实姓名和密码*/
157
		function valOld() {
158
			var nameval = /^\w{0,20}$/;
159
			if(nameval.test(weiChatName.value)) {
160
				plus.nativeUI.toast("姓名最长为10个汉字或20个英文字符", toastStyle);
161
				return;
162
			} else if(setpassword.value.length < 6){
163
				plus.nativeUI.toast("密码由6-24个字符组成,区分大小写", toastStyle);
164
				return;
165
			}else{
166
				completeReg();
167
			}
168
		}
169
		binding.addEventListener("tap",function(){
170
			codeVal();
171
		});
172
		//绑定提交
173
		function completeReg() {
174
			console.log(state)
175
			console.log(ws.openid);
176
			console.log(phoneCode.value);
177
			
178
			mui.ajax(baseUrl + '/ajax/mobileReg', {
179
				data: {
180
					state: state,
181
					mobilePhone: phone.value,
182
					validateCode: oPhoneCode.value,
183
					password: setpassword.value,
184
					name:weiChatName.value,
185
					oauthType :"weixin" , 
186
					openid : ws.openid
187
				},
188
				dataType: 'json', //数据格式类型
189
				type: 'post', //http请求类型
190
				success: function(data) {
191
					console.log(JSON.stringify(data))
192
					if(data.success) {
193
						var userId = data.data;
194
						plus.storage.setItem('userid', userId);
195
						plus.nativeUI.toast("已完成注册,请填写个人信息", toastStyle);
196
						mui.openWindow({
197
							url: 'fillinfo.html',
198
							id: 'fillinfo.html',
199
							extras: {
200
								userid: userId
201
							},
202
							show: {
203
								aniShow: "slide-in-right"
204
							}
205
						});
206
					}
207
				},
208
				error: function() {
209
					plus.nativeUI.toast("服务器链接超时", toastStyle);
210
				}
211
			});
212
		}
213
		/*点击已有账号跳已有账号页面*/
214
		document.getElementById("alreadyAccount").addEventListener("tap",function(){
215
			mui.openWindow({
216
							url: '../html/backBindOn.html',
217
							id: 'backBindOn.html',
218
							show: {
219
								aniShow: "slide-in-right"
220
							},
221
							extras: {
222
								openid:ws.openid
223
							}
224
						});
225
		})
226
		/*用户协议*/
227
		document.getElementById("protocollink").addEventListener('tap',function(){
228
			mui.openWindow({
229
				url: '../html/privacy.html',
230
				id: '../html/privacy.html',
231
				show: {
232
					aniShow: "slide-in-right"
233
				}
234
			});
235
		});
236
})

+ 93 - 0
app/js/weChatlogin.js

@ -0,0 +1,93 @@
1
mui.plusReady(function() {
2
	plus.oauth.getServices(function(services) {
3
		auths = {};
4
		for(var i in services) {
5
			var t = services[i];
6
			auths[t.id] = t;
7

8
		}
9
	}, function(e) {
10
		alert("获取登录服务列表失败:" + e.message + " - " + e.code);
11
	});
12
	document.getElementById("weixin").addEventListener("tap", function() {
13
		var s = auths["weixin"];
14
		if(s) {
15
			if(!s.authResult) {
16
				s.login(function(e) {
17
					mui.toast("登录认证成功!");
18
					authUserInfo(s);
19
				}, function(e) {
20
					mui.toast("登录认证失败!");
21
				});
22
			} else {
23
				mui.toast("已经登录认证!");
24
				authLogout();
25
			}
26
		} else {
27
			mui.toast("不支微信登录");
28
		}
29
	});
30

31
	function authLogout() {
32
		for(var i in auths) {
33
			var s = auths[i];
34
			if(s.authResult) {
35
				s.logout(function(e) {
36
					console.log("注销登录认证成功!");
37
				}, function(e) {
38
					console.log("注销登录认证失败!");
39
				});
40
			}
41
		}
42
	}
43

44
	function authUserInfo(s) {
45
		console.log(s);
46
		s.getUserInfo(function(e) {
47
			var josnStr = JSON.stringify(s.userInfo);
48
			var jsonObj = s.userInfo;
49
			console.log("获取用户信息成功:" + josnStr);
50
			console.log(jsonObj.openid)
51
			authLogout();
52
			console.log(jsonObj.openid)
53
			weChat(jsonObj.openid,jsonObj.nickname);
54
		}, function(e) {
55
			alert("获取用户信息失败:" + e.message + " - " + e.code);
56
		});
57
	}
58

59
	function weChat(weiChatId,weixinName) {
60
		mui.ajax(baseUrl + "/ajax/oauth/openidLogin", {
61
			dataType: 'json', //数据格式类型
62
			type: 'POST', //http请求类型
63
			timeout: 10000, //超时设置
64
			data: {
65
				oauthType: 'weixin',
66
				openid: weiChatId
67
			},
68
			success: function(data) {
69
				if(data.success) {
70
					if(data.data == null) {
71
						mui.openWindow({
72
							url: '../html/backBindUn.html',
73
							id: 'backBindUn.html',
74
							show: {
75
								autoShow: true,
76
								aniShow: "slide-in-right"
77
							},
78
							 extras:{
79
							 	name:weixinName,
80
							 	openid:weiChatId
81
							 }
82
						});
83
					}
84
				}
85
			},
86
			error: function(x) {
87
				console.log(JSON.stringify(x))
88
				plus.nativeUI.toast("服务器链接超时", toastStyle);
89
				return;
90
			}
91
		});
92
	}
93
})