From 86858893672e1c354b02a98ee5813057b1b892a9 Mon Sep 17 00:00:00 2001
From: MewX <imewx@qq.com>
Date: Sun, 15 Jul 2018 11:28:29 +0800
Subject: [PATCH] Added wechat miniprogram practice

---
 MobileFrameworks/wechat/miniprogram/app.js    | 39 ++++++++++++++
 MobileFrameworks/wechat/miniprogram/app.json  | 12 +++++
 MobileFrameworks/wechat/miniprogram/app.wxss  | 10 ++++
 .../wechat/miniprogram/pages/index/index.js   | 54 +++++++++++++++++++
 .../wechat/miniprogram/pages/index/index.wxml | 13 +++++
 .../wechat/miniprogram/pages/index/index.wxss | 21 ++++++++
 .../wechat/miniprogram/pages/logs/logs.js     | 15 ++++++
 .../wechat/miniprogram/pages/logs/logs.json   |  3 ++
 .../wechat/miniprogram/pages/logs/logs.wxml   |  6 +++
 .../wechat/miniprogram/pages/logs/logs.wxss   |  8 +++
 .../wechat/miniprogram/project.config.json    | 36 +++++++++++++
 .../wechat/miniprogram/utils/util.js          | 19 +++++++
 12 files changed, 236 insertions(+)
 create mode 100644 MobileFrameworks/wechat/miniprogram/app.js
 create mode 100644 MobileFrameworks/wechat/miniprogram/app.json
 create mode 100644 MobileFrameworks/wechat/miniprogram/app.wxss
 create mode 100644 MobileFrameworks/wechat/miniprogram/pages/index/index.js
 create mode 100644 MobileFrameworks/wechat/miniprogram/pages/index/index.wxml
 create mode 100644 MobileFrameworks/wechat/miniprogram/pages/index/index.wxss
 create mode 100644 MobileFrameworks/wechat/miniprogram/pages/logs/logs.js
 create mode 100644 MobileFrameworks/wechat/miniprogram/pages/logs/logs.json
 create mode 100644 MobileFrameworks/wechat/miniprogram/pages/logs/logs.wxml
 create mode 100644 MobileFrameworks/wechat/miniprogram/pages/logs/logs.wxss
 create mode 100644 MobileFrameworks/wechat/miniprogram/project.config.json
 create mode 100644 MobileFrameworks/wechat/miniprogram/utils/util.js

diff --git a/MobileFrameworks/wechat/miniprogram/app.js b/MobileFrameworks/wechat/miniprogram/app.js
new file mode 100644
index 0000000..b545190
--- /dev/null
+++ b/MobileFrameworks/wechat/miniprogram/app.js
@@ -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
+  }
+})
\ No newline at end of file
diff --git a/MobileFrameworks/wechat/miniprogram/app.json b/MobileFrameworks/wechat/miniprogram/app.json
new file mode 100644
index 0000000..29b5dce
--- /dev/null
+++ b/MobileFrameworks/wechat/miniprogram/app.json
@@ -0,0 +1,12 @@
+{
+  "pages":[
+    "pages/index/index",
+    "pages/logs/logs"
+  ],
+  "window":{
+    "backgroundTextStyle":"light",
+    "navigationBarBackgroundColor": "#fff",
+    "navigationBarTitleText": "WeChat",
+    "navigationBarTextStyle":"black"
+  }
+}
diff --git a/MobileFrameworks/wechat/miniprogram/app.wxss b/MobileFrameworks/wechat/miniprogram/app.wxss
new file mode 100644
index 0000000..06c6fc9
--- /dev/null
+++ b/MobileFrameworks/wechat/miniprogram/app.wxss
@@ -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;
+} 
diff --git a/MobileFrameworks/wechat/miniprogram/pages/index/index.js b/MobileFrameworks/wechat/miniprogram/pages/index/index.js
new file mode 100644
index 0000000..608a282
--- /dev/null
+++ b/MobileFrameworks/wechat/miniprogram/pages/index/index.js
@@ -0,0 +1,54 @@
+//index.js
+//获取应用实例
+const app = getApp()
+
+Page({
+  data: {
+    motto: 'Hello World',
+    userInfo: {},
+    hasUserInfo: false,
+    canIUse: wx.canIUse('button.open-type.getUserInfo')
+  },
+  //事件处理函数
+  bindViewTap: 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
+          })
+        }
+      })
+    }
+  },
+  getUserInfo: function(e) {
+    console.log(e)
+    app.globalData.userInfo = e.detail.userInfo
+    this.setData({
+      userInfo: e.detail.userInfo,
+      hasUserInfo: true
+    })
+  }
+})
diff --git a/MobileFrameworks/wechat/miniprogram/pages/index/index.wxml b/MobileFrameworks/wechat/miniprogram/pages/index/index.wxml
new file mode 100644
index 0000000..0243bda
--- /dev/null
+++ b/MobileFrameworks/wechat/miniprogram/pages/index/index.wxml
@@ -0,0 +1,13 @@
+<!--index.wxml-->
+<view class="container">
+  <view class="userinfo">
+    <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
+    <block wx:else>
+      <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
+      <text class="userinfo-nickname">{{userInfo.nickName}}</text>
+    </block>
+  </view>
+  <view class="usermotto">
+    <text class="user-motto">{{motto}}</text>
+  </view>
+</view>
diff --git a/MobileFrameworks/wechat/miniprogram/pages/index/index.wxss b/MobileFrameworks/wechat/miniprogram/pages/index/index.wxss
new file mode 100644
index 0000000..ce30de0
--- /dev/null
+++ b/MobileFrameworks/wechat/miniprogram/pages/index/index.wxss
@@ -0,0 +1,21 @@
+/**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;
+}
\ No newline at end of file
diff --git a/MobileFrameworks/wechat/miniprogram/pages/logs/logs.js b/MobileFrameworks/wechat/miniprogram/pages/logs/logs.js
new file mode 100644
index 0000000..b2b967d
--- /dev/null
+++ b/MobileFrameworks/wechat/miniprogram/pages/logs/logs.js
@@ -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))
+      })
+    })
+  }
+})
diff --git a/MobileFrameworks/wechat/miniprogram/pages/logs/logs.json b/MobileFrameworks/wechat/miniprogram/pages/logs/logs.json
new file mode 100644
index 0000000..28379bc
--- /dev/null
+++ b/MobileFrameworks/wechat/miniprogram/pages/logs/logs.json
@@ -0,0 +1,3 @@
+{
+  "navigationBarTitleText": "查看启动日志"
+}
\ No newline at end of file
diff --git a/MobileFrameworks/wechat/miniprogram/pages/logs/logs.wxml b/MobileFrameworks/wechat/miniprogram/pages/logs/logs.wxml
new file mode 100644
index 0000000..b5a85ac
--- /dev/null
+++ b/MobileFrameworks/wechat/miniprogram/pages/logs/logs.wxml
@@ -0,0 +1,6 @@
+<!--logs.wxml-->
+<view class="container log-list">
+  <block wx:for="{{logs}}" wx:for-item="log">
+    <text class="log-item">{{index + 1}}. {{log}}</text>
+  </block>
+</view>
diff --git a/MobileFrameworks/wechat/miniprogram/pages/logs/logs.wxss b/MobileFrameworks/wechat/miniprogram/pages/logs/logs.wxss
new file mode 100644
index 0000000..94d4b88
--- /dev/null
+++ b/MobileFrameworks/wechat/miniprogram/pages/logs/logs.wxss
@@ -0,0 +1,8 @@
+.log-list {
+  display: flex;
+  flex-direction: column;
+  padding: 40rpx;
+}
+.log-item {
+  margin: 10rpx;
+}
diff --git a/MobileFrameworks/wechat/miniprogram/project.config.json b/MobileFrameworks/wechat/miniprogram/project.config.json
new file mode 100644
index 0000000..5ae933c
--- /dev/null
+++ b/MobileFrameworks/wechat/miniprogram/project.config.json
@@ -0,0 +1,36 @@
+{
+	"description": "项目配置文件。",
+	"packOptions": {
+		"ignore": []
+	},
+	"setting": {
+		"urlCheck": true,
+		"es6": true,
+		"postcss": true,
+		"minified": true,
+		"newFeature": true
+	},
+	"compileType": "miniprogram",
+	"libVersion": "2.0.4",
+	"appid": "wx6749044cd787c9c4",
+	"projectname": "MewX",
+	"isGameTourist": false,
+	"condition": {
+		"search": {
+			"current": -1,
+			"list": []
+		},
+		"conversation": {
+			"current": -1,
+			"list": []
+		},
+		"game": {
+			"currentL": -1,
+			"list": []
+		},
+		"miniprogram": {
+			"current": -1,
+			"list": []
+		}
+	}
+}
\ No newline at end of file
diff --git a/MobileFrameworks/wechat/miniprogram/utils/util.js b/MobileFrameworks/wechat/miniprogram/utils/util.js
new file mode 100644
index 0000000..dbadbb8
--- /dev/null
+++ b/MobileFrameworks/wechat/miniprogram/utils/util.js
@@ -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
+}