import store from '@/store' import * as util from '@/utils/util' /** * 打开地图查看位置 * @param {Object} = options * options.lat 经度 * options.lon 纬度 * options.name 名字 * options.address 地址 */ export const openAddressMap = (options, cb) => { if (!options.lat || !options.lon) return console.warn('请传入经纬度'); uni.openLocation({ latitude: Number(options.lat), longitude: Number(options.lon), name: options.name || '', address: options.address || '', complete: res => cb ? cb() : null, }); } /** * 富文本图片格式化 * * @param {string} 路径 content:我是富文本内容 */ export const formatRich = (content) => { let res = '' if (content) { res = content.replace(new RegExp(' { if (isLogin) { if (checkLogin()) { uni.navigateTo({ url: `/${url}`, success: res => { res.eventChannel.emit(eventName, eventData) } }) } else { navTo('pages/login/index') } } else { uni.navigateTo({ url: `/${url}`, success: res => { res.eventChannel.emit(eventName, eventData) } }) } } /** * 跳转传参 * @param {boolean} isLogin:true * @param {string} path:'' * @param {object} params:{} * @param {Function} cellback:()=>{} */ export const navToPage = (isLogin = false, path = '', params = {}, cellback) => { if (isLogin) { if (checkLogin()) { if (cellback) cellback else navTo(path, params) } else { navTo('pages/login/index') } } else { if (cellback) cellback else navTo(path, params) } return { isLogin, path, params, cellback } } /** * 图片预览,传入current:当前索引和urls图片列表 * @param {Number} current:0 * @param {Array|string} urls:['']|'' */ export const previewImage = async (current = 0, urls) => { if (typeof urls == 'string') urls = [urls] uni.previewImage({ current, urls, }) } /** * 文本复制 * @param {Number} data:'要复制的文本' */ export const copyText = (data, showToast = true) => { uni.setClipboardData({ data, showToast, }); } /** * 电话拨号 * @param {string} phoneNumber:'15011111111' */ export const cellPhone = (phoneNumber, cb) => { uni.makePhoneCall({ phoneNumber, success: (res) => { if (cb) cb(res) } }) } /** * 二次确认框 * @param {String} title - "标题" * @param {String} content - "内容" * @param {Function} confirm - 确认 * @param {Function} cancel - 取消 */ export const showModel = (options) => { uni.showModal({ title: options.title || '温馨提示', content: options.content, showCancel: options.showCancel || true, success(res) { if (res.confirm) { options.confirm && options.confirm(); } if (res.cancel) { options.cancel && options.cancel(); } } }) } /** * 显示成功提示框 */ export const showSuccess = (msg, callback) => { uni.showToast({ title: msg, icon: 'success', mask: true, duration: 1500, success() { callback && callback() } }) } /** * 显示失败提示框 */ export const showError = (msg, callback) => { uni.showModal({ title: '友情提示', content: msg, showCancel: false, success(res) { callback && callback() } }) } /** * 显示纯文字提示框 */ export const showToast = (msg, icon = 'none', duration = 1500, mask = true) => { uni.showToast({ title: msg, // 提示的内容 icon, mask, // 是否显示透明蒙层,防止触摸穿透 duration // 提示的延迟时间,单位毫秒,默认:1500 }) } /** * tabBar页面路径列表 (用于链接跳转时判断) * tabBarLinks为常量, 无需修改 */ export const getTabBarLinks = () => { const tabBarLinks = [ 'pages/home/index', 'pages/camp/index', 'pages/games/index', 'pages/user/index', ] return tabBarLinks } /** * 生成转发的url参数(string格式) */ export const getShareUrlParams = (params) => { return util.urlEncode({ refereeId: store.getters.userId, // 推荐人ID ...params }) } /** * 跳转到指定页面url * 支持tabBar页面 * @param {string} url 页面路径 * @param {object} query 页面参数 * @param {string} modo 跳转类型 默认:navigateTo) 可选:redirectTo */ export const navTo = (url, query = {}, modo = 'navigateTo') => { if (!url || url.length == 0) { return false } // tabBar页面, 使用switchTab if (util.inArray(url, getTabBarLinks())) { uni.switchTab({ url: `/${url}` }) return true } // 生成query参数 const queryStr = query ? '?' + util.urlEncode(query) : '' // 普通页面, 使用navigateTo modo === 'navigateTo' && uni.navigateTo({ url: `/${url}${queryStr}`, }) // 特殊指定, 使用redirectTo modo === 'redirectTo' && uni.redirectTo({ url: `/${url}${queryStr}`, }) return true } /** * 退回页面 * @param {integer} page 页面数量 */ export const navBack = (page = 1) => { uni.navigateBack({ delta: page }); } /** * 验证是否已登录 */ export const checkLogin = () => { return !!store.getters.userId } /** * 发起支付请求 微信小支付 * @param {Object} 参数 */ export const wxPayment = (option) => { const options = { timeStamp: '', nonceStr: '', prepay_id: '', paySign: '', ...option } return new Promise((resolve, reject) => { uni.requestPayment({ provider: 'wxpay', timeStamp: options.timeStamp, nonceStr: options.nonceStr, 'package': `prepay_id=${options.prepay_id}`, signType: 'MD5', paySign: options.paySign, success: res => resolve(res), fail: res => reject(res) }) }) }