uni-stat-tabs.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <template>
  2. <view class="uni-stat--tab-x">
  3. <view v-if="label" class="uni-label-text hide-on-phone">{{label + ':'}}</view>
  4. <view class="uni-stat--tab">
  5. <view v-if="!renderTabs.length" class="uni-stat--tab-item uni-stat--tab-item-disabled"
  6. :class="[`uni-stat--tab-item-${type}`]">
  7. {{placeholder}}
  8. </view>
  9. <view v-else v-for="(item, index) in renderTabs" :key="index" @click="change(item, index)"
  10. class="uni-stat--tab-item" :class="[
  11. index === currentTab ? `uni-stat--tab-item-${type}-active` : '' , `uni-stat--tab-item-${type}`,
  12. item.disabled ? 'uni-stat--tab-item-disabled' : ''
  13. ]">
  14. <!-- #ifdef MP -->
  15. {{item.name}}
  16. <!-- #endif -->
  17. <!-- #ifndef MP -->
  18. <uni-tooltip>
  19. {{item.name}}
  20. <uni-icons v-if="item.tooltip" type="help" color="#666" />
  21. <template v-if="item.tooltip" v-slot:content>
  22. <view class="uni-stat-tooltip-s">
  23. {{item.tooltip}}
  24. </view>
  25. </template>
  26. </uni-tooltip>
  27. <!-- #endif -->
  28. </view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. export default {
  34. name: "uni-stat-tabs",
  35. data() {
  36. return {
  37. currentTab: 0,
  38. renderTabs: []
  39. };
  40. },
  41. props: {
  42. type: {
  43. type: String,
  44. default: 'line'
  45. },
  46. value: {
  47. type: [String, Number],
  48. default: ''
  49. },
  50. modelValue: {
  51. type: [String, Number],
  52. default: ''
  53. },
  54. current: {
  55. type: [String, Number],
  56. default: 0
  57. },
  58. mode: {
  59. type: String,
  60. default: ''
  61. },
  62. today: {
  63. type: Boolean,
  64. default: false
  65. },
  66. yesterday: {
  67. type: Boolean,
  68. default: true
  69. },
  70. disabled: {
  71. type: Boolean,
  72. default: false
  73. },
  74. tooltip: {
  75. type: Boolean,
  76. default: false
  77. },
  78. all: {
  79. type: Boolean,
  80. default: true
  81. },
  82. label: {
  83. type: String,
  84. default: ''
  85. },
  86. placeholder: {
  87. type: String,
  88. default: '暂无选项'
  89. },
  90. tabs: {
  91. type: Array,
  92. default: () => {
  93. return []
  94. }
  95. }
  96. },
  97. created() {
  98. this.last = `${this.mode.replace('-', '_')}_last_data`
  99. },
  100. mounted() {
  101. this.init()
  102. },
  103. watch: {
  104. current(val) {
  105. this.currentTab = val
  106. },
  107. // value(val) {
  108. // this.currentTab = val
  109. // },
  110. tabs: {
  111. immediate: false,
  112. handler(val) {
  113. this.init()
  114. }
  115. },
  116. renderTabs(val) {
  117. const index = this.current
  118. if (this.mode && val.length && index >= 0) {
  119. this.$nextTick(function() {
  120. const item = this.renderTabs[index]
  121. this.change(item, index)
  122. })
  123. }
  124. }
  125. },
  126. methods: {
  127. init() {
  128. if (this.mode.indexOf('platform') > -1) {
  129. this.renderTabs = uni.getStorageSync(this.last)
  130. this.getPlatform()
  131. } else if (this.mode === 'date') {
  132. const dates = [{
  133. _id: 7,
  134. name: '最近七天',
  135. }, {
  136. _id: 30,
  137. name: '最近30天',
  138. }, {
  139. _id: 90,
  140. name: '最近90天',
  141. }]
  142. if (this.yesterday) {
  143. dates.unshift({
  144. _id: 1,
  145. name: '昨天',
  146. })
  147. }
  148. if (this.today) {
  149. dates.unshift({
  150. _id: 0,
  151. name: '今天',
  152. })
  153. }
  154. this.renderTabs = dates
  155. } else {
  156. this.renderTabs = this.tabs
  157. }
  158. },
  159. change(item, index) {
  160. if (item.disabled) return
  161. const id = item._id
  162. const name = item.name
  163. this.currentTab = index
  164. this.emit(id, index, name, item)
  165. },
  166. emit(id, index, name, item) {
  167. this.$emit('change', id, index, name, item)
  168. this.$emit('input', id, index, name)
  169. this.$emit('update:modelValue', id, index, name)
  170. },
  171. getPlatform() {
  172. const db = uniCloud.database()
  173. const appList = db.collection('uni-stat-app-platforms')
  174. .get()
  175. .then(res => {
  176. let platforms = res.result.data
  177. platforms = platforms.filter(p => p.hasOwnProperty('enable') ? p.enable : true)
  178. platforms.sort((a, b) => a.order - b.order)
  179. if (this.mode === 'platform-channel') {
  180. platforms = platforms.filter(item => /^android|ios$/.test(item.code))
  181. let _id = platforms.map(p => `platform_id == "${p._id}"`).join(' || ')
  182. _id = `(${_id})`
  183. this.setAllItem(platforms, _id)
  184. } else if (this.mode === 'platform-scene') {
  185. platforms = platforms.filter(item => /mp-/.test(item.code))
  186. let _id = platforms.map(p => `platform_id == "${p._id}"`).join(' || ')
  187. _id = `(${_id})`
  188. this.setAllItem(platforms, _id)
  189. } else {
  190. this.setAllItem(platforms)
  191. }
  192. uni.setStorageSync(this.last, platforms)
  193. this.renderTabs = platforms
  194. })
  195. },
  196. setAllItem(platforms, _id = '', name = '全部') {
  197. this.all && platforms.unshift({
  198. name,
  199. _id
  200. })
  201. }
  202. }
  203. }
  204. </script>
  205. <style lang="scss">
  206. .uni-stat-tooltip-s {
  207. width: 160px;
  208. white-space: normal;
  209. }
  210. .uni-label-text {
  211. font-size: 14px;
  212. font-weight: bold;
  213. color: #555;
  214. margin-top: 17px;
  215. margin-bottom: 17px;
  216. margin-right: 5px;
  217. // display: flex;
  218. // align-items: center;
  219. // justify-content: center;
  220. }
  221. .uni-stat--tab-x {
  222. display: flex;
  223. margin: 0 15px;
  224. white-space: nowrap;
  225. }
  226. .uni-stat--tab {
  227. display: flex;
  228. flex-wrap: wrap;
  229. }
  230. .uni-stat {
  231. &--tab {
  232. &-item {
  233. white-space: nowrap;
  234. font-size: 14px;
  235. color: #666;
  236. text-align: center;
  237. cursor: pointer;
  238. box-sizing: border-box;
  239. margin: 15px 0;
  240. &-disabled {
  241. cursor: unset;
  242. opacity: 0.4;
  243. }
  244. &-line {
  245. margin-right: 30px;
  246. padding: 2px 0;
  247. border-bottom: 1px solid transparent;
  248. &:last-child {
  249. margin-right: 0;
  250. }
  251. &-active {
  252. color: $uni-color-primary;
  253. border-bottom: 1px solid $uni-color-primary;
  254. // &-disabled {
  255. // color: #666;
  256. // border-color: #666;
  257. // }
  258. }
  259. }
  260. &-boldLine {
  261. box-sizing: border-box;
  262. margin-right: 30px;
  263. padding: 2px 0;
  264. border-bottom: 2px solid transparent;
  265. &:last-child {
  266. margin-right: 0;
  267. }
  268. &-active {
  269. box-sizing: border-box;
  270. color: $uni-color-primary;
  271. border-bottom: 2px solid $uni-color-primary;
  272. }
  273. }
  274. &-box {
  275. padding: 5px 15px;
  276. border: 1px solid #dcdfe6;
  277. // margin: 0;
  278. &:not(:last-child) {
  279. border-right-color: transparent;
  280. }
  281. &-active {
  282. box-sizing: border-box;
  283. border: 1px solid $uni-color-primary !important;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. /* #ifndef APP-NVUE */
  290. @media screen and (max-width: 500px) {
  291. .hide-on-phone {
  292. display: none;
  293. }
  294. .uni-stat--tab {
  295. flex-wrap: unset;
  296. overflow-x: auto !important;
  297. }
  298. ::-webkit-scrollbar {
  299. display: none;
  300. }
  301. }
  302. /* #endif */
  303. </style>