app-nav-bar.vue 12 KB

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