页面

js

// pages/profile/profile.js
import {
  request
} from "../../request/index.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAuth: false, //是否授权
    userInfo: {}, //用户信息
    useId: ''
  },
  // 获取授权

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const myAuth = wx.getStorageSync('isAuth')
    if (myAuth) {
      const myUser = wx.getStorageSync('userinfo')
      this.setData({
        isAuth: myAuth,
        userInfo: JSON.parse(myUser)
      })
    }

  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息,
    // 开发者每次通过该接口获取用户个人信息均需用户确认,
    // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
    const _that = this
    wx.getUserProfile({
      desc: '“获取你的昵称、头像', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
      success: (res) => {
        console.log(res.userInfo, 'res')
        let userN = res.userInfo.nickName
        let userIN = res.userInfo
        wx.request({
          url: 'http://localhost:3000/users/login',
          data: {
            username: userN
          },
          method: 'POST',
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (result) {
          //  console.log(result.data.status, 'res.status');
            if (result.data.status === 400) {
              wx.showToast({
                title: '请先注册',
                icon: 'error'
              })
            } else {
              const myID = result.data.body.data.ID
              _that.setData({
                useId: myID
              })
              wx.setStorageSync('userId', myID)
              _that.setData({
                userInfo: res.userInfo,
                isAuth: true
              })
              wx.setStorageSync('userinfo', JSON.stringify(userIN))
              wx.setStorageSync('isAuth', _that.data.isAuth)
            }

          },
          fail: function (res) {
            console.log(".....fail.....");

          }
        })

      },
      fail: function (res) {
        console.log(".....fail.....");

      }
    })

  },
  exit() {
    wx.showModal({
      content: "确定退出吗"
    }).then(res => {
      if (res.confirm) {
        console.log("用户点击了确定");
        if (this.data.isAuth) {
          this.setData({
            isAuth: false,
            userInfo: {}
          })
          //清空登录的缓存
          wx.setStorageSync('userinfo', null)
          wx.setStorageSync('isAuth', false)
          wx.setStorageSync('userId', null)
        }
      } else if (res.cancel) {
        console.log("用户点击了取消");
      }
    })
  },
  goToFeeback() {
    wx.navigateTo({
      url: '/pages/feeback/feeback'
    })
  },
  goToMyFav() {
    if (this.data.isAuth) {
      wx.navigateTo({
        url: '/pages/myCollection/myCollection',
      })
    } else {
      wx.showToast({
        title: "请先登录",
        icon: 'error'
      })
    }
  },
  goToLogout() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  goToMyInfo() {
    if (this.data.isAuth) {
      wx.navigateTo({
        url: '/pages/myInfo/myInfo',
      })
    } else {
      wx.showToast({
        title: "请先登录",
        icon: 'error'
      })
    }

  }
})

  wxml

<!--pages/profile/profile.wxml-->

<view>
  <image class='background' src="/image/bg.jpg" mode="widthFix"></image>
  <!-- 游客 -->
     <!-- 登录前 --> 
  <view class="{{isAuth?'profile_none':'profile_header'}}">
    <image src="/image/my.png" mode="heightFix"></image>
    <view  class="profile_login" bindtap="getUserProfile">登录</view>
  </view>
   <!-- 登录后 --> 
  <view class="{{isAuth?'profile_header':'profile_none'}}">
    <image src="{{userInfo.avatarUrl}}" mode="heightFix"></image>
    <view class="{{isAuth?'profile_hello':'profile_none'}}">
      <!-- <view>你好</view> -->
      <view>{{userInfo.nickName}}</view>
    </view>
  </view>
  <!-- 其他功能 -->
  <view class="profile_container">
    <view bindtap="goToMyFav">
      <image src="/image/fav.png" mode="heightFix"></image>
      <view>我的收藏</view>
    </view>
    <view bindtap="goToMyInfo">
      <image src="/image/my_exp.png" mode="heightFix"></image>
      <view>我的资料</view>
    </view>

  </view>

  <view class="profile_more">
    <view class="profile_more_connact">
      <view>联系我们</view>
      <view>888-888-8888</view>
    </view>
    <view class="profile_more_connact">
      <view>使用反馈</view>
      <image src="/image/right.png" mode="heightFix" bindtap="goToFeeback"></image>
    </view>
    <view class="profile_more_connact">
      <view>关于我们</view>
      <!-- <image src="/image/right.png" mode="heightFix"></image> -->
    </view>
  </view>

  <!-- 注销 -->

  <view class="{{isAuth?'profile_leave':'profile_none'}}"  bindtap="exit">
    <view>退出登录</view>
    <image src="/image/leave.png" mode="widthFix"></image>
  </view>

  <view  class="{{isAuth?'profile_none':'profile_leave'}}" bindtap="goToLogout">
    <view>注册账号</view>
    <image src="/image/register.png" mode="widthFix"></image>
  </view>

</view>

  

posted @ 2022-04-23 16:00  AoYeDDM  阅读(406)  评论(0编辑  收藏  举报