u-no-network.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <u-overlay
  3. :show="!isConnected"
  4. :zIndex="zIndex"
  5. @touchmove.stop.prevent="noop"
  6. :customStyle="{
  7. backgroundColor: '#fff',
  8. display: 'flex',
  9. justifyContent: 'center'
  10. }"
  11. >
  12. <view class="u-no-network">
  13. <u-icon
  14. :name="image"
  15. size="150"
  16. imgMode="widthFit"
  17. class="u-no-network__error-icon"
  18. ></u-icon>
  19. <text class="u-no-network__tips">{{ tips }}</text>
  20. <!-- 只有APP平台,才能跳转设置页,因为需要调用plus环境 -->
  21. <!-- #ifdef APP-PLUS -->
  22. <view class="u-no-network__app">
  23. <text class="u-no-network__app__setting">请检查网络,或前往</text>
  24. <text class="u-no-network__app__to-setting" @tap="openSettings"
  25. >设置</text
  26. >
  27. </view>
  28. <!-- #endif -->
  29. <view class="u-no-network__retry">
  30. <u-button
  31. size="mini"
  32. text="重试"
  33. type="primary"
  34. plain
  35. @click="retry"
  36. ></u-button>
  37. </view>
  38. </view>
  39. </u-overlay>
  40. </template>
  41. <script>
  42. import props from './props.js'
  43. /**
  44. * noNetwork 无网络提示
  45. * @description 该组件无需任何配置,引入即可,内部自动处理所有功能和事件。
  46. * @tutorial https://www.uviewui.com/components/noNetwork.html
  47. * @property {String} tips 没有网络时的提示语 (默认:'哎呀,网络信号丢失' )
  48. * @property {String | Number} zIndex 组件的z-index值
  49. * @property {String} image 无网络的图片提示,可用的src地址或base64图片
  50. * @event {Function} retry 用户点击页面的"重试"按钮时触发
  51. * @example <u-no-network></u-no-network>
  52. */
  53. export default {
  54. name: 'u-no-network',
  55. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  56. data () {
  57. return {
  58. isConnected: true, // 是否有网络连接
  59. networkType: 'none' // 网络类型
  60. }
  61. },
  62. mounted () {
  63. this.isIOS = uni.getSystemInfoSync().platform === 'ios'
  64. uni.onNetworkStatusChange(res => {
  65. this.isConnected = res.isConnected
  66. this.networkType = res.networkType
  67. this.emitEvent(this.networkType)
  68. })
  69. uni.getNetworkType({
  70. success: res => {
  71. this.networkType = res.networkType
  72. this.emitEvent(this.networkType)
  73. if (res.networkType == 'none') {
  74. this.isConnected = false
  75. } else {
  76. this.isConnected = true
  77. }
  78. }
  79. })
  80. },
  81. methods: {
  82. retry () {
  83. // 重新检查网络
  84. uni.getNetworkType({
  85. success: res => {
  86. this.networkType = res.networkType
  87. this.emitEvent(this.networkType)
  88. if (res.networkType == 'none') {
  89. uni.$u.toast('无网络连接')
  90. this.isConnected = false
  91. } else {
  92. uni.$u.toast('网络已连接')
  93. this.isConnected = true
  94. }
  95. }
  96. })
  97. this.$emit('retry')
  98. },
  99. // 发出事件给父组件
  100. emitEvent (networkType) {
  101. this.$emit(networkType === 'none' ? 'disconnected' : 'connected')
  102. },
  103. async openSettings () {
  104. if (this.networkType == 'none') {
  105. this.openSystemSettings()
  106. return
  107. }
  108. },
  109. openAppSettings () {
  110. this.gotoAppSetting()
  111. },
  112. openSystemSettings () {
  113. // 以下方法来自5+范畴,如需深究,请自行查阅相关文档
  114. // https://ask.dcloud.net.cn/docs/
  115. if (this.isIOS) {
  116. this.gotoiOSSetting()
  117. } else {
  118. this.gotoAndroidSetting()
  119. }
  120. },
  121. network () {
  122. var result = null
  123. var cellularData = plus.ios.newObject('CTCellularData')
  124. var state = cellularData.plusGetAttribute('restrictedState')
  125. if (state == 0) {
  126. result = null
  127. } else if (state == 2) {
  128. result = 1
  129. } else if (state == 1) {
  130. result = 2
  131. }
  132. plus.ios.deleteObject(cellularData)
  133. return result
  134. },
  135. gotoAppSetting () {
  136. if (this.isIOS) {
  137. var UIApplication = plus.ios.import('UIApplication')
  138. var application2 = UIApplication.sharedApplication()
  139. var NSURL2 = plus.ios.import('NSURL')
  140. var setting2 = NSURL2.URLWithString('app-settings:')
  141. application2.openURL(setting2)
  142. plus.ios.deleteObject(setting2)
  143. plus.ios.deleteObject(NSURL2)
  144. plus.ios.deleteObject(application2)
  145. } else {
  146. var Intent = plus.android.importClass('android.content.Intent')
  147. var Settings = plus.android.importClass('android.provider.Settings')
  148. var Uri = plus.android.importClass('android.net.Uri')
  149. var mainActivity = plus.android.runtimeMainActivity()
  150. var intent = new Intent()
  151. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
  152. var uri = Uri.fromParts('package', mainActivity.getPackageName(), null)
  153. intent.setData(uri)
  154. mainActivity.startActivity(intent)
  155. }
  156. },
  157. gotoiOSSetting () {
  158. var UIApplication = plus.ios.import('UIApplication')
  159. var application2 = UIApplication.sharedApplication()
  160. var NSURL2 = plus.ios.import('NSURL')
  161. var setting2 = NSURL2.URLWithString('App-prefs:root=General')
  162. application2.openURL(setting2)
  163. plus.ios.deleteObject(setting2)
  164. plus.ios.deleteObject(NSURL2)
  165. plus.ios.deleteObject(application2)
  166. },
  167. gotoAndroidSetting () {
  168. var Intent = plus.android.importClass('android.content.Intent')
  169. var Settings = plus.android.importClass('android.provider.Settings')
  170. var mainActivity = plus.android.runtimeMainActivity()
  171. var intent = new Intent(Settings.ACTION_SETTINGS)
  172. mainActivity.startActivity(intent)
  173. }
  174. }
  175. }
  176. </script>
  177. <style lang="scss" scoped>
  178. @import '../../libs/css/components.scss';
  179. .u-no-network {
  180. @include flex(column);
  181. justify-content: center;
  182. align-items: center;
  183. margin-top: -100px;
  184. &__tips {
  185. color: $u-tips-color;
  186. font-size: 32rpx;
  187. margin-top: 15px;
  188. }
  189. &__app {
  190. @include flex(row);
  191. margin-top: 6px;
  192. &__setting {
  193. color: $u-light-color;
  194. font-size: 13px;
  195. }
  196. &__to-setting {
  197. font-size: 13px;
  198. color: $u-primary;
  199. margin-left: 3px;
  200. }
  201. }
  202. &__retry {
  203. @include flex(row);
  204. justify-content: center;
  205. margin-top: 15px;
  206. }
  207. }
  208. </style>