目录
一、后台准备 1、application.yml 2、generatorConfig.xml 3、WxHomeController 4、MinoaApplication.java
二、小程序首页动态数据加载及优化 1、config/app.js 2、util.js 3、index.wxml 4、index.js 5、page.wxs
一、后台准备
1、application.yml
spring :
datasource :
type : com.zaxxer.hikari.HikariDataSource
driver-class-name : com.mysql.jdbc.Driver
url : jdbc: mysql: //localhost: 3306/t280? useUnicode=true&characterEncoding=UTF-8&useSSL=false
username : root
password : 123456
mybatis :
mapper-locations : classpath*:mapper/*.xml
type-aliases-package : com.xnx.minoa.model
2、generatorConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
< generatorConfiguration>
< properties resource = " jdbc.properties" />
< classPathEntry location = " D:\0803\mvn_repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar" />
< context id = " infoGuardian" >
< commentGenerator>
< property name = " suppressAllComments" value = " true" />
< property name = " suppressDate" value = " true" />
</ commentGenerator>
< jdbcConnection driverClass = " ${jdbc.driver}"
connectionURL = " ${jdbc.url}" userId = " ${jdbc.username}" password = " ${jdbc.password}" />
< javaTypeResolver>
< property name = " forceBigDecimals" value = " false" />
</ javaTypeResolver>
< javaModelGenerator targetPackage = " com.xnx.minoa.model"
targetProject = " src/main/java" >
< property name = " enableSubPackages" value = " false" />
< property name = " constructorBased" value = " true" />
< property name = " trimStrings" value = " false" />
< property name = " immutable" value = " false" />
</ javaModelGenerator>
< sqlMapGenerator targetPackage = " mapper"
targetProject = " src/main/resources" >
< property name = " enableSubPackages" value = " false" />
</ sqlMapGenerator>
< javaClientGenerator targetPackage = " com.xnx.minoa.mapper"
targetProject = " src/main/java" type = " XMLMAPPER" >
< property name = " enableSubPackages" value = " false" />
</ javaClientGenerator>
< table schema = " " tableName = " t_oa_data_dict" domainObjectName = " Dict"
enableCountByExample = " false" enableDeleteByExample = " false"
enableSelectByExample = " false" enableUpdateByExample = " false" >
</ table>
< table schema = " " tableName = " t_oa_data_item" domainObjectName = " Item"
enableCountByExample = " false" enableDeleteByExample = " false"
enableSelectByExample = " false" enableUpdateByExample = " false" >
</ table>
< table schema = " " tableName = " t_oa_meeting_feedback" domainObjectName = " Feedback"
enableCountByExample = " false" enableDeleteByExample = " false"
enableSelectByExample = " false" enableUpdateByExample = " false" >
</ table>
< table schema = " " tableName = " t_oa_meeting_info" domainObjectName = " Info"
enableCountByExample = " false" enableDeleteByExample = " false"
enableSelectByExample = " false" enableUpdateByExample = " false" >
</ table>
< table schema = " " tableName = " t_oa_meeting_option" domainObjectName = " Option"
enableCountByExample = " false" enableDeleteByExample = " false"
enableSelectByExample = " false" enableUpdateByExample = " false" >
</ table>
< table schema = " " tableName = " t_oa_meeting_room" domainObjectName = " Room"
enableCountByExample = " false" enableDeleteByExample = " false"
enableSelectByExample = " false" enableUpdateByExample = " false" >
</ table>
< table schema = " " tableName = " t_oa_meeting_vote" domainObjectName = " Vote"
enableCountByExample = " false" enableDeleteByExample = " false"
enableSelectByExample = " false" enableUpdateByExample = " false" >
</ table>
< table schema = " " tableName = " t_oa_permission" domainObjectName = " Permission"
enableCountByExample = " false" enableDeleteByExample = " false"
enableSelectByExample = " false" enableUpdateByExample = " false" >
</ table>
< table schema = " " tableName = " t_oa_role" domainObjectName = " Role"
enableCountByExample = " false" enableDeleteByExample = " false"
enableSelectByExample = " false" enableUpdateByExample = " false" >
</ table>
< table schema = " " tableName = " t_oa_role_permission" domainObjectName = " RolePermission"
enableCountByExample = " false" enableDeleteByExample = " false"
enableSelectByExample = " false" enableUpdateByExample = " false" >
</ table>
< table schema = " " tableName = " t_oa_user" domainObjectName = " User"
enableCountByExample = " false" enableDeleteByExample = " false"
enableSelectByExample = " false" enableUpdateByExample = " false" >
</ table>
</ context>
</ generatorConfiguration>
3、WxHomeController
package com. xnx. minoa. wxcontroller ;
import com. xnx. minoa. mapper. InfoMapper ;
import com. xnx. minoa. model. Info ;
import com. xnx. minoa. util. ResponseUtil ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ;
import java. util. HashMap ;
import java. util. List ;
import java. util. Map ;
@RestController
@RequestMapping ( "/wx/home" )
public class WxHomeController {
@Autowired
private InfoMapper infoMapper;
@RequestMapping ( "/index" )
public Object index ( Info info) {
List < Info > infoList = infoMapper. list ( info) ;
Map < Object , Object > data = new HashMap < Object , Object > ( ) ;
data. put ( "infoList" , infoList) ;
return ResponseUtil . ok ( data) ;
}
}
4、MinoaApplication.java
package com. xnx. minoa ;
import org. mybatis. spring. annotation. MapperScan ;
import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ;
@MapperScan ( "com.xnx.minoa.mapper" )
@SpringBootApplication
public class MinoaApplication {
public static void main ( String [ ] args) {
SpringApplication . run ( MinoaApplication . class , args) ;
}
}
二、小程序首页动态数据加载及优化
1、config/app.js
var WxApiRoot = 'http://localhost:8080/wx/' ;
module. exports = {
IndexUrl : WxApiRoot + 'home/index' ,
SwiperImgs : WxApiRoot+ 'swiperImgs' ,
MettingInfos : WxApiRoot+ 'meeting/list' ,
} ;
2、util.js
const formatTime = date => {
const year = date. getFullYear ( )
const month = date. getMonth ( ) + 1
const day = date. getDate ( )
const hour = date. getHours ( )
const minute = date. getMinutes ( )
const second = date. getSeconds ( )
return ` ${ [ year, month, day] . map ( formatNumber) . join ( '/' ) } ${ [ hour, minute, second] . map ( formatNumber) . join ( ':' ) } `
}
const formatNumber = n => {
n = n. toString ( )
return n[ 1 ] ? n : ` 0 ${ n} `
}
module. exports = {
formatTime
}
function request ( url, data = { } , method = "GET" ) {
return new Promise ( function ( resolve, reject ) {
wx. request ( {
url : url,
data : data,
method : method,
header : {
'Content-Type' : 'application/json' ,
} ,
success : function ( res ) {
if ( res. statusCode == 200 ) {
resolve ( res. data) ;
} else {
reject ( res. errMsg) ;
}
} ,
fail : function ( err ) {
reject ( err)
}
} )
} ) ;
}
module. exports = {
formatTime, request
}
3、index.wxml
< wxs src = " /utils/page.wxs" module = " tools" />
< view>
< swiper autoplay = " true" indicator-dots = " true" indicator-color = " #fff" indicator-active-color = " #00f" >
< block wx: for= " {{imgSrcs}}" wx: key= " text" >
< swiper-item>
< view>
< image src = " {{item.img}}" class = " swiper-item" />
</ view>
</ swiper-item>
</ block>
</ swiper>
</ view>
< view class = " mobi-title" >
< text class = " mobi-icon" > </ text>
< text> 会议信息</ text>
</ view>
< block wx: for-items= " {{lists}}" wx: for-item= " item" wx: key= " item.id" >
< view class = " list" data-id = " {{item.id}}" >
< view class = " list-img" >
< image class = " video-img" mode = " scaleToFill" src = " {{item.image != null ? item.image : '/static/persons/1.jpg'}}" > </ image>
</ view>
< view class = " list-detail" >
< view class = " list-title" > < text> {{item.title}}</ text> </ view>
< view class = " list-tag" >
< view class = " state" > {{tools.getState(item.state)}}</ view>
< view class = " join" > < text class = " list-num" > {{tools.getNumber(item.canyuze)}}</ text> 人报名</ view>
</ view>
< view class = " list-info" > < text> {{item.location}}</ text> |< text> {{tools.formatDate(item.starttime)}}</ text> </ view>
</ view>
</ view>
</ block>
< view class = " section bottom-line" >
< text> 到底啦</ text>
</ view>
4、index.js
const api = require ( "../../config/app" )
const util = require ( "../../utils/util" )
Page ( {
data : {
imgSrcs : [
] ,
lists : [
]
} ,
onLoad ( options ) {
this . loadMeetingInfos ( ) ;
} ,
onReady ( ) {
} ,
onShow ( ) {
} ,
onHide ( ) {
} ,
onUnload ( ) {
} ,
loadSwiperImgs ( ) {
let that= this ;
wx. request ( {
url : api. SwiperImgs,
dataType : 'json' ,
success ( res ) {
console. log ( res)
that. setData ( {
imgSrcs : res. data. images
} )
}
} )
} ,
loadMeetingInfos ( ) {
util. request ( api. IndexUrl) . then ( res => {
this . setData ( {
lists : res. data. infoList
} )
} ) . catch ( res => {
console. log ( '服器没有开启,使用模拟数据!' )
} )
}
} )
5、page.wxs
function getState(state) {
// 状态:0取消会议1待审核2驳回3待开4进行中5开启投票6结束会议,默认值为1
if(state == 0 ) {
return '取消会议' ;
} else if(state == 1 ) {
return '待审核' ;
} else if(state == 2 ) {
return '驳回' ;
} else if(state == 3 ) {
return '待开' ;
} else if(state == 4 ) {
return '进行中' ;
} else if(state == 5 ) {
return '开启投票' ;
} else if(state == 6 ) {
return '结束会议' ;
}
return '其它' ;
}
var getNumber = function(str) {
var s = str+'' ;
var array = s.split ( ',' ) ;
var len = array.length;
return len;
}
function formatDate(ts, option) {
var date = getDate ( ts)
var year = date.getFullYear ( )
var month = date.getMonth ( ) + 1
var day = date.getDate ( )
var week = date.getDay ( )
var hour = date.getHours ( )
var minute = date.getMinutes ( )
var second = date.getSeconds ( )
//获取 年月日
if ( option == 'YY-MM-DD' ) return [year, month, day].map ( formatNumber) .join ( '-' )
//获取 年月
if ( option == 'YY-MM' ) return [year, month].map ( formatNumber) .join ( '-' )
//获取 年
if ( option == 'YY' ) return [year].map ( formatNumber) .toString ( )
//获取 月
if ( option == 'MM' ) return [mont].map ( formatNumber) .toString ( )
//获取 日
if ( option == 'DD' ) return [day].map ( formatNumber) .toString ( )
//获取 年月日 周一 至 周日
if ( option == 'YY-MM-DD Week' ) return [year, month, day].map ( formatNumber) .join ( '-' ) + ' ' + getWeek ( week)
//获取 月日 周一 至 周日
if ( option == 'MM-DD Week' ) return [month, day].map ( formatNumber) .join ( '-' ) + ' ' + getWeek ( week)
//获取 周一 至 周日
if ( option == 'Week' ) return getWeek ( week)
//获取 时分秒
if ( option == 'hh-mm-ss' ) return [hour, minute, second].map ( formatNumber) .join ( ':' )
//获取 时分
if ( option == 'hh-mm' ) return [hour, minute].map ( formatNumber) .join ( ':' )
//获取 分秒
if ( option == 'mm-dd' ) return [minute, second].map ( formatNumber) .join ( ':' )
//获取 时
if ( option == 'hh' ) return [hour].map ( formatNumber) .toString ( )
//获取 分
if ( option == 'mm' ) return [minute].map ( formatNumber) .toString ( )
//获取 秒
if ( option == 'ss' ) return [second].map ( formatNumber) .toString ( )
//默认 时分秒 年月日
return [year, month, day].map ( formatNumber) .join ( '-' ) + ' ' + [hour, minute, second].map ( formatNumber) .join ( ':' )
}
function formatNumber(n) {
n = n.toString ( )
return n[1] ? n : '0' + n
}
function getWeek(n) {
switch(n) {
case 1:
return '星期一'
case 2:
return '星期二'
case 3:
return '星期三'
case 4:
return '星期四'
case 5:
return '星期五'
case 6:
return '星期六'
case 7:
return '星期日'
}
}
module.exports = {
getState : getState,
getNumber : getNumber,
formatDate : formatDate
} ;