serveConfigSelect.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <view id="serveConfig">
  3. <view class="top">
  4. <u-icon name="arrow-left" size="17px" color="#000" :bold="true" @click="navigateTo"></u-icon>
  5. </view>
  6. <view class="content">
  7. <text class="title">选择服务器地址</text>
  8. <u-icon class="icons" name="scan" color="#2a98ff" size="22" @tap="serveClick(1)"></u-icon>
  9. <text class="setting" @click="serveClick(2)"> 添加 </text>
  10. </view>
  11. <view class="bottom">
  12. <div class="serveList">
  13. <u-radio-group v-model="radiovalue" placement="column" @change="handleRadio">
  14. <div style="display: flex; background-color: #f5f6fa !important; margin-bottom: 15px; padding: 10px; border-radius: 8px" v-for="(item, index) in radiolist" :key="index">
  15. <u-radio :customStyle="{ margin: 'auto 0' }" :value="item.id" :name="item.id"></u-radio>
  16. <div style="margin: auto 0 auto 0; width: 100%" @click="handleRadio(item.id)">
  17. <div style="margin-bottom: 5px; font-weight: 600">{{ item.linkUrl }}</div>
  18. <div style="color: #a0a4af; font-size: 12px">备注:{{ item.content }}</div>
  19. </div>
  20. <div v-if="item.id != radiovalue" style="margin: auto 0 auto 0; height: 100%" @click="deleteRadio(index)">
  21. <u-icon name="close" color="#a0a4af" size="14"></u-icon>
  22. </div>
  23. </div>
  24. </u-radio-group>
  25. </div>
  26. <u-button v-if="radiolist.length > 0" type="primary" style="width: 100%; height: 45px; font-size: 17px; margin-top: 20px" @click="handleSubmit()" shape="circle"> 保 存</u-button>
  27. </view>
  28. </view>
  29. </template>
  30. <script setup>
  31. import config from "@/config";
  32. import { onLoad, onShow, onHide, onLaunch } from "@dcloudio/uni-app";
  33. import { ref, onMounted, inject, shallowRef, reactive, toRefs, getCurrentInstance, watchEffect } from "vue";
  34. import { getToken, setToken, removeToken } from "@/utils/auth";
  35. import publicStore from "@/store/modules/public.js";
  36. const publicStores = publicStore();
  37. const { proxy } = getCurrentInstance();
  38. const dataList = reactive({
  39. radiolist: [],
  40. radiovalue: "",
  41. });
  42. const uForm = ref(null);
  43. const { radiolist, radiovalue } = toRefs(dataList);
  44. /**
  45. * @获取serveList
  46. */
  47. /**
  48. * @切换单选
  49. * @按钮点击事件
  50. */
  51. function handleRadio(id) {
  52. radiovalue.value = id;
  53. let serveList = uni.getStorageSync("serveList");
  54. serveList[0].radiovalue = radiovalue.value;
  55. uni.setStorageSync("serveList", serveList);
  56. }
  57. /**
  58. * @删除
  59. * @按钮点击事件
  60. */
  61. function deleteRadio(index) {
  62. let serveList = uni.getStorageSync("serveList");
  63. serveList[0].radiolist.splice(index, 1);
  64. uni.setStorageSync("serveList", serveList);
  65. init();
  66. }
  67. /**
  68. * @扫一扫
  69. * @按钮点击事件
  70. */
  71. function serveClick(type) {
  72. if (type == 1) {
  73. uni.scanCode({
  74. success: async (e) => {
  75. uni.showToast({
  76. title: "扫码成功",
  77. icon: "none",
  78. });
  79. publicStores.getServeList(e.result, "");
  80. init();
  81. },
  82. fail: (err) => {
  83. uni.showToast({
  84. title: "扫码失败",
  85. icon: "none",
  86. });
  87. console.log("扫码失败", err);
  88. },
  89. complete: () => {
  90. // uni.showToast({
  91. // title: "扫码结束",
  92. // icon: "none",
  93. // });
  94. console.log("扫码结束");
  95. },
  96. });
  97. } else if (type == 2) {
  98. proxy.$tab.redirectTo("/pages/serveConfig");
  99. }
  100. }
  101. /**
  102. * @保存
  103. * @按钮点击事件
  104. */
  105. function handleSubmit() {
  106. let serveList = uni.getStorageSync("serveList");
  107. if (serveList.length > 0) {
  108. serveList[0].radiolist.forEach((el) => {
  109. if (el.id == serveList[0].radiovalue) {
  110. uni.setStorageSync("serveUrl", el.linkUrl);
  111. config.baseUrl = "http://" + uni.getStorageSync("serveUrl") + "/prod-api";
  112. navigateTo();
  113. }
  114. });
  115. }
  116. }
  117. /**
  118. * @跳转登录
  119. */
  120. function navigateTo() {
  121. proxy.$tab.reLaunch("/pages/login");
  122. }
  123. /**
  124. * @初始化
  125. */
  126. function init() {
  127. let serveList = uni.getStorageSync("serveList");
  128. if (serveList.length > 0) {
  129. radiovalue.value = serveList[0].radiovalue;
  130. radiolist.value = serveList[0].radiolist;
  131. }
  132. proxy.$tab.redirectTo("/pages/serveConfigSelect"); //重载当前页
  133. }
  134. onLoad((options) => {
  135. init();
  136. });
  137. </script>
  138. <style>
  139. page {
  140. background-color: #ffffff;
  141. }
  142. </style>
  143. <style lang="scss" scoped>
  144. #serveConfig {
  145. position: relative;
  146. z-index: 1;
  147. width: 100%;
  148. padding: 0 40px;
  149. margin: auto;
  150. padding-top: 20%;
  151. .top {
  152. }
  153. .content {
  154. display: flex;
  155. margin: 30px 0;
  156. .title {
  157. margin: auto auto auto 0;
  158. color: #000;
  159. font-size: 18px;
  160. }
  161. .icons {
  162. margin: auto 5px auto 0;
  163. }
  164. .setting {
  165. color: #2a98ff;
  166. margin: auto 0;
  167. }
  168. }
  169. .bottom {
  170. .serveList {
  171. max-height: calc(59px * 6);
  172. overflow: auto;
  173. }
  174. // .u-radio {
  175. // :deep(.u-radio__icon-wrap) {
  176. // background-color: transparent !important;
  177. // }
  178. // :deep(.u-icon) {
  179. // width: 8px;
  180. // height: 8px;
  181. // background-color: rgb(41, 121, 255);
  182. // border-radius: 100%;
  183. // }
  184. // :deep(.u-icon__icon) {
  185. // display: none;
  186. // }
  187. // }
  188. }
  189. }
  190. </style>