柒零 hace 1 año
padre
commit
b55db38d38
Se han modificado 4 ficheros con 157 adiciones y 76 borrados
  1. 18 0
      app.js
  2. 131 68
      componets/authorized-login-dialog/authorizedLoginDialog.js
  3. 4 4
      pages/data/data.js
  4. 4 4
      pages/data/data.wxml

+ 18 - 0
app.js

@@ -1,6 +1,7 @@
 
 
 // app.js
+const homeApi_empower = "https://aipush.aidsleep.cn";
 App({
   onLaunch() {
     wx.hideTabBar()
@@ -14,6 +15,23 @@ App({
     wx.login({
       success: res => {
         // 发送 res.code 到后台换取 openId, sessionKey, unionId
+        wx.request({
+          url: `${homeApi_empower}/wxssk`,
+          method: 'POST',
+          data: {
+            code: res.code,
+          },
+          success: function (loginRes) {
+            // 处理服务器返回的结果  
+            console.log('登录获取结果:', loginRes.data);
+            if (loginRes.data) { 
+              wx.setStorageSync("openid", loginRes.data.openid);
+              wx.setStorageSync("token", loginRes.data.token);
+              wx.setStorageSync("unionid", loginRes.data.unionid);
+
+            }
+          }
+        })
       }
     })
   },

+ 131 - 68
componets/authorized-login-dialog/authorizedLoginDialog.js

@@ -15,18 +15,18 @@ Component({
         console.log(newVal, oldVal, changePath)
         const that = this;
         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 {
           console.log('that.data.wxCode==' + that.data.wxCode);
         }
@@ -44,6 +44,7 @@ Component({
     canIUseGetUserProfile: wx.canIUse('getUserProfile'),
     canIUseNicknameComp: wx.canIUse('input.type.nickname'),
     avatarUrl: defaultAvatarUrl,
+    tempAvatarUrl: '',
     showLoading: false,
     encryptphone: "",
     phoneNumber: "",
@@ -67,11 +68,86 @@ Component({
     // 选择头像 后期需要上传
     onChooseAvatar(e) {
       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();
     },
+
+    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) {
       console.log('e.detail==' + JSON.stringify(e.detail));
@@ -103,8 +179,7 @@ Component({
           data: {
             encryptedData: e.detail.encryptedData,
             iv: e.detail.iv,
-            code: that.data.wxCode,
-            token: token_empower,
+            token: wx.getStorageSync("token") || token_empower,
           },
           success: function (res) {
             // 处理服务器返回的结果  
@@ -153,56 +228,48 @@ Component({
           showLoading: 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;
       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,
+            token: wx.getStorageSync('token') || token_empower,
           },
           method: 'POST',
           success: function (intervalRes) {

+ 4 - 4
pages/data/data.js

@@ -4219,10 +4219,10 @@ Page({
 		// console.log("睡眠时间片段=>", timePeriod.length);
 		// console.log("睡眠数据=>", sleepData);
 		// console.log("睡眠数据=>", sleepData.length);
-		var awakeColor = [{ offset: 0, color: '#F7FCBE' }, { offset: 1, color: '#FDBE25' }];;
-		var lightSleepColor = [{ offset: 0, color: '#FFBCBD' }, { offset: 1, color: '#FA6263' }];
-		var sleptInColor = [{ offset: 0, color: '#FD7778' }, { offset: 1, color: '#AA3CFF' }];
-		var deepSleepColor = [{ offset: 0, color: '#AA3CFF' }, { offset: 1, color: '#7C3CFF' }];
+		var awakeColor = [{ offset: 0, color: '#F7E3BA' }, { offset: 1, color: '#F7E3BA' }];;
+		var lightSleepColor = [{ offset: 0, color: '#AAD09F' }, { offset: 1, color: '#AAD09F' }];
+		var sleptInColor = [{ offset: 0, color: '#75BDE0' }, { offset: 1, color: '#75BDE0' }];
+		var deepSleepColor = [{ offset: 0, color: '#3D7299' }, { offset: 1, color: '#3D7299' }];
 		// var linearGradient = new echarts.graphic.LinearGradient(
 		// 	0, 0, 0, 1, // 方向从左到右  
 		// 	[

+ 4 - 4
pages/data/data.wxml

@@ -555,25 +555,25 @@
 		<view class="card-h" style="margin-top: 30rpx;justify-content: space-around;">
 			<view class="card-item">
 				<view
-					style="width: 8rpx;height: 24rpx;margin-right: 16rpx;  background: linear-gradient( 180deg, #AA3CFF 0%, #7C3CFF 100%);border-radius: 2rpx">
+					style="width: 8rpx;height: 24rpx;margin-right: 16rpx;  background: linear-gradient( 180deg, #3D7299 0%, #3D7299 100%);border-radius: 2rpx">
 				</view>
 				<text class="card-t3">深睡</text>
 			</view>
 			<view class="card-item">
 				<view
-					style="width: 8rpx;height: 24rpx;margin-right: 16rpx; background: linear-gradient( 180deg, #FD7778 0%, #AA3CFF 100%);border-radius: 2rpx">
+					style="width: 8rpx;height: 24rpx;margin-right: 16rpx; background: linear-gradient( 180deg, #75BDE0 0%, #75BDE0 100%);border-radius: 2rpx">
 				</view>
 				<text class="card-t3">浅睡</text>
 			</view>
 			<view class="card-item">
 				<view
-					style="width: 8rpx;height: 24rpx;margin-right: 16rpx; background: linear-gradient( 180deg, #FFBCBD 0%, #FA6263 100%);border-radius: 2rpx">
+					style="width: 8rpx;height: 24rpx;margin-right: 16rpx; background: linear-gradient( 180deg, #AAD09F 0%, #AAD09F 100%);border-radius: 2rpx">
 				</view>
 				<text class="card-t3">清醒</text>
 			</view>
 			<view class="card-item">
 				<view
-					style="width: 8rpx;height: 24rpx;margin-right: 16rpx;  background: linear-gradient( 180deg, #F7FCBE 0%, #FDBE25 100%);border-radius: 2rpx">
+					style="width: 8rpx;height: 24rpx;margin-right: 16rpx;  background: linear-gradient( 180deg, #F7E3BA 0%, #F7E3BA 100%);border-radius: 2rpx">
 				</view>
 				<text class="card-t3">REM</text>
 			</view>