• 微信小程序 —— 会议OA项目首页布局与Mock数据交互


    14天阅读挑战赛
    如果世界上有奇迹,那一定是努力的另一个名字。

    目录

    一、小程序布局

    1.1 Flex布局

    1.2 Flex属性

     

    二、OA会议首页搭建

    2.1 首页底部菜单

    2.2 创建后端结口

    2.3 Mock模拟数据

    2.4 首页轮播图搭建

    2.5 首页内容搭建 


    一、小程序布局

    1.1 Flex布局

    布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性

    • Flex是Flexible Box的缩写,意为”弹性布局”,用来为盒状模型提供最大的灵活性。
    • 任何一个容器都可以指定为Flex布局。
    • display: ‘flex’

      

    容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;交叉轴的开始位置叫做cross start,结束位置叫做cross end。

            项目默认沿主轴排列。单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size。

    1.2 Flex属性

    • flex-direction 主轴的方向 默认为row
    • flex-wrap 如果一条轴线排不下,如何换行
    • flex-flow 是flex-direction属性和flex-wrap属性的简写形式
    • justify-content 定义了项目在主轴上的对齐方式
    • align-items 定义项目在交叉轴上如何对齐
    • align-content 属性定义了多根轴线的对齐方式

    注意,设为Flex布局以后,子元素的float、clear和vertical-align属性将失效。


    更多详情:Flex 布局语法教程 | 菜鸟教程

    二、OA会议首页搭建

    2.1 首页底部菜单

    在该项目资源管理器创建以下路径,用来存放图标:

    /static/tabBar/coding-active.png

    创建新的小程序项目之后,在app.json里面pages新建页面和绑定tabBer

    1. {
    2. "pages": [
    3. "pages/index/index",
    4. "pages/meeting/list/list",
    5. "pages/vote/list/list",
    6. "pages/ucenter/index/index",
    7. "pages/logs/logs"
    8. ],
    9. "window": {
    10. "backgroundTextStyle": "light",
    11. "navigationBarBackgroundColor": "#fff",
    12. "navigationBarTitleText": "Weixin",
    13. "navigationBarTextStyle": "black"
    14. },
    15. "tabBar": {
    16. "list": [
    17. {
    18. "pagePath": "pages/index/index",
    19. "text": "首页",
    20. "iconPath": "/static/tabBar/coding.png",
    21. "selectedIconPath": "/static/tabBar/coding-active.png"
    22. },
    23. {
    24. "pagePath": "pages/meeting/list/list",
    25. "iconPath": "/static/tabBar/sdk.png",
    26. "selectedIconPath": "/static/tabBar/sdk-active.png",
    27. "text": "会议"
    28. },
    29. {
    30. "pagePath": "pages/vote/list/list",
    31. "iconPath": "/static/tabBar/template.png",
    32. "selectedIconPath": "/static/tabBar/template-active.png",
    33. "text": "投票"
    34. },
    35. {
    36. "pagePath": "pages/ucenter/index/index",
    37. "iconPath": "/static/tabBar/component.png",
    38. "selectedIconPath": "/static/tabBar/component-active.png",
    39. "text": "设置"
    40. }
    41. ]
    42. },
    43. "style": "v2",
    44. "sitemapLocation": "sitemap.json"
    45. }

    2.2 创建后端结口

    在该项目资源管理器创建config/api.js文件

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

    2.3 Mock模拟数据

    本次没有连接后端,利用小程序Mock模拟假数据。

    打开调试器,按照一下示例进行操作

    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.4 首页轮播图搭建

    首先打开不校验合法域名设置:

    编写轮播图页面:

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

    编写js

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

    效果图:

    2.5 首页内容搭建 

     1. 利用Mack模拟数据

    JSON数据包:

    1. {
    2. "data": {
    3. "lists": [
    4. {
    5. "id": "1",
    6. "image": "/static/persons/1.jpg",
    7. "title": "对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】",
    8. "num":"304",
    9. "state":"进行中",
    10. "starttime": "2022-03-13 00:00:00",
    11. "location": "深圳市·南山区"
    12. },
    13. {
    14. "id": "1",
    15. "image": "/static/persons/2.jpg",
    16. "title": "AI WORLD 2016世界人工智能大会",
    17. "num":"380",
    18. "state":"已结束",
    19. "starttime": "2022-03-15 00:00:00",
    20. "location": "北京市·朝阳区"
    21. },
    22. {
    23. "id": "1",
    24. "image": "/static/persons/3.jpg",
    25. "title": "H100太空商业大会",
    26. "num":"500",
    27. "state":"进行中",
    28. "starttime": "2022-03-13 00:00:00",
    29. "location": "大连市"
    30. },
    31. {
    32. "id": "1",
    33. "image": "/static/persons/4.jpg",
    34. "title": "报名年度盛事,大咖云集!2016凤凰国际论坛邀您“与世界对话”",
    35. "num":"150",
    36. "state":"已结束",
    37. "starttime": "2022-03-13 00:00:00",
    38. "location": "北京市·朝阳区"
    39. },
    40. {
    41. "id": "1",
    42. "image": "/static/persons/5.jpg",
    43. "title": "新质生活 · 品质时代 2016消费升级创新大会",
    44. "num":"217",
    45. "state":"进行中",
    46. "starttime": "2022-03-13 00:00:00",
    47. "location": "北京市·朝阳区"
    48. }
    49. ]
    50. },
    51. "statusCode": "200",
    52. "header": {
    53. "content-type":"applicaiton/json;charset=utf-8"
    54. }
    55. }

    2. 添加js方法

    1. index.js
    2. //首页会议信息的ajax
    3. loadMeetingInfos() {
    4. let that = this;
    5. wx.request({
    6. url: api.MettingInfos,
    7. dataType: 'json',
    8. success(res) {
    9. console.log(res),
    10. that.setData({
    11. lists: res.data.lists
    12. })
    13. }
    14. })
    15. },

    3. 首页界面:

    1. <view class="indexbg">
    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 class="mobi-title">
    12. <text class="mobi-text">会议信息text>
    13. view>
    14. <block wx:for-items="{{lists}}" wx:for-item="item" wx:key="item.id" class="bg">
    15. <view class="list" data-id="{{item.id}}">
    16. <view class="list-img">
    17. <image class="video-img" mode="scaleToFill" src="{{item.image}}">image>
    18. view>
    19. <view class="list-detail">
    20. <view class="list-title"><text>{{item.title}}text>view>
    21. <view class="list-tag">
    22. <view class="state">{{item.state}}view>
    23. <view class="join"><text class="list-num">{{item.num}}text>人报名view>
    24. view>
    25. <view class="list-info"><text>{{item.location}}text>|<text>{{item.starttime}}text>view>
    26. view>
    27. view>
    28. block>
    29. <view class="section">
    30. <text>到底啦text>
    31. view>
    32. view>

    4. 界面样式:

    1. /**index.wxss**/
    2. .userinfo {
    3. display: flex;
    4. flex-direction: column;
    5. align-items: center;
    6. color: #aaa;
    7. }
    8. .userinfo-avatar {
    9. overflow: hidden;
    10. width: 128rpx;
    11. height: 128rpx;
    12. margin: 20rpx;
    13. border-radius: 50%;
    14. }
    15. .usermotto {
    16. margin-top: 200px;
    17. }
    18. /**index.wxss**/
    19. .section {
    20. color: #aaa;
    21. display: flex;
    22. justify-content: center;
    23. }
    24. .list-info {
    25. color: #aaa;
    26. }
    27. .list-num {
    28. color: red;
    29. /* font-weight: 700; */
    30. }
    31. .join {
    32. padding: 0px 0px 0px 10px;
    33. color: #aaa;
    34. }
    35. .state {
    36. margin: 0px 6px 0px 6px;
    37. border: 1px solid #4083ff;
    38. color: #4083ff;
    39. padding: 3px 5px 3px 5px;
    40. }
    41. .list-tag {
    42. padding: 3px 0px 10px 0px;
    43. display: flex;
    44. align-items: center;
    45. }
    46. .list-title {
    47. display: flex;
    48. justify-content: space-between;
    49. font-size: 11pt;
    50. color: #333;
    51. font-weight: bold;
    52. }
    53. .list-detail {
    54. display: flex;
    55. flex-direction: column;
    56. margin: 0px 0px 0px 15px;
    57. }
    58. .video-img {
    59. margin-top: 5px;
    60. width: 80px;
    61. height: 80px;
    62. }
    63. .list {
    64. display: flex;
    65. flex-direction: row;
    66. background-color: seashell;
    67. border-bottom: 1px solid #cecece;
    68. padding: 10px;
    69. }
    70. .mobi-text {
    71. font-weight: 700;
    72. padding: 15px;
    73. }
    74. /* .mobi-icon {
    75. border-left: 5px solid #57f564;
    76. } */
    77. .indexbg{
    78. background-color: rgba(219, 219, 219, 0.678);
    79. }
    80. .mobi-title {
    81. /* background-color: rgba(219, 219, 219, 0.678); */
    82. margin: 10px 0px 10px 0px;
    83. }
    84. .swiper-item {
    85. height: 300rpx;
    86. width: 100%;
    87. border-radius: 10rpx;
    88. }
    89. .userinfo {
    90. display: flex;
    91. flex-direction: column;
    92. align-items: center;
    93. color: #aaa;
    94. }
    95. .userinfo-avatar {
    96. overflow: hidden;
    97. width: 128rpx;
    98. height: 128rpx;
    99. margin: 20rpx;
    100. border-radius: 50%;
    101. }
    102. .usermotto {
    103. margin-top: 200px;
    104. }

    5. 效果展示

  • 相关阅读:
    windows用脚本编译qt的项目
    java编程基础总结——22. Queue接口及其实现子类
    疫情后时代变了吗?
    使用Pyarmor保护Python脚本不被反向工程
    第三章 常用布局
    Linux shell编程学习笔记7:只读变量
    MySQL学习笔记(十二)锁
    【华为机试真题 JAVA】最长子字符串的长度-100
    什么是SFTP
    搭建的maven仓库如何在本地配置maven下载仓库以及仓库下载优先级
  • 原文地址:https://blog.csdn.net/Justw320/article/details/133873102