u-no-network.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. methods: {
  87. retry() {
  88. // 重新检查网络
  89. uni.getNetworkType({
  90. success: (res) => {
  91. this.networkType = res.networkType
  92. this.emitEvent(this.networkType)
  93. if (res.networkType == 'none') {
  94. uni.$u.toast('无网络连接')
  95. this.isConnected = false
  96. } else {
  97. uni.$u.toast('网络已连接')
  98. this.isConnected = true
  99. }
  100. }
  101. })
  102. this.$emit('retry')
  103. },
  104. // 发出事件给父组件
  105. emitEvent(networkType) {
  106. this.$emit(networkType === 'none' ? 'disconnected' : 'connected')
  107. },
  108. async openSettings() {
  109. if (this.networkType == "none") {
  110. this.openSystemSettings()
  111. return
  112. }
  113. },
  114. openAppSettings() {
  115. this.gotoAppSetting()
  116. },
  117. openSystemSettings() {
  118. // 以下方法来自5+范畴,如需深究,请自行查阅相关文档
  119. // https://ask.dcloud.net.cn/docs/
  120. if (this.isIOS) {
  121. this.gotoiOSSetting()
  122. } else {
  123. this.gotoAndroidSetting()
  124. }
  125. },
  126. network() {
  127. var result = null
  128. var cellularData = plus.ios.newObject("CTCellularData")
  129. var state = cellularData.plusGetAttribute("restrictedState")
  130. if (state == 0) {
  131. result = null
  132. } else if (state == 2) {
  133. result = 1
  134. } else if (state == 1) {
  135. result = 2
  136. }
  137. plus.ios.deleteObject(cellularData)
  138. return result
  139. },
  140. gotoAppSetting() {
  141. if (this.isIOS) {
  142. var UIApplication = plus.ios.import("UIApplication")
  143. var application2 = UIApplication.sharedApplication()
  144. var NSURL2 = plus.ios.import("NSURL")
  145. var setting2 = NSURL2.URLWithString("app-settings:")
  146. application2.openURL(setting2)
  147. plus.ios.deleteObject(setting2)
  148. plus.ios.deleteObject(NSURL2)
  149. plus.ios.deleteObject(application2)
  150. } else {
  151. var Intent = plus.android.importClass("android.content.Intent")
  152. var Settings = plus.android.importClass("android.provider.Settings")
  153. var Uri = plus.android.importClass("android.net.Uri")
  154. var mainActivity = plus.android.runtimeMainActivity()
  155. var intent = new Intent()
  156. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
  157. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null)
  158. intent.setData(uri)
  159. mainActivity.startActivity(intent)
  160. }
  161. },
  162. gotoiOSSetting() {
  163. var UIApplication = plus.ios.import("UIApplication")
  164. var application2 = UIApplication.sharedApplication()
  165. var NSURL2 = plus.ios.import("NSURL")
  166. var setting2 = NSURL2.URLWithString("App-prefs:root=General")
  167. application2.openURL(setting2)
  168. plus.ios.deleteObject(setting2)
  169. plus.ios.deleteObject(NSURL2)
  170. plus.ios.deleteObject(application2)
  171. },
  172. gotoAndroidSetting() {
  173. var Intent = plus.android.importClass("android.content.Intent")
  174. var Settings = plus.android.importClass("android.provider.Settings")
  175. var mainActivity = plus.android.runtimeMainActivity()
  176. var intent = new Intent(Settings.ACTION_SETTINGS)
  177. mainActivity.startActivity(intent)
  178. }
  179. }
  180. }
  181. </script>
  182. <style lang="scss" scoped>
  183. @import "../../libs/css/components.scss";
  184. .u-no-network {
  185. @include flex(column);
  186. justify-content: center;
  187. align-items: center;
  188. margin-top: -100px;
  189. &__tips {
  190. color: $u-tips-color;
  191. font-size: 14px;
  192. margin-top: 15px;
  193. }
  194. &__app {
  195. @include flex(row);
  196. margin-top: 6px;
  197. &__setting {
  198. color: $u-light-color;
  199. font-size: 13px;
  200. }
  201. &__to-setting {
  202. font-size: 13px;
  203. color: $u-primary;
  204. margin-left: 3px;
  205. }
  206. }
  207. &__retry {
  208. @include flex(row);
  209. justify-content: center;
  210. margin-top: 15px;
  211. }
  212. }
  213. </style>