瑶都万能墙
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.

222 lines
6.0 KiB

8 months ago
  1. <template>
  2. <uv-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="uv-no-network"
  14. >
  15. <uv-icon
  16. :name="image"
  17. size="150"
  18. imgMode="widthFit"
  19. class="uv-no-network__error-icon"
  20. ></uv-icon>
  21. <text class="uv-no-network__tips">{{tips}}</text>
  22. <!-- 只有APP平台才能跳转设置页因为需要调用plus环境 -->
  23. <!-- #ifdef APP-PLUS -->
  24. <view class="uv-no-network__app">
  25. <text class="uv-no-network__app__setting">请检查网络或前往</text>
  26. <text
  27. class="uv-no-network__app__to-setting"
  28. @tap="openSettings"
  29. >设置</text>
  30. </view>
  31. <!-- #endif -->
  32. <view class="uv-no-network__retry">
  33. <uv-button
  34. size="mini"
  35. text="重试"
  36. type="primary"
  37. plain
  38. @click="retry"
  39. ></uv-button>
  40. </view>
  41. </view>
  42. </uv-overlay>
  43. </template>
  44. <script>
  45. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  46. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  47. import props from './props.js';
  48. /**
  49. * noNetwork 无网络提示
  50. * @description 该组件无需任何配置引入即可内部自动处理所有功能和事件
  51. * @tutorial https://www.uvui.cn/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 <uv-no-network></uv-no-network>
  57. */
  58. export default {
  59. name: "uv-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. this.$uv.toast('无网络连接')
  95. this.isConnected = false
  96. } else {
  97. this.$uv.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 '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  184. @import '@/uni_modules/uv-ui-tools/libs/css/color.scss';
  185. .uv-no-network {
  186. @include flex(column);
  187. justify-content: center;
  188. align-items: center;
  189. margin-top: -100px;
  190. &__tips {
  191. color: $uv-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: $uv-light-color;
  200. font-size: 13px;
  201. }
  202. &__to-setting {
  203. font-size: 13px;
  204. color: $uv-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>