app.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import store from '@/store'
  2. import * as util from '@/utils/util'
  3. import { paginate } from '@/common/constant'
  4. import Config from './config'
  5. /**
  6. * 显示成功提示框
  7. */
  8. export const showSuccess = (msg, callback) => {
  9. uni.showToast({
  10. title: msg,
  11. icon: 'success',
  12. // #ifndef MP-ALIPAY
  13. mask: true,
  14. // #endif
  15. duration: 1500,
  16. success() {
  17. callback && callback()
  18. }
  19. })
  20. }
  21. /**
  22. * 显示失败提示框
  23. */
  24. export const showError = (msg, callback) => {
  25. uni.showModal({
  26. title: '友情提示',
  27. content: msg,
  28. showCancel: false,
  29. success(res) {
  30. callback && callback()
  31. }
  32. })
  33. }
  34. /**
  35. * 显示纯文字提示框
  36. */
  37. export const showToast = (msg, duration = 1500, mask = true) => {
  38. uni.showToast({
  39. title: msg, // 提示的内容
  40. icon: 'none',
  41. // #ifndef MP-ALIPAY
  42. mask, // 是否显示透明蒙层,防止触摸穿透 (支付宝小程序不支持)
  43. // #endif
  44. duration // 提示的延迟时间,单位毫秒,默认:1500
  45. })
  46. }
  47. /**
  48. * tabBar页面路径列表 (用于链接跳转时判断)
  49. * tabBarLinks为常量, 无需修改
  50. */
  51. export const getTabBarLinks = () => {
  52. const tabBarLinks = [
  53. 'pages/index/index',
  54. 'pages/category/index',
  55. 'pages/cart/index',
  56. 'pages/user/index'
  57. ]
  58. return tabBarLinks
  59. }
  60. /**
  61. * 生成完整的H5地址 [带参数]
  62. * @param {string} baseUrl H5访问地址
  63. * @param {string} path 页面路径
  64. * @param {object} params 页面参数
  65. * @return {string}
  66. */
  67. export const buildUrL = (h5Url, path, params) => {
  68. let complete = h5Url
  69. if (!util.isEmpty(path)) {
  70. complete += '#/' + path
  71. const shareParamsStr = getShareUrlParams(params)
  72. if (!util.isEmpty(shareParamsStr)) {
  73. complete += '?' + shareParamsStr
  74. }
  75. }
  76. return complete
  77. }
  78. /**
  79. * 生成转发的url参数(string格式)
  80. * @param {object} params
  81. * @return {string}
  82. */
  83. export const getShareUrlParams = params => {
  84. return util.urlEncode(getShareParams(params))
  85. }
  86. /**
  87. * 生成转发的url参数(object格式)
  88. * @param {object} params
  89. * @return {object}
  90. */
  91. export const getShareParams = params => {
  92. return {
  93. refereeId: store.getters.userId, // 推荐人ID
  94. ...params
  95. }
  96. }
  97. /**
  98. * 跳转到指定页面url
  99. * 支持tabBar页面
  100. * @param {string} url 页面路径
  101. * @param {object} query 页面参数
  102. * @param {string} modo 跳转类型(默认navigateTo)
  103. */
  104. export const navTo = (url, query = {}, modo = 'navigateTo') => {
  105. if (!url || url.length == 0) {
  106. return false
  107. }
  108. // tabBar页面, 使用switchTab
  109. if (util.inArray(url, getTabBarLinks())) {
  110. uni.switchTab({
  111. url: `/${url}`
  112. })
  113. return true
  114. }
  115. // 生成query参数
  116. const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''
  117. // 普通页面, 使用navigateTo
  118. modo === 'navigateTo' && uni.navigateTo({
  119. url: `/${url}${queryStr}`
  120. })
  121. // 特殊指定, 使用redirectTo
  122. modo === 'redirectTo' && uni.redirectTo({
  123. url: `/${url}${queryStr}`
  124. })
  125. return true
  126. }
  127. /**
  128. * 获取当前页面数据
  129. * @param {object}
  130. */
  131. export const getCurrentPage = () => {
  132. const pages = getCurrentPages()
  133. const pathInfo = pages[pages.length - 1].$page.fullPath.split('?')
  134. return { path: pathInfo[0].slice(1), query: util.urlDecode(pathInfo[1]) }
  135. }
  136. /**
  137. * 获取购物车商品总数量
  138. * @param {*} value
  139. */
  140. export const getCartTotalNum = (value) => {
  141. const cartTotal = uni.getStorageSync('cartTotalNum') || 0
  142. return checkLogin() ? cartTotal : 0
  143. }
  144. /**
  145. * 记录购物车商品总数量
  146. * @param {*} value
  147. */
  148. export const setCartTotalNum = (value) => {
  149. uni.setStorageSync('cartTotalNum', Number(value))
  150. }
  151. /**
  152. * 设置购物车tabbar的角标
  153. * 该方法只能在tabbar页面中调用, 其他页面调用会报错
  154. */
  155. export const setCartTabBadge = () => {
  156. const cartTabbarIndex = 2
  157. const cartTotal = getCartTotalNum()
  158. if (cartTotal > 0) {
  159. uni.setTabBarBadge({
  160. index: cartTabbarIndex,
  161. text: `${cartTotal}`
  162. })
  163. } else {
  164. uni.removeTabBarBadge({
  165. index: cartTabbarIndex
  166. })
  167. }
  168. return
  169. }
  170. /**
  171. * 验证是否已登录
  172. */
  173. export const checkLogin = () => {
  174. return !!store.getters.userId
  175. }
  176. /**
  177. * 加载更多列表数据
  178. * @param {Object} resList 新列表数据
  179. * @param {Object} oldList 旧列表数据
  180. * @param {int} pageNo 当前页码
  181. */
  182. export const getEmptyPaginateObj = () => {
  183. return util.cloneObj(paginate)
  184. }
  185. /**
  186. * 加载更多列表数据
  187. * @param {Object} resList 新列表数据
  188. * @param {Object} oldList 旧列表数据
  189. * @param {int} pageNo 当前页码
  190. */
  191. export const getMoreListData = (resList, oldList, pageNo) => {
  192. // 如果是第一页需手动制空列表
  193. if (pageNo == 1) oldList.data = []
  194. // 合并新数据
  195. return oldList.data.concat(resList.data)
  196. }
  197. /**
  198. * scene解码
  199. * 用于解码微信小程序二维码参数,并返回对象
  200. */
  201. export const sceneDecode = (str) => {
  202. if (str === undefined)
  203. return {}
  204. const data = {}
  205. const params = decodeURIComponent(str).split(',')
  206. for (const i in params) {
  207. const val = params[i].split(':');
  208. val.length > 0 && val[0] && (data[val[0]] = val[1] || null)
  209. }
  210. return data
  211. }
  212. /**
  213. * 获取二维码场景值(scene)
  214. */
  215. export const getSceneData = query => {
  216. return util.hasOwnProperty(query, 'scene') ? sceneDecode(query.scene) : {}
  217. }
  218. /**
  219. * 验证指定的功能模块是否开启
  220. */
  221. export const checkModuleKey = moduleKey => {
  222. return util.inArray(moduleKey, store.getters.modules)
  223. }
  224. /**
  225. * 验证指定的功能模块是否开启(批量)
  226. */
  227. export const checkModules = moduleKeys => {
  228. return moduleKeys.filter(val => checkModuleKey(val)).length > 0
  229. }
  230. /**
  231. * 过滤开启的功能模块(批量)
  232. */
  233. export const filterModules = array => {
  234. return array.filter(item => !item.moduleKey || checkModuleKey(item.moduleKey))
  235. }
  236. /**
  237. * link对象点击事件
  238. * 支持tabBar页面
  239. */
  240. export const onLink = linkObj => {
  241. if (!linkObj) return false
  242. // 跳转到指定页面
  243. if (linkObj.type === 'PAGE') {
  244. navTo(linkObj.param.path, linkObj.param.query)
  245. }
  246. // 跳转到自定义路径
  247. if (linkObj.type === 'CUSTOM') {
  248. navTo(linkObj.param.path, util.urlDecode(linkObj.param.queryStr))
  249. }
  250. // 跳转到微信小程序
  251. // #ifdef MP-WEIXIN
  252. if (linkObj.type === 'MP-WEIXIN') {
  253. uni.navigateToMiniProgram({
  254. appId: linkObj.param.appId,
  255. path: linkObj.param.path
  256. })
  257. }
  258. // #endif
  259. // 跳转到H5外部链接
  260. if (linkObj.type === 'URL') {
  261. // #ifdef H5
  262. window.open(linkObj.param.url)
  263. // #endif
  264. // #ifdef APP-PLUS
  265. plus.runtime.openWeb(linkObj.param.url)
  266. // #endif
  267. // #ifdef MP-WEIXIN
  268. navTo('pages/web-view/index', { src: encodeURIComponent(linkObj.param.url) })
  269. // #endif
  270. // #ifdef MP
  271. // uni.setClipboardData({
  272. // data: linkObj.param.url,
  273. // success: () => showToast('链接已复制'),
  274. // fail: ({ errMsg }) => showToast('复制失败 ' + errMsg)
  275. // })
  276. // #endif
  277. }
  278. return true
  279. }
  280. // style变量样式转为字符串 (因小程序端不支持styleObject语法)
  281. export const styleObj2Str = appTheme => {
  282. let str = ''
  283. for (const index in appTheme) {
  284. const name = util.formatToLine(index)
  285. str += `--${name}:${appTheme[index]};`
  286. }
  287. return str
  288. }