u-no-network.vue 6.1 KB

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