emoji-img.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <template>
  2. <view class="scroll-view-box img">
  3. <scroll-view scroll-y="true" @scrolltolower="scrolltolower" class="swiper-item-box">
  4. <view class="z_index2 flex_r list">
  5. <view class="icon_ list-item">
  6. <view class="icon_ list_item_img" @click="addEmoji">
  7. <image
  8. class="img"
  9. src="data:image/svg+xml;base64,PHN2ZyBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiBkYXRhLXNwbS1hbmNob3ItaWQ9ImEzMTN4LnNlYXJjaF9pbmRleC4wLmk0LjU5ZTIzYTgxQUtrSDd6IiB3aWR0aD0iMjAwIiBoZWlnaHQ9IjIwMCI+PHBhdGggZD0iTTUzMy4zMzMgNDkwLjY2N2gyODEuNmMxMi44IDAgMjEuMzM0IDguNTMzIDIxLjMzNCAyMS4zMzNzLTguNTM0IDIxLjMzMy0yMS4zMzQgMjEuMzMzaC0yODEuNnYyODEuNmMwIDEyLjgtOC41MzMgMjEuMzM0LTIxLjMzMyAyMS4zMzRzLTIxLjMzMy04LjUzNC0yMS4zMzMtMjEuMzM0di0yODEuNmgtMjgxLjZjLTEyLjggMC0yMS4zMzQtOC41MzMtMjEuMzM0LTIxLjMzM3M4LjUzNC0yMS4zMzMgMjEuMzM0LTIxLjMzM2gyODEuNnYtMjgxLjZjMC0xMi44IDguNTMzLTIxLjMzNCAyMS4zMzMtMjEuMzM0czIxLjMzMyA4LjUzNCAyMS4zMzMgMjEuMzM0djI4MS42eiIgZGF0YS1zcG0tYW5jaG9yLWlkPSJhMzEzeC5zZWFyY2hfaW5kZXguMC5pNS41OWUyM2E4MUFLa0g3eiIgY2xhc3M9InNlbGVjdGVkIiBmaWxsPSIjM2QzZDNkIi8+PC9zdmc+"
  10. mode="aspectFill"
  11. ></image>
  12. </view>
  13. </view>
  14. <view class="icon_ list-item" v-for="(item, index) in list" :key="item.id" @longpress.stop="longpress(item)">
  15. <view class="list-item-img">
  16. <view class="edit-choice" :class="{ edit_choice: item.isSelect }" @click.stop="onItem(item, index)" v-if="isEdit">
  17. <view class="icon_ edit-choice-select" :class="{ edit_choice_select: item.isSelect }">
  18. <image
  19. class="img"
  20. src="data:image/svg+xml;base64,PHN2ZyBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iMjAwIiBoZWlnaHQ9IjIwMCI+PHBhdGggZD0iTTkzMS4wMTMgMjI1LjA4Yy0yNC42MzItMjUuODk2LTY1LjAxNS0yNi4zODMtOTAuMTk0LTEuMUw0MDYuMTkyIDY2MC4yNTkgMTgxLjcyMyA0MjkuNDk2Yy0yMy4zMS0yMy45ODctNjIuMzkyLTIyLjY1Ni04Ny4zMTYgMi45NDYtMjQuOTIzIDI1LjYwNi0yNi4yMjEgNjUuNzk1LTIuODgzIDg5Ljc3N2wyNzQuMzg1IDI4Mi4wNjljMjMuMzA0IDIzLjk1NCA2Mi4zOTEgMjIuNjIzIDg3LjMxLTIuOTc4IDUuNzEtNS44NjUgOS45ODUtMTIuNTE0IDEzLjE5Mi0xOS41NDUgMi40NjctMS44NDYgNC45MjgtMy41OTcgNy4xNjMtNS44NjVsNDU2LjM3My00NTguMDkyYzI1LjIxMy0yNS4zMDggMjUuNjY4LTY2LjggMS4wNzEtOTIuNzI3aC0uMDA1em0wIDB6IiBmaWxsPSIjZmZmIi8+PC9zdmc+"
  21. mode="aspectFill"
  22. ></image>
  23. </view>
  24. </view>
  25. <!-- #ifdef H5 || MP -->
  26. <image class="img" :src="item.url" mode="aspectFill" @click="onEmoji(item)"></image>
  27. <!-- #endif -->
  28. <!-- #ifdef APP -->
  29. <view @click="onEmoji(item)">
  30. <cacheImage
  31. :src="item.url"
  32. :ext="item.ext"
  33. mstyle="
  34. {
  35. width: 150rpx;
  36. height: 150rpx;
  37. }
  38. "
  39. ></cacheImage>
  40. </view>
  41. <!-- #endif -->
  42. </view>
  43. </view>
  44. </view>
  45. <view style="height: 70rpx" v-show="isEdit"></view>
  46. </scroll-view>
  47. <view class="color__ text_30 flex_r fa_c edit" :class="{ edit_: isEdit }">
  48. <view class="edit-item" @click="cancel">取消</view>
  49. <view class="flex1"></view>
  50. <view class="edit-item" style="color: #d66362" @click="deleteFn">删除</view>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. // #ifdef APP
  56. import cacheImage from '../cache-image/cache-image.vue';
  57. // #endif
  58. import { show, throttle } from '@/utils/index.js';
  59. let listCache = [];
  60. let no = false;
  61. let page = 1;
  62. export default {
  63. components: {
  64. // #ifdef APP
  65. cacheImage
  66. // #endif
  67. },
  68. props: {
  69. value: {
  70. type: Boolean,
  71. default: false
  72. },
  73. isShow: {
  74. type: Boolean,
  75. default: false
  76. }
  77. },
  78. data() {
  79. return {
  80. isEdit: false,
  81. list: []
  82. };
  83. },
  84. created() {
  85. uni.$off('collectionEmoji', this.collectionEmoji);
  86. uni.$on('collectionEmoji', this.collectionEmoji);
  87. },
  88. watch: {
  89. value: {
  90. handler(newName, oldName) {
  91. this.isEdit = newName;
  92. },
  93. immediate: true
  94. },
  95. isShow: {
  96. handler(newName, oldName) {
  97. if (newName) {
  98. if (listCache.length) return (this.list = listCache);
  99. this.getList();
  100. }
  101. },
  102. immediate: true
  103. }
  104. },
  105. methods: {
  106. // 监听收藏表情包
  107. collectionEmoji(e) {
  108. // 执行两次,原因未知
  109. throttle(() => {
  110. this.list.unshift({
  111. ...e,
  112. isSelect: 0,
  113. id: new Date().getTime()
  114. });
  115. listCache = this.list;
  116. }, 1000);
  117. },
  118. longpress(item) {
  119. return;
  120. this.isEdit = true;
  121. item.isSelect = !item.isSelect;
  122. },
  123. scrolltolower() {
  124. if (no) return;
  125. page++;
  126. this.getList();
  127. },
  128. async getList() {
  129. this.list = [
  130. {
  131. id: 1,
  132. url: 'https://pic4.zhimg.com/50/v2-884e7c312b0776099b021170e780ca98_hd.gif?source=1940ef5c',
  133. path: 'emoji/fadacai.gif',
  134. isSelect: 0,
  135. ext: 'gif'
  136. },
  137. {
  138. id: 2,
  139. url: 'https://tse1-mm.cn.bing.net/th/id/OIP-C.3uqdEQWnOBPx0G40jIjh_gHaHa?rs=1&pid=ImgDetMain',
  140. path: 'emoji/fadacai.gif',
  141. isSelect: 0,
  142. ext: 'png'
  143. },
  144. {
  145. id: 3,
  146. url: 'https://pic3.zhimg.com/v2-4fa5f90ee5c75973d637000acc67c9f6_b.gif',
  147. path: 'emoji/fadacai.gif',
  148. isSelect: 0,
  149. ext: 'gif'
  150. },
  151. {
  152. id: 4,
  153. url: 'https://ts1.cn.mm.bing.net/th/id/R-C.b9e42f61bfc70941452284e2ada6816b?rik=LDOFmBD7XU2S%2bQ&riu=http%3a%2f%2fimg.zcool.cn%2fcommunity%2f019b7d580d6e63a84a0d304faaecf5.gif&ehk=g6iEiGATRFhX88Yqu1JyAuOFZ4xkAiPI0oWVoA8HPI0%3d&risl=&pid=ImgRaw&r=0',
  154. path: 'emoji/fadacai.gif',
  155. isSelect: 0,
  156. ext: 'gif'
  157. }
  158. ];
  159. },
  160. //
  161. onItem(item, index) {
  162. item['isSelect'] = !item['isSelect'];
  163. },
  164. // 取消
  165. cancel() {
  166. this.isEdit = false;
  167. this.$emit('input', this.isEdit);
  168. this.list.map((item, index) => {
  169. item['isSelect'] = false;
  170. return item;
  171. });
  172. },
  173. // 删除
  174. deleteFn() {
  175. let deleteItem = this.list.filter((item, index) => {
  176. return item.isSelect;
  177. });
  178. if (!deleteItem.length) return show('需选择表情包');
  179. console.log(deleteItem);
  180. uni.showModal({
  181. content: '确定删除选中的表情包?',
  182. success: async (res) => {
  183. if (res.confirm) {
  184. this.list = this.list.filter((item, index) => {
  185. return !item.isSelect;
  186. });
  187. listCache = this.list;
  188. } else if (res.cancel) {
  189. console.log('用户点击取消');
  190. this.isEdit = false;
  191. this.$emit('input', this.isEdit);
  192. }
  193. }
  194. });
  195. },
  196. onEmoji(item) {
  197. throttle(() => {
  198. this.$emit('onEmoji', item);
  199. }, 600);
  200. },
  201. addEmoji() {
  202. uni.chooseImage({
  203. count: 1,
  204. success: (res) => {
  205. uni.showLoading({
  206. title: '正在添加',
  207. mask: true
  208. });
  209. console.log(res);
  210. const tempFilePaths = res.tempFilePaths[0];
  211. show('已添加表情');
  212. this.list.unshift({
  213. url: tempFilePaths,
  214. ext: 'png',
  215. isSelect: 0,
  216. id: new Date().getTime()
  217. });
  218. listCache = this.list;
  219. }
  220. });
  221. },
  222. API_EmojiList() {
  223. return new Promise((res) => {
  224. http.get(
  225. 'xxxxxxxx',
  226. {
  227. page: page
  228. },
  229. true,
  230. (r) => {
  231. if (r.data.code == 0) return res(r);
  232. return show(r.data.msg), res(false);
  233. }
  234. );
  235. });
  236. }
  237. }
  238. };
  239. </script>
  240. <style lang="scss" scoped>
  241. .scroll-view-box {
  242. position: relative;
  243. width: 100%;
  244. height: 100%;
  245. }
  246. .swiper-item-box {
  247. position: relative;
  248. width: 100%;
  249. height: 100%;
  250. .list {
  251. width: 100%;
  252. flex-wrap: wrap;
  253. }
  254. .list-item {
  255. width: 25%;
  256. height: 200rpx;
  257. .list-item-img {
  258. position: relative;
  259. width: 150rpx;
  260. height: 150rpx;
  261. .edit-choice {
  262. position: absolute;
  263. z-index: 2;
  264. top: 0;
  265. left: 0;
  266. width: 100%;
  267. height: 100%;
  268. .edit-choice-select {
  269. position: absolute;
  270. top: 10rpx;
  271. right: 10rpx;
  272. width: 40rpx;
  273. height: 40rpx;
  274. border-radius: 50%;
  275. border: 1px solid #fff;
  276. background-color: rgba(216, 216, 216, 0.5);
  277. .img {
  278. width: 70%;
  279. height: 70%;
  280. }
  281. }
  282. .edit_choice_select {
  283. border: 1px solid #02c162;
  284. background-color: #02c162;
  285. }
  286. }
  287. .edit_choice {
  288. background-color: rgba(255, 255, 255, 0.8);
  289. }
  290. }
  291. .list_item_img {
  292. box-sizing: border-box;
  293. width: 150rpx;
  294. height: 150rpx;
  295. border-radius: 20rpx;
  296. border: 2px dashed #3d3d3d;
  297. .img {
  298. width: 70%;
  299. height: 70%;
  300. }
  301. }
  302. }
  303. }
  304. .edit {
  305. box-sizing: border-box;
  306. padding: 0 30rpx;
  307. position: absolute;
  308. z-index: 3;
  309. bottom: -74rpx;
  310. left: 0;
  311. width: 100%;
  312. height: 70rpx;
  313. transition: all 0.3s;
  314. background-color: rgba(246, 246, 246, 0.8);
  315. backdrop-filter: blur(10px);
  316. .edit-item {
  317. padding: 0 10rpx;
  318. }
  319. }
  320. .edit_ {
  321. bottom: 0;
  322. }
  323. </style>