123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- import * as util from './util'
- import storage from "@/utils/storage";
- import {deletePageStyle, setPageStyle} from "@/utils/uniFunction";
- import {isObject} from "./util";
- /**
- * 显示成功提示框
- */
- export const showSuccess = (msg, callback = null, duration = 1500) => {
- uni.showToast({
- title: `${msg}`,
- icon: 'success',
- mask: true,
- duration,
- success() {
- if (callback)
- setTimeout(() => {
- callback()
- }, duration)
- }
- })
- }
- /**
- * 显示失败提示框
- */
- export const showError = (msg, callback = null, duration = 1500) => {
- uni.showModal({
- title: '友情提示',
- content: `${msg}`,
- showCancel: false,
- success(res) {
- if (callback)
- setTimeout(() => {
- callback()
- }, duration)
- },
- fail(err) {
- console.log('showError fail', err)
- },
- })
- }
- /**
- * 显示失败提示框
- */
- export const confirmModal = (msg, options = {}) => {
- return new Promise((resolve, reject) => {
- let allOptions = Object.assign({
- title: '温馨提示',
- content: `${msg}`,
- showCancel: true,
- success: res => {
- if (res.confirm) {
- resolve(res)
- } else {
- reject(res)
- }
- },
- fail: res => reject(res)
- }, options)
- uni.showModal(allOptions)
- })
- }
- /**
- * 显示纯文字提示框
- */
- export const showToast = (msg, duration = 1500) => {
- let data = {
- title: `${msg}`,
- icon: 'none',
- duration
- }
- if (!isObject(duration)) {
- data.duration = duration
- } else {
- data = Object.assign(data, duration)
- }
- uni.showToast(data)
- }
- /**
- * tabBar页面路径列表 (用于链接跳转时判断)
- * tabBarLinks为常量, 无需修改
- */
- export const getTabBarLinks = () => {
- const tabBarLinks = [
- // 'pages/index/index',
- ]
- return tabBarLinks
- }
- /**
- * 生成完整的H5地址 [带参数]
- * @param {string} h5Url H5访问地址
- * @param {string} path 页面路径
- * @param {object} params 页面参数
- * @return {string}
- */
- export const buildUrL = (h5Url, path, params) => {
- let complete = h5Url
- if (!util.isEmpty(path)) {
- complete += '#/' + path
- const shareParamsStr = getShareUrlParams(params)
- if (!util.isEmpty(shareParamsStr)) {
- complete += '?' + shareParamsStr
- }
- }
- return complete
- }
- /**
- * 跳转到指定页面url
- * 支持tabBar页面
- * @param {string} url
- * @param {object} query
- */
- export const navTo = (url, query = {}, isLogin = false) => {
- if (!url || url.length == 0) {
- return false
- }
- if (isLogin && !checkLogin()) {
- checkLogin(true)
- return;
- }
- // 生成query参数
- const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''
- // tabBar页面, 使用switchTab
- if (util.inArray(url, getTabBarLinks())) {
- if (queryStr) {
- uni.reLaunch({
- url: `/${url}${queryStr}`
- })
- } else {
- uni.switchTab({
- url: `/${url}`
- })
- }
- return true
- }
- // 普通页面, 使用navigateTo
- uni.navigateTo({
- url: `/${url}${queryStr}`
- })
- return true
- }
- /**
- * 跳转到指定页面url 并关闭当前页面
- * 支持tabBar页面
- * @param {string} url
- * @param {object} query
- * @param type
- */
- export const redirectTo = (url, query = {}, type = 1) => {
- if (!url || url.length == 0) {
- return false
- }
- // tabBar页面, 使用switchTab
- if (util.inArray(url, getTabBarLinks())) {
- uni.switchTab({
- url: `/${url}`
- })
- return true
- }
- // 生成query参数
- const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''
- if (type === 1) {
- uni.redirectTo({
- url: `/${url}${queryStr}`
- })
- return true
- } else if (type === 2) {
- uni.reLaunch({
- url: `/${url}${queryStr}`
- })
- return true
- }
- return true
- }
- /**
- * 验证是否已登录
- */
- export const checkLogin = (isLogin = false, is_confirm = false) => {
- let token = storage.getKey('LOGIN_TEACHER_TOKEN');
- if (token) return !!token;
- if (isLogin) {
- // 要登录
- if (is_confirm == true) {
- uni.showModal({
- title: '温馨提示',
- content: '此时此刻需要您登录喔~',
- // showCancel: false,
- confirmText: "去登录",
- cancelText: "再逛会",
- success: res => {
- if (res.confirm) {
- uni.navigateTo({
- url: "/pages/index/init"
- })
- }
- if (res.cancel && getCurrentPages().length > 1) {
- uni.navigateBack()
- }
- }
- })
- } else {
- redirectTo('/pages/index/init')
- }
- } else {
- // 无需登录
- return !!token
- }
- }
- export const navigateBack = (num = 1, callback) => {
- uni.navigateBack({
- delta: num,
- success(res) {
- if (callback) callback()
- }
- })
- }
- /**
- * 设置页面标题
- * @param title
- */
- export const setPageTitle = (title = '标题') => {
- uni.setNavigationBarTitle({
- title
- });
- }
- /**
- * 获取指定页面信息
- * @param num
- * @returns {Page.PageInstance<AnyObject, {}>}
- */
- export const getBeforePage = (num = 1) => {
- let pages = getCurrentPages(); // 当前页面
- let index = pages.length - 1
- let beforePage = pages[index - num]; // 前一个页面
- return beforePage
- }
- /**
- * 锁定滚动
- * @param pageStyle
- * @returns {string}
- */
- export const lockScroll = (pageStyle) => {
- return setPageStyle(pageStyle, {
- overflow: 'hidden',
- 'max-height': '100vh', // h5 问题
- })
- }
- /**
- * 解除锁定滚动
- * @param pageStyle
- * @returns {string}
- */
- export const unLockScroll = (pageStyle) => {
- return deletePageStyle(pageStyle, ['overflow', 'max-height'])
- }
- export const adMethod = (v) => {
- if (!v.method) return !1
- switch (v.method) {
- case 'adgo':
- if (v.line && checkLogin()) {
- uni.navigateTo({
- url: v.line,
- fail: function () {
- uni.switchTab({
- url: v.line
- })
- }
- })
- }
- break;
- }
- }
|