提交 57c03de0f42bce3d227bc9a4171ad4a4cdc17f43

作者 wangyu
0 个父辈

创建项目

  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 +})
  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 +}
  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 +}
  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 +})
  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>
  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 +}
  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 +})
  1 +{
  2 + "navigationBarTitleText": "查看启动日志"
  3 +}
  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>
  1 +.log-list {
  2 + display: flex;
  3 + flex-direction: column;
  4 + padding: 40rpx;
  5 +}
  6 +.log-item {
  7 + margin: 10rpx;
  8 +}
  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 +}
  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 +}
注册登录 后发表评论