app-nav-bar.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. <view class="tt t-omit">{{selectedProperties.name}}</view><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. selectedProperties: state => state.user.selectedProperties
  132. }),
  133. maxWidth() {
  134. return () => {
  135. let xstyle = parseInt(this.xStyle);
  136. let width = 0;
  137. switch (xstyle) {
  138. case 1:
  139. if (this.position === 'center') {
  140. width = uni.upx2px(360);
  141. } else {
  142. width = uni.upx2px(500);
  143. }
  144. break;
  145. case 2:
  146. width = uni.upx2px(400);
  147. break;
  148. case 4:
  149. width = uni.upx2px(200);
  150. break;
  151. default:
  152. break;
  153. }
  154. if (width && this.pagesLength > 1) {
  155. width -= uni.upx2px(42);
  156. }
  157. return Object.assign({}, {
  158. 'max-width': width + 'px',
  159. });
  160. }
  161. },
  162. hiddenHeight() {
  163. return () => {
  164. let barHeight;
  165. // #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO
  166. barHeight = this.statusBarHeight;
  167. // #endif
  168. barHeight = barHeight || 0;
  169. let height = barHeight + this.mBarHeight;
  170. this.$emit('headHeight', height);
  171. return {
  172. height: height + 'px',
  173. }
  174. }
  175. },
  176. ordinaryStyle() {
  177. return () => {
  178. let color = '';
  179. let backgroundColor = '';
  180. if (this.hasMallSetting == 1) {
  181. color = this.mallNavbar.top_text_color;
  182. backgroundColor = this.mallNavbar.top_background_color;
  183. } else {
  184. color = this.color;
  185. backgroundColor = this.backgroundColor;
  186. }
  187. let barHeight;
  188. // #ifdef MP
  189. barHeight = this.statusBarHeight;
  190. // #endif
  191. barHeight = barHeight || 0;
  192. color = color || '#000000';
  193. backgroundColor = backgroundColor || '#FFFFFF';
  194. return Object.assign({}, {
  195. color, backgroundColor,
  196. height: (barHeight + this.mBarHeight) + 'px',
  197. paddingTop: barHeight + 'px',
  198. })
  199. }
  200. },
  201. hasJump() {
  202. return [2, 4].indexOf(parseInt(this.xStyle)) !== -1
  203. },
  204. showLeftIcon() {
  205. return [2, 3,5].indexOf(parseInt(this.xStyle)) !== -1 && this.leftIcon
  206. },
  207. showTitle() {
  208. return [1, 2, 4].indexOf(parseInt(this.xStyle)) !== -1 && this.title
  209. },
  210. showLink() {
  211. return [3, 4].indexOf(parseInt(this.xStyle)) !== -1 && this.link
  212. },
  213. pagesLength() {
  214. return getCurrentPages().length;
  215. },
  216. },
  217. mounted() {
  218. this.doSomething();
  219. },
  220. methods: {
  221. doSomething() {
  222. const maxHeight = 54;
  223. const maxWidth = 100;
  224. const self = this;
  225. uni.getImageInfo({
  226. src: self.leftIcon,
  227. success: function (res) {
  228. let {height, width} = res;
  229. if (height <= maxHeight && width <= maxWidth) {
  230. // height = height;
  231. // width = width;
  232. }
  233. if (height <= maxHeight && width >= maxWidth) {
  234. height = height / (width / maxWidth);
  235. width = maxWidth;
  236. }
  237. if (height >= maxHeight && width <= maxWidth) {
  238. height = maxHeight;
  239. width = width / (height / maxHeight);
  240. }
  241. if (height > maxHeight && width >= maxWidth) {
  242. if (maxWidth / maxHeight > width / height) {
  243. width = width / (height / maxHeight);
  244. height = maxHeight;
  245. } else {
  246. height = height / (width / maxWidth);
  247. width = maxWidth;
  248. }
  249. }
  250. self.hw_style = {
  251. height: uni.upx2px(height) + 'px',
  252. width: uni.upx2px(width) + 'px',
  253. };
  254. console.log(self.hw_style);
  255. }
  256. })
  257. },
  258. navGoodsSearch() {
  259. uni.navigateTo({
  260. url: '/pages/search/search',
  261. })
  262. },
  263. onClickBack() {
  264. uni.navigateBack({
  265. delta: 1
  266. })
  267. },
  268. leftClick(){
  269. this.$emit('leftClick')
  270. }
  271. },
  272. }
  273. </script>
  274. <style lang="scss" scoped>
  275. .hxj-area{
  276. width: 260rpx;
  277. font-size: 28rpx;
  278. font-weight: 500;
  279. color: #FFFFFF;
  280. .tt{
  281. max-width: 240rpx;
  282. }
  283. image{
  284. width: 24rpx;
  285. height: 20rpx;
  286. margin-left: 6rpx;
  287. }
  288. }
  289. /* #ifdef H5 */
  290. $bar-height: 0;
  291. /* #endif */
  292. /* #ifdef MP */
  293. $bar-height: 20px;
  294. /* #endif */
  295. $app-height: 44px + $bar-height;
  296. $app-bg-color: #FFFFFF;
  297. .app-nav-bar {
  298. .hidden-height {
  299. height: $app-height;
  300. }
  301. .app-navbar--fixed {
  302. position: fixed;
  303. z-index: 9998;
  304. top: 0;
  305. }
  306. .app-navbar--shadow {
  307. box-shadow: 0 1px 6px #ccc;
  308. }
  309. .app-navbar--border {
  310. border-bottom-width: #{1rpx};
  311. border-bottom-style: solid;
  312. border-bottom-color: $app-bg-color;
  313. }
  314. .app-navbar--content {
  315. width: 100vw;
  316. // background-color: $app-bg-color;
  317. overflow: hidden;
  318. }
  319. }
  320. .app-navbar__bar {
  321. height: $app-height;
  322. padding-top: $bar-height;
  323. .app-navbar__box {
  324. position: relative;
  325. width: 100%;
  326. .app-navbar__back {
  327. padding-left: #{26rpx};
  328. .icon-back {
  329. // background-image: url("https://shop.9026.com/web/statics/image/mall/static/icon/h5-back-2.png");
  330. background-image: url("https://t17.9026.com/web/statics/image/index/back.png");
  331. background-repeat: no-repeat;
  332. background-size: 100% 100%;
  333. // height: #{27rpx};
  334. // width: #{16rpx};
  335. height: #{33rpx};
  336. width: #{33rpx};
  337. display: block;
  338. }
  339. }
  340. .app-navbar__right {
  341. > view {
  342. padding-left: #{26rpx};
  343. }
  344. .left-icon image {
  345. max-height: #{54rpx};
  346. max-width: #{100rpx};
  347. height: 0;
  348. width: 0;
  349. display: block;
  350. }
  351. .title {
  352. font-size: #{32rpx};
  353. font-weight: bold;
  354. > view {
  355. max-width: 50vw;
  356. }
  357. }
  358. .link-search {
  359. width: #{298rpx};
  360. height: #{55rpx};
  361. background-color: #FFFFFF;
  362. border-radius: #{40rpx};
  363. position: relative;
  364. .link-box {
  365. position: absolute;
  366. top: 0;
  367. left: 0;
  368. font-size: #{22rpx};
  369. height: #{55rpx};
  370. padding: 0 #{24rpx};
  371. width: 100%;
  372. border: 1upx solid #e2e2e2;
  373. border-radius: #{40rpx};
  374. .search-placeholder {
  375. max-width: #{218rpx};
  376. }
  377. .search-icon {
  378. background-image: url("https://shop.9026.com/web/statics/image/mall/static/icon/icon-search.png");
  379. background-repeat: no-repeat;
  380. background-size: 100% 100%;
  381. height: #{26rpx};
  382. width: #{26rpx};
  383. }
  384. }
  385. }
  386. }
  387. .app-navbar__right-center {
  388. margin-right: 0;
  389. .title {
  390. padding-left: 0;
  391. }
  392. }
  393. .app-navbar__right-center.right__back {
  394. margin-right: calc(#{26rpx} + #{16rpx});
  395. }
  396. }
  397. }
  398. </style>