这次职芽微信小程序总结
微信授权
- 通过微信授权获取用户信息 方法:
wx.getSetting({ succcess(res){ //如果已经授权则直接调用 wx.getUserInfo({成功后返回用户信息,存储用户信息到全局变量getApp().globalData.userInfo= res.userInfo}) } })
- 通过getApp()存储的全局变量在其他文件中读取为undefine的问题
通过在存储时给全局变量一个回调函数 getApp().cb = function(res.userInfo) {} app.cb = function(res){ that.setData({userInfo:app.globalData}) }
app.cb();来读取里面的全局变量;原理是通过给全局对象挂载一个原型方法,通过原型方法访问全局对象的属性;
##app.js
- 获取openId
通过在app.js的onlaunch函数中通过wx.login({成功返回的code,通过code发送请求给后台获取openId})
- wx.setStorageSync('openId',openId) 存储openId
###tab切换
<view class="{ {index==barCur?'current bar':'bar'}}" wx:for="{ {hotwords}}" wx:key="index" bindtap='changeCur' data-id="{ {index}}">{ {item}}</view>
changeCur(e){ this.setData({barCur:e.currentTarget.dataset.id}) }, ###页面跳转
- 页面跳转一般用wx.navigateTo({url:'/pages/my/my'})
- 假如要跳转到tabBar里定义的页面则:wx.switchTo({url:''})
##微信支付 主要是看项目需要比如我这里需要
- 先将商品ID和用户ID给发请求给后台
- 后台返回给我商品订单号
- 再给后台商品订单号和openId得到后台返回的微信支付所需的 msg.data.appId,小程序APPID msg.data.timeStamp,时间戳 msg.data.nonceStr, 随机字符串 msg.data.package, 统一下单接口返回的预订单号 msg.data.sign 签名
- 通过小程序提供的支付接口wx.requestPayment({})
wx.requestPayment({ 'timeStamp': timeStamp, 'nonceStr': nonceStr, 'package': mypackage, 'signType': 'MD5', 'paySign': sign, 'success': function(res) { }, 'fail': function(res) { }
})
####保存图片到本地相册 思路:先下载网络图片,成功以后通过小程序api存在相册
wx.downloadFile({ url: ''+this.data.userInfo.avatarUrl, success: function (res) { console.log(res);
wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success() { },
####从本地相册选取图片
wx.chooseImage({ count:1, // 在相册选取的数量 success: function(res) { const tempFilePaths = res.tempFilePaths; console.log(tempFilePaths); that.setData({"userInfo.avatarUrl":tempFilePaths[0]}) }, })