zoukankan      html  css  js  c++  java
  • 小程序对接后端数据--未改进写法

    import API from './utils/api.js'
    import Utils from './utils/util.js'
    import Mock from './utils/mock.js'
    
    const APP_NAME = '***********'
    const APP_ID = '*********'
    
    let aldstat = require("./utils/ald-stat.js")
    
    App({
        onLaunch: function () {
            // 展示本地存储能力
            var logs = wx.getStorageSync('logs') || []
            logs.unshift(Date.now())
            wx.setStorageSync('logs', logs)
        },
    
        authReadyCallback: null,
    
        requestAuth: function () {
            let that = this
    
            wx.getUserInfo({
                success: infoRes => {
                    // wx.showLoading({
                    //     title: '信息读取中',
                    // })
                    // 可以将 res 发送给后台解码出 unionId
                    that.globalData.userInfo = infoRes.userInfo
                    // console.log(that.globalData.userInfo)
    
                    wx.login({
                        success: loginRes => {
                            that.getUserSessionInfo(infoRes, loginRes, that, () => {
                                if (typeof (that.authReadyCallback) == 'function') {
                                    that.authReadyCallback(that.globalData)
                                }
                            })
                        }
                    })
                },
                fail: function () {
                    that.reAuthUserInfo()
                }
            })
        },
    
        globalData: {
            userInfo: null,
            sessionInfo: null,
            gameInfo: {},
        },
    
        /** custome defined by lunixy */
        juzigo: {
            api: API,
            appName: APP_NAME,
            utils: Utils,
            jump: href => {
                wx.navigateTo({
                    url: '/pages/webview/index?href=' + href,
                })
            }
        },
    
        /**
         * mock 数据
         */
        mock: Mock,
    
        /**
         * 重新获取用户信息授权
         */
        reAuthUserInfo: function (callback) {
            var that = this
            wx.hideLoading()
            wx.showModal({
                title: '请授权',
                content: '授权后才能使用小程序',
                showCancel: false,
                success: function (data) {
                    wx.openSetting({
                        success: function (res) {
                            wx.showLoading({
                                title: '信息读取中',
                            })
                            that.requestAuth()
                        }
                    })
                }
            })
        },
    
        /**
         * 获取用户 session 信息
         */
        getUserSessionInfo: function (infoRes, loginRes, that, callback) {
            wx.getStorage({
                key: 'sessionInfo',
                success: function (res) {
                    that.globalData.sessionInfo = res.data
                    callback(res.data)
                },
                fail: function () {
                    that.setUserSessionInfo(infoRes, loginRes.code, that, callback)
                }
            })
        },
    
        /**
         * 设置用户 session 信息
         */
        setUserSessionInfo: function (infoRes, jsCode, that, callback) {
            API.post('wechatJscode2Session', { app_name: APP_NAME, js_code: jsCode, grant_type: 'authorization_code' }, {}, sessionRes => {
                if (sessionRes && sessionRes.unionid) {
                    that.globalData.sessionInfo = sessionRes
                    wx.setStorage({
                        key: 'sessionInfo',
                        data: sessionRes,
                        success: function () {
                            callback(sessionRes)
                        }
                    })
                } else if (sessionRes && sessionRes.session_key) {
                    API.post('wechatDecryptUnionid', { encryptedData: infoRes.encryptedData, iv: infoRes.iv, appid: APP_ID, sessionKey: sessionRes.session_key }, {}, data => {
                        if (data.unionid) {
                            sessionRes.unionid = data.unionid
                            that.globalData.sessionInfo = sessionRes
                            wx.setStorage({
                                key: 'sessionInfo',
                                data: sessionRes,
                                success: function () {
                                    callback(sessionRes)
                                }
                            })
                        } else {
                            wx.showModal({
                                title: '提示',
                                content: '请求授权出错,请重试',
                                showCancel: false,
                                success: function () {
                                    that.requestAuth()
                                }
                            })
                        }
                    })
                }
            })
        },
    
    })

    api.js中写入后端的 解密用户数据得到 unionid以及 获取微信 session info的接口(其他两个引入文件还没有细看..)

    数据调用——获取计划列表

    onLoad: function () {
            let that = this
    
            app.authReadyCallback = globalData=>{
    
                setTimeout(() => {
                    console.log(globalData)
    
                    wx.request({
                        url: 'http://popfeng.wechat.api.dev.juzigo.com:1088/api/tripcalendar/main/list-for-user',
                        header: {
                            "Content-Type": "application/x-www-form-urlencoded"
                        },
                        data: { unionid:globalData.sessionInfo.unionid },
                        method: "POST",
                        success: function (res) {
                            that.setData({
                                info: res.data.data.list
                            })
                        }
                    })
                }, 300)
            }
            app.requestAuth()
        },
    

     info存在page中存放列表信息

  • 相关阅读:
    解决Requires: libc.so.6(GLIBC_2.14)(64bit)错误解决方法
    Linux下安装rpm出现error: Failed dependencies
    使用WordPress搭建个人博客
    Log4j不写入日志文件排错记录
    JAVA EE,JAVA SE,JAVA ME,JDK,JRE,JVM之间的区别
    [转]【maven】解决Missing artifact jdk.tools:jdk.tools:jar:1.6
    Eclipse新建项目介绍
    Android开发-API指南-<permission-group>
    Android开发-API指南-<permission>
    Android开发-API指南-<path-permission>
  • 原文地址:https://www.cnblogs.com/zhaotq/p/8377769.html
Copyright © 2011-2022 走看看