u-navbar.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. let url=window.location.href.split("#/")[1];
  252. let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
  253. let curRoute = routes[routes.length - 1].route
  254. var nowtime=new Date().getTime();
  255. this.descriptionHtml="";
  256. var list =this.carhelp.getConfigMessage()
  257. if(list&&list.length){
  258. //var list=JSON.parse(message);
  259. for(var i in list){
  260. // if(list[i].name=='*'){
  261. // this.descriptionShow=true;
  262. // this.description=list[i].value;
  263. // }
  264. if(false){
  265. console.log(list[i].url)
  266. console.log(curRoute)
  267. }
  268. if(list[i].url==curRoute){
  269. this.descriptionShow=true;
  270. this.description=list[i].value;
  271. }
  272. if(list[i].url==url||url.indexOf(list[i].url)==0){
  273. this.descriptionShow=true;
  274. this.description=list[i].value;
  275. if(list[i].startTime){
  276. var startTime=new Date(list[i].startTime).getTime()
  277. if(startTime>nowtime){
  278. this.descriptionShow=false;
  279. continue;
  280. }
  281. }
  282. if(list[i].endTime){
  283. var endTime=new Date(list[i].endTime).getTime()
  284. if(endTime<nowtime){
  285. this.descriptionShow=false;
  286. continue;
  287. }
  288. }
  289. if(this.descriptionShow&&list[i].type!='1'){
  290. var sz=['top','top','modal']
  291. this.descriptionModel=sz[list[i].type];
  292. this.descriptionTitle=list[i].title;
  293. }else{
  294. if(list[i].remark){
  295. this.descriptionTitle=list[i].title;
  296. this.descriptionHtml=list[i].remark;
  297. }
  298. }
  299. if(this.descriptionShow){
  300. break;
  301. }
  302. }
  303. }
  304. }
  305. },
  306. methods: {
  307. alerttipsCk(){
  308. if(this.descriptionHtml){
  309. this.descriptionShow2=true;
  310. }
  311. },
  312. navberBack(){
  313. const pages=getCurrentPages()
  314. if(pages.length===1){
  315. history.back()
  316. }else{
  317. uni.navigateBack();
  318. }
  319. },
  320. goBack() {
  321. //console.log("修复bug, 刷新后不能返回问题")
  322. // 如果自定义了点击返回按钮的函数,则执行,否则执行返回逻辑
  323. if (typeof this.customBack === 'function') {
  324. // 在微信,支付宝等环境(H5正常),会导致父组件定义的customBack()函数体中的this变成子组件的this
  325. // 通过bind()方法,绑定父组件的this,让this.customBack()的this为父组件的上下文
  326. this.customBack.bind(this.$u.$parent.call(this))();
  327. } else {
  328. this.navberBack();
  329. }
  330. }
  331. }
  332. };
  333. </script>
  334. <style scoped lang="scss">
  335. // @import "../../libs/css/style.components.scss";
  336. .u-navbar {
  337. width: 100%;
  338. }
  339. .u-navbar-fixed {
  340. position: fixed;
  341. left: 0;
  342. right: 0;
  343. top: 0;
  344. z-index: 991;
  345. }
  346. .u-status-bar {
  347. width: 100%;
  348. }
  349. .u-navbar-inner {
  350. // @include vue-flex;
  351. display: flex;
  352. justify-content: space-between;
  353. position: relative;
  354. align-items: center;
  355. }
  356. .u-back-wrap {
  357. display: flex;
  358. align-items: center;
  359. flex: 1;
  360. flex-grow: 0;
  361. padding: 14rpx 14rpx 14rpx 24rpx;
  362. }
  363. .u-back-text {
  364. padding-left: 4rpx;
  365. font-size: 30rpx;
  366. }
  367. .u-navbar-content-title {
  368. display: flex;
  369. align-items: center;
  370. justify-content: center;
  371. flex: 1;
  372. position: absolute;
  373. left: 0;
  374. right: 0;
  375. height: 60rpx;
  376. text-align: center;
  377. flex-shrink: 0;
  378. }
  379. .u-navbar-centent-slot {
  380. flex: 1;
  381. }
  382. .u-title {
  383. line-height: 60rpx;
  384. font-size: 32rpx;
  385. flex: 1;
  386. }
  387. .u-navbar-right {
  388. flex: 1;
  389. display: flex;
  390. align-items: center;
  391. justify-content: flex-end;
  392. }
  393. .u-slot-content {
  394. flex: 1;
  395. display: flex;
  396. align-items: center;
  397. }
  398. </style>