u-navbar.vue 15 KB

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