wk-nav-bar.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <template>
  2. <view class="wk-nav-bar linear-gradient">
  3. <view v-if="showLeft" class="left">
  4. <button class="button white-btn" @click="_handleBack">
  5. <view class="back-icon">
  6. <text class="wk wk-arrow-right icon" />
  7. </view>
  8. </button>
  9. <!-- #ifdef MP-WEIXIN -->
  10. <slot />
  11. <button
  12. v-if="commands.length > 0"
  13. class="button white-btn command-btn"
  14. @click="handleToggleCommand">
  15. <image :src="$static('images/icon/list.png')" class="list-icon" />
  16. </button>
  17. <!-- #endif -->
  18. </view>
  19. <view class="title">
  20. {{ title }}
  21. </view>
  22. <view class="right">
  23. <!-- #ifndef MP-WEIXIN -->
  24. <button
  25. v-if="commands.length > 0"
  26. class="button white-btn"
  27. @click="handleToggleCommand">
  28. <text class="wk wk-more icon" />
  29. </button>
  30. <slot />
  31. <!-- #endif -->
  32. </view>
  33. <view
  34. v-if="showCommand"
  35. class="command-popup"
  36. @click="handleToggleCommand">
  37. <view class="command-mask" />
  38. <view class="command-body">
  39. <view
  40. v-for="(command, index) in commands"
  41. :key="index"
  42. class="command-item"
  43. @click="handleCommand(command)">
  44. <text
  45. v-if="command.icon"
  46. :class="command.icon"
  47. class="icon wk" />
  48. <image
  49. v-else-if="command.imgIcon"
  50. :src="$static(`images/more/${command.imgIcon}.png`)"
  51. alt=""
  52. class="img-icon" />
  53. <text class="label">
  54. {{ command.label }}
  55. </text>
  56. </view>
  57. </view>
  58. </view>
  59. <uni-popup ref="popup" type="dialog">
  60. <wk-popup-expiration />
  61. </uni-popup>
  62. </view>
  63. </template>
  64. <script>
  65. /**
  66. * 头部导航控制
  67. * @author yxk
  68. * @property {String} title 导航栏标题
  69. * @property {Boolean} showLeft 是否显示左侧返回按钮,默认 true 显示
  70. * @property {Boolean} pervent 是否阻止默认返回事件,配合 back 回调事件使用
  71. * @property {Boolean} refreshPrev 在navigateBack返回时是否设置上一页刷新标志,默认 false 不设置
  72. * @property {Array<Command>} commandList 右侧更多操作命令
  73. * @property {String} command.icon 命令字体图标
  74. * @property {String} command.label 命令名称
  75. * @property {Boolean} command.noCheck 命令是否校验权限,为 true 时不校验权限
  76. * @property {String} command.auth 命令权限值
  77. * @event {Function} back 点击返回按钮时触发,如果 pervent 设置为 true 则不会触发默认返回需手动调用返回
  78. * @event {Function} command 点击更多中的命令时触发,event.detail = 当前点击的命令
  79. */
  80. import { mapGetters } from 'vuex'
  81. export default {
  82. name: 'WkNavBar',
  83. inheritAttrs: false,
  84. props: {
  85. title: {
  86. type: String,
  87. default: ''
  88. },
  89. showLeft: {
  90. type: Boolean,
  91. default: true
  92. },
  93. pervent: {
  94. type: Boolean,
  95. default: false
  96. },
  97. commandList: {
  98. type: Array,
  99. default: () => []
  100. },
  101. refreshPrev: {
  102. type: Boolean,
  103. default: false
  104. }
  105. },
  106. data() {
  107. return {
  108. showCommand: false,
  109. commands: []
  110. }
  111. },
  112. computed: {
  113. ...mapGetters({
  114. showExpiration: 'user/showExpiration'
  115. })
  116. },
  117. watch: {
  118. authData: {
  119. handler(val) {
  120. if (!val) return
  121. this.calcCommands()
  122. },
  123. deep: true
  124. },
  125. commandList: {
  126. handler() {
  127. this.calcCommands()
  128. },
  129. deep: true
  130. },
  131. showExpiration: {
  132. handler() {
  133. if (this.showExpiration) {
  134. this.$nextTick(function() {
  135. this.$refs.popup.open()
  136. })
  137. }
  138. },
  139. immediate: true,
  140. deep: true
  141. }
  142. },
  143. created() {
  144. this.calcCommands()
  145. },
  146. methods: {
  147. _handleBack() {
  148. this.$emit('back')
  149. if (this.pervent) return
  150. if (this.refreshPrev) {
  151. this.$refreshAndToPrev(this)
  152. } else {
  153. this.$Router.navigateBack()
  154. }
  155. },
  156. calcCommands() {
  157. this.showCommand = false
  158. this.commands = []
  159. this.commandList.forEach(item => {
  160. console.log(item.label, item.auth, this.$auth(item.auth))
  161. if (item.noCheck || !item.auth) {
  162. // 如果不需要进行权限校验
  163. this.commands.push(item)
  164. } else if (this.$auth(item.auth)) {
  165. // 权限校验
  166. this.commands.push(item)
  167. }
  168. })
  169. },
  170. handleToggleCommand() {
  171. if (this.commands.length === 0) {
  172. this.showCommand = false
  173. return
  174. }
  175. this.showCommand = !this.showCommand
  176. },
  177. handleCommand(command) {
  178. this.$emit('command', command)
  179. },
  180. handleCloseExpiration(next) {
  181. next()
  182. }
  183. }
  184. }
  185. </script>
  186. <style scoped lang="scss">
  187. .wk-nav-bar {
  188. position: relative;
  189. width: 100%;
  190. height: 88rpx;
  191. font-size: 32rpx;
  192. color: white;
  193. @include center;
  194. .left, .right {
  195. position: absolute;
  196. top: 0;
  197. height: 100%;
  198. @include center;
  199. }
  200. ::v-deep .button {
  201. .icon {
  202. font-size: 30rpx;
  203. color: white;
  204. font-weight: bold;
  205. }
  206. }
  207. .left {
  208. left: 10rpx;
  209. .back-icon {
  210. height: 100%;
  211. transform: rotate(180deg);
  212. font-weight: bold;
  213. @include center;
  214. }
  215. .command-btn {
  216. padding: 16rpx 15rpx;
  217. @include center;
  218. .list-icon {
  219. width: 40rpx;
  220. height: 40rpx;
  221. transform: translateY(3rpx);
  222. }
  223. }
  224. }
  225. .right {
  226. right: 10rpx;
  227. @include right;
  228. .button {
  229. margin-left: 26rpx;
  230. &:first-child {
  231. margin-left: 0;
  232. }
  233. }
  234. }
  235. }
  236. .command-popup {
  237. position: fixed;
  238. top: 0;
  239. left: 0;
  240. z-index: 2020;
  241. width: 100%;
  242. height: 100%;
  243. line-height: 1;
  244. font-size: 26rpx;
  245. .command-mask {
  246. width: 100%;
  247. height: 100%;
  248. background-color: rgba(255,255,255,0);
  249. }
  250. .command-body {
  251. position: absolute;
  252. /* #ifdef MP-WEIXIN */
  253. left: 20rpx;
  254. /* #endif */
  255. /* #ifndef MP-WEIXIN */
  256. right: 20rpx;
  257. /* #endif */
  258. top: calc(var(--status-bar-height) + 75rpx);
  259. min-width: 40%;
  260. background-color: white;
  261. box-shadow: 0 0 15rpx 0 rgba(0,0,0,.2);
  262. border-radius: 10rpx;
  263. padding: 15rpx 30rpx;
  264. overflow: hidden;
  265. .command-item {
  266. color: #333;
  267. border-bottom: 1rpx solid $border-color;
  268. padding: 26rpx 18rpx;
  269. @include left;
  270. .icon {
  271. color: #999999;
  272. font-size: 32rpx;
  273. margin-right: 20rpx;
  274. }
  275. .img-icon {
  276. width: 36rpx;
  277. height: 36rpx;
  278. margin-right: 20rpx;
  279. }
  280. &:last-child {
  281. border-bottom: 0 none;
  282. }
  283. }
  284. }
  285. }
  286. </style>