ax-custom-title.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <view v-if="visible" class="ax ax-custom-title" :style="{padding}">
  3. <view class="__body" :style="{height}">
  4. <slot>
  5. <view @click="back()">
  6. <text v-if="this.pages.length>1" class="icon-back"></text>
  7. <text class="title">{{title || titleText}}</text>
  8. </view>
  9. </slot>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. import PagesJSON from "@/pages.json";
  15. export default {
  16. name:"ax-custom-title",
  17. props:{
  18. // 显示首页按钮
  19. title:{type:String,default:''},
  20. // PC模式下无插槽内容也任然显示组件
  21. unlock:{type:Boolean,default:false},
  22. },
  23. data() {
  24. return {
  25. titleText: '',
  26. visible: false,
  27. padding: '0px',
  28. height: 'auto'
  29. };
  30. },
  31. async created() {
  32. const sys = await this.systemInfo();
  33. this.visible = sys.windowHeight === sys.screenHeight;
  34. if(this.visible==false) return;
  35. var rect = {top:0,left:0,right:0,bottom:0,width:0,height:0}
  36. if(sys.uniPlatform.indexOf('mp-')===0) rect = uni.getMenuButtonBoundingClientRect();
  37. var interval = sys.windowWidth - (rect.right || sys.windowWidth-10);
  38. const data = {
  39. top: rect.top,
  40. left: interval,
  41. right: (rect.width || interval*-1) + interval * 2,
  42. bottom: interval,
  43. componentHeight: rect.top+rect.height+interval,
  44. statusBarHeight: sys.statusBarHeight
  45. }
  46. var top = `${data.top}px`;
  47. var left = `${data.left}px`;
  48. var right = `${data.right}px`;
  49. var bottom = `${data.bottom}px`;
  50. if(['windows','mac'].includes(sys.platform)){
  51. top = 0;
  52. bottom = 0;
  53. right = left;
  54. rect.height = 45;
  55. if(this.unlock == false && !this.$slots.default) this.visible = false;
  56. }
  57. this.padding = [top,right,bottom,left].join(' ');
  58. this.height = `${rect.height || 50}px`;
  59. const currentPage = Array.from(getCurrentPages()).pop();
  60. // 先在主包页面中查找
  61. let pageConfig = PagesJSON.pages.find(i => i.path == currentPage.route);
  62. // 如果主包中找不到,则在分包中查找
  63. if (!pageConfig && PagesJSON.subPackages) {
  64. for (const subPkg of PagesJSON.subPackages) {
  65. const subPage = subPkg.pages.find(p => `${subPkg.root}/${p.path}` == currentPage.route);
  66. if (subPage) {
  67. pageConfig = subPage;
  68. break;
  69. }
  70. }
  71. }
  72. this.titleText = pageConfig?.style?.navigationBarTitleText || '';
  73. this.$emit('display',data);
  74. },
  75. computed:{
  76. pages(){
  77. return getCurrentPages();
  78. }
  79. },
  80. methods:{
  81. systemInfo(){
  82. return new Promise((resolve,reject)=>{
  83. uni.getSystemInfo({
  84. success: res=>resolve(res),
  85. fail: err=>reject(err)
  86. });
  87. });
  88. },
  89. back(){
  90. uni.navigateBack({delta: 1});
  91. }
  92. }
  93. }
  94. </script>
  95. <style scoped>
  96. @import url(../ax/ax.css);
  97. .ax-title-bar{
  98. width: 100vw;
  99. color: #000;
  100. position: relative;
  101. z-index: 999999;
  102. }
  103. .__body{
  104. display: flex;
  105. align-items: center;
  106. justify-content: space-between;
  107. }
  108. .__body::after{
  109. content: '';
  110. }
  111. .icon-back{
  112. margin-right: 5px;
  113. }
  114. .icon-back::before{
  115. content: "";
  116. display: inline-block;
  117. width: 10px;
  118. height: 10px;
  119. border-width: 0 0 2px 2px;
  120. border-style: solid;
  121. border-color: #000;
  122. transform-origin: center center;
  123. transform: rotate(45deg) translateY(-3px) translateX(1px);
  124. }
  125. </style>