谢创宏 3 роки тому
коміт
18a23490e4

+ 19 - 0
app.js

@@ -0,0 +1,19 @@
1
+// app.js
2
+App({
3
+  onLaunch() {
4
+    // 展示本地存储能力
5
+    const 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
+  globalData: {
17
+    userInfo: null
18
+  }
19
+})

+ 15 - 0
app.json

@@ -0,0 +1,15 @@
1
+{
2
+  "pages": [
3
+    "pages/index/index",
4
+    "pages/logs/logs"
5
+  ],
6
+  "window": {
7
+    "backgroundTextStyle": "light",
8
+    "navigationBarBackgroundColor": "#fff",
9
+    "navigationBarTitleText": "Weixin",
10
+    "navigationBarTextStyle": "black"
11
+  },
12
+  "style": "v2",
13
+  "sitemapLocation": "sitemap.json",
14
+  "lazyCodeLoading": "requiredComponents"
15
+}

+ 10 - 0
app.wxss

@@ -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
+} 

+ 48 - 0
pages/index/index.js

@@ -0,0 +1,48 @@
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
+    canIUseGetUserProfile: false,
12
+    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
13
+  },
14
+  // 事件处理函数
15
+  bindViewTap() {
16
+    wx.navigateTo({
17
+      url: '../logs/logs'
18
+    })
19
+  },
20
+  onLoad() {
21
+    if (wx.getUserProfile) {
22
+      this.setData({
23
+        canIUseGetUserProfile: true
24
+      })
25
+    }
26
+  },
27
+  getUserProfile(e) {
28
+    // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
29
+    wx.getUserProfile({
30
+      desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
31
+      success: (res) => {
32
+        console.log(res)
33
+        this.setData({
34
+          userInfo: res.userInfo,
35
+          hasUserInfo: true
36
+        })
37
+      }
38
+    })
39
+  },
40
+  getUserInfo(e) {
41
+    // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
42
+    console.log(e)
43
+    this.setData({
44
+      userInfo: e.detail.userInfo,
45
+      hasUserInfo: true
46
+    })
47
+  }
48
+})

+ 3 - 0
pages/index/index.json

@@ -0,0 +1,3 @@
1
+{
2
+  "usingComponents": {}
3
+}

+ 23 - 0
pages/index/index.wxml

@@ -0,0 +1,23 @@
1
+<!--index.wxml-->
2
+<view class="container">
3
+  <view class="userinfo">
4
+    <block wx:if="{{canIUseOpenData}}">
5
+      <view class="userinfo-avatar" bindtap="bindViewTap">
6
+        <open-data type="userAvatarUrl"></open-data>
7
+      </view>
8
+      <open-data type="userNickName"></open-data>
9
+    </block>
10
+    <block wx:elif="{{!hasUserInfo}}">
11
+      <button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 获取头像昵称 </button>
12
+      <button wx:elif="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
13
+      <view wx:else> 请使用1.4.4及以上版本基础库 </view>
14
+    </block>
15
+    <block wx:else>
16
+      <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
17
+      <text class="userinfo-nickname">{{userInfo.nickName}}</text>
18
+    </block>
19
+  </view>
20
+  <view class="usermotto">
21
+    <text class="user-motto">{{motto}}</text>
22
+  </view>
23
+</view>

+ 19 - 0
pages/index/index.wxss

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

+ 18 - 0
pages/logs/logs.js

@@ -0,0 +1,18 @@
1
+// logs.js
2
+const util = require('../../utils/util.js')
3
+
4
+Page({
5
+  data: {
6
+    logs: []
7
+  },
8
+  onLoad() {
9
+    this.setData({
10
+      logs: (wx.getStorageSync('logs') || []).map(log => {
11
+        return {
12
+          date: util.formatTime(new Date(log)),
13
+          timeStamp: log
14
+        }
15
+      })
16
+    })
17
+  }
18
+})

+ 4 - 0
pages/logs/logs.json

@@ -0,0 +1,4 @@
1
+{
2
+  "navigationBarTitleText": "查看启动日志",
3
+  "usingComponents": {}
4
+}

+ 6 - 0
pages/logs/logs.wxml

@@ -0,0 +1,6 @@
1
+<!--logs.wxml-->
2
+<view class="container log-list">
3
+  <block wx:for="{{logs}}" wx:key="timeStamp" wx:for-item="log">
4
+    <text class="log-item">{{index + 1}}. {{log.date}}</text>
5
+  </block>
6
+</view>

+ 8 - 0
pages/logs/logs.wxss

@@ -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
+}

+ 68 - 0
project.config.json

@@ -0,0 +1,68 @@
1
+{
2
+  "description": "项目配置文件",
3
+  "packOptions": {
4
+    "ignore": []
5
+  },
6
+  "setting": {
7
+    "bundle": false,
8
+    "userConfirmedBundleSwitch": false,
9
+    "urlCheck": true,
10
+    "scopeDataCheck": false,
11
+    "coverView": true,
12
+    "es6": true,
13
+    "postcss": true,
14
+    "compileHotReLoad": false,
15
+    "lazyloadPlaceholderEnable": false,
16
+    "preloadBackgroundData": false,
17
+    "minified": true,
18
+    "autoAudits": false,
19
+    "newFeature": false,
20
+    "uglifyFileName": false,
21
+    "uploadWithSourceMap": true,
22
+    "useIsolateContext": true,
23
+    "nodeModules": false,
24
+    "enhance": true,
25
+    "useMultiFrameRuntime": true,
26
+    "useApiHook": true,
27
+    "useApiHostProcess": true,
28
+    "showShadowRootInWxmlPanel": true,
29
+    "packNpmManually": false,
30
+    "enableEngineNative": false,
31
+    "packNpmRelationList": [],
32
+    "minifyWXSS": true,
33
+    "showES6CompileOption": false
34
+  },
35
+  "compileType": "miniprogram",
36
+  "libVersion": "2.21.0",
37
+  "appid": "wx14a2493635b91838",
38
+  "projectname": "miniprogram-2",
39
+  "debugOptions": {
40
+    "hidedInDevtools": []
41
+  },
42
+  "scripts": {},
43
+  "staticServerOptions": {
44
+    "baseURL": "",
45
+    "servePath": ""
46
+  },
47
+  "isGameTourist": false,
48
+  "condition": {
49
+    "search": {
50
+      "list": []
51
+    },
52
+    "conversation": {
53
+      "list": []
54
+    },
55
+    "game": {
56
+      "list": []
57
+    },
58
+    "plugin": {
59
+      "list": []
60
+    },
61
+    "gamePlugin": {
62
+      "list": []
63
+    },
64
+    "miniprogram": {
65
+      "list": []
66
+    }
67
+  }
68
+}

+ 7 - 0
sitemap.json

@@ -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
+}

+ 19 - 0
utils/util.js

@@ -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
19
+}