头条小程序支持一键获取手机号码登录

master
gongfuxiang 2022-08-02 23:23:30 +08:00
parent bea63fcf6d
commit 12c5bbad1a
2 changed files with 90 additions and 72 deletions

131
App.vue
View File

@ -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('授权失败');
}
});
}, },
/** /**

View File

@ -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) { // getUserProfilelogin
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);
}
} }
}, },