123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- //app.js
- const {
- request,
- serverName
- } = require('./utils/services');
- import {
- getWxUserInfo,
- wxUserLogin
- } from './utils/request'
- App({
- onLaunch: function () {
- console.log('wx', wx)
- // 展示本地存储能力
- var logs = wx.getStorageSync('logs') || []
- logs.unshift(Date.now())
- wx.setStorageSync('logs', logs)
- this.autoLogin();
- let loginSessionKey = wx.getStorageSync("token") || "";
- if (loginSessionKey) {
- wx.request({
- url: serverName + '/wx/api/user/getBrowsedExhibitions',
- data: {
- loginSessionKey
- },
- header: {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- method: "post",
- success: (res) => {
- if (res.data.code == 0) {
- let cookieIds = res.data.data.ids || undefined
- if (cookieIds) {
- this.globalData.cookieIDs = cookieIds.split(',');
- }
- } else {
- return
- }
- }
- })
- } else {}
- // 获取用户信息
- 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)
- }
- }
- })
- }
- }
- })
- const updateManager = wx.getUpdateManager()
- updateManager.onCheckForUpdate((res) => {
- // 请求完新版本信息的回调
- console.log(res.hasUpdate)
- })
- updateManager.onUpdateReady(() => {
- wx.showModal({
- title: '更新提示',
- content: '新版本已经准备好,是否重启应用?',
- success: function (res) {
- if (res.confirm) {
- // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
- updateManager.applyUpdate()
- }
- }
- })
- })
- updateManager.onUpdateFailed(() => {
- // 新的版本下载失败
- })
- },
- onShow() {
- },
- autoLogin(callback) {
- wx.login({
- success: async res => {
- let {
- code
- } = res;
- // debugger
- if (code) {
- const result = await wxUserLogin(code)
- console.log('result', result)
- if (result.code === 0) {
- const {
- loginSessionKey,
- sessionKey,
- token
- } = result.data
- wx.setStorageSync('loginSessionKey', loginSessionKey)
- wx.setStorageSync('token', token)
- wx.setStorageSync('sessionKey', sessionKey)
- const res = await getWxUserInfo(sessionKey)
- const userInfo = wx.getStorageInfoSync('userInfo')
- if (res.data) {
- const mergeObj = {
- ...userInfo,
- ...res.data
- }
- wx.setStorageSync('userInfo', mergeObj)
- callback && callback(mergeObj);
- }
- }
- }
- }
- })
- },
- //应用关闭的函数
- onHide: function () {
- let {
- cookieIDs
- } = this.globalData;
- let ids = undefined;
- console.log(cookieIDs)
- if (cookieIDs) {
- if (cookieIDs.length > 9) {
- cookieIDs.length = 10
- }
- ids = cookieIDs.join(",") || undefined;
- }
- let loginSessionKey = wx.getStorageSync("token") || "";
- //关闭应用的时候发送你浏览过的场景
- if (loginSessionKey) {
- wx.request({
- url: serverName + '/wx/api/user/saveBrowsedExhibitions',
- data: {
- ids: ids,
- loginSessionKey
- },
- header: {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- method: "post",
- success: (res) => {}
- })
- } else {
- return
- }
- },
- globalData: {
- userInfo: null,
- city: "",
- collectedArr: [],
- collectedChange: false,
- clickToSelect: false,
- isLogin: false,
- cookieIDs: [],
- currentUrl: '',
- currentShareImg: '',
- envVersion: "trial"
- }
- })
- let app = getApp()
- export default app
|