mixin.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 { mapState } from 'vuex'
  8. Vue.mixin({
  9. data () {
  10. return {
  11. initScroll: null
  12. }
  13. },
  14. computed: {
  15. user() {
  16. return this.$store.state.option.user
  17. },
  18. sortEnterprises () {
  19. if (this.user.data.enterprises) {
  20. let ens = this.user.data.enterprises.slice()
  21. if (ens && ens.length) {
  22. ens.sort(function (a, b) {
  23. return b.lastLoginTime - a.lastLoginTime
  24. })
  25. }
  26. return ens
  27. } else {
  28. return ''
  29. }
  30. },
  31. // 判断是否erp嵌入
  32. isInFrame () {
  33. if (this.$route.query.type === 'erp') {
  34. this.$store.commit('option/ADD_COOKIES', 'type=erp;')
  35. return true
  36. } else {
  37. let cookies = this.$store.state.option.cookies
  38. let cookieArr = cookies.split(';')
  39. let cookieObj = {}
  40. for (let i = 0; i < cookieArr.length; i++) {
  41. if (cookieArr[i].indexOf('=') > -1) {
  42. let tmpArr = cookieArr[i].split('=')
  43. cookieObj[tmpArr[0].trim()] = tmpArr[1].trim()
  44. }
  45. }
  46. return cookieObj.type === 'erp'
  47. }
  48. },
  49. currentEnName () {
  50. if (this.user.data.enterprise) {
  51. return this.user.data.enterprise.uu ? this.user.data.enterprise.enName : this.user.data.userName + '(个人账户)'
  52. } else {
  53. return {}
  54. }
  55. },
  56. baseUtils () {
  57. return baseUtils
  58. }
  59. },
  60. methods: {
  61. goLastPage: function () {
  62. window.history.back(-1)
  63. },
  64. preventTouchMove (e) {
  65. e.preventDefault()
  66. },
  67. stopPropagation: function (e) {
  68. if (e) {
  69. e.stopPropagation()
  70. }
  71. },
  72. _initscroll() {
  73. if (!this.initScroll) {
  74. this.initScroll = new BScroll(this.$refs.mobileModalBox, {
  75. click: true
  76. })
  77. } else {
  78. this.initScroll.destroy()
  79. this.initScroll = new BScroll(this.$refs.mobileModalBox, {
  80. click: true
  81. })
  82. }
  83. },
  84. login: function (url) {
  85. this.$router.push(`/auth/login${url ? '?returnUrl=' + url : ''}`)
  86. },
  87. goStoreApply: function () {
  88. if (this.user.logged) {
  89. this.$http.get('/basic/vendor/transactionInfo').then(response => {
  90. if (response.data.isOpenStore) {
  91. window.location.href = '/vendor#/store/maintain'
  92. } else {
  93. window.location.href = '/vendor#/store-apply'
  94. }
  95. }, err => {
  96. this.$message.error('该账户未开通卖家中心')
  97. console.log(err)
  98. })
  99. } else {
  100. this.login()
  101. }
  102. }
  103. },
  104. filters: {
  105. time: function (time) {
  106. if (typeof time === 'number') {
  107. if (!time) {
  108. return '无'
  109. } else {
  110. let d = new Date(time)
  111. let year = d.getFullYear()
  112. let month = d.getMonth() + 1
  113. let day = d.getDate() < 10 ? '0' + d.getDate() : '' + d.getDate()
  114. let hour = d.getHours() < 10 ? '0' + d.getHours() : '' + d.getHours()
  115. let minutes = d.getMinutes() < 10 ? '0' + d.getMinutes() : '' + d.getMinutes()
  116. let seconds = d.getSeconds() < 10 ? '0' + d.getSeconds() : '' + d.getSeconds()
  117. return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds
  118. }
  119. }
  120. }
  121. }
  122. })