123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- 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('<img', 'gi'),
- "<img style='width:100%;vertical-align: middle;'")
- res = res.replace(new RegExp('<p', 'gi'),
- "<img style='word-break: break-all;'")
- }
- return res
- }
- /**
- * 非tabbar页面自定义事件跳转传参(注:刷新丢失)
- * @param {boolean} 是否需要登录 isLogin:false
- * @param {string} 路径 url:'pages/home/index'
- * @param {string} 事件名 eventName:'sendData'
- * @param {object} 传参 eventData:{a:1}
- */
- export const navToPageParams = (isLogin = false, url, eventName = '', eventData = {}) => {
- 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>|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)
- })
- })
- }
|