1:小程序中没有DOM和BOM对象,固然也就没有jquery和zeipto;
2: 小程序中元素在模拟编辑器上可以用length属性,但是手机上不可用length;length属性用for in 循环解决
3:全局变量的问题,存储全局变量直接app.globalData=“”即可,也可以存储在storage中,防止异步调用获取不到的问题,所以存取建议都用同步的,加后缀Sync;
4:封装方法体,需要写到page({})外边;
5:设置变量的方法是this.setData({}),尽量在每个需要用到this 的地方将this留住,that=this;
6:经常利用的字符串截取:
微信小程序授权总结:
wxml:
<image class="headPic" src='{{userInfo.avatarUrl ? userInfo.avatarUrl : src}}'></image>
<view class="userName">{{userInfo.nickName ? userInfo.nickName : nickName}}</view>
index.js:
data: {
nickName:'未知',
src:'../images/img_default.png',
userInfo: {},
hasUserInfo: false
}
onLoad: function () {
var that=this;
//调用应用实例的方法获取全局数据
app.getUserInfo(function (userInfo) {
//更新数据
that.setData({
userInfo: userInfo
})
});
},
getUserInfo: function (e) {
console.log(e)
app.globalData.userInfo = e.detail.userInfo
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
}
app.js:
App({
onLaunch: function () {
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
this.getUserInfo();
},
getUserInfo: function (cb, fun) {
var that = this;
if (this.globalData.userInfo) {
typeof cb == "function" && cb(this.globalData.userInfo)
} else {
//调用登录接口
wx.login({
success: function (loginRes) {
wx.getUserInfo({
success: function (res) {
console.log(res)
that.globalData.userInfo = res.userInfo;
typeof cb == "function" && cb(that.globalData.userInfo);
wx.request({
url: 'https://wzcx.chetuobang.com/wz_api/?service=WashCar.index',
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
encryptedData: res.encryptedData,
iv: res.iv,
code: loginRes.code
},
success: function (re) {
//4.解密成功后 获取自己服务器返回的结果
//console.log(re)
var unionId = that.globalData.unionId = re.data.unionId;
wx.setStorageSync('openId', re.data.openId);
wx.setStorageSync('unionId', unionId)
console.log(wx.getStorageSync('unionId'));
}
})
},
fail: function () {
console.log("sbai")
wx.redirectTo({
url: '../../pages/logs/logs',
})
}
})
},
fail: function () {
console.log("sibai");
}
});
}
},
globalData: {
userInfo: null,
unionId: null
}
})
小程序授权问题更新:
//app.js
App({
onLaunch: function () {
},
onShow:function(options){
this.globalData.option = options
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
get_userinfo: function (cb) {
var that = this
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.login({
success: rs => {
wx.request({
url: 'xxxx',
header: {
'content-type': 'application/json'
},
data: {
code: rs.code,
},
success: function (res) {
// console.log(res);
if(res.data.ret==460){
wx.navigateBack({
delta:1
})
}
//设置用户信息
wx.getUserInfo({
success: (rss) => {
// console.log(rss);
wx.setStorageSync("avatar", rss.userInfo.avatarUrl)
if (rss.userInfo.nickName != wx.getStorageSync("nick")) {
wx.request({
url: 'xxxxx',
header: {
'content-type': 'application/json'
},
data: {
uid: wx.getStorageSync('uid'),
voucher: wx.getStorageSync('voucher'),
encryptedData: rss.encryptedData,
iv: rss.iv
},
success: (res) => {
wx.setStorageSync();
}
})
}
}
})
if (that.globalData.option.scene == 1044) {
wx.getShareInfo({
shareTicket: that.globalData.option.shareTicket,
success: (res) => {
wx.request({
url: that.globalData.base_url + '/weixinapi.php?method=setShareData',
method: "GET",
data: {
uid,
voucher,
encryptedData: res.encryptedData,
iv: res.iv
},
success: (rs) => {
cb();
}
})
}
})
} else {
cb()
}
}
});
}
})
}
else {
//还没有授权
wx.getUserInfo({
success: (rss) => {
wx.setStorageSync("avatar", rss.userInfo.avatarUrl)
wx.login({
success: res => {
//点击授权成功
if (res.code) {
wx.request({
url: 'xxxx',
header: {
'content-type': 'application/json'
},
data: {
code: res.code,
},
success: function (res) {
if (res.data.ret == 460) {
wx.navigateBack({
delta: 1
})
}
wx.setStorageSync('uid', uid)
wx.setStorageSync('nick', name)
wx.setStorageSync('voucher', voucher);
wx.request({
url: 'xxxx',
header: {
'content-type': 'application/json'
},
data: {
uid: wx.getStorageSync('uid'),
voucher: wx.getStorageSync('voucher'),
encryptedData: rss.encryptedData,
iv: rss.iv
},
success: (res) => {
}
})
if (that.globalData.option.scene == 1044) {
wx.getShareInfo({
shareTicket: that.globalData.option.shareTicket,
success: (res) => {
wx.request({
url: that.globalData.base_url + 'setShareData',
method: "GET",
data: {
uid,
voucher,
encryptedData: res.encryptedData,
iv: res.iv
},
success: (res) => {
// console.log("------------------")
// console.log(res)
cb();
}
})
}
})
} else {
cb();
}
}
});
}
}
})
},
fail: (res) => {
//点击授权拒绝
wx.showModal({
title: '提示',
content: '获取用户信息失败,需要授权才能继续使用!',
showCancel: false,
confirmText: "授权",
success: (res) => {
if (res.confirm) {
wx.openSetting({
success: (res) => {
if (res.authSetting["scope.userInfo"]) {
//授权成功
} else {
//仍然没有授权
}
}
})
}
}
})
}
})
}
}
})
},
globalData: {
}
})
分享:
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
wx.showShareMenu({
withShareTicket: true
})
var that = this;
var voucher = wx.getStorageSync("voucher");
var uid = wx.getStorageSync("uid");
var nick = wx.getStorageSync("nick")
return {
title: nick+"@我,快来一起挑战赢娃娃吧!",
path: "pages/index/index",
success: (res) => {
if (res.shareTickets[0]){
wx.getShareInfo({
shareTicket: res.shareTickets[0],
success: (rs) => {
wx.request({
url: base_url + "/weixinapi.php?method=setShareData",
method: "GET",
data: {
uid,
voucher,
uid,
encryptedData:rs.encryptedData,
iv:rs.iv
},
success: (res) => {
if(res.data.ret==1){
if(res.data.addTz){
wx.showToast({
title: '获得挑战次数+1',
})
}else{
wx.showToast({
title: '今天已经分享过这个群了!',
icon:"none"
})
}
}else{
wx.showToast({
title: '获得机会失败!',
icon:"none"
})
}
}
})
},
fail: (rs) => {
wx.showToast({
title: '获取分享失败!',
icon: "none"
})
}
})
}else{
wx.showToast({
title: '分享的不是群哦!',
icon:"none"
})
}
}
}
},