index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <u-popup :show="popShow" @close="cancel" mode="center" :is-mask-click="false">
  3. <view class="popup-wrapper">
  4. <view class="title">服务器设置</view>
  5. <u-form labelPosition="left" labelAlign="right" :model="formData" :rules="rules" ref="formRef"
  6. label-width="180">
  7. <u-form-item label="服务器地址" prop="hostname" class="form-required">
  8. <uni-data-select @change="setServerStatus = false" :clear="false" v-model="formData.protocal"
  9. :localdata="[
  10. { value: 'http://', text: 'http://' },
  11. { value: 'https://', text: 'https://' }
  12. ]"></uni-data-select>
  13. <u-input v-model="formData.hostname" placeholder="输入" @change="setServerStatus = false">
  14. </u-input>
  15. </u-form-item>
  16. <u-form-item label="端口号" prop="port" class="form-required">
  17. <u-input v-model="formData.port" placeholder="输入" @change="setServerStatus = false">
  18. </u-input>
  19. </u-form-item>
  20. </u-form>
  21. <view class="footer">
  22. <u-button size="small" @click="cancel">取消</u-button>
  23. <u-button size="small" @click="handleTest">测试</u-button>
  24. <u-button size="small" type="success" @click="confirm" :disabled="!setServerStatus">确定</u-button>
  25. </view>
  26. </view>
  27. </u-popup>
  28. </template>
  29. <script>
  30. import Vue from 'vue'
  31. export default {
  32. data() {
  33. return {
  34. popShow: false,
  35. setServerStatus: false,
  36. formData: {
  37. protocal: 'http://',
  38. hostname: '',
  39. port: ''
  40. },
  41. apiInfo: {},
  42. rules: {
  43. hostname: {
  44. type: 'string',
  45. required: true,
  46. message: '请输入服务器地址',
  47. trigger: ['blur']
  48. },
  49. port: {
  50. type: 'number',
  51. required: true,
  52. message: '请输入正确的端口号',
  53. trigger: ['blur']
  54. }
  55. }
  56. }
  57. },
  58. created() {
  59. this.apiInfo = uni.getStorageSync('apiInfo')
  60. this.formData.hostname = this.apiInfo.hostname
  61. this.formData.port = this.apiInfo.port
  62. this.formData.protocal = this.apiInfo.protocal || 'http://'
  63. },
  64. methods: {
  65. open() {
  66. this.formData.hostname = this.apiInfo.hostname
  67. this.formData.port = this.apiInfo.port
  68. this.formData.protocal = this.apiInfo.protocal || 'http://'
  69. this.popShow = true
  70. },
  71. cancel() {
  72. // this.$refs.formRef && this.$refs.formRef.resetFields()
  73. this.popShow = false
  74. },
  75. serverCheck() {
  76. },
  77. handleTest() {
  78. this.setServerStatus = false
  79. this.$refs.formRef.validate().then(res => {
  80. uni.showLoading({
  81. mask: true
  82. })
  83. uni.request({
  84. url: `${this.formData.protocal}${this.formData.hostname}:${this.formData.port}/api/main/connection/getConnectionTest`,
  85. method: 'get',
  86. timeout:5000,
  87. success: res => {
  88. console.log(res);
  89. this.url = res.data.code
  90. if (res.data.code == 0) {
  91. uni.$u.toast('连接成功!')
  92. this.setServerStatus = true
  93. } else {
  94. uni.$u.toast('连接失败!')
  95. }
  96. },
  97. fail: f => {
  98. console.log(f);
  99. uni.$u.toast('连接失败!')
  100. uni.hideLoading()
  101. }
  102. })
  103. })
  104. },
  105. handleCheck() {
  106. },
  107. confirm() {
  108. this.$refs.formRef
  109. .validate()
  110. .then(res => {
  111. Vue.prototype.apiWebUrl =
  112. `${this.formData.protocal}${this.formData.hostname}:${this.formData.port}/web`
  113. Vue.prototype.apiUrl =
  114. `${this.formData.protocal}${this.formData.hostname}:${this.formData.port}/api`
  115. Vue.prototype.webviewUrl = `${this.formData.protocal}${this.formData.hostname}:${this.formData.port}`
  116. uni.setStorageSync('apiInfo', this.formData)
  117. this.$emit('setServerStatus', true)
  118. this.cancel()
  119. })
  120. .catch(errors => {
  121. uni.$u.toast('校验失败')
  122. })
  123. }
  124. }
  125. }
  126. </script>
  127. <style lang="scss" scoped>
  128. .popup-wrapper {
  129. padding: 0 20rpx;
  130. .title {
  131. text-align: center;
  132. font-weight: bold;
  133. padding: 20rpx;
  134. }
  135. /deep/.u-form {
  136. .u-input__content__field-wrapper__field,
  137. .u-form-item__body__left__content__label {
  138. font-size: 28rpx !important;
  139. }
  140. .u-form-item__body__right__content__slot {}
  141. }
  142. /deep/.uni-stat__select {
  143. // height: 76rpx !important;
  144. width: 150rpx;
  145. flex: 0;
  146. .uni-select {
  147. width: 150rpx;
  148. font-size: 28rpx !important;
  149. }
  150. .uni-stat-box,
  151. .uni-select__input-box {
  152. // height: 76rpx !important;
  153. }
  154. }
  155. /deep/.u-input {
  156. flex: 1;
  157. padding: 12rpx 18rpx !important;
  158. border: 1px solid #e5e5e5;
  159. }
  160. .footer {
  161. display: flex;
  162. justify-content: space-between;
  163. align-items: center;
  164. padding: 20rpx 0;
  165. .u-button+.u-button {
  166. margin-left: 20rpx;
  167. }
  168. }
  169. }
  170. </style>