index.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <template>
  2. <view :class="className" :style="box_style">
  3. <!-- style -->
  4. <view v-html="css"></view>
  5. <!-- 列表 -->
  6. <template v-if="datas.preview_color">
  7. <view v-if="list.length <= 0" style="text-align: center;font-size: 32rpx;line-height: 64rpx">
  8. 请先添加按钮
  9. </view>
  10. <view v-if="list && list.length > 0&& !datas.is_scroll" :style="cswiper_style" class="cswiper">
  11. <view v-for="(items, idx) in scroll_list" :key="idx" class="button-box">
  12. <view v-if="item.is_minApp==1" @click="$until.u_gopage('U_button',item)" class="li"
  13. :style="li_style" v-for="(item, index) in items" :key="item.id">
  14. <image :style="liimg_style" class="button-img" :src="item.image || emptyImage" alt=""></image>
  15. <span :style="texts_style" class="button-title">{{ item.text }}</span>
  16. </view>
  17. <!-- #ifdef MP-WEIXIN -->
  18. <view v-else class="li" :style="li_style" @click="$until.u_gopage('U_button',item)"
  19. :key="`${index}-${item.id}`">
  20. <image :style="liimg_style" class="button-img" :src="item.image || emptyImage" alt=""></image>
  21. <span :style="texts_style" class="button-title">{{ item.text }}</span>
  22. </view>
  23. <!-- #endif -->
  24. <!-- #ifdef H5 -->
  25. <view v-else class="li" :style="li_style">
  26. <wx-open-launch-weapp :username="item.gh_id" :path="item.minApp_link"
  27. style="text-align: center;">
  28. <image :style="liimg_style" class="button-img" :src="item.image || emptyImage" alt="">
  29. </image>
  30. </wx-open-launch-weapp>
  31. <span :style="texts_style" class="button-title">{{ item.text }}</span>
  32. </view>
  33. <!-- #endif -->
  34. </view>
  35. </view>
  36. <swiper v-if="list && list.length > 0&& datas.is_scroll" :indicator-dots="true" :autoplay="false"
  37. :interval="3000" :current="currentIndex" :style="cswiper_style" class="cswiper">
  38. <swiper-item v-for="(items, idx) in scroll_list" :key="idx" class="button-box">
  39. <view v-if="item.is_minApp==1" @click="$until.u_gopage('U_button',item)" class="li"
  40. :style="li_style" v-for="(item, index) in items" :key="item.id">
  41. <image :style="liimg_style" class="button-img" :src="item.image || emptyImage" alt=""></image>
  42. <span :style="texts_style" class="button-title">{{ item.text }}</span>
  43. </view>
  44. <!-- #ifdef MP-WEIXIN -->
  45. <view v-else class="li" :style="li_style" @click="$until.u_gopage('U_button',item)"
  46. :key="`${index}-${item.id}`">
  47. <image :style="liimg_style" class="button-img" :src="item.image || emptyImage" alt=""></image>
  48. <span :style="texts_style" class="button-title">{{ item.text }}</span>
  49. </view>
  50. <!-- #endif -->
  51. <!-- #ifdef H5 -->
  52. <view v-else class="li" :style="li_style">
  53. <wx-open-launch-weapp :username="item.gh_id" :path="item.minApp_link"
  54. style="text-align: center;">
  55. <image :style="liimg_style" class="button-img" :src="item.image || emptyImage" alt="">
  56. </image>
  57. </wx-open-launch-weapp>
  58. <span :style="texts_style" class="button-title">{{ item.text }}</span>
  59. </view>
  60. <!-- #endif -->
  61. </swiper-item>
  62. </swiper>
  63. </template>
  64. </view>
  65. </template>
  66. <script>
  67. // 自定义样式
  68. const css = function() {
  69. if (this.datas.preview_color) {
  70. const {
  71. padding_top,
  72. padding_bottom,
  73. button_size,
  74. border_radius,
  75. button_number,
  76. preview_color,
  77. font_size,
  78. text_style,
  79. font_color,
  80. button_line,
  81. } = this.datas;
  82. let cheight = 0;
  83. if (button_line == 1) {
  84. cheight = (button_line * (padding_top + padding_bottom + button_size) + 40) * 2;
  85. } else {
  86. cheight = (button_line * (padding_top + padding_bottom + button_size) + 80) * 2;
  87. }
  88. return `
  89. .component-${this.id} {
  90. background-color: ${preview_color.color || '#f8f8f8'};
  91. }
  92. .component-${this.id} .button-box{
  93. background-color: ${preview_color.color || '#f8f8f8'};
  94. background-image: url(${preview_color.isColor == 2 ? preview_color.image : ''});
  95. background-size: 100% 100%;
  96. background-position: center;
  97. background-repeat: no-repeat;
  98. }
  99. .component-${this.id} .cswiper{
  100. height: ${cheight}rpx;
  101. }
  102. .component-${this.id} .button-box .li{
  103. width: ${button_number == 4 ? '25' : '20'}%;
  104. }
  105. .component-${this.id} .button-img{
  106. width: ${button_size}px;
  107. height: ${button_size}px;
  108. border-radius: ${border_radius}px;
  109. }
  110. .component-${this.id} .button-title{
  111. font-size:${font_size}px;
  112. font-weight: ${text_style.includes('1') ? 'bold' : ''};
  113. font-style: ${text_style.includes('2') ? 'italic' : ''};
  114. color: ${font_color};
  115. }
  116. `;
  117. }
  118. };
  119. export default {
  120. props: ['datas', 'styles', 'languages'],
  121. data() {
  122. let siteinfo = getApp().globalData.siteinfo;
  123. let data = {
  124. settingFile: siteinfo,
  125. emptyImage: siteinfo.root_img + '/static/app/image.png',
  126. scroll_list: [], //轮播时用的数值结构
  127. // 轮播图设置
  128. swiperOption: {
  129. autoplay: false,
  130. // loop: true, loop模式第二轮循环的时候第一张会闪烁
  131. delay: 3000,
  132. observer: true,
  133. // autoHeight: true,
  134. pagination: {
  135. el: '.swiper-pagination',
  136. }
  137. },
  138. currentIndex: 0,
  139. };
  140. return data;
  141. },
  142. computed: {
  143. box_style() {
  144. if (this.datas.preview_color) {
  145. const {
  146. preview_color
  147. } = this.datas;
  148. return `
  149. background-color: ${preview_color.color || '#f8f8f8'};
  150. `;
  151. }
  152. },
  153. cswiper_style() {
  154. if (this.datas.preview_color) {
  155. const {
  156. button_line,
  157. padding_top,
  158. button_size,
  159. padding_bottom
  160. } = this.datas;
  161. let cheight = 0;
  162. if (button_line == 1) {
  163. cheight = (button_line * (padding_top + padding_bottom + button_size) + 40) * 2;
  164. } else {
  165. cheight = (button_line * (padding_top + padding_bottom + button_size) + 80) * 2;
  166. }
  167. return `
  168. height: ${cheight}rpx;
  169. `;
  170. }
  171. },
  172. texts_style() {
  173. if (this.datas.preview_color) {
  174. const {
  175. font_size,
  176. text_style,
  177. font_color
  178. } = this.datas;
  179. return `
  180. font-size:${font_size}px;
  181. font-weight: ${text_style.includes('1') ? 'bold' : ''};
  182. font-style: ${text_style.includes('2') ? 'italic' : ''};
  183. color: ${font_color};
  184. `;
  185. }
  186. },
  187. liimg_style() {
  188. if (this.datas.preview_color) {
  189. const {
  190. button_size,
  191. border_radius
  192. } = this.datas;
  193. return `
  194. width: ${button_size}px;
  195. height: ${button_size}px;
  196. border-radius: ${border_radius}px;
  197. `;
  198. }
  199. },
  200. li_style() {
  201. if (this.datas.preview_color) {
  202. const {
  203. button_number,
  204. } = this.datas;
  205. return `
  206. width: ${button_number == 4 ? '25' : '20'}%;
  207. `;
  208. }
  209. },
  210. css() {
  211. return '<style>' + css.call(this) + '</style>';
  212. },
  213. className() {
  214. const name = ['component-wrapper', `component-${this.id}`];
  215. return name;
  216. },
  217. list() {
  218. return this.datas.list;
  219. },
  220. button_number() {
  221. return this.datas.button_number;
  222. },
  223. button_line() {
  224. return this.datas.button_line || 1;
  225. }
  226. },
  227. watch: {
  228. list: {
  229. deep: true, //深度监听
  230. handler: function() {
  231. let list = this.datas.list;
  232. this.scroll_list = [];
  233. if (!this.datas.button_line) {
  234. this.datas.button_line = 1
  235. }
  236. let page = Math.ceil(this.datas.list.length / this.datas.button_number / this.datas
  237. .button_line); //==> 页数
  238. let num = this.datas.button_number * this.datas.button_line; // ==> 每页多少个
  239. for (let i = 0; i < page; i++) {
  240. this.scroll_list.push(list.slice(i * num, (i + 1) * num))
  241. }
  242. }
  243. },
  244. button_number(newValue) {
  245. let list = this.datas.list;
  246. this.scroll_list = [];
  247. if (!this.datas.button_line) {
  248. this.datas.button_line = 1
  249. }
  250. let page = Math.ceil(this.datas.list.length / newValue / this.datas.button_line); //==> 页数
  251. let num = newValue * this.datas.button_line; // ==> 每页多少个
  252. for (let i = 0; i < page; i++) {
  253. this.scroll_list.push(list.slice(i * num, (i + 1) * num))
  254. }
  255. },
  256. button_line(newValue) {
  257. let list = this.datas.list;
  258. this.scroll_list = [];
  259. if (!newValue) {
  260. newValue = 1
  261. }
  262. let page = Math.ceil(this.datas.list.length / this.datas.button_number / newValue); //==> 页数
  263. let num = this.datas.button_number * newValue; // ==> 每页多少个
  264. for (let i = 0; i < page; i++) {
  265. this.scroll_list.push(list.slice(i * num, (i + 1) * num))
  266. }
  267. }
  268. },
  269. methods: {
  270. initButtons() {
  271. if (this.datas.list) {
  272. let list = this.datas.list;
  273. this.scroll_list = [];
  274. if (!this.datas.button_line) {
  275. this.datas.button_line = 1
  276. }
  277. let page = Math.ceil(this.datas.list.length / this.datas.button_number / this.datas
  278. .button_line); //==> 页数
  279. let num = this.datas.button_number * this.datas.button_line; // ==> 每页多少个
  280. for (let i = 0; i < page; i++) {
  281. this.scroll_list.push(list.slice(i * num, (i + 1) * num))
  282. }
  283. }
  284. }
  285. },
  286. mounted() {
  287. this.$nextTick(() => {
  288. this.initButtons();
  289. })
  290. },
  291. };
  292. </script>
  293. <style lang="less" scoped>
  294. // 默认
  295. .component-wrapper {
  296. // display: flex;
  297. justify-content: center;
  298. width: 750rpx;
  299. .button-box {
  300. display: flex;
  301. flex-wrap: wrap;
  302. width: 100%;
  303. .li {
  304. position: relative;
  305. margin: 20rpx 0;
  306. list-style: none;
  307. display: flex;
  308. flex-direction: column;
  309. align-self: center;
  310. justify-self: center;
  311. }
  312. .button-img {
  313. margin: 0 auto;
  314. }
  315. .button-title {
  316. text-align: center;
  317. margin: 0;
  318. text-overflow: ellipsis;
  319. padding-top: 10rpx;
  320. white-space: nowrap;
  321. word-wrap: break-word;
  322. overflow: hidden;
  323. }
  324. }
  325. }
  326. </style>