yk-screenRecord.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <view class="zzc_mol" v-if="isRecording"></view>
  3. </template>
  4. <script>
  5. export default {
  6. data() {
  7. return {
  8. isRecording: false
  9. }
  10. },
  11. created() {
  12. this.screenInit();
  13. },
  14. onUnload() {
  15. this.AllowScreenshots()
  16. },
  17. beforeDestroy() {
  18. this.AllowScreenshots()
  19. },
  20. methods: {
  21. screenInit() {
  22. let _this = this;
  23. // #ifdef H5
  24. uni.showToast({
  25. title: '请在APP或者小程序环境下操作',
  26. icon: 'error'
  27. })
  28. // #endif
  29. // #ifndef H5
  30. uni.getSystemInfo({
  31. success: function(e) {
  32. // #ifdef APP-PLUS
  33. if (e.platform == 'android') {
  34. //注意:一旦开启禁止截屏/录屏后将会全局生效,关闭页面时记得放开允许截屏/录屏
  35. _this.NoscreenCapture();
  36. } else {
  37. //在APP IOS端截屏、录屏暂时没办法实现,可以寻求原生解决
  38. }
  39. // #endif
  40. // #ifdef MP-WEIXIN
  41. if (e.platform == 'android') {
  42. //微信小程序在安卓手机上 禁止截屏/录屏(注意:禁止后录屏动作还是会进行,但录屏后的视频是黑屏的)
  43. wx.setVisualEffectOnCapture({
  44. visualEffect: 'hidden', //传入 hidden 则表示在截屏/录屏时隐藏屏幕
  45. success: (res) => {
  46. console.log(res)
  47. },
  48. fail: (err) => {
  49. console.log(err)
  50. },
  51. complete: (res) => {
  52. console.log(res)
  53. }
  54. })
  55. } else {
  56. //微信小程序在IOS手机上,注意:目前微信小程序在ios上也只能通过监听录屏状态,然后通过添加view层来进行阻止,截屏暂时无法实现
  57. //监听用户录屏事件
  58. wx.onScreenRecordingStateChanged(function(res) {
  59. if (res.state == 'start') {
  60. _this.isRecording = true
  61. } else {
  62. _this.isRecording = false
  63. }
  64. })
  65. //查询用户是否在录屏
  66. wx.getScreenRecordingState({
  67. success: (res) => {
  68. if (res.state == 'on') {
  69. _this.isRecording = true
  70. } else if (res.state == 'off') {
  71. _this.isRecording = false
  72. }
  73. },
  74. fail: (err) => {
  75. _this.isRecording = false
  76. }
  77. })
  78. }
  79. // #endif
  80. }
  81. })
  82. // #endif
  83. },
  84. //安卓端禁止截屏
  85. NoscreenCapture() {
  86. let osname = plus.os.name;
  87. if (osname == "Android") {
  88. var activity = plus.android.runtimeMainActivity();
  89. plus.android.invoke(plus.android.invoke(activity, "getWindow"), "addFlags", 0x00002000);
  90. }
  91. },
  92. //安卓端允许截屏
  93. AllowScreenshots() {
  94. let _this = this;
  95. uni.getSystemInfo({
  96. success: function(e) {
  97. // #ifdef MP-WEIXIN
  98. if (e.platform == 'android') {
  99. //微信小程序在安卓手机上 禁止截屏/录屏(注意:禁止后录屏动作还是会进行,但录屏后的视频是黑屏的)
  100. wx.setVisualEffectOnCapture({
  101. visualEffect: 'none', //传入 hidden 则表示在截屏/录屏时隐藏屏幕
  102. success: (res) => {
  103. console.log(res)
  104. },
  105. fail: (err) => {
  106. console.log(err)
  107. },
  108. complete: (res) => {
  109. console.log(res)
  110. }
  111. })
  112. } else {
  113. //微信小程序在IOS手机上,注意:目前微信小程序在ios上也只能通过监听录屏状态,然后通过添加view层来进行阻止,截屏暂时无法实现
  114. //监听用户录屏事件
  115. wx.onScreenRecordingStateChanged(function(res) {
  116. if (res.state == 'start') {
  117. _this.isRecording = false
  118. } else {
  119. _this.isRecording = false
  120. }
  121. })
  122. //查询用户是否在录屏
  123. wx.getScreenRecordingState({
  124. success: (res) => {
  125. if (res.state == 'on') {
  126. _this.isRecording = false
  127. } else if (res.state == 'off') {
  128. _this.isRecording = false
  129. }
  130. },
  131. fail: (err) => {
  132. _this.isRecording = false
  133. }
  134. })
  135. }
  136. // #endif
  137. }
  138. })
  139. // #ifdef APP-PLUS
  140. let osname = plus.os.name;
  141. if (osname == "Android") {
  142. var activity = plus.android.runtimeMainActivity();
  143. plus.android.invoke(plus.android.invoke(activity, "getWindow"), "clearFlags", 0x00002000);
  144. }
  145. // #endif
  146. }
  147. }
  148. }
  149. </script>
  150. <style>
  151. .zzc_mol {
  152. background: #000;
  153. width: 100%;
  154. height: 100%;
  155. position: fixed;
  156. z-index: 99999;
  157. top: 0;
  158. left: 0;
  159. }
  160. </style>