123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- import store from '../store'
- import * as util from './util'
- import { paginate } from '@/common/constant'
- import * as MessageApi from '@/api/message'
- export const getPlatform = () => {
-
- const platform = 'App'
-
-
- const platform = 'App'
-
-
- const platform = 'H5'
-
-
- const platform = 'MP-WEIXIN'
-
-
- const platform = 'MP-ALIPAY'
-
-
- const platform = 'MP-BAIDU'
-
-
- const platform = 'MP-TOUTIAO'
-
-
- const platform = 'MP-QQ'
-
-
- const platform = 'MP-360'
-
- return platform;
- }
- 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 => {
- uni.showToast({
- title: msg,
- icon: 'none'
- })
- }
- export const getTabBarLinks = () => {
- const tabBarLinks = [
- 'pages/index/index',
- 'pages/category/index',
- 'pages/order/index',
- 'pages/user/index'
- ]
- return tabBarLinks
- }
- export const getShareUrlParams = (params) => {
- return util.urlEncode({
- refereeId: store.getters.userId,
- ...params
- })
- }
- export const navTo = (url, query = {}) => {
- if (!url || url.length == 0) {
- return false
- }
-
- if (util.inArray(url, getTabBarLinks())) {
- uni.switchTab({
- url: `/${url}`
- })
- return true
- }
-
- const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''
-
- uni.navigateTo({
- url: `/${url}${queryStr}`
- })
- return true
- }
- export const setCartTotalNum = (value) => {
- uni.setStorageSync('cartTotalNum', Number(value))
- }
- export const setCartTabBadge = () => {
- const cartTabbarIndex = 1
- const cartTotal = uni.getStorageSync('cartTotalNum') || 0;
- if (cartTotal > 0) {
- uni.setTabBarBadge({
- index: cartTabbarIndex,
- text: `${cartTotal}`
- })
- } else {
- uni.removeTabBarBadge({
- index: cartTabbarIndex
- })
- }
- return
- }
- export const checkLogin = () => {
- return !!store.getters.userId;
- }
- export const needLogin = () => {
- const isLogin = checkLogin();
- if (!isLogin) {
- uni.showModal({
- title: '温馨提示',
- content: '此时此刻需要您登录喔~',
- confirmText: "去登录",
- cancelText: "再逛会",
- success: res => {
- if (res.confirm) {
- uni.navigateTo({
- url: "/pages/login/index"
- })
- }
- }
- })
- }
- }
- export const wxPayment = (option) => {
- const options = {
- timeStamp: '',
- nonceStr: '',
- prepay_id: '',
- paySign: '',
- ...option
- }
-
- if (isWechat()) {
- return new Promise((resolve, reject) => {
- wxH5Payment(options,
- function(res) {
- resolve(res);
- },
- function(err) {
- reject(err);
- });
- })
- }
-
-
- if (getPlatform() == 'H5' && option.mweb_url) {
- h5Pay(option.mweb_url, option.backUrl);
- return true;
- }
-
-
- return new Promise((resolve, reject) => {
- uni.requestPayment({
- provider: 'wxpay',
- timeStamp: options.timeStamp,
- nonceStr: options.nonceStr,
- 'package': options.package,
- signType: 'MD5',
- paySign: options.paySign,
- success: res => resolve(res),
- fail: res => reject(res)
- })
- })
- }
- export const getEmptyPaginateObj = () => {
- return util.cloneObj(paginate)
- }
- export const getMoreListData = (resList, oldList, pageNo) => {
-
- if (pageNo == 1) oldList.content = [];
-
- return oldList.content.concat(resList.content);
- }
- export const showMessage = () => {
- const app = this
- if (!store.getters.userId) {
- return false
- }
- return new Promise((resolve, reject) => {
- MessageApi.getOne()
- .then(result => {
- if (result.data.content) {
- uni.showModal({
- title: result.data.title ? result.data.title : '友情提示',
- content: result.data.content,
- showCancel: false,
- success(res) {
- if (res.confirm) {
-
- MessageApi.readed({'msgId' : result.data.msgId})
- .then(result => {
-
- })
- }
- }
- })
- }
- })
- })
- }
- export const isWechat = () => {
- try {
- const ua = window.navigator.userAgent.toLowerCase();
- if (ua.match(/micromessenger/i) == 'micromessenger') {
- return true;
- } else {
- return false;
- }
- } catch (e) {
- return false;
- }
- }
- export const wxH5Payment = (data, callback_succ_func, callback_error_func) => {
- if (!isWechat()) {
- return false;
- }
- if (typeof WeixinJSBridge == "undefined") {
- if (document.addEventListener) {
- document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
- } else if (document.attachEvent) {
- document.attachEvent('WeixinJSBridgeReady', jsApiCall);
- document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
- }
- } else {
- jsApiCall(data, callback_succ_func, callback_error_func);
- }
- }
- export const jsApiCall = (data, callback_succ_func, callback_error_func) => {
-
- WeixinJSBridge.invoke('getBrandWCPayRequest', {
- appId:data['appId'],
- timeStamp: data['timeStamp'],
- nonceStr: data['nonceStr'],
- package: data['package'],
- signType: data['signType'],
- paySign: data['paySign'],
- },
- function(res) {
- var msg = res.err_msg ?res.err_msg :res.errMsg;
-
- switch (msg) {
- case 'get_brand_wcpay_request:ok':
- if(callback_succ_func){
- callback_succ_func(res);
- }
- break;
- default:
- WeixinJSBridge.log('支付失败!'+msg+',请返回重试.');
- if(callback_error_func){
- callback_error_func({msg:msg});
- }
- break;
- }
- })
- }
- export const h5Pay = (url, backUrl) => {
-
- let redirectUrl ='&redirect_url=' + encodeURIComponent(backUrl);
-
- let linkUrl = url + redirectUrl
- const system = uni.getSystemInfoSync()
- if (system.platform == 'ios') {
-
- window.location.href = linkUrl;
- } else {
- window.open(linkUrl);
- }
- }
|