正在显示
12 个修改的文件
包含
236 行增加
和
0 行删除
app.js
0 → 100644
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 | +}) | |
\ No newline at end of file | ... | ... |
app.json
0 → 100644
app.wxss
0 → 100644
pages/index/index.js
0 → 100644
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.wxml
0 → 100644
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
0 → 100644
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 | +} | |
\ No newline at end of file | ... | ... |
pages/logs/logs.js
0 → 100644
pages/logs/logs.json
0 → 100644
pages/logs/logs.wxml
0 → 100644
pages/logs/logs.wxss
0 → 100644
project.config.json
0 → 100644
1 | +{ | |
2 | + "description": "项目配置文件。", | |
3 | + "packOptions": { | |
4 | + "ignore": [] | |
5 | + }, | |
6 | + "setting": { | |
7 | + "urlCheck": true, | |
8 | + "es6": true, | |
9 | + "postcss": true, | |
10 | + "minified": true, | |
11 | + "newFeature": true | |
12 | + }, | |
13 | + "compileType": "miniprogram", | |
14 | + "libVersion": "2.2.2", | |
15 | + "appid": "wxe91c67adba6ac02d", | |
16 | + "projectname": "%E8%87%AA%E7%84%B6%E4%BA%BA%E7%A8%8E%E6%94%B6", | |
17 | + "isGameTourist": false, | |
18 | + "condition": { | |
19 | + "search": { | |
20 | + "current": -1, | |
21 | + "list": [] | |
22 | + }, | |
23 | + "conversation": { | |
24 | + "current": -1, | |
25 | + "list": [] | |
26 | + }, | |
27 | + "game": { | |
28 | + "currentL": -1, | |
29 | + "list": [] | |
30 | + }, | |
31 | + "miniprogram": { | |
32 | + "current": -1, | |
33 | + "list": [] | |
34 | + } | |
35 | + } | |
36 | +} | |
\ No newline at end of file | ... | ... |
utils/util.js
0 → 100644
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 | +} | ... | ... |
请
注册
或
登录
后发表评论