|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- //app.js
- App({
- onLaunch: function () {
- // 展示本地存储能力
- // var logs = wx.getStorageSync('logs') || []
- // logs.unshift(Date.now())
- // wx.setStorageSync('logs', logs)
-
-
- // 登录
- wx.login({
- success: res => {
- // 发送 res.code 到后台换取 openId, sessionKey, unionId
- console.log(res)
- if (res.code){
- wx.request({
- url: 'https://laomenkuang.jiyou-tech.com/apiWx/openid',
- header: {
- code: res.code
- },
- success: e => {
- console.log(e)
- if (e.data.code == 200) {
- this.globalData.openId = e.data.data.openid;
- this.globalData.session_key = e.data.data.session_key;
- this.globalData.token = e.data.data.token;
- wx.setStorageSync('openid', e.data.data.openid)
- wx.setStorageSync('session_key', e.data.data.session_key)
- wx.setStorageSync('token', e.data.data.token)
-
- }
- }
- })
- } else {
- console.log("登陆失败" + res.errMsg)
- }
- }
- })
- // 获取用户信息
- wx.getSetting({
- success: res => {
- console.log(res)
- if (res.authSetting['scope.userInfo']) {
- // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
- wx.getUserInfo({
- success: e => {
- // 可以将 res 发送给后台解码出 unionId
- this.globalData.userInfo = e.userInfo
- // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
- // 所以此处加入 callback 以防止这种情况
- if (this.userInfoReadyCallback) {
- this.userInfoReadyCallback(res)
- }
- }
- })
- }else{
- wx.authorize({
- scope: 'scope.record',
- success() {
- // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
- wx.startRecord()
- }
- })
- }
- }
- })
- },
- globalData: {
- userInfo: null,
- openId:"",
- session_key:"",
- token:"",
- userInfo:"",
- },
- wxRequest:function(url,data){//接口调用
- wx.request({
- url: url,
- data: data,
- header: {
- openid: this.globalData.openId
- },
- success(res) {
- console.log(res.data)
- }
- })
- }
- })
|