u-navbar.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. startGz:{
  200. type: Boolean,
  201. default: true
  202. }
  203. },
  204. data() {
  205. return {
  206. gotoGzShow:false,
  207. projectName: "",
  208. description:'',
  209. descriptionHtml:'',
  210. descriptionShow:false,
  211. descriptionShow2:false,
  212. descriptionTitle:"",
  213. descriptionModel:"top",
  214. menuButtonInfo: menuButtonInfo,
  215. statusBarHeight: systemInfo.statusBarHeight
  216. };
  217. },
  218. computed: {
  219. // 导航栏内部盒子的样式
  220. navbarInnerStyle() {
  221. let style = {};
  222. // 导航栏宽度,如果在小程序下,导航栏宽度为胶囊的左边到屏幕左边的距离
  223. style.height = this.navbarHeight + 'px';
  224. // // 如果是各家小程序,导航栏内部的宽度需要减少右边胶囊的宽度
  225. // #ifdef MP
  226. let rightButtonWidth = systemInfo.windowWidth - menuButtonInfo.left;
  227. style.marginRight = rightButtonWidth + 'px';
  228. // #endif
  229. return style;
  230. },
  231. // 整个导航栏的样式
  232. navbarStyle() {
  233. let style = {};
  234. style.zIndex = this.zIndex ? this.zIndex : this.$u.zIndex.navbar;
  235. // 合并用户传递的背景色对象
  236. Object.assign(style, this.background);
  237. return style;
  238. },
  239. // 导航中间的标题的样式
  240. titleStyle() {
  241. let style = {};
  242. // #ifndef MP
  243. style.left = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
  244. style.right = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
  245. // #endif
  246. // #ifdef MP
  247. // 此处是为了让标题显示区域即使在小程序有右侧胶囊的情况下也能处于屏幕的中间,是通过绝对定位实现的
  248. let rightButtonWidth = systemInfo.windowWidth - menuButtonInfo.left;
  249. style.left = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
  250. style.right = rightButtonWidth - (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + rightButtonWidth +
  251. 'px';
  252. // #endif
  253. style.width = uni.upx2px(this.titleWidth) + 'px';
  254. return style;
  255. },
  256. // 转换字符数值为真正的数值
  257. navbarHeight() {
  258. // #ifdef APP-PLUS || H5
  259. return this.height ? this.height : 44;
  260. // #endif
  261. // #ifdef MP
  262. // 小程序特别处理,让导航栏高度 = 胶囊高度 + 两倍胶囊顶部与状态栏底部的距离之差(相当于同时获得了导航栏底部与胶囊底部的距离)
  263. // 此方法有缺陷,暂不用(会导致少了几个px),采用直接固定值的方式
  264. // return menuButtonInfo.height + (menuButtonInfo.top - this.statusBarHeight) * 2;//导航高度
  265. let height = systemInfo.platform == 'ios' ? 44 : 48;
  266. return this.height ? this.height : height;
  267. // #endif
  268. }
  269. },
  270. created() {
  271. this.projectName = process.car.ProjectName;
  272. var nowtime = parseUnixTime(currentTimeStamp(), '{y}-{m}-{d} {h}')
  273. var time=this.carhelp.get("getTips_$");
  274. if((time&&nowtime==time)){
  275. this.getConfigMessage()
  276. }else{
  277. API.getTips().then((response) => {
  278. this.carhelp.set("getTips_$",nowtime);
  279. this.carhelp.setConfigMessage(response.data.tipsList);
  280. this.getConfigMessage()
  281. }).catch(error => {
  282. })
  283. }
  284. if (this.startGz&&this.carhelp.getGzDate() && !this.gotoGzShow) {
  285. this.checkSubscribe()
  286. }
  287. // this.checkSubscribe()
  288. },
  289. methods: {
  290. checkSubscribe() {
  291. API_weixin.checkSubscribe({
  292. openId: this.carhelp.getOpenId()
  293. }).then((res) => {
  294. if (res.data == "0") {
  295. this.gotoGzShow = true;
  296. } else {
  297. this.carhelp.setGzDate()
  298. }
  299. //setGzDate
  300. }).catch(error => {
  301. uni.showToast({
  302. title: error
  303. })
  304. })
  305. },
  306. cancelGz() {
  307. this.carhelp.setGzDate()
  308. },
  309. gotoGz() {
  310. this.carhelp.setGzDate()
  311. this.gotoGzShow = false;
  312. var url = "https://mp.weixin.qq.com/s/mCHz1nNvg0xAICiBeIyKRQ";
  313. window.location.href = url
  314. },
  315. getConfigMessage(){
  316. let url=window.location.href.split("#/")[1];
  317. let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
  318. let curRoute = routes[routes.length - 1].route
  319. var nowtime=new Date().getTime();
  320. this.descriptionHtml="";
  321. var list =this.carhelp.getConfigMessage()
  322. if(list&&list.length){
  323. //var list=JSON.parse(message);
  324. for(var i in list){
  325. // if(list[i].name=='*'){
  326. // this.descriptionShow=true;
  327. // this.description=list[i].value;
  328. // }
  329. if(false){
  330. console.log(list[i].url)
  331. console.log(curRoute)
  332. }
  333. if(list[i].url==curRoute){
  334. this.descriptionShow=true;
  335. this.description=list[i].value;
  336. }
  337. if(list[i].url==url||url.indexOf(list[i].url)==0){
  338. this.descriptionShow=true;
  339. this.description=list[i].value;
  340. if(list[i].startTime){
  341. var startTime=new Date(list[i].startTime).getTime()
  342. if(startTime>nowtime){
  343. this.descriptionShow=false;
  344. continue;
  345. }
  346. }
  347. if(list[i].endTime){
  348. var endTime=new Date(list[i].endTime).getTime()
  349. if(endTime<nowtime){
  350. this.descriptionShow=false;
  351. continue;
  352. }
  353. }
  354. if(this.descriptionShow&&list[i].type!='1'){
  355. var sz=['top','top','modal']
  356. this.descriptionModel=sz[list[i].type];
  357. this.descriptionTitle=list[i].title;
  358. }else{
  359. if(list[i].remark){
  360. this.descriptionTitle=list[i].title;
  361. this.descriptionHtml=list[i].remark;
  362. }
  363. }
  364. if(this.descriptionShow){
  365. break;
  366. }
  367. }
  368. }
  369. }
  370. },
  371. alerttipsCk(){
  372. if(this.descriptionHtml){
  373. this.descriptionShow2=true;
  374. }
  375. },
  376. navberBack(){
  377. const pages=getCurrentPages()
  378. if(pages.length===1){
  379. history.back()
  380. }else{
  381. uni.navigateBack();
  382. }
  383. },
  384. goBack() {
  385. //console.log("修复bug, 刷新后不能返回问题")
  386. // 如果自定义了点击返回按钮的函数,则执行,否则执行返回逻辑
  387. if (typeof this.customBack === 'function') {
  388. // 在微信,支付宝等环境(H5正常),会导致父组件定义的customBack()函数体中的this变成子组件的this
  389. // 通过bind()方法,绑定父组件的this,让this.customBack()的this为父组件的上下文
  390. this.customBack.bind(this.$u.$parent.call(this))();
  391. } else {
  392. this.navberBack();
  393. }
  394. }
  395. }
  396. };
  397. </script>
  398. <style scoped lang="scss">
  399. // @import "../../libs/css/style.components.scss";
  400. .u-line-1{
  401. overflow: unset;
  402. }
  403. .u-navbar {
  404. width: 100%;
  405. }
  406. .u-navbar-fixed {
  407. position: fixed;
  408. left: 0;
  409. right: 0;
  410. top: 0;
  411. z-index: 991;
  412. }
  413. .u-status-bar {
  414. width: 100%;
  415. }
  416. .u-navbar-inner {
  417. // @include vue-flex;
  418. display: flex;
  419. justify-content: space-between;
  420. position: relative;
  421. align-items: center;
  422. }
  423. .u-back-wrap {
  424. display: flex;
  425. align-items: center;
  426. flex: 1;
  427. flex-grow: 0;
  428. padding: 14rpx 14rpx 14rpx 24rpx;
  429. }
  430. .u-back-text {
  431. padding-left: 4rpx;
  432. font-size: 30rpx;
  433. }
  434. .u-navbar-content-title {
  435. display: flex;
  436. align-items: center;
  437. justify-content: center;
  438. flex: 1;
  439. position: absolute;
  440. left: 0;
  441. right: 0;
  442. height: 60rpx;
  443. text-align: center;
  444. flex-shrink: 0;
  445. }
  446. .u-navbar-centent-slot {
  447. flex: 1;
  448. }
  449. .u-title {
  450. line-height: 60rpx;
  451. font-size: 32rpx;
  452. flex: 1;
  453. }
  454. .u-navbar-right {
  455. flex: 1;
  456. display: flex;
  457. align-items: center;
  458. justify-content: flex-end;
  459. }
  460. .u-slot-content {
  461. flex: 1;
  462. display: flex;
  463. align-items: center;
  464. }
  465. </style>