头条小程序支持一键获取手机号码登录
parent
bea63fcf6d
commit
12c5bbad1a
131
App.vue
131
App.vue
|
|
@ -318,68 +318,81 @@
|
||||||
user_login(object, method) {
|
user_login(object, method) {
|
||||||
var login_data = uni.getStorageSync(this.data.cache_user_login_key) || null;
|
var login_data = uni.getStorageSync(this.data.cache_user_login_key) || null;
|
||||||
if (login_data == null) {
|
if (login_data == null) {
|
||||||
var self = this;
|
this.user_login_handle(object, method, true);
|
||||||
uni.showLoading({
|
|
||||||
title: "授权中..."
|
|
||||||
});
|
|
||||||
var action = 'login';
|
|
||||||
// #ifdef MP-BAIDU
|
|
||||||
action = 'getLoginCode';
|
|
||||||
// #endif
|
|
||||||
uni[action]({
|
|
||||||
success: res => {
|
|
||||||
if (res.code) {
|
|
||||||
uni.request({
|
|
||||||
url: self.get_request_url('appminiuserauth', 'user'),
|
|
||||||
method: 'POST',
|
|
||||||
data: {
|
|
||||||
authcode: res.code
|
|
||||||
},
|
|
||||||
dataType: 'json',
|
|
||||||
success: res => {
|
|
||||||
uni.hideLoading();
|
|
||||||
if (res.data.code == 0) {
|
|
||||||
var data = res.data.data;
|
|
||||||
if ((data.is_user_exist || 0) == 1) {
|
|
||||||
uni.setStorage({
|
|
||||||
key: self.data.cache_user_info_key,
|
|
||||||
data: data,
|
|
||||||
success: res => {
|
|
||||||
if (typeof object === 'object' && (method || null) != null) {
|
|
||||||
object[method]();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
fail: () => {
|
|
||||||
self.showToast('用户信息缓存失败');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
uni.setStorage({
|
|
||||||
key: self.data.cache_user_login_key,
|
|
||||||
data: data
|
|
||||||
});
|
|
||||||
self.login_to_auth();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
uni.hideLoading();
|
|
||||||
self.showToast(res.data.msg);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
fail: () => {
|
|
||||||
uni.hideLoading();
|
|
||||||
self.showToast('服务器请求出错');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
fail: e => {
|
|
||||||
uni.hideLoading();
|
|
||||||
self.showToast('授权失败');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
this.login_to_auth();
|
this.login_to_auth();
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户登录处理
|
||||||
|
* object 回调操作对象
|
||||||
|
* method 回调操作对象的函数
|
||||||
|
* auth_data 授权数据
|
||||||
|
* is_to_auth 是否进入授权
|
||||||
|
*/
|
||||||
|
user_login_handle(object, method, is_to_auth = true) {
|
||||||
|
var self = this;
|
||||||
|
uni.showLoading({
|
||||||
|
title: "授权中..."
|
||||||
|
});
|
||||||
|
var action = 'login';
|
||||||
|
// #ifdef MP-BAIDU
|
||||||
|
action = 'getLoginCode';
|
||||||
|
// #endif
|
||||||
|
uni[action]({
|
||||||
|
success: res => {
|
||||||
|
if (res.code) {
|
||||||
|
uni.request({
|
||||||
|
url: self.get_request_url('appminiuserauth', 'user'),
|
||||||
|
method: 'POST',
|
||||||
|
data: {
|
||||||
|
authcode: res.code
|
||||||
|
},
|
||||||
|
dataType: 'json',
|
||||||
|
success: res => {
|
||||||
|
uni.hideLoading();
|
||||||
|
if (res.data.code == 0) {
|
||||||
|
var data = res.data.data;
|
||||||
|
if ((data.is_user_exist || 0) == 1) {
|
||||||
|
uni.setStorage({
|
||||||
|
key: self.data.cache_user_info_key,
|
||||||
|
data: data,
|
||||||
|
success: res => {
|
||||||
|
if (typeof object === 'object' && (method || null) != null) {
|
||||||
|
object[method]();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail: () => {
|
||||||
|
self.showToast('用户信息缓存失败');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
uni.setStorage({
|
||||||
|
key: self.data.cache_user_login_key,
|
||||||
|
data: data
|
||||||
|
});
|
||||||
|
if(is_to_auth) {
|
||||||
|
self.login_to_auth();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
uni.hideLoading();
|
||||||
|
self.showToast(res.data.msg);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail: () => {
|
||||||
|
uni.hideLoading();
|
||||||
|
self.showToast('服务器请求出错');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail: e => {
|
||||||
|
uni.hideLoading();
|
||||||
|
self.showToast('授权失败');
|
||||||
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -555,17 +555,17 @@
|
||||||
this.user_auth_code(userinfo);
|
this.user_auth_code(userinfo);
|
||||||
// #endif
|
// #endif
|
||||||
// #ifdef MP-TOUTIAO
|
// #ifdef MP-TOUTIAO
|
||||||
if(e == 1) {
|
uni.getUserProfile({
|
||||||
var self = this;
|
force: true,
|
||||||
uni.getUserInfo({
|
success: (res) => {
|
||||||
success (res) {
|
// 由于头条getUserProfile内置了login
|
||||||
var userinfo = JSON.parse(res.rawData);
|
// 会刷新前面的login会导致后面获取手机号码数据解析失败
|
||||||
self.user_auth_code(userinfo);
|
// 这里重新获取sessionKey登录
|
||||||
}
|
app.globalData.user_login_handle(null, null, false);
|
||||||
});
|
// 用户授权登录
|
||||||
} else {
|
this.user_auth_code(res.userInfo);
|
||||||
app.globalData.auth_check(this, 'get_user_info_event', 'scope.userInfo');
|
}
|
||||||
}
|
});
|
||||||
// #endif
|
// #endif
|
||||||
// #ifdef MP-KUAISHOU
|
// #ifdef MP-KUAISHOU
|
||||||
var userinfo = e.target.userInfo;
|
var userinfo = e.target.userInfo;
|
||||||
|
|
@ -573,7 +573,7 @@
|
||||||
// #endif
|
// #endif
|
||||||
},
|
},
|
||||||
|
|
||||||
// 用户授权
|
// 用户授权登录
|
||||||
// auth_data 授权数据
|
// auth_data 授权数据
|
||||||
user_auth_code(auth_data) {
|
user_auth_code(auth_data) {
|
||||||
if ((auth_data || null) != null) {
|
if ((auth_data || null) != null) {
|
||||||
|
|
@ -876,9 +876,14 @@
|
||||||
},
|
},
|
||||||
fail: () => {
|
fail: () => {
|
||||||
uni.hideLoading();
|
uni.hideLoading();
|
||||||
self.showToast('服务器请求出错');
|
app.globalData.showToast('服务器请求出错');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
var msg = e.errmsg || e.errMsg || e.detail.errmsg || e.detail.errMsg || null;
|
||||||
|
if(msg != null) {
|
||||||
|
app.globalData.showToast(msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue