APICloud微信登录

APICloud微信登录

					open_wx: function () {
					if (!this.ischeck) {

						api.toast({
							msg: '必须同意用户协议和隐私政策',
							duration: 2000,
							location: 'middle'
						});

						return;

					}
					console.log(456)
					api.showProgress();
					var wx = api.require('wxPlus');
					wx.auth({
						apiKey: 'MY_APIKEY',
						scope: 'snsapi_userinfo'
					}, function (ret, err) {
						console.log(JSON.stringify(ret))
						console.log(JSON.stringify(err))
						if (ret.status) {
							wx.getToken({//获取
								apiKey: 'MY_APIKEY',
								apiSecret: '',
								code: ret.code
							}, function (ret, err) {
								console.log(JSON.stringify(ret))
								console.log(JSON.stringify(err))
								var openId = ret.openId;
								console.log(openId + 'openId')
								if (ret.status) {
									wx.getUserInfo({//获取微信用户信息
										accessToken: ret.accessToken,
										openId: ret.openId
									}, function (res, err) {

										console.log(JSON.stringify(ret))
										console.log(JSON.stringify(err))

										if (ret.status) {
											if (res.sex == 0) {
												var sex = 2
											} else {
												var sex = 1
											}
											api.hideProgress();
											console.log(res.openId)
											api.ajax({
												url: login_checkBindWeChat,
												method: 'post',
												headers: { 'Authorization': $api.getStorage('token') },
												timeout: 30,
												returnAll: false,
												data: {
													values: {
														wxOpenId: ret.openId,
														device: vm.device
													}
												}
											}, function (ret, err) {
												console.log(JSON.stringify(ret))
												console.log(JSON.stringify(err))
												if (ret) {
													if (ret.code == 1) {
														api.toast({
															msg: '登录成功',
															duration: 2000,
															location: 'middle'
														});
														$api.setStorage("token", ret.data);
														api.sendEvent({
															name: 'logined'
														});

														setTimeout(function () {
															api.closeToWin({
																name: 'root'
															});
															api.sendEvent({
																name: 'on_indexs',
																extra: {
																	on_indexs: 0
																}
															});
															api.sendEvent({
																name: 'selectCoupon'
															})
														}, 100)
													} else if (ret.code == 12) {
														console.log(openId)
														api.openWin({
															name: 'bindPhone',
															url: 'widget://html/login/bind_win.html',
															rect: {
																x: 0,
																y: 0,
																w: 'auto',
																h: api.winHeight
															},
															pageParam: {
																nickname: res.nickname,
																wxOpenId: openId,
																device: vm.device
															},
															animation: {
																type: "push",                //动画类型(详见动画类型常量)
																subType: "from_right",       //动画子类型(详见动画子类型常量)
																duration: 300
															},
															bgColor: "#fff",
															bounces: false,
														});
													} else {
														api.toast({
															msg: ret.msg,
															duration: 2000,
															location: 'middle'
														});
													}
												}
											});


										}
									});
								}
							});
						} else {
							if (err.code == 3) {
								api.hideProgress();
								api.toast({
									msg: '当前设备未安装微信客户端',
									duration: 2000,
									location: 'middle'
								});
							}
						}
					});
				},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值