app-nav-bar.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <template>
  2. <view class="app-nav-bar">
  3. <view v-if="hasHeight" class="hidden-height" :style="[hiddenHeight()]"></view>
  4. <view
  5. :class="{ 'app-navbar--fixed': fixed, 'app-navbar--shadow': shadow,'app-navbar--border': border, 'app-navbar--content':true}">
  6. <view :style="[ordinaryStyle()]" class="app-navbar__bar cross-center">
  7. <view class="dir-left-nowrap app-navbar__box">
  8. <view class="app-navbar__back cross-center box-grow-0" @click="onClickBack" v-if="pagesLength > 1">
  9. <view class="icon-back"></view>
  10. </view>
  11. <view
  12. :class="{'main-center app-navbar__right-center': position === 'center' && xStyle== 1, 'right__back': pagesLength > 1}"
  13. class="box-grow-1 dir-left-nowrap app-navbar__right">
  14. <view v-if="showLeftIcon">
  15. <template v-if="xStyle==5">
  16. <view class="main-left cross-center hxj-area" @click="leftClick">
  17. 九林语<image src="https://t17.9026.com/web/statics/image/index/arrow-down.png" mode=""></image>
  18. </view>
  19. </template>
  20. <template v-else>
  21. <app-jump-button form :open_type="link.openType"
  22. :url="link.url"
  23. :params="link.params">
  24. <view class="cross-center box-grow-0 left-icon">
  25. <image :style="[hw_style]" :src="leftIcon"></image>
  26. </view>
  27. </app-jump-button>
  28. </template>
  29. </view>
  30. <view v-if="showTitle" class="cross-center box-grow-0 title">
  31. <app-jump-button v-if="hasJump" form :open_type="link.openType"
  32. :url="link.url"
  33. :params="link.params">
  34. <view class="t-omit" :style="[maxWidth()]">{{ title }}</view>
  35. </app-jump-button>
  36. <view v-else class="t-omit" :style="[maxWidth()]">{{ title }}</view>
  37. </view>
  38. <view v-if="showLink" class="cross-center box-grow-0 link">
  39. <view class="link-search" @click="navGoodsSearch"
  40. :style="{color: placeholderColor}">
  41. <view class="link-box cross-center main-between">
  42. <view class="search-placeholder t-omit">{{ placeholder }}</view>
  43. <view class="search-icon"></view>
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. import {mapState} from "vuex";
  55. export default {
  56. name: 'app-nav-bar',
  57. data() {
  58. return {
  59. hw_style: {},
  60. }
  61. },
  62. watch: {
  63. 'leftIcon'(newValue, oldValue) {
  64. console.log('lefticon 监听测试');
  65. this.doSomething();
  66. }
  67. },
  68. props: {
  69. fixed: {
  70. type: [Boolean, String],
  71. default: true
  72. },
  73. shadow: {
  74. type: [String, Boolean],
  75. default: false
  76. },
  77. hasHeight: {
  78. type: Boolean,
  79. default: true,
  80. },
  81. border: {
  82. type: [String, Boolean],
  83. default: false
  84. },
  85. backgroundColor: {
  86. type: String,
  87. default: "#FFFFFF"
  88. },
  89. leftIcon: {
  90. type: String,
  91. default: ""
  92. },
  93. link: {
  94. type: [Object,Array]
  95. },
  96. title: {
  97. type: String,
  98. default: ""
  99. },
  100. xStyle: {
  101. type: [Number, String],
  102. default: 1
  103. },
  104. hasMallSetting: {
  105. type: [Number, String],
  106. default: 1
  107. },
  108. color: {
  109. type: String,
  110. default: "#000000"
  111. },
  112. position: {
  113. type: String,
  114. default: "center"
  115. },
  116. placeholder: {
  117. type: String,
  118. default: "搜索"
  119. },
  120. placeholderColor: {
  121. type: String,
  122. default: "#666666"
  123. }
  124. },
  125. computed: {
  126. ...mapState({
  127. statusBarHeight: state => state.gConfig.systemInfo.statusBarHeight,
  128. mBarHeight: state => state.gConfig.mBarHeight,
  129. appImg: state => state.mallConfig.__wxapp_img.mall,
  130. mallNavbar: state => state.mallConfig.navbar
  131. }),
  132. maxWidth() {
  133. return () => {
  134. let xstyle = parseInt(this.xStyle);
  135. let width = 0;
  136. switch (xstyle) {
  137. case 1:
  138. if (this.position === 'center') {
  139. width = uni.upx2px(360);
  140. } else {
  141. width = uni.upx2px(500);
  142. }
  143. break;
  144. case 2:
  145. width = uni.upx2px(400);
  146. break;
  147. case 4:
  148. width = uni.upx2px(200);
  149. break;
  150. default:
  151. break;
  152. }
  153. if (width && this.pagesLength > 1) {
  154. width -= uni.upx2px(42);
  155. }
  156. return Object.assign({}, {
  157. 'max-width': width + 'px',
  158. });
  159. }
  160. },
  161. hiddenHeight() {
  162. return () => {
  163. let barHeight;
  164. // #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO
  165. barHeight = this.statusBarHeight;
  166. // #endif
  167. barHeight = barHeight || 0;
  168. let height = barHeight + this.mBarHeight;
  169. this.$emit('headHeight', height);
  170. return {
  171. height: height + 'px',
  172. }
  173. }
  174. },
  175. ordinaryStyle() {
  176. return () => {
  177. let color = '';
  178. let backgroundColor = '';
  179. if (this.hasMallSetting == 1) {
  180. color = this.mallNavbar.top_text_color;
  181. backgroundColor = this.mallNavbar.top_background_color;
  182. } else {
  183. color = this.color;
  184. backgroundColor = this.backgroundColor;
  185. }
  186. let barHeight;
  187. // #ifdef MP
  188. barHeight = this.statusBarHeight;
  189. // #endif
  190. barHeight = barHeight || 0;
  191. color = color || '#000000';
  192. backgroundColor = backgroundColor || '#FFFFFF';
  193. return Object.assign({}, {
  194. color, backgroundColor,
  195. height: (barHeight + this.mBarHeight) + 'px',
  196. paddingTop: barHeight + 'px',
  197. })
  198. }
  199. },
  200. hasJump() {
  201. return [2, 4].indexOf(parseInt(this.xStyle)) !== -1
  202. },
  203. showLeftIcon() {
  204. return [2, 3,5].indexOf(parseInt(this.xStyle)) !== -1 && this.leftIcon
  205. },
  206. showTitle() {
  207. return [1, 2, 4].indexOf(parseInt(this.xStyle)) !== -1 && this.title
  208. },
  209. showLink() {
  210. return [3, 4].indexOf(parseInt(this.xStyle)) !== -1 && this.link
  211. },
  212. pagesLength() {
  213. return getCurrentPages().length;
  214. },
  215. },
  216. mounted() {
  217. this.doSomething();
  218. },
  219. methods: {
  220. doSomething() {
  221. const maxHeight = 54;
  222. const maxWidth = 100;
  223. const self = this;
  224. uni.getImageInfo({
  225. src: self.leftIcon,
  226. success: function (res) {
  227. let {height, width} = res;
  228. if (height <= maxHeight && width <= maxWidth) {
  229. // height = height;
  230. // width = width;
  231. }
  232. if (height <= maxHeight && width >= maxWidth) {
  233. height = height / (width / maxWidth);
  234. width = maxWidth;
  235. }
  236. if (height >= maxHeight && width <= maxWidth) {
  237. height = maxHeight;
  238. width = width / (height / maxHeight);
  239. }
  240. if (height > maxHeight && width >= maxWidth) {
  241. if (maxWidth / maxHeight > width / height) {
  242. width = width / (height / maxHeight);
  243. height = maxHeight;
  244. } else {
  245. height = height / (width / maxWidth);
  246. width = maxWidth;
  247. }
  248. }
  249. self.hw_style = {
  250. height: uni.upx2px(height) + 'px',
  251. width: uni.upx2px(width) + 'px',
  252. };
  253. console.log(self.hw_style);
  254. }
  255. })
  256. },
  257. navGoodsSearch() {
  258. uni.navigateTo({
  259. url: '/pages/search/search',
  260. })
  261. },
  262. onClickBack() {
  263. uni.navigateBack({
  264. delta: 1
  265. })
  266. },
  267. leftClick(){
  268. this.$emit('leftClick')
  269. }
  270. },
  271. }
  272. </script>
  273. <style lang="scss" scoped>
  274. .hxj-area{
  275. font-size: 28rpx;
  276. font-weight: 500;
  277. color: #FFFFFF;
  278. image{
  279. width: 24rpx;
  280. height: 20rpx;
  281. margin-left: 6rpx;
  282. }
  283. }
  284. /* #ifdef H5 */
  285. $bar-height: 0;
  286. /* #endif */
  287. /* #ifdef MP */
  288. $bar-height: 20px;
  289. /* #endif */
  290. $app-height: 44px + $bar-height;
  291. $app-bg-color: #FFFFFF;
  292. .app-nav-bar {
  293. .hidden-height {
  294. height: $app-height;
  295. }
  296. .app-navbar--fixed {
  297. position: fixed;
  298. z-index: 9998;
  299. top: 0;
  300. }
  301. .app-navbar--shadow {
  302. box-shadow: 0 1px 6px #ccc;
  303. }
  304. .app-navbar--border {
  305. border-bottom-width: #{1rpx};
  306. border-bottom-style: solid;
  307. border-bottom-color: $app-bg-color;
  308. }
  309. .app-navbar--content {
  310. width: 100vw;
  311. // background-color: $app-bg-color;
  312. overflow: hidden;
  313. }
  314. }
  315. .app-navbar__bar {
  316. height: $app-height;
  317. padding-top: $bar-height;
  318. .app-navbar__box {
  319. position: relative;
  320. width: 100%;
  321. .app-navbar__back {
  322. padding-left: #{26rpx};
  323. .icon-back {
  324. background-image: url("https://shop.9026.com/web/statics/image/mall/static/icon/h5-back-2.png");
  325. background-repeat: no-repeat;
  326. background-size: 100% 100%;
  327. height: #{27rpx};
  328. width: #{16rpx};
  329. display: block;
  330. }
  331. }
  332. .app-navbar__right {
  333. > view {
  334. padding-left: #{26rpx};
  335. }
  336. .left-icon image {
  337. max-height: #{54rpx};
  338. max-width: #{100rpx};
  339. height: 0;
  340. width: 0;
  341. display: block;
  342. }
  343. .title {
  344. font-size: #{32rpx};
  345. font-weight: bold;
  346. > view {
  347. max-width: 50vw;
  348. }
  349. }
  350. .link-search {
  351. width: #{298rpx};
  352. height: #{55rpx};
  353. background-color: #FFFFFF;
  354. border-radius: #{40rpx};
  355. position: relative;
  356. .link-box {
  357. position: absolute;
  358. top: 0;
  359. left: 0;
  360. font-size: #{22rpx};
  361. height: #{55rpx};
  362. padding: 0 #{24rpx};
  363. width: 100%;
  364. border: 1upx solid #e2e2e2;
  365. border-radius: #{40rpx};
  366. .search-placeholder {
  367. max-width: #{218rpx};
  368. }
  369. .search-icon {
  370. background-image: url("https://shop.9026.com/web/statics/image/mall/static/icon/icon-search.png");
  371. background-repeat: no-repeat;
  372. background-size: 100% 100%;
  373. height: #{26rpx};
  374. width: #{26rpx};
  375. }
  376. }
  377. }
  378. }
  379. .app-navbar__right-center {
  380. margin-right: 0;
  381. .title {
  382. padding-left: 0;
  383. }
  384. }
  385. .app-navbar__right-center.right__back {
  386. margin-right: calc(#{26rpx} + #{16rpx});
  387. }
  388. }
  389. }
  390. </style>