wechat微信小程序panda我的界面(登录、我的基本信息的修改和保存)

运行截图

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

登录

登录功能是通过向后端发起POST数据请求,将表单username和password提交到服务器端,在服务器端对账号密码进行验证,如果无误,创建一个token(有效时长为1小时),在响应数据的时候将其携带返回。将token存入缓存中,以便后续使用。

当用户发送http网络请求的时候,会把token放在请求头Header里面,系统会判断token是否正确,以便判断用户是否合法
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

wxml

<view class="content">
  <!-- <view class="hr"></view> -->
  <image src="../../images/icon/pandaHomeLogo.gif" mode="widthFix" style="width:100%;"></image>
  <form bindsubmit="loginSubmit" bindreset="formReset">
    <view class="bg">
       <view class="item">
          <view class="name">账户</view>
          <view class="value">
            <input type="text" placeholder="手机号/会员名/邮箱" placeholder-class="holder" name="name"/>
          </view>
       </view>
       <view class="line"></view>
       <view class="item">
          <view class="name">密码</view>
          <view class="value">
            <input type="text" password placeholder="请输入密码" placeholder-class="holder" name="password"/>
          </view>
       </view>
    </view>
    <view class="btntwo">
    <button class="btn" form-type="submit" size="default" type="primary">登录</button>
    <button class="rightbtn"  size="default" bindtap="reMePage" type="default">返回</button>
  </view>
  </form>
</view>

wxss

.content{
  font-family: "Microsoft YaHei";
  background-color: #F5F5F5;
  height: 600px;
}
.hr{
  height: 100px;
}
.bg{
  width:100%;
  height: 100px;
  background-color: #ffffff;
}
.item{
  display: flex;
  flex-direction: row;
  height: 50px;
  line-height: 50px;
  align-items: center;
}
.name{
  width:20%;
  margin-left: 10px;
  font-size: 16px;
  font-weight: bold;
}
.value{
  width:80%;
  line-height: 60px;
  margin-left: 10px;
  font-size: 16px;
}
.holder{
  color:#AEAEAE;
  font-size: 16px;
}
.line{
  border:1px solid #cccccc;
  opacity: 0.2;
}

.btntwo{
  display: flex;
}
.btn{
  margin-top:20px;
  margin-left:12%;
  background-color: #FE4E62;
  width: 35%;
  color: #ffffff;
}

.rightbtn{
  margin-right:12%;
  margin-top:20px;
  background-color: #e7b2bf;
  width: 35%;
  color: #7e0707;
}

js

Page({
  data: {
    name: "",
    password: ""
  },

  // 简单从缓存中比对(未调用)
  formSubmit: function (e) {
    var user = e.detail.value;
    var name = user.name;
    var password = user.password;
    var users = wx.getStorageSync("users");
    //  console.log(users)
    //  console.log(name)
    for (var i = 0; i < users.length; i++) {
      if (name == users[i].name && password == users[i].password) {
        wx.setStorageSync("user", user);
        wx.showToast({
          title: '登录成功',
          icon: 'success',
          duration: 1000
        });
        wx.navigateBack({
          delta: 1
        })
        break;
      } else {
        wx.showToast({
          title: '登录失败',
          icon: 'error',
          duration: 1000
        });
      }
    }
  },
// 登录提交表单
  loginSubmit(e) {
    var user = e.detail.value;
    var name = user.name;
    var password = user.password;

    // console.log(name)
    // console.log(password)

    var page = this;
    wx.request({
      // 请求的url
      // http://127.0.0.1:8181/sysUser/login
      // Request Method: POST
      url: 'http://127.0.0.1:8181/sysUser/login',
      data:{"userName":""+name,"password":""+password},
      // 调试用
      // data: {
      //   "userName": "admin",
      //   "password": "123456"
      // },
      method: 'POST',
      header: {
        "Content-Type": "application/json"
      },

      success: function (res) {

        // console.log(res.data.code)
        // res.data.code后端的响应码
        if (res.data.code == 200) {
          wx.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 1000
          });
          // wx.navigateBack({
          //   delta: 1
          // })
        }
        else {
          wx.showToast({
            title: '登录失败',
            icon: 'error',
            duration: 1000
          });
        }

        var userMes = res.data.data
        var sysUser = userMes.sysUser
        var token = userMes.token
        console.log(token)
        console.log(sysUser)
        // 存sysUserInfo
        wx.setStorage({
          key: "sysUser",
          data: sysUser
        })
        // 存token
        wx.setStorage({
          key: "token",
          data: token
        })
        //  wx.navigateBack({
        //     delta: 1
        //   })
        
        
      }
    })
  },
  
  // 返回上一级
  reMePage(){
       wx.navigateBack({
            delta: 1
          })
    }
})

我的基本信息的修改和保存

在加载个人资料界面,进行判断用户是否登录,如果token为空,则表示没有登录;反之,则登录。在登录的时候会把用户的个人信息存入缓存。

在加载个人资料页面,首先从缓存中读取sysUser的数据并回显。

如果用户有更新个人基本信息的需求,便直接修改原来的用户信息,点击保存,把修改的用户信息覆盖原来的sysUser,再发送http请求对后台数据库更新。
在这里插入图片描述
在这里插入图片描述

wxml




<form bindsubmit="saveUserInfo" >
         
         
   <!-- 头像 -->
   <view class="form-item">
    <view class="form-item-hd">头像</view>
    <view class="">
      <image src="http://127.0.0.1:8181/{{userPicture}}" mode="widthFix" bindtap="changeAvatar" style="width: 50px;"></image>
    </view>
   </view>
        <view class="form-item">
            <view class="form-item-hd">用户名</view>
            <view class="form-item-bd">
                <input type="text" name="name" value="{{name}}" placeholder="请输入用户名" maxlength="10"/>
            </view>
        </view>

        <view class="form-item">
            <view class="form-item-hd">邮箱</view>
            <view class="form-item-bd">
                <input type="text" name="email" value="{{email}}" placeholder="请输入邮箱" maxlength="25"/>
            </view>
        </view>
        <view class="form-item">
            <view class="form-item-hd">手机号码</view>
            <view class="form-item-bd">
                <input type="number" name="tel" value="{{tel}}" placeholder="请输入手机号码" maxlength="11"/>
            </view>
        </view>

        <view class="form-item">
        <view class="form-item-hd">性别</view>
            <view class="form-item-bd">
                <picker mode="selector" value="{{genderIndex}}" range="{{genderArray}}" bindchange="changeGender">
                    <block wx:if="{{gender == ''}}">
                        <view class="input input-placeholder">请选择性别</view>
                        <input type="text" name="gender" value="" class="hidden" maxlength="1"/>
                    </block>
                    <block wx:else>
                        <view class="input">{{genderArray[genderIndex]}}</view>
                        <input type="text" name="gender" value="{{genderArray[genderIndex]}}" class="hidden" maxlength="1"/>
                    </block>
                </picker>
            </view>
        </view>
        <view class="form-item">
            <view class="form-item-hd">生日</view>
            <view class="form-item-bd">
                <picker mode="date" value="{{birthday}}" start="1900-01-01" end="{{birthdayEndDate}}" bindchange="changeBirthday">
                    <block wx:if="{{birthday == ''}}">
                        <view class="input input-placeholder">请选择生日</view>
                        <input type="text" name="birthday" value="" class="hidden" maxlength="10"/>
                    </block>
                    <block wx:else>
                        <view class="input">{{birthday}}</view>
                        <input type="text" name="birthday" value="{{birthday}}" class="hidden" maxlength="10"/>
                    </block>
                </picker>
                
            </view>
        </view>
    
    <view class="form-list">
        <view class="form-item">
            <view class="form-item-hd">个性签名</view>
            <view class="form-item-bd">
                <input type="text" name="intro" value="{{intro}}" placeholder="请输入个性签名" maxlength="25"/>
            </view>
        </view>
    </view>
    <!-- <button formType="submit" class="edit-btn"  >保 存</button>
    <button  class="edit-btn">返 回</button> -->

    <view class="btntwo">
    <button class="btn" form-type="submit" size="default" type="primary">保存</button>
    <button class="rightbtn"  size="default" bindtap="reMePage" type="default">返回</button>
  </view>
</form>

wxss

/* pages/modifyUserInfo/modifyUserInfo.wxss */
.form-list {
  position: relative;
  margin-bottom: 20rpx;
}

.form-list:first-child {
  margin-top: 20rpx;
}

.form-list::before, .form-list::after {
  content: '';
  display: block;
  position: absolute;
  left: 0;
  height: 1px;
  width: 100%;
  transform: scaleY(.5);
  background-color: #e3e3e3;
}

.form-list::before {
  top: 0;
}

.form-list::after {
  bottom: 0;
}

.form-item {
  position: relative;
  box-sizing: border-box;
  display: flex;
  width: 100%;
  padding: 15rpx 40rpx;
}

.form-item::before {
  content: '';
  display: block;
  position: absolute;
  z-index: 99;
  left: 0;
  top: 0;
  height: 1px;
  width: 100%;
  transform: scaleY(.5);
  background-color: #e3e3e3;
}

.form-item:first-child::before {
  display: none;
}

.form-item-hd {
  width: 150rpx;
  height: 60rpx;
  line-height: 60rpx;
}

.form-item-bd {
  flex: 1;
  height: 60rpx;
}

input, .input {
  box-sizing: border-box;
  width: 100%;
  height: 60rpx;
  line-height: 60rpx;
  background-color: #fff;
  padding: 0 15rpx;
}

.input-placeholder {
  color: #808080;
}

.edit-btn {
  width: 300rpx;
  height: 80rpx;
  margin: 0 auto;
  font-size: 32rpx;
  text-align: center;
  line-height: 80rpx;
  background-color: #47a86c;
  border-radius: 80rpx;
  color: #fff;
}

.hidden {
  display: none;
}


.btntwo{
  display: flex;
}
.btn{
  margin-top:20px;
  margin-left:12%;
  background-color: #FE4E62;
  width: 35%;
  color: #ffffff;
}

.rightbtn{
  margin-right:12%;
  margin-top:20px;
  background-color: #e7b2bf;
  width: 35%;
  color: #7e0707;
}

js

Page({
  data:{
    name: '',
    genderArray: ['男', '女'],
    gender: 0,
    genderIndex: 0,
    age: 18,
    email:'',
    tel: '',
    birthday: '',
    intro: '',
    // imgUrl:'/images/user/2022/06/20/0831f209fe5e4aebb758404d7b3f41de.jpg',
    userPicture:'/images/user/2022/06/20/0831f209fe5e4aebb758404d7b3f41de.jpg'
  },

  onLoad:function(options){

    // 首先判断一下是否登录
    // 如果没有登录(判断token)
    var token = wx.getStorageSync("token");
    if(token.length == 0){
      // 未登录,跳转界面
      wx.navigateTo({
        url: '../login/login'
      })
      // console.log("1111111111111null")
    }else{
      // 已登录,加载User信息
      var sysUser = wx.getStorageSync("sysUser");
      if(sysUser){
        this.setData({name:sysUser.userName});
        this.setData({email:sysUser.email});
        this.setData({tel:sysUser.phoneNumber});
        this.setData({gender:sysUser.sex});
        this.setData({birthday:sysUser.birthday});
        this.setData({intro:sysUser.autograph});
        // JSON.parse(subject.moviePoster)[0]
        this.setData({userPicture:JSON.parse(sysUser.userPicture)[0]});
      //  this.setData({imgUrl:sysUser.userPicture})
      }
      // console.log(2222222222)
      // console.log(token.length)
    }

  
  },
  changeGender: function(e) {
    console.log(e)
    var genderIndex = e.detail.value
    if (genderIndex != "null") {
      this.setData({
        genderIndex: genderIndex,
        gender: this.data.genderArray[this.data.genderIndex]
      })
    }
  },
  changeBirthday: function(e) {
    var birthday = e.detail.value
    if (birthday != "null") {
      this.setData(
        {birthday: birthday}
      )
    }
  },


  saveUserInfo(e) {
    // console.log(11);
    
    var page = this;
    var token = wx.getStorageSync("token");
    var sysUser = wx.getStorageSync("sysUser");

    // userTemp表单里面的数据
    var userTemp = e.detail.value;
    console.log(e);
    console.log(userTemp);
    sysUser.userName = userTemp.name;
    sysUser.email = userTemp.email;
    sysUser.phoneNumber = userTemp.tel;
    sysUser.birthday = userTemp.birthday;
    sysUser.autograph = userTemp.intro;
    // 赋值性别
    if (userTemp.gender == '男') {
      sysUser.sex = true;
      // console.log("man");
      // console.log(userTemp.gender);
    } else {
      sysUser.sex = false;
      // console.log("woman");
      // console.log(userTemp.gender);
    }
    
    // 覆盖原来的sysUser
    wx.setStorageSync("sysUser",sysUser);   
   console.log(sysUser);

    wx.request({
      // 请求的url
     // http://127.0.0.1:8181/sysUser  PUT
      url: 'http://127.0.0.1:8181/sysUser',
      data:{"userId":sysUser.userId,
      "userName":sysUser.userName,
      "password":sysUser.password,
      "salt":sysUser.salt,
      "email":sysUser.email,
      "phoneNumber":sysUser.phoneNumber,
      "sex":sysUser.sex,
      "userPicture":sysUser.userPicture,
      "roleId":sysUser.roleId,
      "birthday":sysUser.birthday,
      "autograph":sysUser.autograph,
      "sysRole":sysUser.sysRole},
      method: 'PUT',
      header: {
        "Content-Type": "application/json",
        "token":token
      },
      success: function (res) {
        console.log(res)
        console.log(res.data.code)
        // res.data.code后端的响应码
        if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 1000
          });
          // wx.navigateBack({
          //   delta: 1
          // })
        }
        else {
          wx.showToast({
            title: '保存失败',
            icon: 'error',
            duration: 1000
          });
        }

      }
    })
  },

  // 上传图片
  changeAvatar: function() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        // tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        this.setData({
          imgUrl: tempFilePaths
        })
        console.log(tempFilePaths)
      }
    })
  },
  reMePage(){
    wx.navigateBack({
         delta: 1
       })
  },
  

 /**
   * 下拉刷新
   */
  onPullDownRefresh() {
    // 下拉刷新
    var self = this;
    setTimeout(function () {
      wx.hideLoading()
    }, 500)
    wx.showLoading({
      title: '加载中...',
    })
    
    self.onLoad();
    wx.stopPullDownRefresh();
    
  }

})

json

{
  "usingComponents": {},
    "navigationBarTitleText": "编辑个人资料",
    "enablePullDownRefresh": true
}
  • 6
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汪程序猿

就当请我吃顿饭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值