File tree Expand file tree Collapse file tree
MobileFrameworks/wechat/miniprogram Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 1+ {
2+ "pages" :[
3+ " pages/index/index" ,
4+ " pages/logs/logs"
5+ ],
6+ "window" :{
7+ "backgroundTextStyle" :" light" ,
8+ "navigationBarBackgroundColor" : " #fff" ,
9+ "navigationBarTitleText" : " WeChat" ,
10+ "navigationBarTextStyle" :" black"
11+ }
12+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 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>
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 1+ {
2+ "navigationBarTitleText" : " 查看启动日志"
3+ }
Original file line number Diff line number Diff line change 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>
Original file line number Diff line number Diff line change 1+ .log-list {
2+ display: flex;
3+ flex-direction: column;
4+ padding: 40rpx;
5+ }
6+ .log-item {
7+ margin: 10rpx;
8+ }
You can’t perform that action at this time.
0 commit comments