小程序对接后端数据--未改进写法

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中存放列表信息

posted @ 2018-01-29 15:59  tianqi.zhao  阅读(280)  评论(0编辑  收藏  举报