yk-screenRecord.vue 4.3 KB

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