123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- // 对uni中的交互反馈相关api组件进行封装 showToast、showModal
- export function uniShowToast(title = '提示语', icon = "none", args = {}) {
- const {
- image = '',
- mask = false,
- duration = 1500,
- position,
- success, fail, complete
- } = args;
- let toast = {
- title,
- icon,
- duration
- };
- // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-BAIDU
- if (image) {
- toast['image'] = image
- }
- // #endif
- // #ifdef H5 || MP-WEIXIN
- if (mask) {
- toast['mask'] = mask
- }
- // #endif
- // #ifdef APP-PLUS
- if (position) {
- toast["position"] = position
- }
- // #endif
- uni.showToast({ ...toast,
- ...callBack(toast, {
- success,
- fail,
- complete
- })
- })
- }
- export function uniHideToast() {
- uni.hideToast()
- }
- export function uniHideLoading() {
- uni.hideLoading()
- }
- export function successToast(title = '成功', args = {}) {
- uniShowToast(title, "success", args)
- }
- export function loadingToast(title = '加载中', args = {}) {
- const {
- mask = true,
- success, fail, complete
- } = args;
- let loading = {
- title,
- };
- if (mask) {
- loading['mask'] = mask
- }
- uni.showLoading({
- ...loading,
- mask: true,
- ...callBack(loading, {
- success,
- fail,
- complete
- })
- });
- }
- export function uniShowModal(title, content, args = {}) {
- const {
- showCancel = true, cancelText = "取消", cancelColor, confirmText = "确定", confirmColor, success, fail, complete
- } = args;
- let modal = {
- title,
- content,
- showCancel,
- cancelText,
- confirmText
- }
- // #ifdef H5 || MP-WEIXIN || MP-BAIDU
- if (cancelColor) {
- modal['cancelColor'] = cancelColor
- }
- if (confirmColor) {
- modal['confirmColor'] = confirmColor
- }
- // #endif
- uni.showModal({ ...modal,
- ...callBack(modal, {
- success,
- fail,
- complete
- })
- })
- }
- export function uniNavigator(url, type = 'navigateTo', args = {}) {
- type = Number(url) ? 'navigateBack' : (type || 'navigateTo');
- const {
- endtime = Number(url) ? 600 : 200,
- animationType,
- animationDuration,
- success,
- fail,
- complete
- } = args;
- let obj = {
- url
- };
- // #ifdef APP-PLUS
- if (animationType) {
- obj['animationType'] = animationType
- }
- if (animationDuration) {
- obj['animationDuration'] = animationDuration
- }
- // #endif
- obj = callBack(obj, {
- success,
- fail,
- complete
- })
- console.log(obj)
- switch (type) {
- case 'navigateTo':
- setTimeout(function() {
- uni.navigateTo(obj)
- }, endtime);
- break;
- case 'redirectTo':
- setTimeout(function() {
- uni.redirectTo(obj)
- }, endtime);
- break;
- case 'reLaunch':
- setTimeout(function() {
- uni.reLaunch(obj)
- }, endtime);
- break;
- case 'switchTab':
- setTimeout(function() {
- uni.switchTab(obj)
- }, endtime);
- break;
- case 'navigateBack':
- setTimeout(function() {
- const history = getCurrentPages();
- if (history.length > 1) {
- uni.navigateBack({
- delta: Number(url)
- })
- } else {
- uni.switchTab({
- url: '/pages/index/index'
- })
- }
- }, endtime);
- break;
- }
- }
- export function uniStartPullDownRefresh(args = {}) {
- uni.startPullDownRefresh(args);
- }
- export function uniStopPullDownRefresh(endtime = 1000) {
- setTimeout(function() {
- uni.stopPullDownRefresh();
- }, endtime);
- }
- // 获取图片信息
- export function uniGetImageInfo(path) {
- return new Promise((resolve, reject) => {
- uni.getImageInfo({
- src: path,
- success: function(res) {
- console.log(res)
- resolve(res)
- },
- fail: function(err) {
- console.log(err)
- reject(err)
- },
- })
- })
- }
- // 获取节点信息
- export function uniSelectorQueryInfo(selector, _this) {
- return new Promise((resolve, reject) => {
- const query = uni.createSelectorQuery().in(_this);
- query.select(selector).boundingClientRect(res => {
- // 获取节点坐标
- resolve(res)
- }).exec();
- })
- }
- // 设置导航栏颜色
- export function uniSetNavigationBarColor(backgroundColor = "#e93323", frontColor = "#ffffff") {
- uni.setNavigationBarColor({
- frontColor,
- backgroundColor,
- animation: {
- duration: 400,
- timingFunc: 'easeIn'
- }
- })
- }
- // 跳转小程序
- export function uniNavigateToMiniProgram(appId, args = {}) {
- // #ifdef MP
- const {
- path,
- extraData = {
- from: '跳转小程序要携带的信息'
- },
- envVersion,
- success,
- fail,
- complete
- } = args;
- let obj = {
- path,
- extraData,
- envVersion
- }
- obj = callBack(obj, {
- success,
- fail,
- complete
- })
- uni.navigateToMiniProgram({
- appId: '',
- ...obj
- })
- // #endif
- }
- // 返回上一个小程序
- export function uniNavigateBackMiniProgram(args = {}) {
- // #ifdef MP
- const {
- extraData = {
- from: '返回上一个小程序要携带的信息'
- },
- success,
- fail,
- complete
- } = args;
- let obj = {
- extraData
- }
- obj = callBack(obj, {
- success,
- fail,
- complete
- })
- uni.navigateBackMiniProgram({
- ...obj
- })
- // #endif
- }
- function callBack(name = {}, options = {}) {
- const {
- success,
- fail,
- complete
- } = options;
- if (success) {
- name['success'] = (res) => {
- success(res)
- }
- }
- if (fail) {
- name['fail'] = (err) => {
- fail(err)
- }
- }
- if (complete) {
- name['complete'] = (res) => {
- complete(res)
- }
- }
- return name
- }
|