app.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import store from '@/store'
  2. import * as util from '@/utils/util'
  3. import { paginate } from '@/common/constant'
  4. /**
  5. * 获取当前运行的终端(App H5 小程序)
  6. * https://uniapp.dcloud.io/platform
  7. */
  8. export const getPlatform = () => {
  9. // #ifdef APP-PLUS
  10. const platform = 'App'
  11. // #endif
  12. // #ifdef APP-PLUS-NVUE
  13. const platform = 'App'
  14. // #endif
  15. // #ifdef H5
  16. const platform = 'H5'
  17. // #endif
  18. // #ifdef MP-WEIXIN
  19. const platform = 'MP-WEIXIN'
  20. // #endif
  21. // #ifdef MP-ALIPAY
  22. const platform = 'MP-ALIPAY'
  23. // #endif
  24. // #ifdef MP-BAIDU
  25. const platform = 'MP-BAIDU'
  26. // #endif
  27. // #ifdef MP-TOUTIAO
  28. const platform = 'MP-TOUTIAO'
  29. // #endif
  30. // #ifdef MP-QQ
  31. const platform = 'MP-QQ'
  32. // #endif
  33. // #ifdef MP-360
  34. const platform = 'MP-360'
  35. // #endif
  36. return platform
  37. }
  38. /**
  39. * 显示成功提示框
  40. */
  41. export const showSuccess = (msg, callback) => {
  42. uni.showToast({
  43. title: msg,
  44. icon: 'success',
  45. mask: true,
  46. duration: 1500,
  47. success() {
  48. callback && callback()
  49. }
  50. })
  51. }
  52. /**
  53. * 显示失败提示框
  54. */
  55. export const showError = (msg, callback) => {
  56. uni.showModal({
  57. title: '友情提示',
  58. content: msg,
  59. showCancel: false,
  60. success(res) {
  61. callback && callback()
  62. }
  63. })
  64. }
  65. /**
  66. * 显示纯文字提示框
  67. */
  68. export const showToast = msg => {
  69. uni.showToast({
  70. title: msg,
  71. icon: 'none'
  72. })
  73. }
  74. /**
  75. * tabBar页面路径列表 (用于链接跳转时判断)
  76. * tabBarLinks为常量, 无需修改
  77. */
  78. export const getTabBarLinks = () => {
  79. const tabBarLinks = [
  80. 'pages/index/index',
  81. 'pages/category/index',
  82. 'pages/cart/index',
  83. 'pages/user/index'
  84. ]
  85. return tabBarLinks
  86. }
  87. /**
  88. * 生成完整的H5地址 [带参数]
  89. * @param {string} h5Url H5访问地址
  90. * @param {string} path 页面路径
  91. * @param {object} params 页面参数
  92. * @return {string}
  93. */
  94. export const buildUrL = (h5Url, path, params) => {
  95. let complete = h5Url
  96. if (!util.isEmpty(path)) {
  97. complete += '#/' + path
  98. const shareParamsStr = getShareUrlParams(params)
  99. if (!util.isEmpty(shareParamsStr)) {
  100. complete += '?' + shareParamsStr
  101. }
  102. }
  103. return complete
  104. }
  105. /**
  106. * 生成转发的url参数(string格式)
  107. */
  108. export const getShareUrlParams = (params) => {
  109. return util.urlEncode({
  110. refereeId: store.getters.userId, // 推荐人ID
  111. ...params
  112. })
  113. }
  114. /**
  115. * 跳转到指定页面url
  116. * 支持tabBar页面
  117. * @param {string} url
  118. * @param {object} query
  119. */
  120. export const navTo = (url, query = {}) => {
  121. if (!url || url.length == 0) {
  122. return false
  123. }
  124. // tabBar页面, 使用switchTab
  125. if (util.inArray(url, getTabBarLinks())) {
  126. uni.switchTab({
  127. url: `/${url}`
  128. })
  129. return true
  130. }
  131. // 生成query参数
  132. const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''
  133. // 普通页面, 使用navigateTo
  134. uni.navigateTo({
  135. url: `/${url}${queryStr}`
  136. })
  137. return true
  138. }
  139. /**
  140. * 获取购物车商品总数量
  141. * @param {*} value
  142. */
  143. export const getCartTotalNum = (value) => {
  144. const cartTotal = uni.getStorageSync('cartTotalNum') || 0
  145. return checkLogin() ? cartTotal : 0
  146. }
  147. /**
  148. * 记录购物车商品总数量
  149. * @param {*} value
  150. */
  151. export const setCartTotalNum = (value) => {
  152. uni.setStorageSync('cartTotalNum', Number(value))
  153. }
  154. /**
  155. * 设置购物车tabbar的角标
  156. * 该方法只能在tabbar页面中调用, 其他页面调用会报错
  157. */
  158. export const setCartTabBadge = () => {
  159. const cartTabbarIndex = 2
  160. const cartTotal = getCartTotalNum()
  161. if (cartTotal > 0) {
  162. uni.setTabBarBadge({
  163. index: cartTabbarIndex,
  164. text: `${cartTotal}`
  165. })
  166. } else {
  167. uni.removeTabBarBadge({
  168. index: cartTabbarIndex
  169. })
  170. }
  171. return
  172. }
  173. /**
  174. * 验证是否已登录
  175. */
  176. export const checkLogin = () => {
  177. return !!store.getters.userId
  178. }
  179. /**
  180. * 发起支付请求
  181. * @param {Object} 参数
  182. */
  183. export const wxPayment = (option) => {
  184. const options = {
  185. timeStamp: '',
  186. nonceStr: '',
  187. prepay_id: '',
  188. paySign: '',
  189. ...option
  190. }
  191. return new Promise((resolve, reject) => {
  192. uni.requestPayment({
  193. provider: 'wxpay',
  194. timeStamp: options.timeStamp,
  195. nonceStr: options.nonceStr,
  196. 'package': `prepay_id=${options.prepay_id}`,
  197. signType: 'MD5',
  198. paySign: options.paySign,
  199. success: res => resolve(res),
  200. fail: res => reject(res)
  201. })
  202. })
  203. }
  204. /**
  205. * 加载更多列表数据
  206. * @param {Object} resList 新列表数据
  207. * @param {Object} oldList 旧列表数据
  208. * @param {int} pageNo 当前页码
  209. */
  210. export const getEmptyPaginateObj = () => {
  211. return util.cloneObj(paginate)
  212. }
  213. /**
  214. * 加载更多列表数据
  215. * @param {Object} resList 新列表数据
  216. * @param {Object} oldList 旧列表数据
  217. * @param {int} pageNo 当前页码
  218. */
  219. export const getMoreListData = (resList, oldList, pageNo) => {
  220. // 如果是第一页需手动制空列表
  221. if (pageNo == 1) oldList.data = []
  222. // 合并新数据
  223. return oldList.data.concat(resList.data)
  224. }