u-no-network.vue 7.1 KB

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