123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <script>
- import { get as $get, postJSON as $postJSON, } from './config/util'
- import { api as $api } from './config/api'
- import $store from './store/index'
- const md5 = require('./config/md5')
-
- export default {
- globalData: {
- isIphoneX: false, // 是否为 iphoneX 及以上机型
- statusBarHeight: 44, // 状态栏高度
- },
- onLaunch: function (options) {
- console.log("App onLaunch options => ", options);
- // 判断当前设备是否为 iphoneX 及以上机型
- this.handleGetIsIphoneX();
- // 判断当前小程序是否为最新版本
- this.handleVersionUpdate();
- },
- onShow: function () {
- console.log('App onShow => ')
- // 执行用户登录
- this.handleUserLogin()
- },
- onHide: function () {
- console.log("App Hide");
- },
- methods: {
- // 判断当前设备是否为 iphoneX 及以上机型
- handleGetIsIphoneX() {
- const systemInfo = uni.getSystemInfoSync()
- this.$scope.globalData.statusBarHeight = systemInfo.statusBarHeight
- if (systemInfo.system.toUpperCase().includes("IOS")) {
- if (systemInfo.safeArea.top > 20) {
- this.$scope.globalData.isIphoneX = true;
- }
- }
- },
- // 判断当前小程序是否为最新版本
- handleVersionUpdate() {
- const that = this;
- // 判断应用的 getUpdateManager 是否在当前版本可用
- if (uni.canIUse("getUpdateManager")) {
- const updateManager = uni.getUpdateManager();
- // 向小程序后台请求完新版本信息
- updateManager.onCheckForUpdate(function (res) {
- if (res.hasUpdate) {
- // 小程序有新版本,静默下载新版本,新版本下载完成
- updateManager.onUpdateReady(function () {
- uni.showModal({
- title: "更新提示",
- content: "小程序已发布新版本,是否重启应用?",
- success: function (res) {
- if (res.confirm) {
- updateManager.applyUpdate();
- } else if (res.cancel) {
- // 强制用户更新,弹出第二次弹窗
- uni.showModal({
- title: "提示",
- content: "小程序已发布新版本,是否重启应用",
- showCancel: false, // 隐藏取消按钮
- success: function (res) {
- // 第二次提示后,强制更新
- if (res.confirm) {
- // 当新版本下载完成,调用该方法会强制当前小程序应用上新版本并重启
- updateManager.applyUpdate();
- } else if (res.cancel) {
- // 重新回到版本更新提示
- that.handleVersionUpdate();
- }
- },
- });
- }
- },
- });
- });
- // 当新版本下载失败
- updateManager.onUpdateFailed(function () {
- uni.showModal({
- title: "提示",
- content: "小程序版本更新失败,请您删除当前小程序并重新打开",
- });
- });
- }
- });
- } else {
- // 提示用户在最新版本的客户端上体验
- uni.showModal({
- title: "温馨提示",
- content: "当前微信版本过低,可能无法使用该功能,请升级到最新版本后重试。",
- });
- }
- },
- // 显示获取用户信息确认弹框
- handleShowModal() {
- return new Promise((resolve) => {
- uni.showModal({
- title: '提示',
- content: '请授权您的昵称、头像',
- success: async (res) => {
- if (res.confirm) {
- resolve('确认')
- }
- }
- })
- })
- },
- // 获取用户微信头像、昵称信息
- handleGetUserProfile() {
- return new Promise((resolve, reject) => {
- uni.getUserProfile({
- desc: '请授权您的昵称、头像', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
- success(res) {
- console.log('getUserProfile => ', res)
- const { avatarUrl, nickName } = res.userInfo
- resolve({ avatarUrl, nickName })
- },
- fail(err) {
- console.log('getUserProfile err => ', err)
- reject(err)
- }
- })
- })
- },
- // 保存用户信息
- async handleSaveUserInfo({ avatarUrl, nickName }) {
- const isLogin = uni.getStorageSync('isLogin')
- let user_id = uni.getStorageSync('user_id')
- if (!isLogin || !user_id) {
- await this.handleUserLogin()
- user_id = uni.getStorageSync('user_id')
- }
- console.log('user_id => ', user_id)
- const url = $api.my_setInfo
- const params = {
- user_id: user_id,
- type: 1, // 头像类型 1链接 2文件
- avatar_file: avatarUrl,
- nickname: nickName,
- }
- const { data: res = {} } = await $postJSON(url, params)
- if (res && res.errno == 0) {
- console.log('res => ', res)
- // 重新获取用户最新信息
- await this.handleUserLogin()
- } else {
- uni.showToast({ title: res.err || '操作失败', icon: 'none' })
- }
- },
- // 用户登录
- handleUserLogin() {
- return new Promise((resolve, reject) => {
- uni.login({
- success: async res => {
- const code = res.code
- try {
- const { data: res = {} } = await $get($api.user_login, { code })
- if (res && res.errno == 0) {
- uni.setStorageSync('user_info', res.rst.user_info)
- uni.setStorageSync('openid', res.rst.openid)
- uni.setStorageSync('session_key', res.rst.session_key)
- uni.setStorageSync('user_id', res.rst.user_id)
- uni.setStorageSync('union_id', res.rst.union_id)
- uni.setStorageSync('ttl', res.rst.ttl)
- const sign = md5.hex_md5(`random=${res.rst.random}&ttl=${res.rst.ttl}&user_id=${res.rst.user_id}`)
- uni.setStorageSync('sign', sign)
- uni.setStorageSync('isLogin', true)
- $store.commit('setIsLogin', true)
- resolve(res)
- } else {
- uni.showToast({ title: res.err, icon: 'none' })
- }
- } catch (error) {
- reject(error)
- console.log('error => ', error)
- }
- },
- })
- })
- },
- },
- };
- </script>
-
- <style lang="scss">
- @import "uview-ui/index.scss";
- /*每个页面公共css */
- </style>
|