u-navbar.vue 13 KB

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