serveConfig.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <template>
  2. <u-navbar :titleStyle="{ color: '#000' }" :autoBack="true" title="" :placeholder="true" :safeAreaInsetTop="true" bgColor="#FFFFFF">
  3. <template #left>
  4. <view class="u-navbar__content__left__item">
  5. <u-icon name="arrow-left" size="20" color="#000"></u-icon>
  6. </view>
  7. </template>
  8. </u-navbar>
  9. <view id="serveConfig-container">
  10. <view class="content-area">
  11. <text class="content-area-title">设置服务器地址</text>
  12. <u-icon class="content-area-icons" name="scan" color="#2a98ff" size="22"></u-icon>
  13. <text class="content-area-setting" @tap="scanClick"> 扫码添加 </text>
  14. </view>
  15. <view class="bottom-area">
  16. <u-input v-model="linkUrl" placeholder="服务器地址(必填)" border="none" clearable> </u-input>
  17. <u-input v-model="port" type="number" placeholder="服务器端口(非必填)" border="none" maxlength="5" clearable> </u-input>
  18. <u-input v-model="content" placeholder="备注(非必填)" border="none" maxlength="20" clearable> </u-input>
  19. <u-button
  20. type="primary"
  21. shape="circle"
  22. :customStyle="{
  23. display: 'block',
  24. width: '100%',
  25. height: '45px',
  26. lineHeight: '45px',
  27. fontSize: '17px',
  28. }"
  29. @click="handleSubmit()"
  30. >
  31. 保 存
  32. </u-button>
  33. </view>
  34. </view>
  35. </template>
  36. <script setup>
  37. /*----------------------------------依赖引入-----------------------------------*/
  38. import { onLoad, onShow, onHide, onLaunch } from "@dcloudio/uni-app";
  39. import { ref, onMounted, inject, shallowRef, reactive, toRefs, getCurrentInstance } from "vue";
  40. /*----------------------------------接口引入-----------------------------------*/
  41. /*----------------------------------组件引入-----------------------------------*/
  42. /*----------------------------------store引入-----------------------------------*/
  43. import { useStores, commonStores } from "@/store/modules/index";
  44. /*----------------------------------公共方法引入-----------------------------------*/
  45. import config from "@/config";
  46. /*----------------------------------公共变量-----------------------------------*/
  47. const { proxy } = getCurrentInstance();
  48. const useStore = useStores();
  49. const commonStore = commonStores();
  50. /*----------------------------------变量声明-----------------------------------*/
  51. const state = reactive({
  52. linkUrl: "", //链接地址
  53. port: "",
  54. content: "",
  55. index: "",
  56. });
  57. const { linkUrl, port, content, index } = toRefs(state);
  58. /**
  59. * @扫一扫
  60. * @按钮点击事件
  61. */
  62. function scanClick() {
  63. uni.scanCode({
  64. autoZoom: false,
  65. success: async (e) => {
  66. uni.showToast({
  67. title: "扫码成功",
  68. icon: "none",
  69. });
  70. linkUrl.value = e.result.split(":")[0];
  71. port.value = e.result.split(":")[1];
  72. },
  73. fail: (err) => {
  74. uni.showToast({
  75. title: "扫码失败",
  76. icon: "none",
  77. });
  78. console.log("扫码失败", err);
  79. },
  80. complete: () => {
  81. // uni.showToast({
  82. // title: "扫码结束",
  83. // icon: "none",
  84. // });
  85. console.log("扫码结束");
  86. },
  87. });
  88. }
  89. /**
  90. * @保存
  91. * @按钮点击事件
  92. */
  93. function handleSubmit() {
  94. if (!linkUrl.value) {
  95. proxy.$modal.msg("请输入链接地址");
  96. return;
  97. }
  98. if (!/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}(?:\.[a-zA-Z0-9]{2,})+$/.test(linkUrl.value)) {
  99. proxy.$modal.msg("请输入正确的链接地址");
  100. return;
  101. }
  102. // if (!port.value) {
  103. // proxy.$modal.msg("请输入端口号");
  104. // return;
  105. // }
  106. if (index.value) {
  107. let serveList = uni.getStorageSync("serveList");
  108. serveList[0].radiolist[index.value].linkUrl = !port.value ? linkUrl.value : linkUrl.value + ":" + port.value;
  109. serveList[0].radiolist[index.value].content = content.value;
  110. uni.setStorageSync("serveUrl", !port.value ? linkUrl.value : linkUrl.value + ":" + port.value);
  111. uni.setStorageSync("serveList", serveList);
  112. } else {
  113. if (!port.value) {
  114. uni.setStorageSync("serveUrl", linkUrl.value);
  115. commonStore.setServeList(linkUrl.value, content.value);
  116. } else {
  117. uni.setStorageSync("serveUrl", linkUrl.value + ":" + port.value);
  118. commonStore.setServeList(linkUrl.value + ":" + port.value, content.value);
  119. }
  120. }
  121. config.baseUrl = "http://" + uni.getStorageSync("serveUrl") + "/prod-api";
  122. navigateTo();
  123. }
  124. /**
  125. * @跳转登录
  126. */
  127. function navigateTo() {
  128. proxy.$tab.navigateBack(1);
  129. }
  130. onLoad((options) => {
  131. if ("index" in options) {
  132. index.value = options.index;
  133. let serveList = uni.getStorageSync("serveList");
  134. var serveArray = serveList[0].radiolist[options.index];
  135. if (serveArray.linkUrl.indexOf(":") != -1) {
  136. linkUrl.value = serveArray.linkUrl.split(":")[0];
  137. port.value = serveArray.linkUrl.split(":")[1];
  138. content.value = serveArray.content;
  139. } else {
  140. linkUrl.value = serveArray.linkUrl;
  141. port.value = "";
  142. content.value = serveArray.content;
  143. }
  144. }
  145. });
  146. </script>
  147. <style lang="scss" scoped>
  148. :deep() {
  149. .u-navbar__content__left,
  150. .u-navbar__content__right {
  151. padding: 0 25px;
  152. top: 50px;
  153. }
  154. }
  155. #serveConfig-container {
  156. position: fixed;
  157. top: 0;
  158. left: 0;
  159. right: 0;
  160. bottom: 0;
  161. z-index: 1;
  162. width: 100%;
  163. margin: auto;
  164. padding: 0 30px;
  165. padding-top: 30%;
  166. //#ifdef MP-WEIXIN
  167. padding-top: 40%;
  168. //#endif
  169. background-color: #ffffff;
  170. .top-area {
  171. }
  172. .content-area {
  173. display: flex;
  174. margin: 30px 0;
  175. &-title {
  176. margin: auto auto auto 0;
  177. color: #000;
  178. font-size: 18px;
  179. }
  180. &-icons {
  181. margin: auto 5px auto 0;
  182. }
  183. &-setting {
  184. color: #2a98ff;
  185. margin: auto 0;
  186. }
  187. }
  188. .bottom-area {
  189. :deep() {
  190. .u-input {
  191. height: 45px;
  192. border-radius: 8px;
  193. padding: 5px 12px !important;
  194. border: 0 !important;
  195. background-color: #f5f6fa !important;
  196. margin-bottom: 15px;
  197. }
  198. .input-placeholder {
  199. color: #c0c4cc !important;
  200. }
  201. .uni-input-wrapper {
  202. font-size: 16px;
  203. }
  204. .u-line {
  205. display: none !important;
  206. }
  207. }
  208. }
  209. }
  210. </style>