serveConfigSelect.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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="{ display: 'block', 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. /*----------------------------------依赖引入-----------------------------------*/
  49. import { onLoad, onShow, onHide, onLaunch } from "@dcloudio/uni-app";
  50. import { ref, onMounted, inject, shallowRef, reactive, toRefs, getCurrentInstance, watchEffect } from "vue";
  51. /*----------------------------------接口引入-----------------------------------*/
  52. /*----------------------------------组件引入-----------------------------------*/
  53. /*----------------------------------store引入-----------------------------------*/
  54. import { useStores, commonStores } from "@/store/modules/index";
  55. /*----------------------------------公共方法引入-----------------------------------*/
  56. import config from "@/config";
  57. /*----------------------------------公共变量-----------------------------------*/
  58. const { proxy } = getCurrentInstance();
  59. const useStore = useStores();
  60. const commonStore = commonStores();
  61. /*----------------------------------变量声明-----------------------------------*/
  62. const state = reactive({
  63. radiolist: [],
  64. radiovalue: "",
  65. });
  66. const { radiolist, radiovalue } = toRefs(state);
  67. /**
  68. * @切换单选
  69. * @按钮点击事件
  70. */
  71. function handleRadio(id) {
  72. radiovalue.value = id;
  73. let serveList = uni.getStorageSync("serveList");
  74. serveList[0].radiovalue = radiovalue.value;
  75. uni.setStorageSync("serveList", serveList);
  76. }
  77. /**
  78. * @删除
  79. * @按钮点击事件
  80. */
  81. function deleteRadio(index) {
  82. let serveList = uni.getStorageSync("serveList");
  83. serveList[0].radiolist.splice(index, 1);
  84. uni.setStorageSync("serveList", serveList);
  85. init();
  86. proxy.$tab.redirectTo("/pages/serveConfigSelect"); //重载当前页
  87. }
  88. /**
  89. * @编辑
  90. * @按钮点击事件
  91. */
  92. function editRadio(index) {
  93. proxy.$tab.navigateTo("/pages/serveConfig?index=" + index);
  94. }
  95. /**
  96. * @扫一扫
  97. * @按钮点击事件
  98. */
  99. function serveClick(type) {
  100. if (type == 1) {
  101. uni.scanCode({
  102. autoZoom: false,
  103. success: async (e) => {
  104. uni.showToast({
  105. title: "扫码成功",
  106. icon: "none",
  107. });
  108. commonStore.setServeList(e.result, "");
  109. init();
  110. proxy.$tab.redirectTo("/pages/serveConfigSelect"); //重载当前页
  111. },
  112. fail: (err) => {
  113. uni.showToast({
  114. title: "扫码失败",
  115. icon: "none",
  116. });
  117. console.log("扫码失败", err);
  118. },
  119. complete: () => {
  120. // uni.showToast({
  121. // title: "扫码结束",
  122. // icon: "none",
  123. // });
  124. console.log("扫码结束");
  125. },
  126. });
  127. } else if (type == 2) {
  128. proxy.$tab.navigateTo("/pages/serveConfig");
  129. }
  130. }
  131. /**
  132. * @保存
  133. * @按钮点击事件
  134. */
  135. function handleSubmit() {
  136. let serveList = uni.getStorageSync("serveList");
  137. if (serveList.length > 0) {
  138. serveList[0].radiolist.forEach((el) => {
  139. if (el.id == serveList[0].radiovalue) {
  140. uni.setStorageSync("serveUrl", el.linkUrl);
  141. config.baseUrl = "https://" + uni.getStorageSync("serveUrl") + "/prod-api";
  142. navigateTo();
  143. }
  144. });
  145. }
  146. }
  147. /**
  148. * @跳转登录
  149. */
  150. function navigateTo() {
  151. proxy.$tab.navigateBack(1);
  152. }
  153. /**
  154. * @初始化
  155. */
  156. function init() {
  157. let serveList = uni.getStorageSync("serveList");
  158. if (serveList.length > 0) {
  159. radiolist.value = serveList[0].radiolist;
  160. radiovalue.value = serveList[0].radiovalue;
  161. }
  162. }
  163. onShow(() => {
  164. init();
  165. });
  166. onLoad((options) => {});
  167. </script>
  168. <style lang="scss" scoped>
  169. #serveConfigSelect-container {
  170. position: fixed;
  171. top: 0;
  172. left: 0;
  173. right: 0;
  174. bottom: 0;
  175. z-index: 1;
  176. width: 100%;
  177. height: 100vh;
  178. margin: auto;
  179. padding: 0 30px;
  180. padding-top: 20%;
  181. //#ifdef MP-WEIXIN
  182. padding-top: 30%;
  183. //#endif
  184. background-color: #ffffff;
  185. .top-area {
  186. }
  187. .content-area {
  188. display: flex;
  189. margin: 30px 0;
  190. &-title {
  191. margin: auto auto auto 0;
  192. color: #000;
  193. font-size: 18px;
  194. }
  195. &-icons {
  196. margin: auto 5px auto 0;
  197. }
  198. &-setting {
  199. color: #2a98ff;
  200. margin: auto 0;
  201. }
  202. }
  203. .bottom-area {
  204. .serveList {
  205. max-height: calc(59px * 6);
  206. overflow: auto;
  207. &-item {
  208. display: flex;
  209. background-color: #f5f6fa !important;
  210. margin-bottom: 15px;
  211. padding: 10px;
  212. border-radius: 8px;
  213. &-center {
  214. margin: auto 0 auto 0;
  215. width: 100%;
  216. &-title {
  217. margin-bottom: 5px;
  218. font-weight: 600;
  219. }
  220. &-content {
  221. color: #a0a4af;
  222. font-size: 12px;
  223. }
  224. }
  225. &-icon {
  226. margin: auto 0 auto 0;
  227. height: 100%;
  228. }
  229. }
  230. }
  231. }
  232. }
  233. </style>