u-no-network.vue 5.9 KB

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