mixin.js 3.4 KB

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