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.

233 lines
21 KiB

3 years ago
  1. <template>
  2. <view class="u-no-network" v-if="!isConnected" :style="{'z-index': uZIndex}" @touchmove.stop.prevent="() => {}">
  3. <view class="u-inner">
  4. <image class="u-error-icon" :src="image" mode="widthFix"></image>
  5. <view class="u-tips">
  6. {{tips}}
  7. </view>
  8. <!-- 只有APP平台才能跳转设置页因为需要调用plus环境 -->
  9. <!-- #ifdef APP-PLUS -->
  10. <view class="u-to-setting">
  11. 请检查网络或前往<text class="u-setting-btn" @tap="openSettings">设置</text>
  12. </view>
  13. <!-- #endif -->
  14. <view class="u-retry" :hover-stay-time="150" @tap="retry" hover-class="u-retry-hover">
  15. 重试
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. /**
  22. * noNetwork 无网络提示
  23. * @description 该组件无需任何配置引入即可内部自动处理所有功能和事件
  24. * @tutorial https://www.uviewui.com/components/noNetwork.html
  25. * @property {String} tips 没有网络时的提示语默认哎呀网络信号丢失
  26. * @property {String Number} zIndex 组件的z-index值默认1080
  27. * @property {String} image 无网络的图片提示可用的src地址或base64图片
  28. * @event {Function} retry 用户点击页面的"重试"按钮时触发
  29. * @example <u-no-network></u-no-network>
  30. */
  31. export default {
  32. name: "u-no-network",
  33. props: {
  34. // 页面文字提示
  35. tips: {
  36. type: String,
  37. default: '哎呀,网络信号丢失'
  38. },
  39. // 一个z-index值,用于设置没有网络这个组件的层次,因为页面可能会有其他定位的元素层级过高,导致此组件被覆盖
  40. zIndex: {
  41. type: [Number, String],
  42. default: ''
  43. },
  44. // image 没有网络的图片提示
  45. image: {
  46. type: String,
  47. default: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAEYCAMAAABFglBLAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i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g4sPCwvjn5+jo6M7Q0u6+vtyEhPXY2Li+wuikpPXW1uXo6dba3Pbg4Na5u+qurqqyt/HJydjb3fjo6LrAxO7Bwey1td6MjOrs7fbc3OTn6Maytf7+/vz19eqqqrzCxvzz87a8wLO6vuqxsf78/PDFxenr7L/FyNTY26+2u/z09MjMzqy0udnZ2dvb27G4vMjN0O7v8P339+Ll5u3v8NDS1ODj5frt7bC3vP76+u24uOKamtTW2MTIy9zc3N7e3vPQ0OCTk8DGyfDDw9LR0c7S1LussNbY2fPOztLU1cLHyrK6vvji4vrv78bKzPX29/np6crP0vjk5Ozu78bLzuepqczR1MHFyMDEyOq1tfTS0vP09cLIy9DU173Ex8bGxvHy88/U18vO0LK4vM7OzsTKzcPJzPLMzM/T1sbMz8HJy+7FxbW8wNTW18XKzvb3+MjLzczP0d3e3+Dh4r3Dxtna29zd3rK5vdPU1cfM0Prq6uOenuPm58HGyfHz8/ro6Lq/w8nO0bzCxcrKytjZ2uXm5vTU1LvBxbW7v+rp6eefn/39/eLi4u3t7Ozs7Pj4+Pr6+vX19fHx8erq6vPz8+Xl5ff39+fn5/v7+/z8/PLx8fX09Pb29ri4uOTk5PHw8OPj4/Py8ubm5vn5+e3s7Pb19fTz8/f29tfX1+7t7cvQ0+zr6/Ly8u3t7fr5+efm5quzuOvq6vT19uvr6/j5+a61uuy6uvb39/T09Ojn5+jq6621uvn6+t/i5KyzuPn4+Le+wvv6+s3S1fj4+fDw8OHk5vv7/Pr7++Xk5LS7v7y8vObl5fz9/ff4+OTj48DEx8XJy+Hk5euysu/x8re9wfn5+v38/Pv8/Pr6++vt7uLi4+Pi4ubl5uXk5dPV1tbV1fHMzNPX2e7u7ejn6PT19fX19PDw7/b29f39/vnr6+Hh4a+3u+7t7q61uePi48PHyf35+enp6fLz9PPz8qmxtuDg4N/f3/Dv7////////1cfN/UAAAEAdFJOU////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////wBT9wclAAAlqElEQVR42uydCXwUVZrAk5CEJBAQshACCGyjIRwBJR0It6JBGZZBkRtm5PBgFXAXHBghqAMz6LjGTrd9n7lvwn0JQhARxYNxVJTMwcwOM+7uzK6Z3VXcrnTXVr2q6q7urrvfq26c+n4egXTSVfXv993veym4JkklKdoj0IBoogHRgGiiAdGAaKIB0YBoogHRgGiiAdFEA6IBUVuwx+vqet/v1YAkhxwmcNT9gvjnsAYkGcT7eN1TtThe+1TdL2o1IEkg+rrvUV90MF9oQBIqv6ijF4a3d51XA5J4jVVXx3z587ojGpDEm/QwkO9pQJJB2CvksAYk8fJUXT31xZG63ppRTwI5QgcgR35Rd1MDkgxSX1f31P33P1V3i3u936HUyc3edWSsjuEakKRRW3r9kVv+JrRsrwbklpKTnXs+z0wfONOtAUm8rJu55oqfkZ0TOzUgCZS3Zh7s54+S5RqQhNGYMsnPISkakCSiQcjkNzUgasvUDaVsBF1fb9GXd//B0ziliPzjEA2IujLqGRaMST+cawgLNpn4q30aEDV1Va+dYRqzNy00RMoYUmdpQNTzcb8sCtuKH8w9Z4iWLuIbVzQgapmOBedDOO6c0WjgENK2vKwBUUUmvBSiUbrqxwZuOU18d5MGhEe/dI6a2WvIxA0HX1hfvH7V8gUpA9Mz3x/VuVTRLxvwdAhH5aJjBj4hv39QAxIl743ITHm6azBPnOCfVLwlc6pR3upYFbLjvIuDlEUqRYZo38JYS4gVt5rNZhMQO46byksoMeG4w8QIjtvMpJA/wP1EdZ0bKvkitkh555mBA3zSLnBlSFnNnlJvEJJ95ItW3pJAamvtZhy3m/T0g7fhthJGiG+EviYgmEJ/wHFP6GsbnlZSoicoucxmGwHVguPTBz543C9LHky/Jnql1w5KxWGo8KsThsAFYneYMOb5mlnP11ZCrwOPjfgGI8RqsZuZtUOsFmbllFhZP+zEbc8vGcqzMibv7qos3rd7Mg+TrQv2vCVkiLYcl4rDYLhKvi79FgFirDU7TPpyHHexINg9ZnOazWazyPxlVhsBzcKAKrl84cHo5zx758H0ASufWGrVBWkxup8YMepCespDV6Jfe3z9kOnc7/OjLydJx2FoJF983norAEkLqSbiw4+ZHGY7+UQhyYCDEZml8+ufzLz+RlBAPpieGYPloczYdaIb8rAMHAbD1yr5WHEBqTW7XIwZwEykBoIcrH3JrkgcL554PShRPpie/hDbE9v97oTIXz2zUhYOw0Lw4glJDMRiNtGrIo1AYUFwYZlsVbVzwyhfUJ4YO1MqWb/hsfRw6rzzPnk4DNlk2sQ/BU9aIGZaS2EOI6LLunBn+Fk+fWFpUJmcvPBSWH2dXz4A/OqTB2XiMBimgJf/NCmBGO0OykM1mW3ILur9cNq1cuATwbjk801htC8QcUQmk0NcIhWHoWKwPBdr9KwVuWvz//nVxYXIgTg
  48. }
  49. },
  50. data() {
  51. return {
  52. isConnected: true, // 是否有网络连接
  53. networkType: "none", // 网络类型
  54. }
  55. },
  56. computed: {
  57. uZIndex() {
  58. return this.zIndex ? this.zIndex : this.$u.zIndex.noNetwork;
  59. }
  60. },
  61. mounted() {
  62. this.isIOS = (uni.getSystemInfoSync().platform === 'ios');
  63. uni.onNetworkStatusChange((res) => {
  64. this.isConnected = res.isConnected;
  65. this.networkType = res.networkType;
  66. });
  67. uni.getNetworkType({
  68. success: (res) => {
  69. this.networkType = res.networkType;
  70. if (res.networkType == 'none') {
  71. this.isConnected = false;
  72. } else {
  73. this.isConnected = true;
  74. }
  75. }
  76. });
  77. },
  78. methods: {
  79. retry() {
  80. // 重新检查网络
  81. uni.getNetworkType({
  82. success: (res) => {
  83. this.networkType = res.networkType;
  84. if (res.networkType == 'none') {
  85. uni.showToast({
  86. title: '无网络连接',
  87. icon: 'none',
  88. position: 'top'
  89. })
  90. this.isConnected = false;
  91. } else {
  92. uni.showToast({
  93. title: '网络已连接',
  94. icon: 'none',
  95. position: 'top'
  96. })
  97. this.isConnected = true;
  98. }
  99. }
  100. });
  101. this.$emit('retry');
  102. },
  103. async openSettings() {
  104. if (this.networkType == "none") {
  105. this.openSystemSettings();
  106. return;
  107. }
  108. },
  109. openAppSettings() {
  110. this.gotoAppSetting();
  111. },
  112. openSystemSettings() {
  113. // 以下方法来自5+范畴,如需深究,请自行查阅相关文档
  114. // https://ask.dcloud.net.cn/docs/
  115. if (this.isIOS) {
  116. this.gotoiOSSetting();
  117. } else {
  118. this.gotoAndroidSetting();
  119. }
  120. },
  121. network() {
  122. var result = null;
  123. var cellularData = plus.ios.newObject("CTCellularData");
  124. var state = cellularData.plusGetAttribute("restrictedState");
  125. if (state == 0) {
  126. result = null;
  127. } else if (state == 2) {
  128. result = 1;
  129. } else if (state == 1) {
  130. result = 2;
  131. }
  132. plus.ios.deleteObject(cellularData);
  133. return result;
  134. },
  135. gotoAppSetting() {
  136. if (this.isIOS) {
  137. var UIApplication = plus.ios.import("UIApplication");
  138. var application2 = UIApplication.sharedApplication();
  139. var NSURL2 = plus.ios.import("NSURL");
  140. var setting2 = NSURL2.URLWithString("app-settings:");
  141. application2.openURL(setting2);
  142. plus.ios.deleteObject(setting2);
  143. plus.ios.deleteObject(NSURL2);
  144. plus.ios.deleteObject(application2);
  145. } else {
  146. var Intent = plus.android.importClass("android.content.Intent");
  147. var Settings = plus.android.importClass("android.provider.Settings");
  148. var Uri = plus.android.importClass("android.net.Uri");
  149. var mainActivity = plus.android.runtimeMainActivity();
  150. var intent = new Intent();
  151. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  152. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  153. intent.setData(uri);
  154. mainActivity.startActivity(intent);
  155. }
  156. },
  157. gotoiOSSetting() {
  158. var UIApplication = plus.ios.import("UIApplication");
  159. var application2 = UIApplication.sharedApplication();
  160. var NSURL2 = plus.ios.import("NSURL");
  161. var setting2 = NSURL2.URLWithString("App-prefs:root=General");
  162. application2.openURL(setting2);
  163. plus.ios.deleteObject(setting2);
  164. plus.ios.deleteObject(NSURL2);
  165. plus.ios.deleteObject(application2);
  166. },
  167. gotoAndroidSetting() {
  168. var Intent = plus.android.importClass("android.content.Intent");
  169. var Settings = plus.android.importClass("android.provider.Settings");
  170. var mainActivity = plus.android.runtimeMainActivity();
  171. var intent = new Intent(Settings.ACTION_SETTINGS);
  172. mainActivity.startActivity(intent);
  173. }
  174. }
  175. }
  176. </script>
  177. <style lang="scss" scoped>
  178. @import "../../libs/css/style.components.scss";
  179. .u-no-network {
  180. background-color: #fff;
  181. position: fixed;
  182. top: 0;
  183. left: 0;
  184. right: 0;
  185. bottom: 0;
  186. }
  187. .u-inner {
  188. height: 100vh;
  189. @include vue-flex;
  190. flex-direction: column;
  191. align-items: center;
  192. justify-content: center;
  193. margin-top: -15%;
  194. }
  195. .u-tips {
  196. color: $u-tips-color;
  197. font-size: 28rpx;
  198. padding: 30rpx 0;
  199. }
  200. .u-error-icon {
  201. width: 300rpx;
  202. }
  203. .u-to-setting {
  204. color: $u-light-color;
  205. font-size: 26rpx;
  206. }
  207. .u-setting-btn {
  208. font-size: 26rpx;
  209. color: $u-type-primary;
  210. }
  211. .u-retry {
  212. margin-top: 30rpx;
  213. border: 1px solid $u-tips-color;
  214. color: $u-tips-color;
  215. font-size: 28rpx;
  216. padding: 6rpx 30rpx;
  217. border-radius: 3px;
  218. }
  219. .u-retry-hover {
  220. color: #fff;
  221. background-color: $u-tips-color;
  222. }
  223. </style>