| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- require("./utils/string.js")
- const ald = require('./libs/ald-stat.js')
- import {
- updataStorageData
- } from './utils/storage.js'
- import {
- getWxCode,
- hasAuth,
- getUserInfo
- } from './utils/wx.js'
- import {
- wxLogin,
- saveLogin,
- checkPhone
- } from './services/wx.js'
- var startTime = Date.now();//启动时间
- App({
- onLaunch: function (options) {
- this.aldstat.sendEvent('小程序的启动时长', {
- time: Date.now() - startTime
- })
- // 小程序版本更新
- if (wx.getUpdateManager) {
- const updateManager = wx.getUpdateManager()
- updateManager.onCheckForUpdate(function (res) {
- // 请求完新版本信息的回调
- console.log(res.hasUpdate)
- })
- updateManager.onUpdateReady(function () {
- wx.showModal({
- title: '更新提示',
- content: '新版本已经准备好,是否重启应用?',
- success(res) {
- if (res.confirm) {
- // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
- updateManager.applyUpdate()
- }
- }
- })
- })
- updateManager.onUpdateFailed(function () {
- // 新版本下载失败
- })
- } else {
- wx.showModal({
- title: '提示',
- content: '当前微信版本过低,部分功能可能无法使用,请升级到最新微信版本后重试。'
- })
- }
- // let sceneArr = [1007, 1008, 1044, 1011, 1012, 1013, 1047, 1048, 1049]
- let shareToken = options.query.shareToken || null
- getWxCode().then(code => {
- return wxLogin({
- code: code,
- shareToken: shareToken
- })
- })
- .then(res => {
- console.log("dd", res)
- this.globalData.openId = res.data.openId ? res.data.openId : '';
- this.globalData.sessionKey = res.data.sessionKey ? res.data.sessionKey : '';
- return hasAuth('scope.userInfo');
- })
- .catch(err => {
- console.log("没用获取用户信息权限")
- wx.navigateTo({
- url: '/pages/login/login',
- })
- return
- })
- .then(data => {
- return getUserInfo()
- })
- .then(res => {
- console.log("ee", res)
- if (!updataStorageData('city')) {
- updataStorageData('city', res.userInfo.city)
- }
- saveLogin({
- encryptedData: encodeURIComponent(res.encryptedData),
- iv: encodeURIComponent(res.iv),
- openid: this.globalData.openId,
- session_key: this.globalData.sessionKey
- }).then(data => {
- console.log("ff", data)
- this.globalData.userId = data.data.hpUser.id ? data.data.hpUser.id : '';
- this.globalData.userToken = data.data.hpUser.userToken ? data.data.hpUser.userToken : '';
- updataStorageData('shareToken', data.data.hpUser.userToken || '') //用户识别码
- wx.setStorageSync("hpuser", data.data.hpUser);
- //在获取用户信息之后,再返回userInfo,防止首页 onLoad先执行
- this.globalData.userInfo = res.userInfo;
- // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
- // 所以此处加入 callback 以防止这种情况
- if (this.userInfoReadyCallback) {
- this.userInfoReadyCallback(res)
- }
- }).catch(data => {
- console.log("gg", data)
- })
- })
- .catch(err => {
- console.log(err)
- })
- },
- onShow(options) {
- console.log("rr", options)
- //群聊信息
- if (options.shareTicket) {
- this.getShareInfo(options.shareTicket)
- }
- },
- globalData: {
- openId: '',
- userInfo: null,
- userId: '',
- userToken: '',
- sessionKey: '',
- city: updataStorageData('city') || '无锡'
- },
- //页面分享
- onShareAppMessage: function () {
- wx.showShareMenu({
- withShareTicket: true,
- success: (res) => { // 成功后要做的事情
- console.log(res)
- },
- fail: function (res) {
- console.log(res)
- }
- })
- },
- //获取群聊分享信息
- getShareInfo(shareTicket) {
- wx.getShareInfo({
- shareTicket: shareTicket,
- success: (res) => {
- //需后台解析数据 encryptedData iv
- console.log(res)
- },
- fail: function (res) {
- console.log(res)
- },
- complete: function (res) { }
- })
- },
- // 跳转登录页面
- goLogin() {
- wx.navigateTo({
- url: '/pages/login/login',
- })
- },
- })
|