index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <template>
  2. <view
  3. v-if="showPopup"
  4. class="uni-popup"
  5. :class="[popupstyle, isDesktop ? 'fixforpc-z-index' : '']"
  6. :style="[{ zIndex: zIndex }]"
  7. @touchmove.stop.prevent="clear"
  8. >
  9. <view @touchstart="touchstart">
  10. <uni-transition
  11. key="1"
  12. v-if="maskShow"
  13. name="mask"
  14. mode-class="fade"
  15. :styles="maskClass"
  16. :duration="duration"
  17. :show="showTrans"
  18. @click="onTap"
  19. />
  20. <uni-transition
  21. key="2"
  22. :mode-class="ani"
  23. name="content"
  24. :styles="{ ...transClass, ...borderRadius }"
  25. :duration="duration"
  26. :show="showTrans"
  27. @click="onTap"
  28. >
  29. <view
  30. v-if="showPopup"
  31. class="uni-popup__wrapper"
  32. :style="[{ backgroundColor: bg }, borderRadius]"
  33. :class="[popupstyle]"
  34. @click="clear"
  35. >
  36. <uni-icons
  37. v-if="showClose"
  38. class="close-icon"
  39. color="#F6F6F6"
  40. type="closeempty"
  41. size="32"
  42. @click="close"
  43. ></uni-icons>
  44. <slot />
  45. </view>
  46. </uni-transition>
  47. </view>
  48. <!-- #ifdef H5 -->
  49. <keypress v-if="maskShow" @esc="onTap" />
  50. <!-- #endif -->
  51. </view>
  52. <!-- #ifdef MP -->
  53. <view v-else style="display: none">
  54. <slot></slot>
  55. </view>
  56. <!-- #endif -->
  57. </template>
  58. <script>
  59. // #ifdef H5
  60. import keypress from './keypress.js';
  61. // #endif
  62. /**
  63. * PopUp 弹出层
  64. * @description 弹出层组件,为了解决遮罩弹层的问题
  65. * @tutorial https://ext.dcloud.net.cn/plugin?id=329
  66. * @property {String} type = [top|center|bottom|left|right|message|dialog|share] 弹出方式
  67. * @value top 顶部弹出
  68. * @value center 中间弹出
  69. * @value bottom 底部弹出
  70. * @value left 左侧弹出
  71. * @value right 右侧弹出
  72. * @value message 消息提示
  73. * @value dialog 对话框
  74. * @value share 底部分享示例
  75. * @property {Boolean} animation = [true|false] 是否开启动画
  76. * @property {Boolean} maskClick = [true|false] 蒙版点击是否关闭弹窗(废弃)
  77. * @property {Boolean} isMaskClick = [true|false] 蒙版点击是否关闭弹窗
  78. * @property {String} backgroundColor 主窗口背景色
  79. * @property {String} maskBackgroundColor 蒙版颜色
  80. * @property {Boolean} safeArea 是否适配底部安全区
  81. * @event {Function} change 打开关闭弹窗触发,e={show: false}
  82. * @event {Function} maskClick 点击遮罩触发
  83. */
  84. export default {
  85. name: 'SuPopup',
  86. components: {
  87. // #ifdef H5
  88. keypress,
  89. // #endif
  90. },
  91. emits: ['change', 'maskClick', 'close'],
  92. props: {
  93. // 开启状态
  94. show: {
  95. type: Boolean,
  96. default: false,
  97. },
  98. // 顶部,底部时有效
  99. space: {
  100. type: Number,
  101. default: 0,
  102. },
  103. // 默认圆角
  104. round: {
  105. type: [String, Number],
  106. default: 0,
  107. },
  108. // 是否显示关闭
  109. showClose: {
  110. type: Boolean,
  111. default: false,
  112. },
  113. // 开启动画
  114. animation: {
  115. type: Boolean,
  116. default: true,
  117. },
  118. // 弹出层类型,可选值,top: 顶部弹出层;bottom:底部弹出层;center:全屏弹出层
  119. // message: 消息提示 ; dialog : 对话框
  120. type: {
  121. type: String,
  122. default: 'bottom',
  123. },
  124. // maskClick
  125. isMaskClick: {
  126. type: Boolean,
  127. default: null,
  128. },
  129. // TODO 2 个版本后废弃属性 ,使用 isMaskClick
  130. maskClick: {
  131. type: Boolean,
  132. default: null,
  133. },
  134. // 可设置none
  135. backgroundColor: {
  136. type: String,
  137. default: '#ffffff',
  138. },
  139. backgroundImage: {
  140. type: String,
  141. default: '',
  142. },
  143. safeArea: {
  144. type: Boolean,
  145. default: true,
  146. },
  147. maskBackgroundColor: {
  148. type: String,
  149. default: 'rgba(0, 0, 0, 0.4)',
  150. },
  151. zIndex: {
  152. type: [String, Number],
  153. default: 10075,
  154. },
  155. },
  156. watch: {
  157. show: {
  158. handler: function (newValue, oldValue) {
  159. if (typeof oldValue === 'undefined' && !newValue) {
  160. return;
  161. }
  162. if (newValue) {
  163. this.open();
  164. } else {
  165. this.close();
  166. }
  167. },
  168. immediate: true,
  169. },
  170. /**
  171. * 监听type类型
  172. */
  173. type: {
  174. handler: function (type) {
  175. if (!this.config[type]) return;
  176. this[this.config[type]](true);
  177. },
  178. immediate: true,
  179. },
  180. isDesktop: {
  181. handler: function (newVal) {
  182. if (!this.config[newVal]) return;
  183. this[this.config[this.type]](true);
  184. },
  185. immediate: true,
  186. },
  187. /**
  188. * 监听遮罩是否可点击
  189. * @param {Object} val
  190. */
  191. maskClick: {
  192. handler: function (val) {
  193. this.mkclick = val;
  194. },
  195. immediate: true,
  196. },
  197. isMaskClick: {
  198. handler: function (val) {
  199. this.mkclick = val;
  200. },
  201. immediate: true,
  202. },
  203. // H5 下禁止底部滚动
  204. showPopup(show) {
  205. // #ifdef H5
  206. // fix by mehaotian 处理 h5 滚动穿透的问题
  207. document.getElementsByTagName('body')[0].style.overflow = show ? 'hidden' : 'visible';
  208. // #endif
  209. },
  210. },
  211. data() {
  212. return {
  213. duration: 300,
  214. ani: [],
  215. showPopup: false,
  216. showTrans: false,
  217. popupWidth: 0,
  218. popupHeight: 0,
  219. config: {
  220. top: 'top',
  221. bottom: 'bottom',
  222. center: 'center',
  223. left: 'left',
  224. right: 'right',
  225. message: 'top',
  226. dialog: 'center',
  227. share: 'bottom',
  228. },
  229. maskClass: {
  230. position: 'fixed',
  231. bottom: 0,
  232. top: 0,
  233. left: 0,
  234. right: 0,
  235. backgroundColor: 'rgba(0, 0, 0, 0.4)',
  236. },
  237. transClass: {
  238. position: 'fixed',
  239. left: 0,
  240. right: 0,
  241. },
  242. maskShow: true,
  243. mkclick: true,
  244. popupstyle: this.isDesktop ? 'fixforpc-top' : 'top',
  245. };
  246. },
  247. computed: {
  248. isDesktop() {
  249. return this.popupWidth >= 500 && this.popupHeight >= 500;
  250. },
  251. bg() {
  252. if (this.backgroundColor === '' || this.backgroundColor === 'none') {
  253. return 'transparent';
  254. }
  255. return this.backgroundColor;
  256. },
  257. borderRadius() {
  258. if (this.round) {
  259. if (this.type === 'bottom') {
  260. return {
  261. 'border-top-left-radius': parseFloat(this.round) + 'px',
  262. 'border-top-right-radius': parseFloat(this.round) + 'px',
  263. };
  264. }
  265. if (this.type === 'center') {
  266. return {
  267. 'border-top-left-radius': parseFloat(this.round) + 'px',
  268. 'border-top-right-radius': parseFloat(this.round) + 'px',
  269. 'border-bottom-left-radius': parseFloat(this.round) + 'px',
  270. 'border-bottom-right-radius': parseFloat(this.round) + 'px',
  271. };
  272. }
  273. if (this.type === 'top') {
  274. return {
  275. 'border-bottom-left-radius': parseFloat(this.round) + 'px',
  276. 'border-bottom-right-radius': parseFloat(this.round) + 'px',
  277. };
  278. }
  279. }
  280. },
  281. },
  282. mounted() {
  283. const fixSize = () => {
  284. const { windowWidth, windowHeight, windowTop, safeArea, screenHeight, safeAreaInsets } = uni.getSystemInfoSync() || {} // sheep.$platform.device;
  285. this.popupWidth = windowWidth;
  286. this.popupHeight = windowHeight + (windowTop || 0);
  287. // TODO fix by mehaotian 是否适配底部安全区 ,目前微信ios 、和 app ios 计算有差异,需要框架修复
  288. if (safeArea && this.safeArea) {
  289. // #ifdef MP-WEIXIN
  290. this.safeAreaInsets = screenHeight - safeArea.bottom;
  291. // #endif
  292. // #ifndef MP-WEIXIN
  293. this.safeAreaInsets = safeAreaInsets.bottom;
  294. // #endif
  295. } else {
  296. this.safeAreaInsets = 0;
  297. }
  298. };
  299. fixSize();
  300. // #ifdef H5
  301. // window.addEventListener('resize', fixSize)
  302. // this.$once('hook:beforeDestroy', () => {
  303. // window.removeEventListener('resize', fixSize)
  304. // })
  305. // #endif
  306. },
  307. // #ifndef VUE3
  308. // TODO vue2
  309. destroyed() {
  310. this.setH5Visible();
  311. },
  312. // #endif
  313. // #ifdef VUE3
  314. // TODO vue3
  315. unmounted() {
  316. this.setH5Visible();
  317. },
  318. // #endif
  319. created() {
  320. // this.mkclick = this.isMaskClick || this.maskClick
  321. if (this.isMaskClick === null && this.maskClick === null) {
  322. this.mkclick = true;
  323. } else {
  324. this.mkclick = this.isMaskClick !== null ? this.isMaskClick : this.maskClick;
  325. }
  326. if (this.animation) {
  327. this.duration = 300;
  328. } else {
  329. this.duration = 0;
  330. }
  331. // TODO 处理 message 组件生命周期异常的问题
  332. this.messageChild = null;
  333. // TODO 解决头条冒泡的问题
  334. this.clearPropagation = false;
  335. this.maskClass.backgroundColor = this.maskBackgroundColor;
  336. },
  337. methods: {
  338. setH5Visible() {
  339. // #ifdef H5
  340. // fix by mehaotian 处理 h5 滚动穿透的问题
  341. document.getElementsByTagName('body')[0].style.overflow = 'visible';
  342. // #endif
  343. },
  344. /**
  345. * 公用方法,不显示遮罩层
  346. */
  347. closeMask() {
  348. this.maskShow = false;
  349. },
  350. /**
  351. * 公用方法,遮罩层禁止点击
  352. */
  353. disableMask() {
  354. this.mkclick = false;
  355. },
  356. // TODO nvue 取消冒泡
  357. clear(e) {
  358. // #ifndef APP-NVUE
  359. e.stopPropagation();
  360. // #endif
  361. this.clearPropagation = true;
  362. },
  363. open(direction) {
  364. // fix by mehaotian 处理快速打开关闭的情况
  365. if (this.showPopup) {
  366. clearTimeout(this.timer);
  367. this.showPopup = false;
  368. }
  369. let innerType = ['top', 'center', 'bottom', 'left', 'right', 'message', 'dialog', 'share'];
  370. if (!(direction && innerType.indexOf(direction) !== -1)) {
  371. direction = this.type;
  372. }
  373. if (!this.config[direction]) {
  374. console.error('缺少类型:', direction);
  375. return;
  376. }
  377. this[this.config[direction]]();
  378. this.$emit('change', {
  379. show: true,
  380. type: direction,
  381. });
  382. },
  383. close(type) {
  384. this.showTrans = false;
  385. this.$emit('change', {
  386. show: false,
  387. type: this.type,
  388. });
  389. this.$emit('close');
  390. clearTimeout(this.timer);
  391. // // 自定义关闭事件
  392. // this.customOpen && this.customClose()
  393. this.timer = setTimeout(() => {
  394. this.showPopup = false;
  395. }, 300);
  396. },
  397. // TODO 处理冒泡事件,头条的冒泡事件有问题 ,先这样兼容
  398. touchstart() {
  399. this.clearPropagation = false;
  400. },
  401. onTap() {
  402. if (this.clearPropagation) {
  403. // fix by mehaotian 兼容 nvue
  404. this.clearPropagation = false;
  405. return;
  406. }
  407. this.$emit('maskClick');
  408. if (!this.mkclick) return;
  409. this.close();
  410. },
  411. /**
  412. * 顶部弹出样式处理
  413. */
  414. top(type) {
  415. this.popupstyle = this.isDesktop ? 'fixforpc-top' : 'top';
  416. this.ani = ['slide-top'];
  417. this.transClass = {
  418. position: 'fixed',
  419. left: 0,
  420. right: 0,
  421. top: this.space + 'px',
  422. backgroundColor: this.bg,
  423. };
  424. // TODO 兼容 type 属性 ,后续会废弃
  425. if (type) return;
  426. this.showPopup = true;
  427. this.showTrans = true;
  428. this.$nextTick(() => {
  429. if (this.messageChild && this.type === 'message') {
  430. this.messageChild.timerClose();
  431. }
  432. });
  433. },
  434. /**
  435. * 底部弹出样式处理
  436. */
  437. bottom(type) {
  438. this.popupstyle = 'bottom';
  439. this.ani = ['slide-bottom'];
  440. this.transClass = {
  441. position: 'fixed',
  442. left: 0,
  443. right: 0,
  444. bottom: 0,
  445. paddingBottom: this.safeAreaInsets + this.space + 'px',
  446. backgroundColor: this.bg,
  447. };
  448. // TODO 兼容 type 属性 ,后续会废弃
  449. if (type) return;
  450. this.showPopup = true;
  451. this.showTrans = true;
  452. },
  453. /**
  454. * 中间弹出样式处理
  455. */
  456. center(type) {
  457. this.popupstyle = 'center';
  458. this.ani = ['zoom-out', 'fade'];
  459. this.transClass = {
  460. position: 'fixed',
  461. /* #ifndef APP-NVUE */
  462. display: 'flex',
  463. flexDirection: 'column',
  464. /* #endif */
  465. bottom: 0,
  466. left: 0,
  467. right: 0,
  468. top: 0,
  469. justifyContent: 'center',
  470. alignItems: 'center',
  471. };
  472. // TODO 兼容 type 属性 ,后续会废弃
  473. if (type) return;
  474. this.showPopup = true;
  475. this.showTrans = true;
  476. },
  477. left(type) {
  478. this.popupstyle = 'left';
  479. this.ani = ['slide-left'];
  480. this.transClass = {
  481. position: 'fixed',
  482. left: 0,
  483. bottom: 0,
  484. top: 0,
  485. backgroundColor: this.bg,
  486. /* #ifndef APP-NVUE */
  487. display: 'flex',
  488. flexDirection: 'column',
  489. /* #endif */
  490. };
  491. // TODO 兼容 type 属性 ,后续会废弃
  492. if (type) return;
  493. this.showPopup = true;
  494. this.showTrans = true;
  495. },
  496. right(type) {
  497. this.popupstyle = 'right';
  498. this.ani = ['slide-right'];
  499. this.transClass = {
  500. position: 'fixed',
  501. bottom: 0,
  502. right: 0,
  503. top: 0,
  504. backgroundColor: this.bg,
  505. /* #ifndef APP-NVUE */
  506. display: 'flex',
  507. flexDirection: 'column',
  508. /* #endif */
  509. };
  510. // TODO 兼容 type 属性 ,后续会废弃
  511. if (type) return;
  512. this.showPopup = true;
  513. this.showTrans = true;
  514. },
  515. },
  516. };
  517. </script>
  518. <style lang="scss">
  519. // 关闭icon
  520. .close-icon {
  521. position: absolute;
  522. left: 50%;
  523. transform: translateX(-50%);
  524. bottom: -80rpx;
  525. z-index: 100;
  526. }
  527. .uni-popup {
  528. position: fixed;
  529. /* #ifndef APP-NVUE */
  530. z-index: 99;
  531. /* #endif */
  532. &.top,
  533. &.left,
  534. &.right {
  535. /* #ifdef H5 */
  536. top: var(--window-top);
  537. /* #endif */
  538. /* #ifndef H5 */
  539. top: 0;
  540. /* #endif */
  541. }
  542. .uni-popup__wrapper {
  543. /* #ifndef APP-NVUE */
  544. display: block;
  545. /* #endif */
  546. position: relative;
  547. background: v-bind(backgroundImage) no-repeat;
  548. background-size: 100% 100%;
  549. /* iphonex 等安全区设置,底部安全区适配 */
  550. /* #ifndef APP-NVUE */
  551. // padding-bottom: constant(safe-area-inset-bottom);
  552. // padding-bottom: env(safe-area-inset-bottom);
  553. /* #endif */
  554. &.left,
  555. &.right {
  556. /* #ifdef H5 */
  557. padding-top: var(--window-top);
  558. /* #endif */
  559. /* #ifndef H5 */
  560. padding-top: 0;
  561. /* #endif */
  562. flex: 1;
  563. }
  564. }
  565. }
  566. .fixforpc-z-index {
  567. /* #ifndef APP-NVUE */
  568. z-index: 999;
  569. /* #endif */
  570. }
  571. .fixforpc-top {
  572. top: 0;
  573. }
  574. </style>