? ?? ?? ?? ?? ????? ????? ????? ?? ??? ??? ?? ??? ?? ?? ??? ???? ????. ??? ??? ????
?? ?? ????? ????? ???? ??? ????
WeChat ???? ????? ??? ???? ?? ?? ?????. ?? ??? ???? ??? ???? ???? ??? ?? ?? ?????.
? ?? ??? ?? ?? ????? ??? ?? ??? ?????. ???? ???? ??? ??? ?? ? ??? ?????? ?????.
??? ?? ??:
?? app.js ??? ?? app(obj)? ?? ????? ??? ???. . ???? ???? ?? ?? ???? ?? ????? ?????. ?? ??? ?? ??? getApp()? ?? ? ????? ?? ?? (getApp().globalData)
//app.js App({ onLaunch: function () { //調(diào)用API從本地緩存中獲取數(shù)據(jù) var logs = wx.getStorageSync('logs') || [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) }, getUserInfo:function(cb){ var that = this if(this.globalData.userInfo){ typeof cb == "function" && cb(this.globalData.userInfo) }else{ //調(diào)用登錄接口 wx.login({ success: function () { wx.getUserInfo({ success: function (res) { that.globalData.userInfo = res.userInfo typeof cb == "function" && cb(that.globalData.userInfo) } }) } }) } }, globalData:{ userInfo:null } })app.js? ?? ??? ???? ??? API ??? ???? ???? ? ??? ?? ????.
onLaunch ?? ??? ?? ???? ???? ??? ? ? ? ???? ????, ? ??? ????? ???? ???
?? ???? ??? ??? onLaunch? ???? ????.
varlogs=wx.getStorageSync('logs') || [] ?? ???? ?? ??? ?????. ?? ?? ??? log=[]? ???? localStorage? ??? ??? ????. HTML5
logs.unshift(Date.now()) ?? ??? ??? ??? ?????.
getUserInfo ??,
???? ? ? ???, ?? ???? ??? ??? ?? ?? ???, ?? ? ??? ???? ?? ?????. ??? ??? ?? ?? ?????? ?? ????? ???? ?? ? ???? ????. ?? ?????? getApp().getUserInfo(function(userinfo){console.log(userinfo);})? ?? ? ???? ???? ??? ??? ????.
getUserInfo:function(cb){//參數(shù)為cb,類型為函數(shù) var that = this if(this.globalData.userInfo){//用戶信息不為空 typeof cb == "function" && cb(this.globalData.userInfo)//如果參數(shù)cb的類型為函數(shù),那么執(zhí)行cb,獲取用戶信息; }else{//如果用戶信息為空,也就是說第一次調(diào)用getUserInfo,會(huì)調(diào)用用戶登錄接口。 wx.login({ success: function () { wx.getUserInfo({ success: function (res) { console.log(res) that.globalData.userInfo = res.userInfo//把用戶信息賦給globalData,如果再次調(diào)用getUserInfo函數(shù)的時(shí)候,不需要調(diào)用登錄接口 typeof cb == "function" && cb(that.globalData.userInfo)//如果參數(shù)cb類型為函數(shù),執(zhí)行cb,獲取用戶信息 } }) } }) } }
globalData
??? ?? ???? ???? ? ?????.
? ?? ??? ??? ?? app.json ??? ???? ???? ? ??? ??? WeChat ???? ????? ????, ??? ??? ??, ? ??? ????, ???? ?? ??? ????, ?? ?? ???? ?? ????.?? ??? ??
??? ??? ????? ????. ??? ??? ?? ????? ???? ???? ???? ??? ?? ?????. ?? ????.
{ "pages":[ "pages/index/index", "pages/logs/logs" ], "window":{ "backgroundTextStyle":"light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "WeChat", "navigationBarTextStyle":"black" } }
?? ?? ???? ??? ? ?? ??? ?????. WeChat ???? ?? ??????? ? ???? ??? js, wxss, wxml? ?? ??? ?? ??? ?? ??? ????.
?? ??????? ?? ???? ??? ??? ???????. index ?? ???? index.js, index.wxml ? index.wxss?? ? ?? ?? ??? ????. ?? ????? js, css, html ??? ???? ??? ??? ?? ?? ??? ??? ?????. js ? ????? ?? ??? ?? ??? wxml ?? ??? ???? js ? ?????? ??? ???? ??? ? ????. ???? ???? ??? ???? ?? ???? ???? ???? ?? ??? ??? ??? ??????. <… .
<!--index.wxml--> <view class="container">//視圖容器 <view bindtap="bindViewTap" class="userinfo">//bindtap為容器綁定點(diǎn)擊觸摸事件,在觸摸離開時(shí)觸發(fā)bindViewTap事件處理函數(shù),bindViewTap通過index.js page()設(shè)置添加 <image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>//大雙括號(hào)的變量來自于index.js的data對(duì)象解析成對(duì)應(yīng)的值,而且是實(shí)時(shí)的 <text class="userinfo-nickname">{{userInfo.nickName}}</text> </view> <view class="usermotto"> <text class="user-motto">{{motto}}</text> </view> </view>index.js, ???? react? ?? ??, ?? ??? ?? ??? ??? ???. page() ???? ?????. ???? ?? ???, ?? ?? ??, ??? ?? ?? ?? ???? OBJECT ????? ?????.
var app = getApp() // 獲取入口文件app的應(yīng)用實(shí)例 Page({ data: { motto: 'Hello World', userInfo: {} }, //自定義事件處理函數(shù),點(diǎn)擊.userinfo的容易觸發(fā)此函數(shù) bindViewTap: function() { wx.navigateTo({//全局對(duì)象wx的跳轉(zhuǎn)頁(yè)面方法 url: '../logs/logs' }) }, onLoad: function () {//發(fā)生頁(yè)面加載時(shí),自動(dòng)觸發(fā)該生命周期函數(shù) console.log('onLoad') var that = this //調(diào)用應(yīng)用實(shí)例的方法獲取全局?jǐn)?shù)據(jù) app.getUserInfo(function(userInfo){ //更新數(shù)據(jù),頁(yè)面自動(dòng)渲染 that.setData({ userInfo:userInfo }) }) } })index.wxss ??? ?? ???? ????? ?? app.wxss? ??? ???? ?????. <… .
logs.wxml ??
<!--logs.wxml--> <view class="container log-list"> <block wx:for="{{logs}}" wx:for-item="log">//block容器作用,無(wú)其他實(shí)際含義。wx:for作用:遍歷logs數(shù)組,遍歷多少次,block塊就會(huì)復(fù)制多少次,for-item等同于為<br>遍歷元素起一個(gè)變量名,方便引用。<br> <text class="log-item">{{index + 1}}. {{log}}</text> </block> </view>
logs.js ??
//logs.js var util = require('../../utils/util.js') //util.js相當(dāng)于一個(gè)函數(shù)庫(kù),我們可以在這個(gè)文件內(nèi)自定義擴(kuò)展和封裝一些常用的函數(shù)和方法 Page({ data: { logs: [] }, onLoad: function () { this.setData({ logs: (wx.getStorageSync('logs') || []).map(function (log) {//通過wx.getStorageSync獲取本地緩存的logs日志數(shù)據(jù) return util.formatTime(new Date(log))//日期格式化 }) }) } })
Logs.json ??
{ "navigationBarTitleText": "查看啟動(dòng)日志" //當(dāng)前頁(yè)面配置文件,設(shè)置window當(dāng)前頁(yè)面頂部導(dǎo)航欄標(biāo)題等相關(guān)內(nèi)容 }
? ??? ?? ???? ??? ??? ????. ? ???? ??? ??? ?????!
?? ?? ???? ????? ????? ?? ??? ??? ?? ?? ?? ?? ? ???? PHP ??? ????? ??????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)