• 微信小程序之后台首页交互


    目录

    一.与后台数据进行交互&request封装

    后台准备

    测试结果

    ​编辑 

     前端

     测试结果

     二.wxs的介绍以及入门 

    测试结果


    一.与后台数据进行交互&request封装

    后台准备

    pom.xml文件编写

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.6.2version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.zkinggroupId>
    12. <artifactId>minoaartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>minoaname>
    15. <description>Demo project for Spring Bootdescription>
    16. <properties>
    17. <java.version>1.8java.version>
    18. <fastjson.version>1.2.70fastjson.version>
    19. <jackson.version>2.9.8jackson.version>
    20. properties>
    21. <dependencies>
    22. <dependency>
    23. <groupId>org.springframework.bootgroupId>
    24. <artifactId>spring-boot-starter-jdbcartifactId>
    25. dependency>
    26. <dependency>
    27. <groupId>org.springframework.bootgroupId>
    28. <artifactId>spring-boot-starter-webartifactId>
    29. dependency>
    30. <dependency>
    31. <groupId>org.mybatis.spring.bootgroupId>
    32. <artifactId>mybatis-spring-boot-starterartifactId>
    33. <version>2.2.1version>
    34. dependency>
    35. <dependency>
    36. <groupId>mysqlgroupId>
    37. <artifactId>mysql-connector-javaartifactId>
    38. <version>5.1.44version>
    39. <scope>runtimescope>
    40. dependency>
    41. <dependency>
    42. <groupId>org.projectlombokgroupId>
    43. <artifactId>lombokartifactId>
    44. <optional>trueoptional>
    45. dependency>
    46. <dependency>
    47. <groupId>com.alibabagroupId>
    48. <artifactId>fastjsonartifactId>
    49. <version>${fastjson.version}version>
    50. dependency>
    51. dependencies>
    52. <build>
    53. <plugins>
    54. <plugin>
    55. <groupId>org.springframework.bootgroupId>
    56. <artifactId>spring-boot-maven-pluginartifactId>
    57. <configuration>
    58. <excludes>
    59. <exclude>
    60. <groupId>org.projectlombokgroupId>
    61. <artifactId>lombokartifactId>
    62. exclude>
    63. excludes>
    64. configuration>
    65. plugin>
    66. <plugin>
    67. <groupId>org.mybatis.generatorgroupId>
    68. <artifactId>mybatis-generator-maven-pluginartifactId>
    69. <version>1.3.2version>
    70. <dependencies>
    71. <dependency>
    72. <groupId>mysqlgroupId>
    73. <artifactId>mysql-connector-javaartifactId>
    74. <version>${mysql.version}version>
    75. dependency>
    76. dependencies>
    77. <configuration>
    78. <overwrite>trueoverwrite>
    79. configuration>
    80. plugin>
    81. plugins>
    82. build>
    83. project>

    建立数据表

    建立数据请求地址类 

    1. package com.zking.minoa.wxcontroller;
    2. import com.zking.minoa.mapper.InfoMapper;
    3. import com.zking.minoa.model.Info;
    4. import com.zking.minoa.util.ResponseUtil;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.web.bind.annotation.RequestMapping;
    7. import org.springframework.web.bind.annotation.RestController;
    8. import java.util.HashMap;
    9. import java.util.List;
    10. import java.util.Map;
    11. /**
    12. * @Autho donkee
    13. * @Since 2022/6/29
    14. */
    15. @RestController
    16. @RequestMapping("/wx/home")
    17. public class WxHomeController {
    18. @Autowired
    19. private InfoMapper infoMapper;
    20. @RequestMapping("/index")
    21. public Object index(Info info) {
    22. List<Info> infoList = infoMapper.list(info);
    23. Map<Object, Object> data = new HashMap<Object, Object>();
    24. data.put("infoList",infoList);
    25. return ResponseUtil.ok(data);
    26. }
    27. }

    定义接口类 

    1. package com.zking.minoa;
    2. import org.mybatis.spring.annotation.MapperScan;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. @MapperScan("com.zking.minoa.mapper") //指mapper接口所在包
    6. @SpringBootApplication
    7. public class MinoaApplication {
    8. public static void main(String[] args) {
    9. SpringApplication.run(MinoaApplication.class, args);
    10. }
    11. }
    测试结果
     
     前端

    先关闭mock

     

     先编写url地址

     编写utils.js

    1. const formatTime = date => {
    2. const year = date.getFullYear()
    3. const month = date.getMonth() + 1
    4. const day = date.getDate()
    5. const hour = date.getHours()
    6. const minute = date.getMinutes()
    7. const second = date.getSeconds()
    8. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
    9. }
    10. const formatNumber = n => {
    11. n = n.toString()
    12. return n[1] ? n : `0${n}`
    13. }
    14. /**
    15. * 封装微信的request请求
    16. */
    17. function request(url, data = {}, method = "GET") {
    18. return new Promise(function (resolve, reject) {
    19. wx.request({
    20. url: url,
    21. data: data,
    22. method: method,
    23. header: {
    24. 'Content-Type': 'application/json',
    25. },
    26. success: function (res) {
    27. if (res.statusCode == 200) {
    28. resolve(res.data);//会把进行中改变成已成功
    29. } else {
    30. reject(res.errMsg);//会把进行中改变成已失败
    31. }
    32. },
    33. fail: function (err) {
    34. reject(err)
    35. }
    36. })
    37. });
    38. }
    39. module.exports = {
    40. formatTime,request
    41. }

    编写index.js 

    1. // index.js
    2. // 获取应用实例
    3. const app = getApp()
    4. const api=require("../../config/app.js")
    5. const util=require("../../utils/util.js")
    6. Page({
    7. data: {
    8. imgSrcs:[
    9. {
    10. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner1.png",
    11. "text": "1"
    12. },
    13. {
    14. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner2.png",
    15. "text": "2"
    16. },
    17. {
    18. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner3.png",
    19. "text": "3"
    20. },
    21. {
    22. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner4.png",
    23. "text": "4"
    24. },
    25. {
    26. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner5.png",
    27. "text": "5"
    28. },
    29. {
    30. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner6.png",
    31. "text": "6"
    32. }
    33. ],
    34. lists:[
    35. ]
    36. },
    37. // 事件处理函数
    38. bindViewTap() {
    39. wx.navigateTo({
    40. url: '../logs/logs'
    41. })
    42. },
    43. loadMeetInfos(){
    44. util.request(api.IndexUrl).then(res=>{
    45. console.log(res);
    46. this.setData({
    47. lists:res.data.infoList
    48. })
    49. });
    50. // let that=this;
    51. // wx.request({
    52. // url: api.IndexUrl,
    53. // dataType: 'json',
    54. // success(res) {
    55. // console.log(res)
    56. // that.setData({
    57. // lists:res.data.data.infoList
    58. // })
    59. // }
    60. // })
    61. },
    62. loadSwiperImgs(){
    63. let that=this;
    64. // wx.request({
    65. // url: api.SwiperImgs,
    66. // dataType: 'json',
    67. // success(res) {
    68. // console.log(res)
    69. // that.setData({
    70. // imgSrcs:res.data.images
    71. // })
    72. // }
    73. // })
    74. },
    75. onLoad() {
    76. if (wx.getUserProfile) {
    77. this.setData({
    78. canIUseGetUserProfile: true
    79. })
    80. }
    81. this.loadSwiperImgs();
    82. this.loadMeetInfos();
    83. },
    84. getUserProfile(e) {
    85. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
    86. wx.getUserProfile({
    87. desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
    88. success: (res) => {
    89. console.log(res)
    90. this.setData({
    91. userInfo: res.userInfo,
    92. hasUserInfo: true
    93. })
    94. }
    95. })
    96. },
    97. getUserInfo(e) {
    98. // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
    99. console.log(e)
    100. this.setData({
    101. userInfo: e.detail.userInfo,
    102. hasUserInfo: true
    103. })
    104. }
    105. })

     编写index.wxml

     

     测试结果

     二.wxs的介绍以及入门 

    WXS 代码可以编写在 wxml 文件中的 标签内,或以 .wxs 为后缀名的文件内。

    模块
    每一个 .wxs 文件和 标签都是一个单独的模块。

    每个模块都有自己独立的作用域。即在一个模块里面定义的变量与函数,默认为私有的,对其他模块不可见。

    一个模块要想对外暴露其内部的私有变量与函数,只能通过 module.exports 实现。

    .wxs 文件
    在微信开发者工具里面,右键可以直接创建 .wxs 文件,在其中直接编写 WXS 脚本。
     

    在utils里面建立一个文件夹,在文件夹建立wxs文件

     

    wxs文件编写 

    1. // /pages/comm.wxs
    2. function getStateName(state){
    3. if(state ==1){
    4. return "待审核"
    5. }else if(state ==2){
    6. return "审核通过"
    7. }else if(state ==3){
    8. return "审核不通过"
    9. }else if(state ==4){
    10. return "待开会议"
    11. }
    12. return "其他";
    13. }
    14. function getNum(canyuze,liexize,zhuchiren){
    15. var person = canyuze+ ","+liexize+","+zhuchiren;
    16. return person.split(",").length;
    17. }
    18. function formatDate(ts, option) {
    19. var date = getDate(ts)
    20. var year = date.getFullYear()
    21. var month = date.getMonth() + 1
    22. var day = date.getDate()
    23. var week = date.getDay()
    24. var hour = date.getHours()
    25. var minute = date.getMinutes()
    26. var second = date.getSeconds()
    27. //获取 年月日
    28. if (option == 'YY-MM-DD') return [year, month, day].map(formatNumber).join('-')
    29. //获取 年月
    30. if (option == 'YY-MM') return [year, month].map(formatNumber).join('-')
    31. //获取 年
    32. if (option == 'YY') return [year].map(formatNumber).toString()
    33. //获取 月
    34. if (option == 'MM') return [mont].map(formatNumber).toString()
    35. //获取 日
    36. if (option == 'DD') return [day].map(formatNumber).toString()
    37. //获取 年月日 周一 至 周日
    38. if (option == 'YY-MM-DD Week') return [year, month, day].map(formatNumber).join('-') + ' ' + getWeek(week)
    39. //获取 月日 周一 至 周日
    40. if (option == 'MM-DD Week') return [month, day].map(formatNumber).join('-') + ' ' + getWeek(week)
    41. //获取 周一 至 周日
    42. if (option == 'Week') return getWeek(week)
    43. //获取 时分秒
    44. if (option == 'hh-mm-ss') return [hour, minute, second].map(formatNumber).join(':')
    45. //获取 时分
    46. if (option == 'hh-mm') return [hour, minute].map(formatNumber).join(':')
    47. //获取 分秒
    48. if (option == 'mm-dd') return [minute, second].map(formatNumber).join(':')
    49. //获取 时
    50. if (option == 'hh') return [hour].map(formatNumber).toString()
    51. //获取 分
    52. if (option == 'mm') return [minute].map(formatNumber).toString()
    53. //获取 秒
    54. if (option == 'ss') return [second].map(formatNumber).toString()
    55. //默认 时分秒 年月日
    56. return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
    57. }
    58. function formatNumber(n) {
    59. n = n.toString()
    60. return n[1] ? n : '0' + n
    61. }
    62. function getWeek(n) {
    63. switch(n) {
    64. case 1:
    65. return '星期一'
    66. case 2:
    67. return '星期二'
    68. case 3:
    69. return '星期三'
    70. case 4:
    71. return '星期四'
    72. case 5:
    73. return '星期五'
    74. case 6:
    75. return '星期六'
    76. case 7:
    77. return '星期日'
    78. }
    79. }
    80. module.exports = {
    81. getStateName: getStateName,
    82. getNum:getNum,
    83. formatDate:formatDate
    84. };

    在index.wxml中编写数据显示

     

    1. <view>
    2. <swiper indicator-dots="true" autoplay="true" >
    3. <block wx:for="{{imgSrcs}}" wx:key="*text">
    4. <swiper-item>
    5. <image src=" {{item.img}}">image>
    6. swiper-item>
    7. block>
    8. swiper>
    9. view>
    10. <wxs src="/utils/comm.wxs" module="tools" />
    11. <view class="mobi-title">
    12. <text class="mobi-icon">text>
    13. <text>会议信息text>
    14. view>
    15. <block wx:for-items="{{lists}}" wx:for-item="item" wx:key="item.id">
    16. <view class="list" data-id="{{item.id}}">
    17. <view class="list-img">
    18. <image class="video-img" mode="scaleToFill" src="{{item.image !=null ? item.image : '/static/persons/2.jpg'}}">image>
    19. view>
    20. <view class="list-detail">
    21. <view class="list-title"><text>{{item.title}}text>view>
    22. <view class="list-tag">
    23. <view class="state">{{tools.getStateName(item.state)}}
    24. view>
    25. <view class="join"><text class="list-num">
    26. {{tools.getNum(item.canyuze,item.liexize,item.zhuchiren)}}text>人报名view>
    27. view>
    28. <view class="list-info"><text>{{item.location}}text>|<text>{{tools.formatDate(item.starttime)}}text>view>
    29. view>
    30. view>
    31. block>
    32. <view class="section bottom-line">
    33. <text>到底啦text>
    34. view>
    测试结果

     

     

     

     

  • 相关阅读:
    c++ 组合模式示例
    应用链的崛起将带来哪些风险与机遇?
    vue存储和使用后端传递过来的token
    Vue 中 KeepAlive 内置缓存使用
    java中spark数据集字段下划线改成驼峰
    【chat】 1:Ubuntu 20.04.3 编译安装moduo master分支
    autox.js的三个版本universal、armeabi-v7a、arm64-v8a的区别
    springboot admin自定义监控里的info信息
    Java面试问题记录
    TLSR8258应用笔记
  • 原文地址:https://blog.csdn.net/2201_75455485/article/details/133955089