u-navbar.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <template>
  2. <view class="">
  3. <view class="u-navbar" v-if="show" :style="[navbarStyle]" :class="{ 'u-navbar-fixed': isFixed, 'u-border-bottom': borderBottom }">
  4. <view class="u-status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
  5. <view class="u-navbar-inner" :style="[navbarInnerStyle]">
  6. <view class="u-back-wrap" v-if="isBack" @tap="goBack">
  7. <view class="u-icon-wrap">
  8. <u-icon :name="backIconName" :color="backIconColor" :size="backIconSize"></u-icon>
  9. </view>
  10. <view class="u-icon-wrap u-back-text u-line-1" v-if="backText" :style="[backTextStyle]">{{ backText }}</view>
  11. </view>
  12. <view class="u-navbar-content-title" v-if="title" :style="[titleStyle]">
  13. <view
  14. class="u-title u-line-1"
  15. :style="{
  16. color: titleColor,
  17. fontSize: titleSize + 'rpx',
  18. fontWeight: titleBold ? 'bold' : 'normal'
  19. }">
  20. {{ title }}
  21. </view>
  22. </view>
  23. <view class="u-slot-content">
  24. <slot></slot>
  25. </view>
  26. <view class="u-slot-right">
  27. <slot name="right"></slot>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- 解决fixed定位后导航栏塌陷的问题 -->
  32. <view class="u-navbar-placeholder" v-if="show&&isFixed && !immersive" :style="{ width: '100%', height: Number(navbarHeight) + statusBarHeight + 'px' }"></view>
  33. <u-alert-tips type="warning" v-if="descriptionModel=='top'&&descriptionShow"
  34. :descStyle="{
  35. fontSize: '14px',
  36. color: '#ef7a30',
  37. }" @click="alerttipsCk"
  38. :show-icon="true" :description="description"></u-alert-tips>
  39. <u-modal v-if="descriptionModel=='modal'&&descriptionShow" v-model="descriptionShow"
  40. confirm-text="知道了"
  41. :title="descriptionTitle" >
  42. <view v-html="description" style="padding: 10px;font-size: 14px;"></view>
  43. </u-modal>
  44. <u-modal v-model="descriptionShow2"
  45. confirm-text="知道了"
  46. :title="descriptionTitle" >
  47. <view v-html="descriptionHtml" style="padding: 10px;font-size: 14px;"></view>
  48. </u-modal>
  49. </view>
  50. </template>
  51. <script>
  52. // 获取系统状态栏的高度
  53. let systemInfo = uni.getSystemInfoSync();
  54. let menuButtonInfo = {};
  55. // 如果是小程序,获取右上角胶囊的尺寸信息,避免导航栏右侧内容与胶囊重叠(支付宝小程序非本API,尚未兼容)
  56. // #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO || MP-QQ
  57. menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  58. // #endif
  59. /**
  60. * navbar 自定义导航栏
  61. * @description 此组件一般用于在特殊情况下,需要自定义导航栏的时候用到,一般建议使用uniapp自带的导航栏。
  62. * @tutorial https://www.uviewui.com/components/navbar.html
  63. * @property {String Number} height 导航栏高度(不包括状态栏高度在内,内部自动加上),注意这里的单位是px(默认44)
  64. * @property {String} back-icon-color 左边返回图标的颜色(默认#606266)
  65. * @property {String} back-icon-name 左边返回图标的名称,只能为uView自带的图标(默认arrow-left)
  66. * @property {String Number} back-icon-size 左边返回图标的大小,单位rpx(默认30)
  67. * @property {String} back-text 返回图标右边的辅助提示文字
  68. * @property {Object} back-text-style 返回图标右边的辅助提示文字的样式,对象形式(默认{ color: '#606266' })
  69. * @property {String} title 导航栏标题,如设置为空字符,将会隐藏标题占位区域
  70. * @property {String Number} title-width 导航栏标题的最大宽度,内容超出会以省略号隐藏,单位rpx(默认250)
  71. * @property {String} title-color 标题的颜色(默认#606266)
  72. * @property {String Number} title-size 导航栏标题字体大小,单位rpx(默认32)
  73. * @property {Function} custom-back 自定义返回逻辑方法
  74. * @property {String Number} z-index 固定在顶部时的z-index值(默认980)
  75. * @property {Boolean} is-back 是否显示导航栏左边返回图标和辅助文字(默认true)
  76. * @property {Object} background 导航栏背景设置,见官网说明(默认{ background: '#ffffff' })
  77. * @property {Boolean} is-fixed 导航栏是否固定在顶部(默认true)
  78. * @property {Boolean} immersive 沉浸式,允许fixed定位后导航栏塌陷,仅fixed定位下生效(默认false)
  79. * @property {Boolean} border-bottom 导航栏底部是否显示下边框,如定义了较深的背景颜色,可取消此值(默认true)
  80. * @example <u-navbar back-text="返回" title="剑未配妥,出门已是江湖"></u-navbar>
  81. */
  82. export default {
  83. name: "u-navbar",
  84. props: {
  85. show:{
  86. type: Boolean,
  87. default:true
  88. },
  89. // 导航栏高度,单位px,非rpx
  90. height: {
  91. type: [String, Number],
  92. default: ''
  93. },
  94. // 返回箭头的颜色
  95. backIconColor: {
  96. type: String,
  97. default: '#606266'
  98. },
  99. // 左边返回的图标
  100. backIconName: {
  101. type: String,
  102. default: 'nav-back'
  103. },
  104. // 左边返回图标的大小,rpx
  105. backIconSize: {
  106. type: [String, Number],
  107. default: '44'
  108. },
  109. // 返回的文字提示
  110. backText: {
  111. type: String,
  112. default: ''
  113. },
  114. // 返回的文字的 样式
  115. backTextStyle: {
  116. type: Object,
  117. default () {
  118. return {
  119. color: '#606266'
  120. }
  121. }
  122. },
  123. // 导航栏标题
  124. title: {
  125. type: String,
  126. default: ''
  127. },
  128. // 标题的宽度,如果需要自定义右侧内容,且右侧内容很多时,可能需要减少这个宽度,单位rpx
  129. titleWidth: {
  130. type: [String, Number],
  131. default: '250'
  132. },
  133. // 标题的颜色
  134. titleColor: {
  135. type: String,
  136. default: '#606266'
  137. },
  138. // 标题字体是否加粗
  139. titleBold: {
  140. type: Boolean,
  141. default: false
  142. },
  143. // 标题的字体大小
  144. titleSize: {
  145. type: [String, Number],
  146. default: 32
  147. },
  148. isBack: {
  149. type: [Boolean, String],
  150. default: true
  151. },
  152. // 对象形式,因为用户可能定义一个纯色,或者线性渐变的颜色
  153. background: {
  154. type: Object,
  155. default () {
  156. return {
  157. background: '#ffffff'
  158. }
  159. }
  160. },
  161. // 导航栏是否固定在顶部
  162. isFixed: {
  163. type: Boolean,
  164. default: true
  165. },
  166. // 是否沉浸式,允许fixed定位后导航栏塌陷,仅fixed定位下生效
  167. immersive: {
  168. type: Boolean,
  169. default: false
  170. },
  171. // 是否显示导航栏的下边框
  172. borderBottom: {
  173. type: Boolean,
  174. default: true
  175. },
  176. zIndex: {
  177. type: [String, Number],
  178. default: ''
  179. },
  180. // 自定义返回逻辑
  181. customBack: {
  182. type: Function,
  183. default: null
  184. }
  185. },
  186. data() {
  187. return {
  188. description:'',
  189. descriptionHtml:'',
  190. descriptionShow:false,
  191. descriptionShow2:false,
  192. descriptionTitle:"",
  193. descriptionModel:"top",
  194. menuButtonInfo: menuButtonInfo,
  195. statusBarHeight: systemInfo.statusBarHeight
  196. };
  197. },
  198. computed: {
  199. // 导航栏内部盒子的样式
  200. navbarInnerStyle() {
  201. let style = {};
  202. // 导航栏宽度,如果在小程序下,导航栏宽度为胶囊的左边到屏幕左边的距离
  203. style.height = this.navbarHeight + 'px';
  204. // // 如果是各家小程序,导航栏内部的宽度需要减少右边胶囊的宽度
  205. // #ifdef MP
  206. let rightButtonWidth = systemInfo.windowWidth - menuButtonInfo.left;
  207. style.marginRight = rightButtonWidth + 'px';
  208. // #endif
  209. return style;
  210. },
  211. // 整个导航栏的样式
  212. navbarStyle() {
  213. let style = {};
  214. style.zIndex = this.zIndex ? this.zIndex : this.$u.zIndex.navbar;
  215. // 合并用户传递的背景色对象
  216. Object.assign(style, this.background);
  217. return style;
  218. },
  219. // 导航中间的标题的样式
  220. titleStyle() {
  221. let style = {};
  222. // #ifndef MP
  223. style.left = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
  224. style.right = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
  225. // #endif
  226. // #ifdef MP
  227. // 此处是为了让标题显示区域即使在小程序有右侧胶囊的情况下也能处于屏幕的中间,是通过绝对定位实现的
  228. let rightButtonWidth = systemInfo.windowWidth - menuButtonInfo.left;
  229. style.left = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
  230. style.right = rightButtonWidth - (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + rightButtonWidth +
  231. 'px';
  232. // #endif
  233. style.width = uni.upx2px(this.titleWidth) + 'px';
  234. return style;
  235. },
  236. // 转换字符数值为真正的数值
  237. navbarHeight() {
  238. // #ifdef APP-PLUS || H5
  239. return this.height ? this.height : 44;
  240. // #endif
  241. // #ifdef MP
  242. // 小程序特别处理,让导航栏高度 = 胶囊高度 + 两倍胶囊顶部与状态栏底部的距离之差(相当于同时获得了导航栏底部与胶囊底部的距离)
  243. // 此方法有缺陷,暂不用(会导致少了几个px),采用直接固定值的方式
  244. // return menuButtonInfo.height + (menuButtonInfo.top - this.statusBarHeight) * 2;//导航高度
  245. let height = systemInfo.platform == 'ios' ? 44 : 48;
  246. return this.height ? this.height : height;
  247. // #endif
  248. }
  249. },
  250. created() {
  251. console.log('aaaa')
  252. let url=window.location.href.split("#/")[1];
  253. let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
  254. let curRoute = routes[routes.length - 1].route
  255. var nowtime=new Date().getTime();
  256. this.descriptionHtml="";
  257. var message =this.carhelp.getConfig().message
  258. if(message){
  259. var list=JSON.parse(message);
  260. console.log(list)
  261. for(var i in list){
  262. // if(list[i].name=='*'){
  263. // this.descriptionShow=true;
  264. // this.description=list[i].value;
  265. // }
  266. if(false){
  267. console.log(list[i].name)
  268. console.log(curRoute)
  269. debugger
  270. }
  271. if(list[i].name==curRoute){
  272. this.descriptionShow=true;
  273. this.description=list[i].value;
  274. }
  275. if(list[i].name==url||url.indexOf(list[i].name)==0){
  276. this.descriptionShow=true;
  277. this.description=list[i].value;
  278. if(list[i].startTime){
  279. var startTime=new Date(list[i].startTime).getTime()
  280. if(startTime>nowtime){
  281. this.descriptionShow=false;
  282. continue;
  283. }
  284. }
  285. if(list[i].endTime){
  286. var endTime=new Date(list[i].endTime).getTime()
  287. if(endTime<nowtime){
  288. this.descriptionShow=false;
  289. continue;
  290. }
  291. }
  292. if(this.descriptionShow&&list[i].model){
  293. this.descriptionModel=list[i].model;
  294. this.descriptionTitle=list[i].title;
  295. }else{
  296. if(list[i].content){
  297. this.descriptionTitle=list[i].title;
  298. this.descriptionHtml=list[i].content;
  299. }
  300. }
  301. if(this.descriptionShow){
  302. break;
  303. }
  304. }
  305. }
  306. }
  307. },
  308. methods: {
  309. alerttipsCk(){
  310. if(this.descriptionHtml){
  311. this.descriptionShow2=true;
  312. }
  313. },
  314. goBack() {
  315. //console.log("修复bug, 刷新后不能返回问题")
  316. // 如果自定义了点击返回按钮的函数,则执行,否则执行返回逻辑
  317. if (typeof this.customBack === 'function') {
  318. // 在微信,支付宝等环境(H5正常),会导致父组件定义的customBack()函数体中的this变成子组件的this
  319. // 通过bind()方法,绑定父组件的this,让this.customBack()的this为父组件的上下文
  320. this.customBack.bind(this.$u.$parent.call(this))();
  321. } else {
  322. const pages=getCurrentPages()
  323. if(pages.length===1){
  324. history.back()
  325. }else{
  326. uni.navigateBack();
  327. }
  328. }
  329. }
  330. }
  331. };
  332. </script>
  333. <style scoped lang="scss">
  334. // @import "../../libs/css/style.components.scss";
  335. .u-navbar {
  336. width: 100%;
  337. }
  338. .u-navbar-fixed {
  339. position: fixed;
  340. left: 0;
  341. right: 0;
  342. top: 0;
  343. z-index: 991;
  344. }
  345. .u-status-bar {
  346. width: 100%;
  347. }
  348. .u-navbar-inner {
  349. // @include vue-flex;
  350. display: flex;
  351. justify-content: space-between;
  352. position: relative;
  353. align-items: center;
  354. }
  355. .u-back-wrap {
  356. display: flex;
  357. align-items: center;
  358. flex: 1;
  359. flex-grow: 0;
  360. padding: 14rpx 14rpx 14rpx 24rpx;
  361. }
  362. .u-back-text {
  363. padding-left: 4rpx;
  364. font-size: 30rpx;
  365. }
  366. .u-navbar-content-title {
  367. display: flex;
  368. align-items: center;
  369. justify-content: center;
  370. flex: 1;
  371. position: absolute;
  372. left: 0;
  373. right: 0;
  374. height: 60rpx;
  375. text-align: center;
  376. flex-shrink: 0;
  377. }
  378. .u-navbar-centent-slot {
  379. flex: 1;
  380. }
  381. .u-title {
  382. line-height: 60rpx;
  383. font-size: 32rpx;
  384. flex: 1;
  385. }
  386. .u-navbar-right {
  387. flex: 1;
  388. display: flex;
  389. align-items: center;
  390. justify-content: flex-end;
  391. }
  392. .u-slot-content {
  393. flex: 1;
  394. display: flex;
  395. align-items: center;
  396. }
  397. </style>