mixin.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* 目前nuxt 版本如果不高于 1.0.0 并不支持vuex的方法,因此获取相对于的东西只能通过this.$store.state来获取
  2. 如果nuxt框架为 1.0.0以上,vuex属性状态这里可以优化
  3. */
  4. import BScroll from 'better-scroll'
  5. import Vue from 'vue'
  6. import baseUtils from '~utils/baseUtils'
  7. import baseUrls from '~utils/baseUrls'
  8. // import { mapState } from 'vuex'
  9. Vue.mixin({
  10. data () {
  11. return {
  12. initScroll: null
  13. }
  14. },
  15. computed: {
  16. user() {
  17. return this.$store.state.option.user
  18. },
  19. sortEnterprises () {
  20. if (this.user.data.enterprises) {
  21. let ens = this.user.data.enterprises.slice()
  22. if (ens && ens.length) {
  23. ens.sort(function (a, b) {
  24. return b.lastLoginTime - a.lastLoginTime
  25. })
  26. }
  27. return ens
  28. } else {
  29. return ''
  30. }
  31. },
  32. // 判断是否erp嵌入
  33. isInFrame () {
  34. if (this.$route.query.type === 'erp') {
  35. this.$store.commit('option/ADD_COOKIES', 'type=erp;')
  36. return true
  37. } else {
  38. let cookies = this.$store.state.option.cookies
  39. let cookieArr = cookies.split(';')
  40. let cookieObj = {}
  41. for (let i = 0; i < cookieArr.length; i++) {
  42. if (cookieArr[i].indexOf('=') > -1) {
  43. let tmpArr = cookieArr[i].split('=')
  44. cookieObj[tmpArr[0].trim()] = tmpArr[1].trim()
  45. }
  46. }
  47. return cookieObj.type === 'erp'
  48. }
  49. },
  50. currentEnName () {
  51. if (this.user.data.enterprise) {
  52. return this.user.data.enterprise.uu ? this.user.data.enterprise.enName : this.user.data.userName + '(个人账户)'
  53. } else {
  54. return {}
  55. }
  56. },
  57. baseUtils () {
  58. return baseUtils
  59. },
  60. baseUrls () {
  61. return baseUrls
  62. },
  63. isMobile () {
  64. return this.$store.state.option.isMobile
  65. }
  66. },
  67. methods: {
  68. goLastPage: function () {
  69. window.history.back(-1)
  70. },
  71. preventTouchMove (e) {
  72. e.preventDefault()
  73. },
  74. stopPropagation: function (e) {
  75. if (e) {
  76. e.stopPropagation()
  77. }
  78. },
  79. _initscroll() {
  80. if (!this.initScroll) {
  81. this.initScroll = new BScroll(this.$refs.mobileModalBox, {
  82. click: true
  83. })
  84. } else {
  85. this.initScroll.destroy()
  86. this.initScroll = new BScroll(this.$refs.mobileModalBox, {
  87. click: true
  88. })
  89. }
  90. },
  91. login: function (url) {
  92. this.$router.push(`/auth/login${url ? '?returnUrl=' + url : ''}`)
  93. },
  94. goStoreApply: function () {
  95. if (this.user.logged) {
  96. if (this.user.data.enterprise.uu) {
  97. this.$http.get('/basic/vendor/transactionInfo').then(response => {
  98. if (response.data.isOpenStore) {
  99. window.location.href = '/vendor#/store/maintain'
  100. } else {
  101. window.location.href = '/vendor#/store-apply'
  102. }
  103. }, err => {
  104. this.$message.error('该账户未开通卖家中心')
  105. console.log(err)
  106. })
  107. } else {
  108. this.$router.push('/personalMaterial')
  109. }
  110. } else {
  111. this.login()
  112. }
  113. },
  114. authorityInterceptor: function (url, callback) {
  115. this.baseUtils.getAuthority(this, url, callback, this.isMobile)
  116. }
  117. },
  118. filters: {
  119. time: function (time) {
  120. if (typeof time === 'number') {
  121. if (!time) {
  122. return '无'
  123. } else {
  124. let d = new Date(time)
  125. let year = d.getFullYear()
  126. let month = d.getMonth() + 1
  127. let day = d.getDate() < 10 ? '0' + d.getDate() : '' + d.getDate()
  128. let hour = d.getHours() < 10 ? '0' + d.getHours() : '' + d.getHours()
  129. let minutes = d.getMinutes() < 10 ? '0' + d.getMinutes() : '' + d.getMinutes()
  130. let seconds = d.getSeconds() < 10 ? '0' + d.getSeconds() : '' + d.getSeconds()
  131. return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds
  132. }
  133. }
  134. }
  135. }
  136. })