u-no-network.vue 5.8 KB

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