serveConfigSelect.vue 5.9 KB

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