|
|
@@ -1,18 +1,57 @@
|
|
|
// componets/authorized-login-dialog/authorizedLoginDialog.js
|
|
|
+const defaultAvatarUrl = "../../static/images/no-avatar.png"
|
|
|
+const homeApi_empower = "https://aipush.aidsleep.cn";
|
|
|
+const token_empower = "b74fd5754c5ef24cf600c39194abdaeb";
|
|
|
Component({
|
|
|
|
|
|
/**
|
|
|
* 组件的属性列表
|
|
|
*/
|
|
|
properties: {
|
|
|
+ showDialog: {
|
|
|
+ type: Boolean,
|
|
|
+ value: false,
|
|
|
+ observer(newVal, oldVal, changePath) {
|
|
|
+ console.log(newVal, oldVal, changePath)
|
|
|
+ const that = this;
|
|
|
+ if (newVal) {
|
|
|
+ console.log("开始登录");
|
|
|
|
|
|
+ wx.login({
|
|
|
+ success: res => {
|
|
|
+ if (res.code) {
|
|
|
+ // 发送 code 到服务器换取 session_key, openid
|
|
|
+ console.log('res.code==' + res.code);
|
|
|
+ that.setData({ wxCode: res.code })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ console.log('that.data.wxCode==' + that.data.wxCode);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
},
|
|
|
|
|
|
+ attached() {
|
|
|
+ },
|
|
|
/**
|
|
|
* 组件的初始数据
|
|
|
*/
|
|
|
data: {
|
|
|
- showDialog:false
|
|
|
+ intervalId: null,
|
|
|
+ canIUseGetUserProfile: wx.canIUse('getUserProfile'),
|
|
|
+ canIUseNicknameComp: wx.canIUse('input.type.nickname'),
|
|
|
+ avatarUrl: defaultAvatarUrl,
|
|
|
+ encryptphone: "",
|
|
|
+ phoneNumber: "",
|
|
|
+ nickName: "",
|
|
|
+ disableCommitBtn: false,
|
|
|
+ hasUserInfo: false,
|
|
|
+ wxCode: "",
|
|
|
+ unionid: "",
|
|
|
+ openid: "",
|
|
|
+ session_key: "",
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
@@ -22,5 +61,187 @@ Component({
|
|
|
onCloseDialog() {
|
|
|
this.setData({ showDialog: false });
|
|
|
},
|
|
|
+ onChooseAvatar(e) {
|
|
|
+ const { avatarUrl } = e.detail
|
|
|
+ this.setData({
|
|
|
+ avatarUrl: avatarUrl,
|
|
|
+ })
|
|
|
+ },
|
|
|
+ onInputNameChange(e) {
|
|
|
+ console.log('e.detail==' + JSON.stringify(e.detail));
|
|
|
+ const nickName = e.detail.value
|
|
|
+ this.setData({
|
|
|
+ nickName: nickName,
|
|
|
+ })
|
|
|
+ },
|
|
|
+ onInputPhoneChange(e) {
|
|
|
+ console.log('e.detail==' + JSON.stringify(e.detail));
|
|
|
+ const phoneNumber = e.detail.value
|
|
|
+ this.setData({
|
|
|
+ phoneNumber: phoneNumber,
|
|
|
+ })
|
|
|
+ },
|
|
|
+ onGetPhoneNumber: function (e) {
|
|
|
+ console.log('onGetPhoneNumber=' + JSON.stringify(e));
|
|
|
+ var that = this;
|
|
|
+ if (e.detail.errMsg === "getPhoneNumber:ok") {
|
|
|
+ // 发送 encryptedData, iv, 和 sessionKey 到你的后台服务器
|
|
|
+ wx.request({
|
|
|
+ url: `${homeApi_empower}/wxtel`,
|
|
|
+ method: 'POST',
|
|
|
+ data: {
|
|
|
+ encryptedData: e.detail.encryptedData,
|
|
|
+ iv: e.detail.iv,
|
|
|
+ code: that.data.wxCode,
|
|
|
+ token: token_empower,
|
|
|
+ },
|
|
|
+ success: function (res) {
|
|
|
+ // 处理服务器返回的结果
|
|
|
+ console.log('手机号获取结果:', res.data);
|
|
|
+ console.log('手机号获取结果:', res.data.encryptphone);
|
|
|
+ console.log('手机号获取结果:', res.data.shieldphone);
|
|
|
+ if (!res.data || !res.data.encryptphone || !res.data.shieldphone) {
|
|
|
+ wx.showToast({
|
|
|
+ title: '无法获取用户手机号',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ console.log('e==' + JSON.stringify(e));
|
|
|
+ console.log('e.detail==' + JSON.stringify(e.detail));
|
|
|
+ that.setData({
|
|
|
+ encryptphone: res.data.encryptphone,
|
|
|
+ phoneNumber: res.data.shieldphone,
|
|
|
+ })
|
|
|
+ wx.setStorageSync('phoneNumber', res.data.shieldphone);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ handleUserInfo: function (e) {
|
|
|
+ if (!this.data.hasUserInfo) {
|
|
|
+ wx.showToast({
|
|
|
+ title: '请设置头像、昵称和手机号',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ var that = this;
|
|
|
+ if (e.detail.userInfo) {
|
|
|
+ // 用户点击允许,获取到用户信息
|
|
|
+ this.setData({
|
|
|
+ disableCommitBtn: true,
|
|
|
+ });
|
|
|
+ wx.showLoading()
|
|
|
+
|
|
|
+ wx.request({
|
|
|
+ url: `${homeApi_empower}/wxlogin`,
|
|
|
+ data: {
|
|
|
+ code: that.data.wxCode,
|
|
|
+ token: token_empower,
|
|
|
+ nickname: that.data.userInfo.nickName,
|
|
|
+ encryptphone: that.data.encryptphone
|
|
|
+ },
|
|
|
+ method: 'POST',
|
|
|
+ success: function (loginRes) {
|
|
|
+ console.log('loginRes=' + JSON.stringify(loginRes));
|
|
|
+ console.log('loginRes.data=' + JSON.stringify(loginRes.data));
|
|
|
+ if (loginRes.data.codes && loginRes.data.codes == 'success') {
|
|
|
+ console.log('123123123');
|
|
|
+ console.log('loginRes.data.back.unionid=' + loginRes.data.back.unionid);
|
|
|
+ console.log('loginRes.data.back.openid=' + loginRes.data.back.openid);
|
|
|
+ wx.setStorageSync('unionid', loginRes.data.back.unionid);
|
|
|
+ wx.setStorageSync('openid', loginRes.data.back.openid);
|
|
|
+ // 获取后台授权
|
|
|
+ that.startInterval();
|
|
|
+ // 可以在这里提示用户进行手机号授权
|
|
|
+ that.setData({
|
|
|
+ unionid: loginRes.data.back.unionid,
|
|
|
+ openid: loginRes.data.back.openid,
|
|
|
+ session_key: loginRes.data.back.session_key,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: function () {
|
|
|
+ wx.hideLoading()
|
|
|
+ wx.showToast({
|
|
|
+ title: '后台授权失败',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ } else {
|
|
|
+ // 用户拒绝授权
|
|
|
+ wx.showToast({
|
|
|
+ title: '您拒绝了授权',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+
|
|
|
+ // 可以选择在这里处理用户拒绝授权后的逻辑,如跳转到其他页面或显示提示信息
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ startInterval: function () {
|
|
|
+ var that = this;
|
|
|
+ this.data.intervalId = setInterval(() => {
|
|
|
+ console.log('后台授权中...');
|
|
|
+ console.log('startInterval-unionid=' + that.data.unionid);
|
|
|
+ console.log('startInterval-openid=' + that.data.openid);
|
|
|
+ wx.request({
|
|
|
+ url: `${homeApi_empower}/wxstatus`,
|
|
|
+ data: {
|
|
|
+ //nickName: that.data.userInfo.nickName,
|
|
|
+ unionid: wx.getStorageSync('unionid'),
|
|
|
+ openid: wx.getStorageSync('openid'),
|
|
|
+ //phoneNumber: that.data.phoneNumber,
|
|
|
+ token: token_empower,
|
|
|
+ },
|
|
|
+ method: 'POST',
|
|
|
+ success: function (intervalRes) {
|
|
|
+ wx.hideLoading()
|
|
|
+ console.log('intervalRes=' + JSON.stringify(intervalRes));
|
|
|
+ console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
|
|
|
+ //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
|
|
|
+ if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
|
|
|
+ wx.setStorageSync('hasAuth', 'hasAuth');
|
|
|
+ wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
|
|
|
+ wx.setStorageSync('roomEmpower', intervalRes.data.room);
|
|
|
+ that.setData({
|
|
|
+ disableCommitBtn: false,
|
|
|
+ });
|
|
|
+ wx.showToast({
|
|
|
+ title: '后台授权成功',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+
|
|
|
+ that.clearInterval();
|
|
|
+ that.triggerEvent('authorizationSuccessful', true);
|
|
|
+ } else {
|
|
|
+ wx.showToast({
|
|
|
+ title: '授权失败,请先办理入住',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: function (error) {
|
|
|
+ wx.hideLoading()
|
|
|
+ that.clearInterval();
|
|
|
+ wx.showToast({
|
|
|
+ title: '后台授权失败',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }, 3000);
|
|
|
+ },
|
|
|
+ clearInterval: function () {
|
|
|
+ var that = this;
|
|
|
+ if (this.data.intervalId) {
|
|
|
+ clearInterval(that.data.intervalId);
|
|
|
+ that.data.intervalId = null;
|
|
|
+ }
|
|
|
+ },
|
|
|
}
|
|
|
})
|