index.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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
  6. labelPosition="left"
  7. labelAlign="right"
  8. :model="formData"
  9. :rules="rules"
  10. ref="formRef"
  11. label-width="180"
  12. >
  13. <u-form-item label="服务器地址" prop="hostname" class="form-required">
  14. <uni-data-select
  15. @change="setServerStatus = false"
  16. :clear="false"
  17. v-model="formData.protocal"
  18. :localdata="[
  19. { value: 'http://', text: 'http://' },
  20. { value: 'https://', text: 'https://' }
  21. ]"
  22. ></uni-data-select>
  23. <u-input
  24. v-model="formData.hostname"
  25. placeholder="输入"
  26. @change="setServerStatus = false"
  27. >
  28. </u-input>
  29. </u-form-item>
  30. <u-form-item label="端口号" prop="port" class="form-required">
  31. <u-input
  32. v-model="formData.port"
  33. placeholder="输入"
  34. @change="setServerStatus = false"
  35. >
  36. </u-input>
  37. </u-form-item>
  38. </u-form>
  39. <view class="footer">
  40. <u-button size="small" @click="cancel">取消</u-button>
  41. <u-button size="small" @click="handleTest">测试</u-button>
  42. <u-button
  43. size="small"
  44. type="success"
  45. @click="confirm"
  46. :disabled="!setServerStatus"
  47. >确定</u-button
  48. >
  49. </view>
  50. </view>
  51. </u-popup>
  52. </template>
  53. <script>
  54. import { get } from '@/utils/api.js'
  55. import Vue from 'vue'
  56. export default {
  57. data () {
  58. return {
  59. popShow: false,
  60. setServerStatus: false,
  61. formData: {
  62. protocal: 'http://',
  63. hostname: '',
  64. port: ''
  65. },
  66. apiInfo: {},
  67. rules: {
  68. hostname: {
  69. type: 'string',
  70. required: true,
  71. message: '请输入服务器地址',
  72. trigger: ['blur']
  73. },
  74. port: {
  75. type: 'number',
  76. required: true,
  77. message: '请输入正确的端口号',
  78. trigger: ['blur']
  79. }
  80. }
  81. }
  82. },
  83. created () {
  84. this.apiInfo = uni.getStorageSync('apiInfo')
  85. this.formData.hostname = this.apiInfo.hostname
  86. this.formData.port = this.apiInfo.port
  87. this.formData.protocal = this.apiInfo.protocal || 'http://'
  88. },
  89. methods: {
  90. open () {
  91. this.formData.hostname = this.apiInfo.hostname
  92. this.formData.port = this.apiInfo.port
  93. this.formData.protocal = this.apiInfo.protocal || 'http://'
  94. this.popShow = true
  95. },
  96. serverCheck () {
  97. const _this = this
  98. if (!this.apiInfo.hostname) {
  99. Vue.prototype.apiWebUrl = ``
  100. Vue.prototype.apiUrl = ``
  101. this.open()
  102. return
  103. }
  104. new Promise(resolve => {
  105. uni.showLoading({
  106. mask: true
  107. })
  108. this.setServerStatus = false
  109. this.handleCheck()
  110. .then(res => {
  111. if (!res.success) {
  112. resolve()
  113. } else {
  114. this.$emit('setServerStatus', true)
  115. this.setServerStatus = true
  116. }
  117. })
  118. .catch(resolve)
  119. .finally(() => {
  120. uni.hideLoading()
  121. })
  122. }).then(() => {
  123. this.$emit('setServerStatus', false)
  124. uni.showModal({
  125. title: '服务器地址异常',
  126. content: '',
  127. confirmText: '设置服务器地址',
  128. success: function (res) {
  129. if (res.confirm) {
  130. _this.open()
  131. }
  132. }
  133. })
  134. })
  135. },
  136. cancel () {
  137. // this.$refs.formRef && this.$refs.formRef.resetFields()
  138. this.popShow = false
  139. },
  140. handleTest () {
  141. this.setServerStatus = false
  142. this.$refs.formRef.validate().then(res => {
  143. uni.showLoading({
  144. mask: true
  145. })
  146. this.handleCheck()
  147. .then(res => {
  148. if (res.success) {
  149. uni.$u.toast('连接成功!')
  150. this.setServerStatus = true
  151. } else {
  152. uni.$u.toast('连接失败!')
  153. }
  154. })
  155. .catch(err => {
  156. uni.$u.toast('连接失败!')
  157. })
  158. .finally(() => {
  159. uni.hideLoading()
  160. })
  161. })
  162. },
  163. handleCheck () {
  164. return get(
  165. `${this.formData.protocal}${this.formData.hostname}:${this.formData.port}/connection/getConnectionTest`,
  166. {},
  167. true,
  168. true,
  169. { timeout: 15000 }
  170. )
  171. },
  172. confirm () {
  173. this.$refs.formRef
  174. .validate()
  175. .then(res => {
  176. Vue.prototype.apiWebUrl = `${this.formData.protocal}${this.formData.hostname}:${this.formData.port}/web`
  177. Vue.prototype.apiUrl = `${this.formData.protocal}${this.formData.hostname}:${this.formData.port}/api`
  178. uni.setStorageSync('apiInfo', this.formData)
  179. this.$emit('setServerStatus', true)
  180. this.cancel()
  181. })
  182. .catch(errors => {
  183. uni.$u.toast('校验失败')
  184. })
  185. }
  186. }
  187. }
  188. </script>
  189. <style lang="scss" scoped>
  190. .popup-wrapper {
  191. padding: 0 20rpx;
  192. .title {
  193. text-align: center;
  194. font-weight: bold;
  195. padding: 20rpx;
  196. }
  197. /deep/.u-form {
  198. .u-input__content__field-wrapper__field,
  199. .u-form-item__body__left__content__label {
  200. font-size: 28rpx !important;
  201. }
  202. .u-form-item__body__right__content__slot {
  203. }
  204. }
  205. /deep/.uni-stat__select {
  206. // height: 76rpx !important;
  207. width: 150rpx;
  208. flex: 0;
  209. .uni-select {
  210. width: 150rpx;
  211. font-size: 28rpx !important;
  212. }
  213. .uni-stat-box,
  214. .uni-select__input-box {
  215. // height: 76rpx !important;
  216. }
  217. }
  218. /deep/.u-input {
  219. flex: 1;
  220. padding: 12rpx 18rpx !important;
  221. border: 1px solid #e5e5e5;
  222. }
  223. .footer {
  224. display: flex;
  225. justify-content: space-between;
  226. align-items: center;
  227. padding: 20rpx 0;
  228. .u-button + .u-button {
  229. margin-left: 20rpx;
  230. }
  231. }
  232. }
  233. </style>