|
@@ -15,18 +15,18 @@ Component({
|
|
|
console.log(newVal, oldVal, changePath)
|
|
console.log(newVal, oldVal, changePath)
|
|
|
const that = this;
|
|
const that = this;
|
|
|
if (newVal) {
|
|
if (newVal) {
|
|
|
- console.log("开始登录");
|
|
|
|
|
|
|
+ // console.log("开始登录");
|
|
|
|
|
|
|
|
- wx.login({
|
|
|
|
|
- success: res => {
|
|
|
|
|
- if (res.code) {
|
|
|
|
|
- // 发送 code 到服务器换取 session_key, openid
|
|
|
|
|
- console.log('res.code==' + res.code);
|
|
|
|
|
- that.setData({ showLoading: false, wxCode: res.code })
|
|
|
|
|
- that.checkHasUserInfo()
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ // wx.login({
|
|
|
|
|
+ // success: res => {
|
|
|
|
|
+ // if (res.code) {
|
|
|
|
|
+ // // 发送 code 到服务器换取 session_key, openid
|
|
|
|
|
+ // console.log('res.code==' + res.code);
|
|
|
|
|
+ // that.setData({ showLoading: false, wxCode: res.code })
|
|
|
|
|
+ // that.checkHasUserInfo()
|
|
|
|
|
+ // }
|
|
|
|
|
+ // }
|
|
|
|
|
+ // });
|
|
|
} else {
|
|
} else {
|
|
|
console.log('that.data.wxCode==' + that.data.wxCode);
|
|
console.log('that.data.wxCode==' + that.data.wxCode);
|
|
|
}
|
|
}
|
|
@@ -44,6 +44,7 @@ Component({
|
|
|
canIUseGetUserProfile: wx.canIUse('getUserProfile'),
|
|
canIUseGetUserProfile: wx.canIUse('getUserProfile'),
|
|
|
canIUseNicknameComp: wx.canIUse('input.type.nickname'),
|
|
canIUseNicknameComp: wx.canIUse('input.type.nickname'),
|
|
|
avatarUrl: defaultAvatarUrl,
|
|
avatarUrl: defaultAvatarUrl,
|
|
|
|
|
+ tempAvatarUrl: '',
|
|
|
showLoading: false,
|
|
showLoading: false,
|
|
|
encryptphone: "",
|
|
encryptphone: "",
|
|
|
phoneNumber: "",
|
|
phoneNumber: "",
|
|
@@ -67,11 +68,86 @@ Component({
|
|
|
// 选择头像 后期需要上传
|
|
// 选择头像 后期需要上传
|
|
|
onChooseAvatar(e) {
|
|
onChooseAvatar(e) {
|
|
|
const { avatarUrl } = e.detail
|
|
const { avatarUrl } = e.detail
|
|
|
- this.setData({
|
|
|
|
|
- avatarUrl: avatarUrl,
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ // this.setData({
|
|
|
|
|
+ // avatarUrl: avatarUrl,
|
|
|
|
|
+ // })
|
|
|
|
|
+
|
|
|
|
|
+ const that = this;
|
|
|
|
|
+
|
|
|
|
|
+ this.setData({ tempAvatarUrl: avatarUrl });
|
|
|
|
|
+ // 下载头像图片
|
|
|
|
|
+ wx.downloadFile({
|
|
|
|
|
+ url: avatarUrl,
|
|
|
|
|
+ success(res) {
|
|
|
|
|
+ if (res.statusCode === 200) {
|
|
|
|
|
+ console.log('download success');
|
|
|
|
|
+ const tempFilePath = res.tempFilePath
|
|
|
|
|
+ that.uploadAvatar(tempFilePath);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
this.checkHasUserInfo();
|
|
this.checkHasUserInfo();
|
|
|
},
|
|
},
|
|
|
|
|
+
|
|
|
|
|
+ uploadAvatar(tempFilePath) {
|
|
|
|
|
+ wx.showLoading({ title: '上传中...' });
|
|
|
|
|
+ const randomString = this.generateRandomString(16);
|
|
|
|
|
+ const base64String = wx.arrayBufferToBase64(new Uint8Array([...randomString].map(c => c.charCodeAt(0))));
|
|
|
|
|
+
|
|
|
|
|
+ wx.request({
|
|
|
|
|
+ url: 'https://aipush.aidsleep.cn/wxuptokens',
|
|
|
|
|
+ method: 'POST',
|
|
|
|
|
+ data: {
|
|
|
|
|
+ rk: base64String
|
|
|
|
|
+ },
|
|
|
|
|
+ success: (res) => {
|
|
|
|
|
+ if (res.data.success) {
|
|
|
|
|
+ this.doUpload(tempFilePath, res.data.token);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ wx.hideLoading();
|
|
|
|
|
+ wx.showToast({ title: '获取上传凭证失败', icon: 'none' });
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ fail: () => {
|
|
|
|
|
+ wx.hideLoading();
|
|
|
|
|
+ wx.showToast({ title: '获取上传凭证失败', icon: 'none' });
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ doUpload(tempFilePath, token) {
|
|
|
|
|
+ wx.uploadFile({
|
|
|
|
|
+ url: 'https://aipush.aidsleep.cn/wxups',
|
|
|
|
|
+ filePath: tempFilePath,
|
|
|
|
|
+ name: 'file',
|
|
|
|
|
+ formData: {
|
|
|
|
|
+ 'token': token
|
|
|
|
|
+ },
|
|
|
|
|
+ success: (res) => {
|
|
|
|
|
+ wx.hideLoading();
|
|
|
|
|
+ const data = JSON.parse(res.data);
|
|
|
|
|
+ if (data.success) {
|
|
|
|
|
+ this.setData({ avatarUrl: data.url });
|
|
|
|
|
+ wx.showToast({ title: '头像上传成功', icon: 'success' });
|
|
|
|
|
+ } else {
|
|
|
|
|
+ wx.showToast({ title: data.message || '头像上传失败', icon: 'none' });
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ fail: () => {
|
|
|
|
|
+ wx.hideLoading();
|
|
|
|
|
+ wx.showToast({ title: '头像上传失败', icon: 'none' });
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ generateRandomString(length) {
|
|
|
|
|
+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
|
|
+ let result = '';
|
|
|
|
|
+ for (let i = 0; i < length; i++) {
|
|
|
|
|
+ result += characters.charAt(Math.floor(Math.random() * characters.length));
|
|
|
|
|
+ }
|
|
|
|
|
+ return result;
|
|
|
|
|
+ },
|
|
|
// 输入昵称
|
|
// 输入昵称
|
|
|
onInputNameChange(e) {
|
|
onInputNameChange(e) {
|
|
|
console.log('e.detail==' + JSON.stringify(e.detail));
|
|
console.log('e.detail==' + JSON.stringify(e.detail));
|
|
@@ -103,8 +179,7 @@ Component({
|
|
|
data: {
|
|
data: {
|
|
|
encryptedData: e.detail.encryptedData,
|
|
encryptedData: e.detail.encryptedData,
|
|
|
iv: e.detail.iv,
|
|
iv: e.detail.iv,
|
|
|
- code: that.data.wxCode,
|
|
|
|
|
- token: token_empower,
|
|
|
|
|
|
|
+ token: wx.getStorageSync("token") || token_empower,
|
|
|
},
|
|
},
|
|
|
success: function (res) {
|
|
success: function (res) {
|
|
|
// 处理服务器返回的结果
|
|
// 处理服务器返回的结果
|
|
@@ -153,56 +228,48 @@ Component({
|
|
|
showLoading: true,
|
|
showLoading: true,
|
|
|
disableCommitBtn: true,
|
|
disableCommitBtn: true,
|
|
|
});
|
|
});
|
|
|
- wx.login({
|
|
|
|
|
- success: res => {
|
|
|
|
|
- if (res.code) {
|
|
|
|
|
- // 发送 code 到服务器换取 session_key, openid
|
|
|
|
|
- console.log('res.code==' + res.code);
|
|
|
|
|
- that.setData({ wxCode: res.code })
|
|
|
|
|
|
|
|
|
|
- wx.request({
|
|
|
|
|
- url: `${homeApi_empower}/wxlogin`,
|
|
|
|
|
- data: {
|
|
|
|
|
- code: that.data.wxCode,
|
|
|
|
|
- token: token_empower,
|
|
|
|
|
- nickname: that.data.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,
|
|
|
|
|
- });
|
|
|
|
|
- } else {
|
|
|
|
|
- that.setData({ showLoading: false });
|
|
|
|
|
- wx.showToast({
|
|
|
|
|
- title: '后台授权失败',
|
|
|
|
|
- icon: 'none'
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- },
|
|
|
|
|
- fail: function () {
|
|
|
|
|
- that.setData({ showLoading: false });
|
|
|
|
|
- wx.showToast({
|
|
|
|
|
- title: '后台授权失败',
|
|
|
|
|
- icon: 'none'
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ wx.request({
|
|
|
|
|
+ url: `${homeApi_empower}/wxlogin`,
|
|
|
|
|
+ data: {
|
|
|
|
|
+ token: wx.getStorageSync("token") || token_empower,
|
|
|
|
|
+ unionid: wx.getStorageSync("unionid"),
|
|
|
|
|
+ openid: wx.getStorageSync("openid"),
|
|
|
|
|
+ avatarUrl: that.data.avatarUrl,
|
|
|
|
|
+ nickname: that.data.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,
|
|
|
|
|
+ });
|
|
|
|
|
+ } else {
|
|
|
|
|
+ that.setData({ showLoading: false });
|
|
|
|
|
+ wx.showToast({
|
|
|
|
|
+ title: '后台授权失败',
|
|
|
|
|
+ icon: 'none'
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
+ },
|
|
|
|
|
+ fail: function () {
|
|
|
|
|
+ that.setData({ showLoading: false });
|
|
|
|
|
+ wx.showToast({
|
|
|
|
|
+ title: '后台授权失败',
|
|
|
|
|
+ icon: 'none'
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|
|
@@ -221,16 +288,12 @@ Component({
|
|
|
var that = this;
|
|
var that = this;
|
|
|
this.data.intervalId = setInterval(() => {
|
|
this.data.intervalId = setInterval(() => {
|
|
|
console.log('后台授权中...');
|
|
console.log('后台授权中...');
|
|
|
- console.log('startInterval-unionid=' + that.data.unionid);
|
|
|
|
|
- console.log('startInterval-openid=' + that.data.openid);
|
|
|
|
|
wx.request({
|
|
wx.request({
|
|
|
url: `${homeApi_empower}/wxstatus`,
|
|
url: `${homeApi_empower}/wxstatus`,
|
|
|
data: {
|
|
data: {
|
|
|
- //nickName: that.data.userInfo.nickName,
|
|
|
|
|
unionid: wx.getStorageSync('unionid'),
|
|
unionid: wx.getStorageSync('unionid'),
|
|
|
openid: wx.getStorageSync('openid'),
|
|
openid: wx.getStorageSync('openid'),
|
|
|
- //phoneNumber: that.data.phoneNumber,
|
|
|
|
|
- token: token_empower,
|
|
|
|
|
|
|
+ token: wx.getStorageSync('token') || token_empower,
|
|
|
},
|
|
},
|
|
|
method: 'POST',
|
|
method: 'POST',
|
|
|
success: function (intervalRes) {
|
|
success: function (intervalRes) {
|