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'
});
}
}
});
},