123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- import {
- loginLog,
- userLogin,
- getMyInfo,
- bindDevice
- } from '~/api/user'
- import {
- createStoreBindings
- } from 'mobx-miniprogram-bindings'
- import {
- store
- } from '~/store/index'
- let storeBindings
- App({
- async onLaunch() {
-
- this.updateApplet()
- this.checkIsIos()
-
- this.initPlugin()
-
- this.getNavbarInfo()
- await loginLog()
- },
- async onShow(options) {
- if (!this.storeBindings) {
- this.storeBindings = createStoreBindings(this, {
- store,
- actions: ['setUser']
- })
- }
- let {
- path,
- scene,
- query
- } = wx.getEnterOptionsSync()
-
- if (query.scene && [1047, 1048].includes(scene) && path == 'pages/index/index') {
- let params = decodeURIComponent(query.scene).split('&')
- if (params.length == 1) {
- this.login(params[0])
- } else {
- this.login()
- }
- } else {
- let shareUid = options.query.uid || ''
- let userChannelCode = '3001'
-
- if ([1045, 1046, 1084].includes(scene)) {
- userChannelCode = '4001'
- }
- this.login(shareUid, userChannelCode)
- }
- },
- async login(shareUid, userChannelCode = '3001') {
- let uid = wx.getStorageSync('uid')
- if (uid) {
- let userInfo = await getMyInfo()
- this.setUser(userInfo.user)
- this.globalData.userInfo = userInfo.user
- this.deviceLogin()
- if (getApp().callBack) {
- getApp().callBack();
- }
- } else {
-
- this.getWXCode().then(async res => {
- if (res.code) {
-
- let data = {
- code: res.code,
- userChannelCode
- }
- if (shareUid != 'undefined' && shareUid) {
- data.shareUid = shareUid
- }
- let userRes = await userLogin(data)
- this.setUser(userRes.data)
- wx.setStorageSync('uid', userRes.data.uid)
- wx.setStorageSync('user', userRes.data)
- this.globalData.userInfo = userRes.data
- this.deviceLogin()
- if (getApp().callBack) {
- getApp().callBack(userRes);
- }
- }
- })
-
- this.setUser({
- grade: 'PRIMARY_FIRST_GRADE'
- })
-
- }
- },
- getWXCode() {
- return new Promise((reslove, reject) => {
- wx.login({
- success: async (res) => {
- if (res.code) {
- reslove(res)
- } else {
- reject(res.errMsg)
- }
- }
- })
- })
- },
- checkIsIos: function () {
- wx.getSystemInfo({
- success: (res) => {
- if (res.system.search('iOS') != -1) {
- this.globalData.isIOS = true
- }
- let {
- scene
- } = wx.getLaunchOptionsSync()
-
- if (scene != 1023) {
- let preTime = wx.getStorageSync('preDesktopTime')
- let flag = !preTime ? true : (new Date() - preTime) / 43200000 > 1 ? true : false
- if (flag || !preTime) {
- this.globalData.desktopTips = true
- wx.setStorage({
- key: "preDesktopTime",
- data: new Date()
- })
- }
- }
- }
- })
- },
-
- deviceLogin() {
-
- let {
- path,
- scene,
- query
- } = wx.getEnterOptionsSync()
-
- var gradeObj = Object.keys({
- "PRESCHOOL": "学前班",
- "PRIMARY_FIRST_GRADE": "一年级",
- "PRIMARY_SECOND_GRADE": "二年级",
- "PRIMARY_THREE_GRADE": "三年级",
- "PRIMARY_SENIOR_GRADE": "四年级",
- "PRIMARY_FIVE_GRADE": "五年级",
- "PRIMARY_SIX_GRADE": "六年级",
- })
- if (query.scene && [1047, 1048].includes(scene) && path == 'pages/index/index') {
- let params = decodeURIComponent(query.scene).split('&')
- if (params.length > 1) {
- bindDevice({
- deviceCode: params[0],
- channelCode: params[1],
- grade: gradeObj[params[2]],
- uid: wx.getStorageSync('uid')
- }).then(res => {
- console.log(res);
- })
- }
- }
-
- },
- getNavbarInfo() {
-
- const systemInfo = wx.getSystemInfoSync();
-
- const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
-
- this.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
- this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
- this.globalData.menuTop = menuButtonInfo.top;
- this.globalData.menuHeight = menuButtonInfo.height;
-
- this.globalData.windowWidth = systemInfo.windowWidth
- this.globalData.windowHeight = systemInfo.windowHeight
- },
- updateApplet() {
-
- if (wx.canIUse('getUpdateManager')) {
- const updateManager = wx.getUpdateManager()
- updateManager.onCheckForUpdate(function (res) {
-
- if (res.hasUpdate) {
- updateManager.onUpdateReady(function () {
- wx.showModal({
- title: '更新提示',
- content: '新版本已经准备好,是否重启应用?',
- success: function (res) {
- if (res.confirm) {
-
- updateManager.applyUpdate()
- }
- }
- })
- })
- updateManager.onUpdateFailed(function () {
-
- wx.showModal({
- title: '已经有新版本了哟~',
- content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
- })
- })
- }
- })
- }
- },
- initPlugin() {
- let miniAppPluginId = 'wxf1a06dafa3350688'
-
- console.log("插件ID:" + miniAppPluginId)
- wx.miniapp.loadNativePlugin({
- pluginId: miniAppPluginId,
- success: (plugin) => {
- this.globalData.plugin = plugin
- console.log('load plugin success', plugin)
- this.globalData.plugin.initPlugin(({}), (res) => {
- if (res === "success") {
- console.log("初始化plugin成功")
- } else {
- console.log("初始化plugin失败")
- }
- })
- },
- fail: (e) => {
- console.log('load plugin fail', e)
- }
- })
- },
- globalData: {
- userInfo: null,
- isIOS: false,
- desktopTips: false,
- navBarHeight: 0,
- menuRight: 0,
- menuTop: 0,
- menuHeight: 0,
- windowWidth: 0,
- windowHeight: 0,
- plugin: {}
- }
- })
|