You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
6.0 KiB

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