IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> 微信小程序-枯木学习笔记3-登录功能 -> 正文阅读

[移动开发]微信小程序-枯木学习笔记3-登录功能

一、学生登录

1、界面原型

在这里插入图片描述

2、功能需求

1、实现登录页面
2、请求java后台登录学生的信息,返回存储到本地浏览器缓存(h5支持)

3、涉猎的技术点

  • 背景图片,高度100%(样式)
  • 轮播图 swiper组件
  • 输入框:image图标、lable标签、bindinput事件
  • 密码输入框
  • 登录按钮:bindtap事件,wxToast消息提醒,wx.request Ajax请求java后台,返回json数据缓存到本地

二、课前资料

1、数据库学生表

在这里插入图片描述

2、启动java后台项目:http://localhost:8088

在这里插入图片描述

3、图片资源

在这里插入图片描述

三、最终代码

1、app.json

全局配置文件,先添加 index 首页模块,
最后添加对应的页面模块 exam、my 声明,在添加tabBar(首页、考试、我的)

{
  "pages":[
    "pages/index/index",
    "pages/exam/exam",
    "pages/my/my",
    "pages/home/home",
    "pages/logs/logs"
  ],
  "window":{
    "backgroundTextStyle":"light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "Weixin",
    "navigationBarTextStyle":"black"
  },
  "tabBar": {
      "backgroundColor": "#563624",
      "color": "#ffff",
      "list": [
          {
            "pagePath":"pages/home/home",
            "text":"首页",
            "iconPath":"pages/images/Home.png"
          },
          {
              "pagePath": "pages/exam/exam",
              "text": "考试",
              "iconPath": "pages/images/My_exam.png"
          },
          {
              "pagePath": "pages/my/my",
              "text": "我的",
              "iconPath": "pages/images/My.png"
          }
      ]
  },
  "style": "v2",
  "sitemapLocation": "sitemap.json"
}

2、index.wxml

页面布局,代码量大集中在这里


<view class="container" style="background-image: url(/pages/images/mini_login_b_2.jpg);"> 
    <swiper style="height: 450rpx; width: 100%;" autoplay="true" interval="3000" indicator-dots="true" circular="true">
      <swiper-item>
        <image src="/pages/images/login_2.jpg" style="width: 100%; height:100%"></image>
      </swiper-item>
      <swiper-item>
        <image src="/pages/images/login_3.jpg" style="width: 100%; height:100%"></image>
      </swiper-item>
      <swiper-item>
        <image src="/pages/images/login_1.jpg" style="width: 100%; height:100%"></image>
      </swiper-item>
      <swiper-item>
        <image src="/pages/images/login_4.jpg" style="width: 100%; height:100%"></image>
      </swiper-item>
    </swiper>
  
  <view style="width: 100%; height: 2px; background-color: #563624; display: block;"><p style="color: #563624; font-size: 2px;">.</p></view>
  <view class="login-form"> 
  
    <!--账号-->
    <view class="inputView"> 
      <image class="nameImage" src="/pages/images/wx_user.png"></image> 
      <label class="loginLab">账号</label> 
      <input class="inputText" placeholder="请输入账号" bindinput="usernameInput" /> 
    </view> 
    
    <view class="line"></view> 
    
    <!--密码-->
    <view class="inputView"> 
      <image class="keyImage" src="/pages/images/wx_login.png"></image> 
      <label class="loginLab">密码</label> 
      <input class="inputText" password="true" placeholder="请输入密码" bindinput="passwordInput" /> 
    </view> 
    
    <!--按钮-->
    <view class="loginBtnView"> 
      <button class="loginBtn" type="primary" size="{{primarySize}}" loading="{{loading}}" plain="{{plain}}" disabled="{{disabled}}" bindtap="login">登录</button> 
    </view> 

  </view>
  
</view>

3、index.wxss

样式修饰

page{
    height: 100%;
}

.container{
    height: 100%;
    display: flex;
    flex-direction: column;
    padding: 0;
    box-sizing: border-box;
}

.login-img{
    width: 750rpx;
}

.login-form{
    margin-top: 40px;
    flex: auto;
    height: 100%;
}

.inputView{
    line-height: 45px;
    border-radius: 20px;
    border:1px solid #999999;
}

.nameImage, .keyImage{
    margin-left: 22px;
    width: 18px;
    height: 16px;
}

.loginLab {
    margin: 15px;
    font-size: 14px;
}

.inputText{
    flex: block;
    float: right;
    text-align: left;
    margin-right: 22px;
    margin-top: 11px;
}

.line{
    margin-top: 8px;
}

.loginBtnView{
    width: 100%;
    height: auto;
}

.loginBtn{
    width: 90%;
    margin-top: 30px;
    border-radius: 10px;
}

4、app.js

声明全局变量,方便统一配置java后台的链接域名和端口号

// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
  globalData: {
    userInfo: null,
    myhost: "http://localhost:8088"
  }
})

5、index.js

页面输入框事件、按钮登录后请求后台并处理返回数据
涉及到微信的api较多,死记

// index.js
// 获取应用实例
const app = getApp()
const myhost = app.globalData.myhost

Page({
  data: {
   id: '',
   password: ''
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    
  },
  
  usernameInput: function(e){
      this.setData({
          id: e.detail.value
      })
  },
  passwordInput: function(e){
      this.setData({
          password: e.detail.value
      })
  },
  login: function(){
      var that = this;
      if(this.data.id.length==0 || this.data.password.length ==0){
          wx.showToast({
              title: "账号或密码不能为空",
              icon: 'none',
              duration: 2000
          })
      }else{
          console.log('wx.request---'+myhost)
          wx.request({
              url: myhost + '/student/login',
              method: 'post',
              data:{
                  id: that.data.id,
                  password: that.data.password
              },
              success(res){
                  if(res.data == null || res.data == ""){
                      wx.showToast({
                        title: '错误啦',
                        icon: 'error',
                        duration: 2000
                      })
                  }else{
                      wx.setStorageSync('student', res.data);
                      wx.switchTab({
                        url: '/pages/home/home',
                      })
                  }
              },
              fail(err){
                console.log("出错拉"+err)
                  console.log(err)
              }
          })
      }
  }
})

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-06-18 23:30:13  更:2022-06-18 23:30:40 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 16:35:18-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码