mixin.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 {deepCopy, goLinkUser, filterStringEllipsis} 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 {
  58. deepCopy: deepCopy,
  59. goLinkUser: goLinkUser,
  60. filterStringEllipsis: filterStringEllipsis
  61. }
  62. }
  63. },
  64. methods: {
  65. goLastPage: function () {
  66. window.history.back(-1)
  67. },
  68. preventTouchMove (e) {
  69. e.preventDefault()
  70. },
  71. stopPropagation: function (e) {
  72. if (e) {
  73. e.stopPropagation()
  74. }
  75. },
  76. _initscroll() {
  77. if (!this.initScroll) {
  78. this.initScroll = new BScroll(this.$refs.mobileModalBox, {
  79. click: true
  80. })
  81. } else {
  82. this.initScroll.destroy()
  83. this.initScroll = new BScroll(this.$refs.mobileModalBox, {
  84. click: true
  85. })
  86. }
  87. },
  88. login: function (url) {
  89. this.$router.push(`/auth/login${url ? '?returnUrl=' + url : ''}`)
  90. },
  91. goStoreApply: function () {
  92. if (this.user.logged) {
  93. this.$http.get('/basic/vendor/transactionInfo').then(response => {
  94. if (response.data.isOpenStore) {
  95. window.location.href = '/vendor#/store/maintain'
  96. } else {
  97. window.location.href = '/vendor#/store-apply'
  98. }
  99. }, err => {
  100. this.$message.error('该账户未开通卖家中心')
  101. console.log(err)
  102. })
  103. } else {
  104. this.login()
  105. }
  106. }
  107. },
  108. filters: {
  109. time: function (time) {
  110. if (typeof time === 'number') {
  111. if (!time) {
  112. return '无'
  113. } else {
  114. let d = new Date(time)
  115. let year = d.getFullYear()
  116. let month = d.getMonth() + 1
  117. let day = d.getDate() < 10 ? '0' + d.getDate() : '' + d.getDate()
  118. let hour = d.getHours() < 10 ? '0' + d.getHours() : '' + d.getHours()
  119. let minutes = d.getMinutes() < 10 ? '0' + d.getMinutes() : '' + d.getMinutes()
  120. let seconds = d.getSeconds() < 10 ? '0' + d.getSeconds() : '' + d.getSeconds()
  121. return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds
  122. }
  123. }
  124. }
  125. }
  126. })