forked from JeffreySu/WechatVideoCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
324 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//app.js | ||
App({ | ||
onLaunch: function () { | ||
// 展示本地存储能力 | ||
var logs = wx.getStorageSync('logs') || [] | ||
logs.unshift(Date.now()) | ||
wx.setStorageSync('logs', logs) | ||
|
||
// 登录 | ||
wx.login({ | ||
success: res => { | ||
// 发送 res.code 到后台换取 openId, sessionKey, unionId | ||
} | ||
}) | ||
// 获取用户信息 | ||
wx.getSetting({ | ||
success: res => { | ||
if (res.authSetting['scope.userInfo']) { | ||
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 | ||
wx.getUserInfo({ | ||
success: res => { | ||
// 可以将 res 发送给后台解码出 unionId | ||
this.globalData.userInfo = res.userInfo | ||
|
||
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 | ||
// 所以此处加入 callback 以防止这种情况 | ||
if (this.userInfoReadyCallback) { | ||
this.userInfoReadyCallback(res) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
}, | ||
globalData: { | ||
userInfo: null | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"pages": [ | ||
"pages/index/index", | ||
"pages/logs/logs", | ||
"pages/userinfo/userinfo" | ||
], | ||
"window": { | ||
"backgroundTextStyle": "light", | ||
"navigationBarBackgroundColor": "#fff", | ||
"navigationBarTitleText": "Senparc WeChat", | ||
"navigationBarTextStyle": "black" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/**app.wxss**/ | ||
.container { | ||
height: 100%; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: space-between; | ||
padding: 200rpx 0; | ||
box-sizing: border-box; | ||
} |
61 changes: 61 additions & 0 deletions
61
课程Demo代码/25-32 小程序/SenparcWeChatCourseWxOpen/pages/index/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//index.js | ||
//获取应用实例 | ||
const app = getApp() | ||
|
||
Page({ | ||
data: { | ||
motto: 'Hello Senparc', | ||
userInfo: {}, | ||
hasUserInfo: false, | ||
canIUse: wx.canIUse('button.open-type.getUserInfo'), | ||
currentTime:'' | ||
}, | ||
//事件处理函数 | ||
bindViewTap1: function() { | ||
wx.navigateTo({ | ||
url: '../logs/logs' | ||
}) | ||
}, | ||
onLoad: function () { | ||
if (app.globalData.userInfo) { | ||
this.setData({ | ||
userInfo: app.globalData.userInfo, | ||
hasUserInfo: true | ||
}) | ||
} else if (this.data.canIUse){ | ||
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 | ||
// 所以此处加入 callback 以防止这种情况 | ||
app.userInfoReadyCallback = res => { | ||
this.setData({ | ||
userInfo: res.userInfo, | ||
hasUserInfo: true | ||
}) | ||
} | ||
} else { | ||
// 在没有 open-type=getUserInfo 版本的兼容处理 | ||
wx.getUserInfo({ | ||
success: res => { | ||
app.globalData.userInfo = res.userInfo | ||
this.setData({ | ||
userInfo: res.userInfo, | ||
hasUserInfo: true | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
//启动计时器 | ||
var that = this; | ||
var interval = setInterval(function(){ | ||
that.setData({currentTime : new Date().toLocaleTimeString()},1000); | ||
}); | ||
}, | ||
getUserInfo: function(e) { | ||
console.log(e) | ||
app.globalData.userInfo = e.detail.userInfo | ||
this.setData({ | ||
userInfo: e.detail.userInfo, | ||
hasUserInfo: true | ||
}) | ||
} | ||
}) |
16 changes: 16 additions & 0 deletions
16
课程Demo代码/25-32 小程序/SenparcWeChatCourseWxOpen/pages/index/index.wxml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!--index.wxml--> | ||
<view class="container"> | ||
<view class="userinfo"> | ||
<button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button> | ||
<block wx:else> | ||
<image bindtap="bindViewTap1" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image> | ||
<text class="userinfo-nickname">{{userInfo.nickName}}</text> | ||
</block> | ||
</view> | ||
<view class="usermotto"> | ||
<text class="user-motto">{{motto}}</text> | ||
</view> | ||
<view class="time"> | ||
<text class="time-text">{{currentTime}}</text> | ||
</view> | ||
</view> |
29 changes: 29 additions & 0 deletions
29
课程Demo代码/25-32 小程序/SenparcWeChatCourseWxOpen/pages/index/index.wxss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/**index.wxss**/ | ||
.userinfo { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
} | ||
|
||
.userinfo-avatar { | ||
width: 128rpx; | ||
height: 128rpx; | ||
margin: 20rpx; | ||
border-radius: 50%; | ||
} | ||
|
||
.userinfo-nickname { | ||
color: #aaa; | ||
} | ||
|
||
.usermotto { | ||
margin-top: 200px; | ||
} | ||
|
||
.time{ | ||
color:red; | ||
} | ||
|
||
.time-text{ | ||
font-weight: bold; | ||
} |
15 changes: 15 additions & 0 deletions
15
课程Demo代码/25-32 小程序/SenparcWeChatCourseWxOpen/pages/logs/logs.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//logs.js | ||
const util = require('../../utils/util.js') | ||
|
||
Page({ | ||
data: { | ||
logs: [] | ||
}, | ||
onLoad: function () { | ||
this.setData({ | ||
logs: (wx.getStorageSync('logs') || []).map(log => { | ||
return util.formatTime(new Date(log)) | ||
}) | ||
}) | ||
} | ||
}) |
3 changes: 3 additions & 0 deletions
3
课程Demo代码/25-32 小程序/SenparcWeChatCourseWxOpen/pages/logs/logs.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"navigationBarTitleText": "查看Senparc启动日志" | ||
} |
6 changes: 6 additions & 0 deletions
6
课程Demo代码/25-32 小程序/SenparcWeChatCourseWxOpen/pages/logs/logs.wxml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<!--logs.wxml--> | ||
<view class="container log-list"> | ||
<block wx:for="{{logs}}" wx:for-item="log1"> | ||
<text class="log-item">{{index + 1}}. {{log1}}</text> | ||
</block> | ||
</view> |
8 changes: 8 additions & 0 deletions
8
课程Demo代码/25-32 小程序/SenparcWeChatCourseWxOpen/pages/logs/logs.wxss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.log-list { | ||
display: flex; | ||
flex-direction: column; | ||
padding: 40rpx; | ||
} | ||
.log-item { | ||
margin: 10rpx; | ||
} |
66 changes: 66 additions & 0 deletions
66
课程Demo代码/25-32 小程序/SenparcWeChatCourseWxOpen/pages/userinfo/userinfo.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// pages/userinfo/userinfo.js | ||
Page({ | ||
|
||
/** | ||
* 页面的初始数据 | ||
*/ | ||
data: { | ||
|
||
}, | ||
|
||
/** | ||
* 生命周期函数--监听页面加载 | ||
*/ | ||
onLoad: function (options) { | ||
|
||
}, | ||
|
||
/** | ||
* 生命周期函数--监听页面初次渲染完成 | ||
*/ | ||
onReady: function () { | ||
|
||
}, | ||
|
||
/** | ||
* 生命周期函数--监听页面显示 | ||
*/ | ||
onShow: function () { | ||
|
||
}, | ||
|
||
/** | ||
* 生命周期函数--监听页面隐藏 | ||
*/ | ||
onHide: function () { | ||
|
||
}, | ||
|
||
/** | ||
* 生命周期函数--监听页面卸载 | ||
*/ | ||
onUnload: function () { | ||
|
||
}, | ||
|
||
/** | ||
* 页面相关事件处理函数--监听用户下拉动作 | ||
*/ | ||
onPullDownRefresh: function () { | ||
|
||
}, | ||
|
||
/** | ||
* 页面上拉触底事件的处理函数 | ||
*/ | ||
onReachBottom: function () { | ||
|
||
}, | ||
|
||
/** | ||
* 用户点击右上角分享 | ||
*/ | ||
onShareAppMessage: function () { | ||
|
||
} | ||
}) |
3 changes: 3 additions & 0 deletions
3
课程Demo代码/25-32 小程序/SenparcWeChatCourseWxOpen/pages/userinfo/userinfo.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"navigationBarTitleText": "用户信息" | ||
} |
2 changes: 2 additions & 0 deletions
2
课程Demo代码/25-32 小程序/SenparcWeChatCourseWxOpen/pages/userinfo/userinfo.wxml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<!--pages/userinfo/userinfo.wxml--> | ||
<text>pages/userinfo/userinfo.wxml</text> |
1 change: 1 addition & 0 deletions
1
课程Demo代码/25-32 小程序/SenparcWeChatCourseWxOpen/pages/userinfo/userinfo.wxss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* pages/userinfo/userinfo.wxss */ |
33 changes: 33 additions & 0 deletions
33
课程Demo代码/25-32 小程序/SenparcWeChatCourseWxOpen/project.config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"description": "项目配置文件。", | ||
"setting": { | ||
"urlCheck": true, | ||
"es6": true, | ||
"postcss": true, | ||
"minified": true, | ||
"newFeature": true | ||
}, | ||
"compileType": "miniprogram", | ||
"libVersion": "1.9.94", | ||
"appid": "wx3192d41fb3e8f7d5", | ||
"projectname": "WX%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91", | ||
"isGameTourist": false, | ||
"condition": { | ||
"search": { | ||
"current": -1, | ||
"list": [] | ||
}, | ||
"conversation": { | ||
"current": -1, | ||
"list": [] | ||
}, | ||
"game": { | ||
"currentL": -1, | ||
"list": [] | ||
}, | ||
"miniprogram": { | ||
"current": -1, | ||
"list": [] | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
课程Demo代码/25-32 小程序/SenparcWeChatCourseWxOpen/utils/util.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
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: formatTime | ||
} |