uni-goods-nav.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <view class="uni-goods-nav">
  3. <!-- 底部占位 -->
  4. <view class="uni-tab__seat" />
  5. <view class="uni-tab__cart-box flex">
  6. <view class="flex uni-tab__cart-sub-left">
  7. <template v-for="(item, index) in options">
  8. <button
  9. class="flex uni-tab__cart-button-left uni-tab__shop-cart"
  10. hover-class="none"
  11. :key="index"
  12. @click="onClick(index, item)"
  13. v-if="item.type === 'contact'"
  14. open-type="contact"
  15. >
  16. <view class="uni-tab__icon">
  17. <uni-icons :type="item.icon" size="18" color="#666"></uni-icons>
  18. <!-- <image class="image" :src="item.icon" mode="widthFix" /> -->
  19. </view>
  20. <text class="uni-tab__text">{{ item.text }}</text>
  21. <view class="flex uni-tab__dot-box">
  22. <text
  23. v-if="item.info"
  24. :class="{ 'uni-tab__dots': item.info > 9 }"
  25. class="uni-tab__dot "
  26. :style="{
  27. backgroundColor: item.infoBackgroundColor ? item.infoBackgroundColor : '#ff0000',
  28. color: item.infoColor ? item.infoColor : '#fff'
  29. }"
  30. >{{ item.info }}</text
  31. >
  32. </view>
  33. </button>
  34. <view
  35. class="flex uni-tab__cart-button-left uni-tab__shop-cart"
  36. :key="index"
  37. @click="onClick(index, item)"
  38. v-else
  39. >
  40. <view class="uni-tab__icon">
  41. <uni-icons :type="item.icon" size="18" color="#666"></uni-icons>
  42. <!-- <image class="image" :src="item.icon" mode="widthFix" /> -->
  43. </view>
  44. <text class="uni-tab__text">{{ item.text }}</text>
  45. <view class="flex uni-tab__dot-box">
  46. <text
  47. v-if="item.info"
  48. :class="{ 'uni-tab__dots': item.info > 9 }"
  49. class="uni-tab__dot "
  50. :style="{
  51. backgroundColor: item.infoBackgroundColor ? item.infoBackgroundColor : '#ff0000',
  52. color: item.infoColor ? item.infoColor : '#fff'
  53. }"
  54. >{{ item.info }}</text
  55. >
  56. </view>
  57. </view>
  58. </template>
  59. </view>
  60. <view :class="{ 'uni-tab__right': fill }" class="flex uni-tab__cart-sub-right ">
  61. <!-- 按钮类型1 -->
  62. <template v-if="navType === 1">
  63. <view
  64. v-for="(item, index) in buttonGroup"
  65. :key="index"
  66. :style="{ background: item.backgroundColor, color: item.color }"
  67. class="flex uni-tab__cart-button-right"
  68. @click="buttonClick(index, item)"
  69. >
  70. <text :style="{ color: item.color }" class="uni-tab__cart-button-right-text">{{ item.text }}</text>
  71. </view>
  72. </template>
  73. <!-- 按钮类型2 -->
  74. <template v-if="navType === 2">
  75. <view
  76. v-for="(item, index) in buttonGroup"
  77. :key="index"
  78. :style="{ background: item.backgroundColor, color: item.color }"
  79. class="flex uni-tab__cart-button-right"
  80. @click="buttonClick(index, item)"
  81. >
  82. <text :style="{ color: item.color }" class="uni-tab__cart-button-right-text has-price">¥{{ item.price | formatPrice}}</text>
  83. <text :style="{ color: item.color }" class="uni-tab__cart-button-right-text has-price">{{ item.text }}</text>
  84. </view>
  85. </template>
  86. </view>
  87. </view>
  88. </view>
  89. </template>
  90. <script>
  91. import { initVueI18n } from '@dcloudio/uni-i18n'
  92. import messages from './i18n/index.js'
  93. const { t } = initVueI18n(messages)
  94. /**
  95. * GoodsNav 商品导航
  96. * @description 商品加入购物车、立即购买等
  97. * @tutorial https://ext.dcloud.net.cn/plugin?id=865
  98. * @property {Array} options 组件参数
  99. * @property {Array} buttonGroup 组件按钮组参数
  100. * @property {Boolean} fill = [true | false] 组件按钮组参数
  101. * @event {Function} click 左侧点击事件
  102. * @event {Function} buttonClick 右侧按钮组点击事件
  103. * @example <uni-goods-nav :fill="true" options="" buttonGroup="buttonGroup" @click="" @buttonClick="" />
  104. */
  105. export default {
  106. name: 'UniGoodsNav',
  107. emits: ['click', 'buttonClick'],
  108. props: {
  109. navType: {
  110. type: Number,
  111. default: 1
  112. },
  113. options: {
  114. type: Array,
  115. default() {
  116. return [
  117. {
  118. icon: 'shop',
  119. text: t('uni-goods-nav.options.shop')
  120. },
  121. {
  122. icon: 'cart',
  123. text: t('uni-goods-nav.options.cart')
  124. }
  125. ]
  126. }
  127. },
  128. buttonGroup: {
  129. type: Array,
  130. default() {
  131. return [
  132. {
  133. text: t('uni-goods-nav.buttonGroup.addToCart'),
  134. backgroundColor: '#ffa200',
  135. color: '#fff'
  136. },
  137. {
  138. text: t('uni-goods-nav.buttonGroup.buyNow'),
  139. backgroundColor: '#ff0000',
  140. color: '#fff'
  141. }
  142. ]
  143. }
  144. },
  145. fill: {
  146. type: Boolean,
  147. default: false
  148. }
  149. },
  150. filters:{
  151. formatPrice(price){
  152. if (!price) return '0.00'
  153. return Number(price).toFixed(2)
  154. }
  155. },
  156. methods: {
  157. onClick(index, item) {
  158. this.$emit('click', {
  159. index,
  160. content: item
  161. })
  162. },
  163. buttonClick(index, item) {
  164. if (uni.report) {
  165. uni.report(item.text, item.text)
  166. }
  167. this.$emit('buttonClick', {
  168. index,
  169. content: item
  170. })
  171. }
  172. }
  173. }
  174. </script>
  175. <style lang="scss" scoped>
  176. .flex {
  177. /* #ifndef APP-NVUE */
  178. display: flex;
  179. /* #endif */
  180. flex-direction: row;
  181. }
  182. .uni-goods-nav {
  183. /* #ifndef APP-NVUE */
  184. display: flex;
  185. /* #endif */
  186. flex: 1;
  187. flex-direction: row;
  188. }
  189. .uni-tab__cart-box {
  190. flex: 1;
  191. height: 50px;
  192. background-color: #fff;
  193. z-index: 900;
  194. }
  195. .uni-tab__cart-sub-left {
  196. padding: 0 5px;
  197. }
  198. .uni-tab__cart-sub-right {
  199. flex: 1;
  200. }
  201. .uni-tab__right {
  202. margin: 5px 0;
  203. margin-right: 10px;
  204. border-radius: 100px;
  205. overflow: hidden;
  206. }
  207. .uni-tab__cart-button-left {
  208. /* #ifndef APP-NVUE */
  209. display: flex;
  210. /* #endif */
  211. // flex: 1;
  212. position: relative;
  213. justify-content: center;
  214. align-items: center;
  215. flex-direction: column;
  216. margin: 0 10px;
  217. /* #ifdef H5 */
  218. cursor: pointer;
  219. /* #endif */
  220. }
  221. .uni-tab__icon {
  222. width: 18px;
  223. height: 18px;
  224. }
  225. .image {
  226. width: 18px;
  227. height: 18px;
  228. }
  229. .uni-tab__text {
  230. margin-top: 3px;
  231. font-size: $uni-font-size-sm;
  232. color: #646566;
  233. }
  234. .uni-tab__cart-button-right {
  235. /* #ifndef APP-NVUE */
  236. display: flex;
  237. flex-direction: column;
  238. /* #endif */
  239. flex: 1;
  240. justify-content: center;
  241. align-items: center;
  242. /* #ifdef H5 */
  243. cursor: pointer;
  244. /* #endif */
  245. }
  246. .uni-tab__cart-button-right-text {
  247. font-size: $uni-font-size-base;
  248. color: #fff;
  249. &.has-price{
  250. font-size: 26rpx;
  251. }
  252. }
  253. .uni-tab__cart-button-right:active {
  254. opacity: 0.7;
  255. }
  256. .uni-tab__dot-box {
  257. /* #ifndef APP-NVUE */
  258. display: flex;
  259. flex-direction: column;
  260. /* #endif */
  261. position: absolute;
  262. right: -2px;
  263. top: 2px;
  264. justify-content: center;
  265. align-items: center;
  266. // width: 0;
  267. // height: 0;
  268. }
  269. .uni-tab__dot {
  270. // width: 30rpx;
  271. // height: 30rpx;
  272. padding: 0 4px;
  273. line-height: 15px;
  274. color: #ffffff;
  275. text-align: center;
  276. font-size: 12px;
  277. background-color: #ff0000;
  278. border-radius: 15px;
  279. }
  280. .uni-tab__dots {
  281. padding: 0 4px;
  282. // width: auto;
  283. border-radius: 15px;
  284. }
  285. .uni-tab__color-y {
  286. background-color: #ffa200;
  287. }
  288. .uni-tab__color-r {
  289. background-color: #ff0000;
  290. }
  291. </style>