123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- // 不含icon提示框
- export const toast = str => {
- return new Promise((resolve, reject) => {
- if (str.length < 20) {
- uni.showToast({
- title: str,
- icon: "none",
- duration: 1500,
- success: () => {
- setTimeout(() => {
- resolve
- }, 1500)
- }
- })
- } else {
- uni.showModal({
- title: "提示",
- content: String(str),
- showCancel: false,
- confirmText: "我知道了",
- success(res) {
- if (res.confirm) {
- resolve(res);
- } else {
- reject();
- }
- }
- });
- }
- })
- };
- // 成功提示框
- export const successToast = (str) => {
- return new Promise((resolve, reject) => {
- uni.showToast({
- title: str || '请求成功',
- icon: 'success',
- duration: 1500,
- success: () => {
- setTimeout(() => {
- resolve();
- }, 1500);
- },
- });
- });
- };
- // loading
- export const showLoading = () => {
- return new Promise((resolve, reject) => {
- uni.showLoading({
- success: () => {
- resolve();
- },
- });
- });
- };
- // 提示loading
- export const tipLoading = (str) => {
- return new Promise((resolve, reject) => {
- uni.showLoading({
- title: str,
- success: () => {
- resolve();
- },
- });
- });
- };
- // 隐藏loading
- export const hideLoading = () => {
- return new Promise((resolve, reject) => {
- uni.hideLoading({
- success: () => {
- resolve();
- },
- });
- });
- };
- // 模态弹窗
- export const modal = (options = {}) => {
- if (!options) return;
- const {
- title,
- content,
- showCancel,
- cancelText,
- cancelColor,
- confirmText,
- confirmColor,
- } = Object.assign({}, options.content ? options : {
- content: options
- });
- return new Promise((resolve, reject) => {
- uni.showModal({
- title: title || '提示',
- content: String(content),
- showCancel: typeof showCancel == 'boolean' ? showCancel : true,
- cancelText: cancelText || '取消',
- cancelColor: cancelColor || '#323233',
- confirmText: confirmText || '确定',
- confirmColor: confirmColor || '#3D8FFD',
- complete(res) {
- if (res.confirm) {
- resolve(res);
- } else {
- reject();
- }
- },
- });
- });
- };
- /**
- * 跳转页面 navigateTo
- * 保留当前页面,跳转到应用内的某个页面
- * @param {String} url 需要跳转的页面
- * @param {Boolean} isAuth 是否需要鉴权
- * @returns
- */
- export const navPage = (url, isAuth = 0) => {
- if ((isAuth && store.state.isLogin) || !isAuth) {
- uni.navigateTo({
- url,
- fail: (err) => {
- console.log('页面跳转失败', url, err);
- },
- });
- } else {
- uni.navigateTo({
- url: '/pages/login/index',
- });
- }
- };
- /**
- * 跳转页面 redirectTo
- * 关闭当前页面,跳转到应用内的某个页面
- * @param {String} url 需要跳转的页面
- * @param {Boolean} isAuth 是否需要鉴权
- * @returns
- */
- export const redPage = (url, isAuth = 0) => {
- if ((isAuth && store.state.isLogin) || !isAuth) {
- uni.redirectTo({
- url,
- fail: (err) => {
- console.log('页面跳转失败', url, err);
- },
- });
- } else {
- uni.navigateTo({
- url: '/pages/login/index',
- });
- }
- };
- /**
- * 返回页面
- * @param {Number} num 返回的页面数
- * @param {Number} time 延时返回
- * @returns
- */
- export const backPage = (num = 1, time = 0) => {
- if (!num) return false;
- setTimeout(() => {
- uni.navigateBack({
- delta: num,
- });
- }, time);
- };
- /**
- * 拨打电话
- * @param {String} phone 用户隐私号
- * @param {String} orderId 服务单id,如果需要请求记录接口
- */
- export const callPhone = (phone) => {
- if (!phone)
- return modal({
- content: '手机号码不存在',
- showCancel: false,
- })
- .then(() => {})
- .catch(() => {});
- uni.makePhoneCall({
- phoneNumber: phone,
- success: () => {
- // logCallPhone(orderId);
- },
- });
- };
- /**
- * 复制
- * @param {String} val 复制内容
- */
- export const copy = (val) => {
- uni.setClipboardData({
- data: val,
- success: () => {
- successToast('复制成功');
- },
- });
- };
- // 获取当前定位
- export const getLocation = async function() {
- return new Promise((resolve, reject) => {
- uni.getLocation({
- type: 'gcj02',
- geocode: true,
- success: (res) => {
- resolve(res);
- },
- fail: (err) => {
- console.log('获取当前定位失败', err);
- uni.authorize({
- scope: 'scope.userLocation',
- success: () => {
- // 允许授权
- uni.getLocation({
- type: 'gcj02',
- geocode: true,
- success: (res) => {
- resolve(res);
- },
- fail: (err) => {
- modal({
- title: '提示',
- content: '定位失败,请重试',
- showCancel: false,
- }).then(() => {});
- },
- });
- },
- fail: () => {
- // 拒绝授权
- modal({
- title: '提示',
- content: '定位失败,请重试',
- showCancel: false,
- // confirmText: '重新定位',
- }).then(() => {
- // getLocation();
- });
- },
- });
- },
- });
- });
- };
- // 获取地址
- export const getAddress = async function() {
- const location = await getLocation();
- return new Promise((resolve, reject) => {
- uni.request({
- url: 'https://restapi.amap.com/v3/geocode/regeo',
- method: 'GET',
- data: {
- location: location.longitude + ',' + location.latitude,
- key: 'b772f8b0ace6bc96c04ae8e48f241e36',
- },
- success: (res) => {
- resolve({
- longitude: location.longitude,
- latitude: location.latitude,
- address: res.data.regeocode.formatted_address,
- province: res.data.regeocode.addressComponent.province,
- city: res.data.regeocode.addressComponent.city,
- area: res.data.regeocode.addressComponent.district,
- street: res.data.regeocode.addressComponent.township,
- });
- },
- fail: function(err) {
- console.log('地址解析失败' + err);
- },
- });
- });
- };
- // 判断微信环境
- export function isWeixin() {
- if (navigator && navigator.userAgent) {
- var ua = navigator.userAgent.toLowerCase();
- if (ua.indexOf('micromessenger') != -1) {
- return true;
- } else {
- return false;
- }
- } else {
- return false
- }
- }
- // 解析地址栏参数
- export function getQueryVariable(variable) {
- // 从?开始获取后面的所有数据
- var query = window.location.search.substring(1);
- // 从字符串&开始分隔成数组split
- var vars = query.split('&');
- // 遍历该数组
- for (var i = 0; i < vars.length; i++) {
- // 从等号部分分割成字符
- var pair = vars[i].split('=');
- // 如果第一个元素等于 传进来的参的话 就输出第二个元素
- if (pair[0] == variable) {
- return pair[1];
- }
- }
- return undefined;
- }
- export default {
- toast,
- successToast,
- showLoading,
- tipLoading,
- hideLoading,
- modal,
- navPage,
- redPage,
- backPage,
- callPhone,
- copy,
- getLocation,
- getAddress,
- };
|