index.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <view class="U000007-template1" :style="{backgroundColor: datas.preview_color}" :class="className">
  3. <!-- style -->
  4. <view v-html="css"></view>
  5. <view :style="decorate_cube" class="decorate-cube" v-if="datas.preview_color">
  6. <view v-if="!havePicture" style="text-align: center;display: flex;height: 100%;font-size: 16px">
  7. <span style="flex: 1;align-self: center">暂无设置图片</span>
  8. </view>
  9. <view v-if="havePicture">
  10. <view v-if="item.value.is_minApp==1" @click="$until.u_gopage('U_cube',item.value)" class="cube-selected"
  11. v-for="(item, index) in datas.picture.cudeSelected" :key="index"
  12. :style="{'width':getCubeSelectedWidth(item)+'px','height':getCubeSelectedHeight(item)+'px','top':getCubeSelectedTop(item)+'px','left':getCubeSelectedLeft(item)+'px',backgroundImage: `url(${item.value.image ? item.value.image : ''})`, backgroundSize: '100% 100%',backgroundPosition: 'center',backgroundRepeat: 'no-repeat'}">
  13. <view class="cube-selected-text">
  14. </view>
  15. </view>
  16. <!-- #ifdef MP-WEIXIN -->
  17. <view v-else class="cube-selected" @click="$until.u_gopage('U_cube',item.value)"
  18. :style="{'width':getCubeSelectedWidth(item)+'px','height':getCubeSelectedHeight(item)+'px','top':getCubeSelectedTop(item)+'px','left':getCubeSelectedLeft(item)+'px',backgroundImage: `url(${item.value.image ? item.value.image : ''})`, backgroundSize: '100% 100%',backgroundPosition: 'center',backgroundRepeat: 'no-repeat'}">
  19. <view class="cube-selected-text">
  20. </view>
  21. </view>
  22. <!-- #endif -->
  23. <!-- #ifdef H5 -->
  24. <view class="cube-selected" v-else
  25. :style="{'width':getCubeSelectedWidth(item)+'px','height':getCubeSelectedHeight(item)+'px','top':getCubeSelectedTop(item)+'px','left':getCubeSelectedLeft(item)+'px',backgroundImage: `url(${item.value.image ? item.value.image : ''})`, backgroundSize: '100% 100%',backgroundPosition: 'center',backgroundRepeat: 'no-repeat'}">
  26. <wx-open-launch-weapp :username="item.value.gh_id" :path="item.value.minApp_link">
  27. <view class="cube-selected-text">
  28. </view>
  29. </wx-open-launch-weapp>
  30. </view>
  31. <!-- #endif -->
  32. </view>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. // #ifdef H5
  38. var wx = require('jweixin-module');
  39. // #endif
  40. // 自定义样式
  41. const css = function() {
  42. if (this.datas.picture) {
  43. const {
  44. margin,
  45. } = this.datas;
  46. return `
  47. .component-${this.id} .decorate-cube {
  48. height: ${this.datas.picture.icon == '1' ? this.cubeItemHeight*2 : this.cubeItemHeight*this.cubeX}px;
  49. margin: 0 ${margin/2}px 0 ${margin/2}px;
  50. }
  51. `;
  52. }
  53. };
  54. export default {
  55. props: ['datas', 'styles'],
  56. data() {
  57. return {
  58. // density:4, //密度
  59. cubeWidth: 375, //魔方宽度
  60. cubeHeight: 375, //魔方高度
  61. cudeSelecting: {
  62. start: null,
  63. end: null,
  64. data: []
  65. },
  66. cudeSelected: [], //已经生成的单元
  67. havePicture: false,
  68. cubeX: 1,
  69. }
  70. },
  71. mounted() {
  72. this.checkhavePicture();
  73. },
  74. computed: {
  75. decorate_cube() {
  76. if (this.datas.picture) {
  77. const {
  78. margin,
  79. } = this.datas;
  80. return `
  81. height: ${this.datas.picture.icon == '1' ? this.cubeItemHeight*2 : this.cubeItemHeight*this.cubeX}px;
  82. margin: 0 ${margin/2}px 0 ${margin/2}px;
  83. `;
  84. }
  85. },
  86. css() {
  87. return '<style>' + css.call(this) + '</style>';
  88. },
  89. className() {
  90. const name = ['component-wrapper', `component-${this.id}`];
  91. return name;
  92. },
  93. //密度值
  94. densityNum: function() {
  95. return parseInt(this.datas.picture.density);
  96. },
  97. //单元魔方高度
  98. cubeItemHeight: function() {
  99. return this.cubeHeight / this.datas.picture.density;
  100. },
  101. //单元魔方宽度
  102. cubeItemWidth: function() {
  103. return this.cubeWidth / this.datas.picture.density;
  104. },
  105. },
  106. watch: {
  107. datas: {
  108. deep: true, //深度监听
  109. handler: function() {
  110. this.havePicture = false;
  111. this.cubeX = 1;
  112. for (let i = 0; i < this.datas.picture.cudeSelected.length; i++) {
  113. if (this.datas.picture.cudeSelected[i].end.x > this.cubeX) {
  114. this.cubeX = this.datas.picture.cudeSelected[i].end.x;
  115. }
  116. if (this.datas.picture.cudeSelected[i].value.image) {
  117. this.havePicture = true;
  118. }
  119. }
  120. if (this.datas.picture.cudeSelected.length === 0 || !this.havePicture) {
  121. this.cubeX = 4;
  122. }
  123. }
  124. }
  125. },
  126. methods: {
  127. checkhavePicture() {
  128. if (this.datas.picture) {
  129. this.havePicture = false;
  130. this.cubeX = 1;
  131. for (let i = 0; i < this.datas.picture.cudeSelected.length; i++) {
  132. if (this.datas.picture.cudeSelected[i].end.x > this.cubeX) {
  133. this.cubeX = this.datas.picture.cudeSelected[i].end.x;
  134. }
  135. if (this.datas.picture.cudeSelected[i].value.image) {
  136. this.havePicture = true;
  137. }
  138. }
  139. if (this.datas.picture.cudeSelected.length === 0 || !this.havePicture) {
  140. this.cubeX = 4;
  141. }
  142. }
  143. },
  144. //判断是否正在选择
  145. isSelecting: function(x, y) {
  146. var cudeSelectingData = this.cudeSelecting.data;
  147. for (var i = 0, len = cudeSelectingData.length; i < len; i++) {
  148. var coord = cudeSelectingData[i];
  149. if (parseInt(coord.x) === parseInt(x) && parseInt(coord.y) === parseInt(y)) {
  150. return true;
  151. }
  152. }
  153. return false;
  154. },
  155. //判断是否已经选择
  156. isSelected: function(x, y) {
  157. var list = this.cudeSelected;
  158. for (var i = 0; i < list.length; i++) {
  159. var data = list[i].data;
  160. for (var j = 0; j < data.length; j++) {
  161. var coord = data[j];
  162. if (parseInt(coord.x) === parseInt(x) && parseInt(coord.y) === parseInt(y)) {
  163. return true;
  164. }
  165. }
  166. }
  167. return false;
  168. },
  169. //计算选中层的宽度
  170. getCubeSelectedWidth: function(item) {
  171. return (parseInt(item.end.y) - parseInt(item.start.y) + 1) * this.cubeItemWidth - this.datas.margin;
  172. },
  173. //计算选中层的高度
  174. getCubeSelectedHeight: function(item) {
  175. return (parseInt(item.end.x) - parseInt(item.start.x) + 1) * this.cubeItemHeight - this.datas.margin;
  176. },
  177. //计算选中层的右边距离
  178. getCubeSelectedTop: function(item) {
  179. return (item.start.x - 1) * this.cubeItemHeight + this.datas.margin / 2;
  180. },
  181. //计算选中层的左边距离
  182. getCubeSelectedLeft: function(item) {
  183. // #ifdef H5
  184. return (item.start.y - 1) * this.cubeItemWidth + this.datas.margin / 4;
  185. // #endif
  186. // #ifdef MP-WEIXIN
  187. return (item.start.y - 1) * this.cubeItemWidth;
  188. // #endif
  189. }
  190. },
  191. };
  192. </script>
  193. <style lang="less" scoped>
  194. // 默认
  195. .component-wrapper {
  196. // width: 750rpx;
  197. }
  198. .decorate-cube {
  199. position: relative;
  200. // width: 100%;
  201. height: 390px;
  202. }
  203. .decorate-cube .cube-row:last-of-type .cube-item {
  204. /*border-right: 1px solid #e5e5e5;*/
  205. }
  206. .decorate-cube .cube-selected {
  207. position: absolute;
  208. background-color: #fff;
  209. /*border: 1px solid #ebedf0;*/
  210. text-align: center;
  211. color: #7d7e80;
  212. cursor: pointer;
  213. box-sizing: border-box;
  214. }
  215. .decorate-cube .cube-selected.cube-selected-click {
  216. background: #e0edff;
  217. /*border: 1px solid #155bd4;*/
  218. color: #29ba9c;
  219. z-index: 2;
  220. cursor: auto;
  221. }
  222. .decorate-cube .cube-selected-text {
  223. font-size: 12px;
  224. width: 100%;
  225. position: absolute;
  226. top: 50%;
  227. left: 50%;
  228. transform: translateX(-50%) translateY(-50%);
  229. }
  230. .decorate-cube .cube-row {
  231. float: left;
  232. list-style: none;
  233. padding: 0;
  234. margin: 0;
  235. }
  236. .decorate-cube .cube-item:first-child {
  237. /*border-top: 1px solid #e5e5e5;*/
  238. }
  239. .decorate-cube .cube-item {
  240. background: #f8f8f8;
  241. /*border-left: 1px solid #e5e5e5;*/
  242. /*border-bottom: 1px solid #e5e5e5;*/
  243. cursor: pointer;
  244. text-align: center;
  245. box-sizing: border-box;
  246. }
  247. .decorate-cube .cube-item.item-selecting {
  248. background: #e0edff;
  249. }
  250. .decorate-cube .cube-item.item-selecting .van-icon {
  251. display: none;
  252. }
  253. .decorate-cube .cube-item.item-selected {
  254. background: #e0edff;
  255. visibility: hidden;
  256. }
  257. .decorate-cube .cube-item.item-selected .van-icon {
  258. display: none;
  259. }
  260. </style>