• 微信小程序开发的OA会议之首页搭建


    目录

    一、小程序的布局

    1. flex是什么

    2. flex布局

    3. flex优势

    4. 总体布局 

    二、轮播图

    1. 组件

    2. 数据请求 

    3. 页面

    三、首页

    1. 视图

    2. 数据

    3. 样式

    每篇收获


    一、小程序的布局

    1. flex是什么

    在微信小程序中,flex是一种布局方式,用于实现弹性盒子布局。通过使用flex布局,可以方便地实现页面元素的自适应和排列。flex布局主要通过设置父容器的flex属性来控制子元素的布局方式和占据空间的比例。具体来说,可以通过设置父容器的flex-direction属性来指定子元素的排列方向(水平或垂直),通过设置父容器的justify-content属性来指定子元素在主轴上的对齐方式,通过设置父容器的align-items属性来指定子元素在交叉轴上的对齐方式,通过设置子元素的flex属性来指定子元素占据父容器剩余空间的比例等。通过灵活运用flex布局,可以实现页面的自适应和灵活布局。

    2. flex布局

    在微信小程序中,flex布局是一种弹性盒子布局,用于实现页面元素的自适应和排列。通过使用flex布局,可以方便地控制页面元素在水平或垂直方向上的排列方式和占据空间的比例。

     

    在微信小程序中,可以通过设置父容器的`display: flex`来启用flex布局。接下来,可以使用一系列的flex属性来控制子元素的布局方式:

    • - `flex-direction`:指定子元素的排列方向,可以是`row`(水平方向),`column`(垂直方向),`row-reverse`(反向水平方向)或`column-reverse`(反向垂直方向)。
    • - `justify-content`:指定子元素在主轴上的对齐方式,可以是`flex-start`(起始位置对齐),`flex-end`(结束位置对齐),`center`(居中对齐),`space-between`(两端对齐,子元素之间平均分布),`space-around`(子元素周围平均分布)等。
    • - `align-items`:指定子元素在交叉轴上的对齐方式,可以是`flex-start`(起始位置对齐),`flex-end`(结束位置对齐),`center`(居中对齐),`baseline`(基线对齐)等。
    • - `flex-wrap`:指定子元素是否换行,可以是`nowrap`(不换行),`wrap`(换行)或`wrap-reverse`(反向换行)。
    • - `align-content`:指定多行子元素在交叉轴上的对齐方式,只有在有多行时才会生效,可以是`flex-start`(起始位置对齐),`flex-end`(结束位置对齐),`center`(居中对齐),`space-between`(两端对齐,行之间平均分布),`space-around`(行周围平均分布)等。

    通过设置以上属性,可以实现页面元素的自适应和灵活排列,使页面在不同屏幕尺寸和设备上都能良好展示。

    3. flex优势

    使用flex布局在微信小程序中进行页面布局有以下几个优势:

    1. 简单易用:相比传统的盒子模型布局,使用flex布局可以更加简单和直观地控制页面元素的排列和占据空间的比例。

    2. 自适应性强:flex布局可以根据不同的屏幕尺寸和设备进行自适应调整,使页面在不同的设备上都能良好展示。

    3. 灵活性高:使用flex布局可以灵活地控制页面元素的排列方式和占据空间的比例,使页面布局更加灵活多变。

    4. 可读性好:使用flex布局可以使代码结构更加清晰和易读,通过简洁的属性设置,可以清楚地表达页面元素的布局关系。

    5. 兼容性好:flex布局在现代浏览器中得到了广泛的支持,同时微信小程序也对其进行了良好的支持,因此可以放心使用。无需担心在不同平台上的兼容性问题。

    综上所述,使用flex布局可以简化页面布局的操作,提高开发效率,同时实现自适应和灵活的布局效果,使页面在不同的设备上都能良好展示。

    4. 总体布局 

    根据我博客中进行创建一个微信小程序项目: 

    开发微信小程序的下载安装及入门icon-default.png?t=N7T8https://blog.csdn.net/SAME_LOVE/article/details/133826628?spm=1001.2014.3001.5501

    之后在项目中增加三个页面;

    ( 可以根据自己的需求进行更改命名 )

    在项目的主体中找到 app.json 这个文件进行增加以上三个文件。

    在该文件中找到 pages ,在里面进行直接页面。

    1. "pages":[
    2. "pages/index/index",
    3. "pages/meeting/list/list",
    4. "pages/vote/list/list",
    5. "pages/ucenter/index/index",
    6. "pages/logs/logs"
    7. ],

    在进行一级页面的布局,在项目的主体中找到 app.json ,进行编写

    1. "tabBar": {
    2. "list": [{
    3. "pagePath": "pages/index/index",
    4. "text": "首页",
    5. "iconPath": "/static/tabBar/coding.png",
    6. "selectedIconPath": "/static/tabBar/coding-active.png"
    7. },
    8. {
    9. "pagePath": "pages/meeting/list/list",
    10. "iconPath": "/static/tabBar/sdk.png",
    11. "selectedIconPath": "/static/tabBar/sdk-active.png",
    12. "text": "会议"
    13. },
    14. {
    15. "pagePath": "pages/vote/list/list",
    16. "iconPath": "/static/tabBar/template.png",
    17. "selectedIconPath": "/static/tabBar/template-active.png",
    18. "text": "投票"
    19. },
    20. {
    21. "pagePath": "pages/ucenter/index/index",
    22. "iconPath": "/static/tabBar/component.png",
    23. "selectedIconPath": "/static/tabBar/component-active.png",
    24. "text": "设置"
    25. }]
    26. },

    在项目的本地路径中,创建一个静态资源文件夹 ( static )

    在静态资源文件夹 ( static )中创建一个放首页轮播图图片的文件夹 : tabBar

    至于什么样的图片 : 可以自己在本地路径中进行增加 。

    pagePath :  填写页面的地址

    iconPath  :  存放未选中的图片 

    selectedIconPath  :  存放已选中的图片 

    text   :   填写标题

    在模拟器中可以看到的效果 : 

    二、轮播图

    1. 组件

    打开调试器,在调试器中选中Mock,点击加号,进行增加一个API信息,如图 :

    API的信息,如图填写即可 : 

    JSON的数据包

    1. {
    2. "data": {
    3. "images":[
    4. {
    5. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner1.png",
    6. "text": "1"
    7. },
    8. {
    9. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner2.png",
    10. "text": "2"
    11. },
    12. {
    13. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner3.png",
    14. "text": "3"
    15. },
    16. {
    17. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner4.png",
    18. "text": "4"
    19. },
    20. {
    21. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner5.png",
    22. "text": "5"
    23. },
    24. {
    25. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner6.png",
    26. "text": "6"
    27. }
    28. ]
    29. },
    30. "statusCode": "200",
    31. "header": {
    32. "content-type":"applicaiton/json;charset=utf-8"
    33. }
    34. }

    然后选择勾选进行使用,如图:

    2. 数据请求 

    在项目的本地路径中,创建一个资源文件夹 ( config)

    在去开发工具中,在中共文件夹中创建一个 app.js 文件,进行代码编写 :

    1. // 以下是业务服务器API地址
    2. // 本机开发API地址
    3. var WxApiRoot = 'http://localhost:8080/demo/wx/';
    4. // 测试环境部署api地址
    5. // var WxApiRoot = 'http://192.168.0.101:8070/demo/wx/';
    6. // 线上平台api地址
    7. //var WxApiRoot = 'https://www.oa-mini.com/demo/wx/';
    8. module.exports = {
    9. IndexUrl: WxApiRoot + 'home/index', //首页数据接口
    10. SwiperImgs: WxApiRoot+'swiperImgs', //轮播图
    11. MettingInfos: WxApiRoot+'meeting/list', //会议信息
    12. };

    3. 页面

    在 index.css 编写样式 

    1. page{
    2. height: 100%;
    3. background-color: #efeff4;
    4. }

    index.wxml  中的代码全部修改为一下代码:

    1. <view>
    2. <swiper autoplay="true" indicator-dots="true" indicator-color="#fff" indicator-active-color="#00f">
    3. <block wx:for="{{imgSrcs}}" wx:key="text">
    4. <swiper-item>
    5. <view>
    6. <image src="{{item.img}}" class="swiper-item" />
    7. view>
    8. swiper-item>
    9. block>
    10. swiper>
    11. view>

    在 index.css 增加以下样式 

    1. .swiper-item {
    2. height: 300rpx;
    3. width: 100%;
    4. border-radius: 10rpx;
    5. }

    index.js  中将全部代码修改为以下代码 :

    1. // index.js
    2. // 获取应用实例
    3. const app = getApp()
    4. const api = require("../../config/app")
    5. Page({
    6. data: {
    7. },
    8. // 事件处理函数
    9. loadSwiperImgs(){
    10. let that=this;
    11. wx.request({
    12. url: api.SwiperImgs,
    13. dataType: 'json',
    14. success(res) {
    15. console.log(res)
    16. that.setData({
    17. imgSrcs:res.data.images
    18. })
    19. }
    20. })
    21. },
    22. onLoad() {
    23. if (wx.getUserProfile) {
    24. this.setData({
    25. canIUseGetUserProfile: true
    26. })
    27. }
    28. this.loadSwiperImgs();
    29. },
    30. getUserProfile(e) {
    31. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
    32. wx.getUserProfile({
    33. desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
    34. success: (res) => {
    35. console.log(res)
    36. this.setData({
    37. userInfo: res.userInfo,
    38. hasUserInfo: true
    39. })
    40. }
    41. })
    42. },
    43. getUserInfo(e) {
    44. // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
    45. console.log(e)
    46. this.setData({
    47. userInfo: e.detail.userInfo,
    48. hasUserInfo: true
    49. })
    50. }
    51. })

    在模拟器中可以看到的效果 :  

    三、首页

    在静态资源文件夹 ( static ) 中创建一个放首页会议用户头像图片的文件夹 : persons

    需要怎样的头像图片,需要自行增加到其中。

    注 : 需要在项目的本地路径下进行创建和增加

    1. 视图

    找到 index.wxml  将所有代码修改为以下代码 : 

    1. <view>
    2. <swiper autoplay="true" indicator-dots="true" indicator-color="#fff" indicator-active-color="#00f">
    3. <block wx:for="{{imgSrcs}}" wx:key="text">
    4. <swiper-item>
    5. <view>
    6. <image src="{{item.img}}" class="swiper-item" />
    7. view>
    8. swiper-item>
    9. block>
    10. swiper>
    11. view>
    12. <view class="mobi-title">
    13. <text class="mobi-icon">text>
    14. <text>会议信息text>
    15. view>
    16. <block wx:for-items="{{lists}}" wx:for-item="item" wx:key="item.id">
    17. <view class="list" data-id="{{item.id}}">
    18. <view class="list-img">
    19. <image class="video-img" mode="scaleToFill" src="{{item.image}}">image>
    20. view>
    21. <view class="list-detail">
    22. <view class="list-title"><text>{{item.title}}text>view>
    23. <view class="list-tag">
    24. <view class="state">{{item.state}}view>
    25. <view class="join"><text class="list-num">{{item.num}} text> 人报名view>
    26. view>
    27. <view class="list-info"><text>{{item.location}}text> | <text>{{item.starttime}}text>view>
    28. view>
    29. view>
    30. block>
    31. <view class="section bottom-line">
    32. <text>到底啦text>
    33. view>

    2. 数据

    找到 index.js 将所有代码修改为以下代码 : 

    1. // index.js
    2. // 获取应用实例
    3. const app = getApp()
    4. const api = require("../../config/app")
    5. Page({
    6. //初始化数据
    7. data: {
    8. "lists": [
    9. {
    10. "id": "1",
    11. "image": "/static/persons/8.jpg",
    12. "title": "对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】",
    13. "num":"304",
    14. "state":"进行中",
    15. "starttime": "2022-03-13 00:00:00",
    16. "location": "深圳市·南山区"
    17. },
    18. {
    19. "id": "1",
    20. "image": "/static/persons/13.jpg",
    21. "title": "AI WORLD 2016世界人工智能大会",
    22. "num":"380",
    23. "state":"已结束",
    24. "starttime": "2022-03-15 00:00:00",
    25. "location": "北京市·朝阳区"
    26. },
    27. {
    28. "id": "1",
    29. "image": "/static/persons/14.jpg",
    30. "title": "H100太空商业大会",
    31. "num":"500",
    32. "state":"进行中",
    33. "starttime": "2022-03-13 00:00:00",
    34. "location": "大连市"
    35. },
    36. {
    37. "id": "1",
    38. "image": "/static/persons/15.gif",
    39. "title": "报名年度盛事,大咖云集!2016凤凰国际论坛邀您“与世界对话”",
    40. "num":"150",
    41. "state":"已结束",
    42. "starttime": "2022-03-13 00:00:00",
    43. "location": "北京市·朝阳区"
    44. },
    45. {
    46. "id": "1",
    47. "image": "/static/persons/16.jpg",
    48. "title": "新质生活 · 品质时代 2016消费升级创新大会",
    49. "num":"217",
    50. "state":"进行中",
    51. "starttime": "2022-03-13 00:00:00",
    52. "location": "北京市·朝阳区"
    53. }
    54. ]
    55. },"statusCode": "200",
    56. "header": {
    57. "content-type":"applicaiton/json;charset=utf-8"
    58. },
    59. // 事件处理函数
    60. // 获取轮播图的方法
    61. loadSwiperImgs(){
    62. let that=this;
    63. wx.request({
    64. url: api.SwiperImgs,
    65. dataType: 'json',
    66. success(res) {
    67. console.log(res)
    68. that.setData({
    69. imgSrcs:res.data.images
    70. })
    71. }
    72. })
    73. },
    74. // 获取首页会议信息的方法
    75. loadMeetingInfos(){
    76. let that=this;
    77. wx.request({
    78. url: api.MettingInfos,
    79. dataType: 'json',
    80. success(res) {
    81. console.log(res)
    82. that.setData({
    83. lists:res.data.lists
    84. })
    85. }
    86. })
    87. },
    88. onLoad() {
    89. if (wx.getUserProfile) {
    90. this.setData({
    91. canIUseGetUserProfile: true
    92. })
    93. }
    94. this.loadSwiperImgs();
    95. },
    96. getUserProfile(e) {
    97. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
    98. wx.getUserProfile({
    99. desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
    100. success: (res) => {
    101. console.log(res)
    102. this.setData({
    103. userInfo: res.userInfo,
    104. hasUserInfo: true
    105. })
    106. }
    107. })
    108. },
    109. getUserInfo(e) {
    110. // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
    111. console.log(e)
    112. this.setData({
    113. userInfo: e.detail.userInfo,
    114. hasUserInfo: true
    115. })
    116. }
    117. })

    3. 样式

     index.wxss 中编写样式,进行美化页面,以下是样式的所有代码 :

    1. /**index.wxss**/
    2. .swiper-item {
    3. height: 300rpx;
    4. width: 100%;
    5. border-radius: 10rpx;
    6. }
    7. .mobi-title {
    8. font-size: 12pt;
    9. color: #777;
    10. line-height: 110%;
    11. font-weight: bold;
    12. width: 100%;
    13. padding: 15rpx;
    14. background-color: #f3f3f3;
    15. }
    16. .mobi-icon {
    17. padding: 0rpx 3rpx;
    18. border-radius: 3rpx;
    19. background-color: #ff7777;
    20. position: relative;
    21. margin-right: 10rpx;
    22. }
    23. /*list*/
    24. .list {
    25. display: flex;
    26. flex-direction: row;
    27. width: 100%;
    28. padding: 0 20rpx 0 0;
    29. border-top: 1px solid #eeeeee;
    30. background-color: #fff;
    31. margin-bottom: 5rpx;
    32. /* border-radius: 20rpx;
    33. box-shadow: 0px 0px 10px 6px rgba(0,0,0,0.1); */
    34. }
    35. .list-img {
    36. display: flex;
    37. margin: 10rpx 10rpx;
    38. width: 150rpx;
    39. height: 220rpx;
    40. justify-content: center;
    41. align-items: center;
    42. }
    43. .list-img .video-img {
    44. width: 120rpx;
    45. height: 120rpx;
    46. }
    47. .list-detail {
    48. margin: 10rpx 10rpx;
    49. display: flex;
    50. flex-direction: column;
    51. width: 600rpx;
    52. height: 220rpx;
    53. }
    54. .list-title text {
    55. font-size: 11pt;
    56. color: #333;
    57. font-weight: bold;
    58. }
    59. .list-detail .list-tag {
    60. display: flex;
    61. height: 70rpx;
    62. }
    63. .list-tag .state {
    64. font-size: 9pt;
    65. color: #81aaf7;
    66. width: 120rpx;
    67. border: 1px solid #93b9ff;
    68. border-radius: 2px;
    69. margin: 10rpx 0rpx;
    70. display: flex;
    71. justify-content: center;
    72. align-items: center;
    73. }
    74. .list-tag .join {
    75. font-size: 11pt;
    76. color: #bbb;
    77. margin-left: 20rpx;
    78. display: flex;
    79. justify-content: center;
    80. align-items: center;
    81. }
    82. .list-tag .list-num {
    83. font-size: 11pt;
    84. color: #ff6666;
    85. }
    86. .list-info {
    87. font-size: 9pt;
    88. color: #bbb;
    89. margin-top: 20rpx;
    90. }
    91. .bottom-line{
    92. display: flex;
    93. height: 60rpx;
    94. justify-content: center;
    95. align-items: center;
    96. background-color: #f3f3f3;
    97. }
    98. .bottom-line text{
    99. font-size: 9pt;
    100. color: #666;
    101. }

    在模拟器中可以看到的效果 :  

    每篇收获

    学习和使用flex布局及完成首页搭建后可以带来以下收获:

    1. 掌握一种现代化的页面布局技术:flex布局是一种现代化的页面布局技术,使用它可以实现灵活、自适应的页面布局效果。学习和掌握flex布局,可以使你在前端开发中拥有更多的布局选择和技术实现方式。

    2. 提高开发效率:相比传统的盒子模型布局,flex布局更加简单易用,通过设置几个属性即可实现复杂的布局效果。这种简洁的布局方式可以提高开发效率,减少开发时间和代码量。

    3. 适应不同设备和屏幕尺寸:flex布局具有自适应性,可以根据不同的设备和屏幕尺寸进行布局调整,使页面在不同的设备上都能良好展示。这使得你的页面可以适应不同的终端和设备,提供更好的用户体验。

    4. 提升代码可读性和维护性:使用flex布局可以使代码结构更加清晰和易读,通过简洁的属性设置,可以清楚地表达页面元素的布局关系。这使得代码更易于理解和维护,减少后期的调试和修改工作。

    总之,学习和使用flex布局可以带来更灵活、高效、自适应的页面布局效果,提高开发效率,同时提升代码可读性和维护性。这些技能和经验对于前端开发者来说是非常有价值的。

  • 相关阅读:
    创建、检查和反编译世界上(几乎)最短的 C# 程序
    OpenGLES:绘制一个颜色渐变的圆
    软件测试基础——缺陷报告全教程,建议转发收藏
    国内镜像源 centos7 安装 MySQL8.0,并配置远程登录
    Java应用程序性能分析与调优实践
    element-ui el-tabs el-tab-pane 的使用
    MMORPG网络游戏开发之Protobuf的基本使用
    iOS15.6 正式版终于发布,适合“养老”的版本来了
    微软:今天起加速推广Windows 11,让更多设备免费升级
    React + TypeScript + Taro前端开发小结
  • 原文地址:https://blog.csdn.net/SAME_LOVE/article/details/133886706