小程序頁面跳轉傳參前端
根目錄下的 app.json 文件json
頁面文件的路徑、窗口表現、設置網絡超時時間、設置多 tabcanvas
{ "pages": [ "pages/index/index", "pages/logs/index" ], "window": { "navigationBarTitleText": "Demo" }, "tabBar": { "list": [{ "pagePath": "pages/index/index", "text": "首頁" }, { "pagePath": "pages/logs/logs", "text": "日誌" }] }, "networkTimeout": { "request": 10000, "downloadFile": 10000 }, "debug": true, "navigateToMiniProgramAppIdList": [ "wxe5f52902cf4de896" ] }
"navigateToMiniProgramAppIdList": [ "wxe5f52902cf4de896" ] wx.navigateToMiniProgram({ appId: '', path: 'pages/index/index?id=1', success(res) { // 打開成功 } })
this指的是當前的對象
that指的是一個臨時的變量小程序
登陸流程後端
https://api.weixin.qq.com/sns/jscode2session
用戶惟一標識(openid)傳給前端並保存微信小程序
獲取code, 請求微信小程序官方接口:api
https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
小程序調用wx.login() 獲取 登陸憑證code ,並回傳到開發者服務器
調用接口wx.login() 獲取臨時登陸憑證(code)服務器
開發者服務器以code換取 用戶惟一標識openid 和 會話密鑰session_key微信
//app.js App({ onLaunch: function() { wx.login({ success: function(res) { if (res.code) { //發起網絡請求 wx.request({ url: 'https://test.com/onLogin', data: { code: res.code } }) } else { console.log('登陸失敗!' + res.errMsg) } } }); } })
下拉菜單網絡
<view class='nav_centent_arr' wx:if="{{nav_centent.length}}"> <view style="height:408rpx"> <block wx:for="{{nav_centent}}" wx:key="index"> <view class='nav_centent'>{{item}}</view> </block> </view> </view>
click_nav: function (e) { if (index == e.currentTarget.dataset.index && this.data.nav_centent != null){ index = e.currentTarget.dataset.index; this.setData({ nav_centent: null, shownavindex: null, }) } else if (this.data.nav_centent == null) { console.log(11) index = e.currentTarget.dataset.index; this.setData({ shownavindex: index, nav_centent: nav_centent_list[Number(index)] }) } else { console.log(22) index = e.currentTarget.dataset.index; this.setData({ shownavindex: index, nav_centent: nav_centent_list[Number(index)] }) } }
nav_title:['1','2','3','4'], shownavindex: null, nav_centent: null
radioChange 是單選框選中事件
radio 是點擊事件,用於獲取點擊的該組件的id
// 獲取該組件的id radio:function(e){ this.setData({ guige_key02: e.currentTarget.dataset.id }) console.log(e.currentTarget.dataset.id) }, // 發貨地址選擇,獲取用戶選擇的單選框的值 radioChange: function (e) { this.setData({ arr_guige02: e.detail.value }) console.log(e.detail.value) },
<checkbox-group bindchange="checkboxChange"> <label class="checkbox" wx:for="{{items}}"> <checkbox value="{{item.name}}" checked="{{item.checked}}"/>{{item.value}} </label> </checkbox-group>
Page({ data: { items: [ { name: 'USA', value: '美國' }, { name: 'CHN', value: '中國', checked: 'true' }, { name: 'BRA', value: '巴西' }, { name: 'JPN', value: '日本' }, { name: 'ENG', value: '英國' }, { name: 'TUR', value: '法國' }, ] }, checkboxChange: function (e) { console.log('checkbox發生change事件,攜帶value值爲:', e.detail.value) } })
實現畫布自適應各類手機尺寸
解決的問題:
畫布,動畫等js裏面的操做,默認是px而不是rpx, 沒法根據手機屏幕自適應
獲取節點的rpx -> px單位
<view id='canvas-container' style='width:200rpx;height:100rpx;'> wx.createSelectorQuery().select('#canvas-container').boundingClientRect(function (rect) { var width = rect.width/2 // 節點的寬度 }).exec()
wx.getSystemInfo({ success: function(res) { myCanvasWidth = res.windowWidth - 56 myCanvasHeight = res.windowHeight - 200 }, }) this.setData({ canvasWidth: myCanvasWidth, canvasHeight: myCanvasHeight })
<canvas class='canvas' style='width:{{canvasWidth}}px; height:{{canvasHeight}}px' disable-scroll='true'> </canvas>
Page({ data: { id:'' }, onLoad: function (options){ var that = this; that.setData({ id: options.id }) console.log(that.data.id) } })