Skip to content

Commit e4790ff

Browse files
committed
Latest learning code (related to if/for)
1 parent c8e75f0 commit e4790ff

24 files changed

+343
-0
lines changed

app.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//app.js
2+
App({
3+
onLaunch: function () {
4+
// 展示本地存储能力
5+
var logs = wx.getStorageSync('logs') || []
6+
logs.unshift(Date.now())
7+
wx.setStorageSync('logs', logs)
8+
9+
// 登录
10+
wx.login({
11+
success: res => {
12+
// 发送 res.code 到后台换取 openId, sessionKey, unionId
13+
}
14+
})
15+
// 获取用户信息
16+
wx.getSetting({
17+
success: res => {
18+
if (res.authSetting['scope.userInfo']) {
19+
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
20+
wx.getUserInfo({
21+
success: res => {
22+
// 可以将 res 发送给后台解码出 unionId
23+
this.globalData.userInfo = res.userInfo
24+
25+
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
26+
// 所以此处加入 callback 以防止这种情况
27+
if (this.userInfoReadyCallback) {
28+
this.userInfoReadyCallback(res)
29+
}
30+
}
31+
})
32+
}
33+
}
34+
})
35+
},
36+
globalData: {
37+
userInfo: null
38+
}
39+
})

app.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"pages":[
3+
"pages/index/index",
4+
"pages/logs/logs",
5+
"pages/learnwxml/learnwxml"
6+
],
7+
"window":{
8+
"backgroundTextStyle":"light",
9+
"navigationBarBackgroundColor": "#ffd",
10+
"navigationBarTitleText": "小程序",
11+
"navigationBarTextStyle":"black"
12+
},
13+
"style": "v2",
14+
"sitemapLocation": "sitemap.json",
15+
"tabBar": {
16+
"list": [
17+
{
18+
"pagePath": "pages/index/index",
19+
"text": "首页",
20+
"iconPath": "images/icon-home.png",
21+
"selectedIconPath": "images/icon-home-hl.png"
22+
},
23+
{
24+
"pagePath": "pages/logs/logs",
25+
"text": "日志",
26+
"iconPath": "images/icon-log.png",
27+
"selectedIconPath": "images/icon-log-hl.png"
28+
},
29+
{
30+
"pagePath": "pages/learnwxml/learnwxml",
31+
"text": "WXML",
32+
"iconPath": "images/icon-html.png",
33+
"selectedIconPath": "images/icon-html-hl.png"
34+
}
35+
]
36+
},
37+
"networkTimeout": {
38+
"request": 10000,
39+
"downloadFile": 10000
40+
},
41+
"debug": true,
42+
"resizable": true,
43+
"darkmode": false
44+
}

app.wxss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**app.wxss**/
2+
.container {
3+
height: 100%;
4+
display: flex;
5+
flex-direction: column;
6+
align-items: center;
7+
justify-content: space-between;
8+
padding: 200rpx 0;
9+
box-sizing: border-box;
10+
}

images/icon-home-hl.png

495 Bytes
Loading

images/icon-home.png

611 Bytes
Loading

images/icon-html-hl.png

16.6 KB
Loading

images/icon-html.png

12.6 KB
Loading

images/icon-log-hl.png

15.9 KB
Loading

images/icon-log.png

15.3 KB
Loading

pages/index/index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//index.js
2+
//获取应用实例
3+
const app = getApp()
4+
5+
Page({
6+
data: {
7+
motto: 'Hello World',
8+
userInfo: {},
9+
hasUserInfo: false,
10+
canIUse: wx.canIUse('button.open-type.getUserInfo')
11+
},
12+
//事件处理函数
13+
bindViewTap: function() {
14+
wx.navigateTo({
15+
url: '../logs/logs'
16+
})
17+
},
18+
onLoad: function () {
19+
if (app.globalData.userInfo) {
20+
this.setData({
21+
userInfo: app.globalData.userInfo,
22+
hasUserInfo: true
23+
})
24+
} else if (this.data.canIUse){
25+
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
26+
// 所以此处加入 callback 以防止这种情况
27+
app.userInfoReadyCallback = res => {
28+
this.setData({
29+
userInfo: res.userInfo,
30+
hasUserInfo: true
31+
})
32+
}
33+
} else {
34+
// 在没有 open-type=getUserInfo 版本的兼容处理
35+
wx.getUserInfo({
36+
success: res => {
37+
app.globalData.userInfo = res.userInfo
38+
this.setData({
39+
userInfo: res.userInfo,
40+
hasUserInfo: true
41+
})
42+
}
43+
})
44+
}
45+
},
46+
getUserInfo: function(e) {
47+
console.log(e)
48+
app.globalData.userInfo = e.detail.userInfo
49+
this.setData({
50+
userInfo: e.detail.userInfo,
51+
hasUserInfo: true
52+
})
53+
}
54+
})

pages/index/index.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"navigationBarTitleText": "home",
3+
"usingComponents": {}
4+
}

pages/index/index.wxml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!--index.wxml-->
2+
<view class="container">
3+
<view class="userinfo">
4+
<button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
5+
<block wx:else>
6+
<image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
7+
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
8+
</block>
9+
</view>
10+
<view class="usermotto">
11+
<text class="user-motto">{{motto}}</text>
12+
</view>
13+
</view>

pages/index/index.wxss

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**index.wxss**/
2+
.userinfo {
3+
display: flex;
4+
flex-direction: column;
5+
align-items: center;
6+
}
7+
8+
.userinfo-avatar {
9+
width: 128rpx;
10+
height: 128rpx;
11+
margin: 20rpx;
12+
border-radius: 50%;
13+
}
14+
15+
.userinfo-nickname {
16+
color: #aaa;
17+
}
18+
19+
.usermotto {
20+
margin-top: 200px;
21+
}

pages/learnwxml/learnwxml.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//learnwxml.js
2+
const util = require('../../utils/util.js')
3+
4+
Page({
5+
data: {
6+
message: "Hello .wxml !",
7+
item_id: 0,
8+
definitely_true: true,
9+
abc_num: [3, 2, 1],
10+
my_name: "The OA"
11+
},
12+
})

pages/learnwxml/learnwxml.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"navigationBarTitleText": "learn",
3+
"usingComponents": {}
4+
}

pages/learnwxml/learnwxml.wxml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!--learnwxml.wxml-->
2+
<view class="container">
3+
<view id="item-{{item_id}}">
4+
Hello, .wxml
5+
</view>
6+
7+
<view wx:if="{{definitely_true ? true : false}}">
8+
True!
9+
</view>
10+
11+
<view wx:if="{{true}}">
12+
3*2*1 = {{ abc_num[0] * abc_num[1] * abc_num[2] }}
13+
</view>
14+
15+
<view>
16+
MY name is "{{my_name}}"
17+
</view>
18+
19+
<view wx:for="{{abc_num}}">
20+
<!-- index & item is the default variable name -->
21+
{{index}} {{"wow"}} {{item}}
22+
</view>
23+
</view>

pages/learnwxml/learnwxml.wxss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**learnwxml.wxss**/
2+
#item-0 {
3+
font-style: italic;
4+
}

pages/logs/logs.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//logs.js
2+
const util = require('../../utils/util.js')
3+
4+
Page({
5+
data: {
6+
logs: []
7+
},
8+
onLoad: function () {
9+
this.setData({
10+
logs: (wx.getStorageSync('logs') || []).map(log => {
11+
return util.formatTime(new Date(log))
12+
})
13+
})
14+
}
15+
})

pages/logs/logs.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"navigationBarTitleText": "log",
3+
"usingComponents": {}
4+
}

pages/logs/logs.wxml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!--logs.wxml-->
2+
<view class="container log-list">
3+
<block wx:for="{{logs}}" wx:for-item="log">
4+
<text class="log-item">{{index + 1}}. {{log}}</text>
5+
</block>
6+
</view>

pages/logs/logs.wxss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.log-list {
2+
display: flex;
3+
flex-direction: column;
4+
padding: 40rpx;
5+
}
6+
.log-item {
7+
margin: 10rpx;
8+
}

project.config.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"description": "Project configuration file",
3+
"packOptions": {
4+
"ignore": []
5+
},
6+
"setting": {
7+
"urlCheck": true,
8+
"es6": true,
9+
"postcss": true,
10+
"preloadBackgroundData": false,
11+
"minified": true,
12+
"newFeature": true,
13+
"coverView": true,
14+
"autoAudits": false,
15+
"showShadowRootInWxmlPanel": true,
16+
"scopeDataCheck": false,
17+
"checkInvalidKey": true,
18+
"checkSiteMap": true,
19+
"uploadWithSourceMap": true,
20+
"babelSetting": {
21+
"ignore": [],
22+
"disablePlugins": [],
23+
"outputPath": ""
24+
},
25+
"useCompilerModule": false,
26+
"userConfirmedUseCompilerModuleSwitch": false
27+
},
28+
"compileType": "miniprogram",
29+
"libVersion": "2.0.4",
30+
"appid": "wx902fa0e8be2bc65e",
31+
"projectname": "proj_ff",
32+
"debugOptions": {
33+
"hidedInDevtools": []
34+
},
35+
"isGameTourist": false,
36+
"simulatorType": "wechat",
37+
"simulatorPluginLibVersion": {},
38+
"condition": {
39+
"search": {
40+
"current": -1,
41+
"list": []
42+
},
43+
"conversation": {
44+
"current": -1,
45+
"list": []
46+
},
47+
"game": {
48+
"currentL": -1,
49+
"list": []
50+
},
51+
"miniprogram": {
52+
"current": -1,
53+
"list": []
54+
}
55+
}
56+
}

sitemap.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"desc": "关于本文件的更多信息,请参考文档 https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html",
3+
"rules": [{
4+
"action": "allow",
5+
"page": "*"
6+
}]
7+
}

utils/util.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
9+
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
10+
}
11+
12+
const formatNumber = n => {
13+
n = n.toString()
14+
return n[1] ? n : '0' + n
15+
}
16+
17+
module.exports = {
18+
formatTime: formatTime
19+
}

0 commit comments

Comments
 (0)