微信小程序 - 第二日 -微信端代码
微信端代码
获取权限
test1
test1.json
默认的,没有改动
test1.wxml
<!--pages/test1/test1.wxml-->
<text>pages/test1/test1.wxml</text>
<button bind:tap="req">点我</button>
<button bind:tap="click">asdha</button>
<navigator url="/pages/test2/test2">跳转到新页面</navigator>
<button bind:tap="cun">存储</button>
<button bind:tap="qu">取sb</button>
<button bind:tap="lu">录音</button>
<button open-type="getUserInfo" bindgetuserinfo="info">授权登录</button>
test1.js
// pages/test1/test1.js
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
},
req: function() {
wx.request({
url: "http://127.0.0.1:8000/test/",
data: {
"name": "bingge"
},
method: "POST",
header: {
"content-type": "application/json"
},
success: function(res) {
console.log(res.data)
}
})
},
click: function() {
//必须是tabBar页面,不能携带参数
// wx.switchTab({
// url:"/pages/test2/test2"
// })
// var name = "sb";
// wx.reLaunch({
// url: "/pages/test3/test3?name="+name
// })
// wx.redirectTo({
// url: "/pages/test3/test3?name=sb"
// })
wx.navigateTo({
url: "/pages/test3/test3?name=sb"
})
},
cun: function() {
wx.setStorageSync('sb', '真的存了吗?')
wx.setStorageSync('sb1', '真的存了吗11?')
},
qu: function() {
console.log(wx.getStorageSync('sb'))
wx.clearStorageSync("sb")
},
lu: function() {
wx.getSetting({
success(res) {
if (!res.authSetting['scope.record']) {
wx.authorize({
scope: 'scope.record',
success() {
// 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
wx.startRecord()
}
})
} else {
wx.startRecord()
}
}
})
},
info: function(res) {
// console.log(res)
wx.checkSession({
success() {
//session_key 未过期,并且在本生命周期一直有效
wx.getUserInfo({
success: function(res) {
// console.log(res)
wx.request({
url: app.globalData.Url + "/getinfo/",
data: {
"encryptedData": res.encryptedData,
"iv": res.iv,
"login_key": wx.getStorageSync("login_key")
},
method: "POST",
header: {
"content-type": "application/json"
},
success: function(res) {
console.log(res)
}
})
}
})
},
// fail() {
// // session_key 已经失效,需要重新执行登录流程
// wx.login() //重新登录
// }
})
}
})
test1.wxss
默认的,未改动
练习页面跳转
test3
就写了一个文件(加了内容)
test3.js
// pages/test3/test3.js
Page({
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
console.log("我是test3")
wx.navigateTo({
url: "/pages/test4/test4"
})
}
})
test4
// pages/test4/test4.js
Page({
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
console.log("我是test4")
wx.navigateBack({
delta: 1
})
}
})
前后台信息校验
app
app.js
//app.js
App({
onLaunch: function() {
var _this = this
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
wx.request({
url: _this.globalData.Url + '/login/',
data: {
"code": res.code
},
header: {
"content-type": "application/json"
},
method: "POST",
success: function(res) {
console.log(res)
wx.setStorageSync("login_key", res.data.data.login_key)
}
})
}
})
// 获取用户信息
// wx.getSetting({
// success: res => {
// if (res.authSetting['scope.userInfo']) {
// // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
// wx.getUserInfo({
// success: res => {
// // 可以将 res 发送给后台解码出 unionId
// this.globalData.userInfo = res.userInfo
// // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// // 所以此处加入 callback 以防止这种情况
// if (this.userInfoReadyCallback) {
// this.userInfoReadyCallback(res)
// }
// }
// })
// }
// }
// })
},
globalData: {
Url: "http://127.0.0.1:8000",
userInfo: null
}
})

浙公网安备 33010602011771号