Browse Source

优化 app类库转移到core目录

Yahoo 4 years ago
parent
commit
e0b2725787

+ 16 - 0
.hbuilderx/launch.json

@@ -0,0 +1,16 @@
+{ // launch.json 配置了启动调试时相关设置,configurations下节点名称可为 app-plus/h5/mp-weixin/mp-baidu/mp-alipay/mp-qq/mp-toutiao/mp-360/
+  // launchtype项可配置值为local或remote, local代表前端连本地云函数,remote代表前端连云端云函数
+    "version": "0.0",
+    "configurations": [{
+     	"default" : 
+     	{
+     		"launchtype" : "remote"
+     	},
+     	"h5" : 
+     	{
+     		"launchtype" : "remote"
+     	},
+     	"type" : "uniCloud"
+     }
+    ]
+}

+ 1 - 1
components/shortcut/index.vue

@@ -30,7 +30,7 @@
 </template>
 
 <script>
-  import { getTabBarLinks } from '@/utils/app'
+  import { getTabBarLinks } from '@/core/app'
 
   export default {
 

+ 239 - 0
core/app.js

@@ -0,0 +1,239 @@
+import store from '@/store'
+import * as util from '@/utils/util'
+import { paginate } from '@/common/constant'
+
+/**
+ * 获取当前运行的终端(App H5 小程序)
+ * https://uniapp.dcloud.io/platform
+ */
+export const getPlatform = () => {
+  // #ifdef APP-PLUS
+  const platform = 'App'
+  // #endif
+  // #ifdef APP-PLUS-NVUE
+  const platform = 'App'
+  // #endif
+  // #ifdef H5
+  const platform = 'H5'
+  // #endif
+  // #ifdef MP-WEIXIN
+  const platform = 'MP-WEIXIN'
+  // #endif
+  // #ifdef MP-ALIPAY
+  const platform = 'MP-ALIPAY'
+  // #endif
+  // #ifdef MP-BAIDU
+  const platform = 'MP-BAIDU'
+  // #endif
+  // #ifdef MP-TOUTIAO
+  const platform = 'MP-TOUTIAO'
+  // #endif
+  // #ifdef MP-QQ
+  const platform = 'MP-QQ'
+  // #endif
+  // #ifdef MP-360
+  const platform = 'MP-360'
+  // #endif
+  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'
+  })
+}
+
+/**
+ * tabBar页面路径列表 (用于链接跳转时判断)
+ * tabBarLinks为常量, 无需修改
+ */
+export const getTabBarLinks = () => {
+  const tabBarLinks = [
+    'pages/index/index',
+    'pages/category/index',
+    'pages/cart/index',
+    'pages/user/index'
+  ]
+  return tabBarLinks
+}
+
+/**
+ * 生成完整的H5地址 [带参数]
+ * @param {string} h5Url H5访问地址
+ * @param {string} path 页面路径
+ * @param {object} params 页面参数
+ * @return {string}
+ */
+export const buildUrL = (h5Url, path, params) => {
+  let complete = h5Url
+  if (!util.isEmpty(path)) {
+    complete += '#/' + path
+    const shareParamsStr = getShareUrlParams(params)
+    if (!util.isEmpty(shareParamsStr)) {
+      complete += '?' + shareParamsStr
+    }
+  }
+  return complete
+}
+
+/**
+ * 生成转发的url参数(string格式)
+ */
+export const getShareUrlParams = (params) => {
+  return util.urlEncode({
+    refereeId: store.getters.userId, // 推荐人ID
+    ...params
+  })
+}
+
+/**
+ * 跳转到指定页面url
+ * 支持tabBar页面
+ * @param {string}  url
+ * @param {object}  query
+ */
+export const navTo = (url, query = {}) => {
+  if (!url || url.length == 0) {
+    return false
+  }
+  // tabBar页面, 使用switchTab
+  if (util.inArray(url, getTabBarLinks())) {
+    uni.switchTab({
+      url: `/${url}`
+    })
+    return true
+  }
+  // 生成query参数
+  const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''
+  // 普通页面, 使用navigateTo
+  uni.navigateTo({
+    url: `/${url}${queryStr}`
+  })
+  return true
+}
+
+/**
+ * 获取购物车商品总数量
+ * @param {*} value 
+ */
+export const getCartTotalNum = (value) => {
+  const cartTotal = uni.getStorageSync('cartTotalNum') || 0
+  return checkLogin() ? cartTotal : 0
+}
+
+/**
+ * 记录购物车商品总数量
+ * @param {*} value 
+ */
+export const setCartTotalNum = (value) => {
+  uni.setStorageSync('cartTotalNum', Number(value))
+}
+
+/**
+ * 设置购物车tabbar的角标
+ * 该方法只能在tabbar页面中调用, 其他页面调用会报错
+ */
+export const setCartTabBadge = () => {
+  const cartTabbarIndex = 2
+  const cartTotal = getCartTotalNum()
+  if (cartTotal > 0) {
+    uni.setTabBarBadge({
+      index: cartTabbarIndex,
+      text: `${cartTotal}`
+    })
+  } else {
+    uni.removeTabBarBadge({
+      index: cartTabbarIndex
+    })
+  }
+  return
+}
+
+/**
+ * 验证是否已登录
+ */
+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)
+    })
+  })
+}
+
+/**
+ * 加载更多列表数据
+ * @param {Object} resList 新列表数据
+ * @param {Object} oldList 旧列表数据
+ * @param {int} pageNo 当前页码
+ */
+export const getEmptyPaginateObj = () => {
+  return util.cloneObj(paginate)
+}
+
+/**
+ * 加载更多列表数据
+ * @param {Object} resList 新列表数据
+ * @param {Object} oldList 旧列表数据
+ * @param {int} pageNo 当前页码
+ */
+export const getMoreListData = (resList, oldList, pageNo) => {
+  // 如果是第一页需手动制空列表
+  if (pageNo == 1) oldList.data = []
+  // 合并新数据
+  return oldList.data.concat(resList.data)
+}

+ 1 - 1
core/bootstrap.js

@@ -1,6 +1,6 @@
 import store from '@/store'
 import storage from '@/utils/storage'
-import { getPlatform } from '@/utils/app'
+import { getPlatform } from '@/core/app'
 import { ACCESS_TOKEN, USER_ID } from '@/store/mutation-types'
 
 export default function Initializer() {

+ 1 - 1
main.js

@@ -10,7 +10,7 @@ import {
   showSuccess,
   showError,
   getShareUrlParams
-} from './utils/app'
+} from './core/app'
 import './js_sdk/ican-H5Api/ican-H5Api'
 
 Vue.config.productionTip = false

+ 1 - 1
pages/article/index.vue

@@ -56,7 +56,7 @@
   import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
   import * as ArticleApi from '@/api/article'
   import * as CategoryApi from '@/api/article/category'
-  import { getEmptyPaginateObj, getMoreListData } from '@/utils/app'
+  import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
 
   const pageSize = 15
 

+ 1 - 1
pages/cart/index.vue

@@ -85,7 +85,7 @@
 
 <script>
   import { inArray, arrayIntersect, debounce } from '@/utils/util'
-  import { checkLogin, setCartTotalNum, setCartTabBadge } from '@/utils/app'
+  import { checkLogin, setCartTotalNum, setCartTabBadge } from '@/core/app'
   import * as CartApi from '@/api/cart'
   import Empty from '@/components/empty'
 

+ 1 - 1
pages/category/index.vue

@@ -62,7 +62,7 @@
 </template>
 
 <script>
-  import { setCartTabBadge } from '@/utils/app'
+  import { setCartTabBadge } from '@/core/app'
   import SettingKeyEnum from '@/common/enum/setting/Key'
   import SettingModel from '@/common/model/Setting'
   import * as CategoryApi from '@/api/category'

+ 1 - 1
pages/checkout/index.vue

@@ -225,7 +225,7 @@
   import { CouponTypeEnum } from '@/common/enum/coupon'
   import DeliveryTypeEnum from '@/common/enum/order/DeliveryType'
   import PayTypeEnum from '@/common/enum/order/PayType'
-  import { wxPayment } from '@/utils/app'
+  import { wxPayment } from '@/core/app'
 
   const CouponColors = ['red', 'blue', 'violet', 'yellow']
 

+ 1 - 1
pages/comment/index.vue

@@ -43,7 +43,7 @@
   import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
   import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
     import AvatarImage from '@/components/avatar-image'
-  import { getEmptyPaginateObj, getMoreListData } from '@/utils/app'
+  import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
   import * as CommentApi from '@/api/comment'
 
   const pageSize = 15

+ 1 - 1
pages/goods/components/SkuPopup.vue

@@ -5,7 +5,7 @@
 </template>
 
 <script>
-  import { setCartTotalNum } from '@/utils/app'
+  import { setCartTotalNum } from '@/core/app'
   import * as CartApi from '@/api/cart'
   import GoodsSkuPopup from '@/components/goods-sku-popup'
 

+ 1 - 1
pages/goods/list.vue

@@ -89,7 +89,7 @@
   import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
   import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
   import * as GoodsApi from '@/api/goods'
-  import { getEmptyPaginateObj, getMoreListData } from '@/utils/app'
+  import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
   import Search from '@/components/search'
 
   const pageSize = 15

+ 1 - 1
pages/help/index.vue

@@ -18,7 +18,7 @@
   import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
   import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
   import * as HelpApi from '@/api/help'
-  import { getEmptyPaginateObj, getMoreListData } from '@/utils/app'
+  import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
 
   const pageSize = 15
 

+ 1 - 1
pages/index/index.vue

@@ -6,7 +6,7 @@
 </template>
 
 <script>
-  import { setCartTabBadge } from '@/utils/app'
+  import { setCartTabBadge } from '@/core/app'
   import * as Api from '@/api/page'
   import Page from '@/components/page'
 

+ 1 - 1
pages/my-coupon/index.vue

@@ -43,7 +43,7 @@
 <script>
   import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
   import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
-  import { getEmptyPaginateObj, getMoreListData } from '@/utils/app'
+  import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
   import * as MyCouponApi from '@/api/myCoupon'
   import { CouponTypeEnum } from '@/common/enum/coupon'
 

+ 1 - 1
pages/order/detail.vue

@@ -264,7 +264,7 @@
     ReceiptStatusEnum
   } from '@/common/enum/order'
   import * as OrderApi from '@/api/order'
-  import { wxPayment } from '@/utils/app'
+  import { wxPayment } from '@/core/app'
 
   export default {
     data() {

+ 2 - 2
pages/order/index.vue

@@ -136,9 +136,9 @@
   } from '@/common/enum/order'
   import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
   import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
-  import { getEmptyPaginateObj, getMoreListData } from '@/utils/app'
+  import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
   import * as OrderApi from '@/api/order'
-  import { wxPayment } from '@/utils/app'
+  import { wxPayment } from '@/core/app'
 
   // 每页记录数量
   const pageSize = 15

+ 1 - 1
pages/points/log.vue

@@ -25,7 +25,7 @@
   import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
   import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
   import * as LogApi from '@/api/points/log'
-  import { getEmptyPaginateObj, getMoreListData } from '@/utils/app'
+  import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
 
   const pageSize = 15
 

+ 1 - 1
pages/refund/index.vue

@@ -54,7 +54,7 @@
 <script>
   import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
   import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
-  import { getEmptyPaginateObj, getMoreListData } from '@/utils/app'
+  import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
   import * as RefundApi from '@/api/refund'
 
   // 每页记录数量

+ 2 - 2
pages/user/index.vue

@@ -126,12 +126,12 @@
 <script>
   import store from '@/store'
   import AvatarImage from '@/components/avatar-image'
-  import { setCartTabBadge } from '@/utils/app'
+  import { setCartTabBadge } from '@/core/app'
   import SettingKeyEnum from '@/common/enum/setting/Key'
   import SettingModel from '@/common/model/Setting'
   import * as UserApi from '@/api/user'
   import * as OrderApi from '@/api/order'
-  import { checkLogin } from '@/utils/app'
+  import { checkLogin } from '@/core/app'
 
   // 订单操作
   const orderNavbar = [

+ 1 - 1
pages/wallet/balance/log.vue

@@ -25,7 +25,7 @@
   import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
   import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
   import * as LogApi from '@/api/balance/log'
-  import { getEmptyPaginateObj, getMoreListData } from '@/utils/app'
+  import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
 
   const pageSize = 15
 

+ 1 - 1
pages/wallet/recharge/index.vue

@@ -53,7 +53,7 @@
   import * as PlanApi from '@/api/recharge/plan'
   import SettingModel from '@/common/model/Setting'
   import SettingKeyEnum from '@/common/enum/setting/Key'
-  import { wxPayment } from '@/utils/app'
+  import { wxPayment } from '@/core/app'
 
   export default {
     data() {

+ 1 - 1
pages/wallet/recharge/order.vue

@@ -25,7 +25,7 @@
   import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
   import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
   import * as OrderApi from '@/api/recharge/order'
-  import { getEmptyPaginateObj, getMoreListData } from '@/utils/app'
+  import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
 
   const pageSize = 15