login.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <template>
  2. <view class="section">
  3. <view style="position:absolute;top:10px;right:10px;z-index:99;color:#2a98ff" @tap="goSeverConfig">服务器配置</view>
  4. <view class="bg">
  5. <!-- <image class="bgImage" :src="bg" v-if="bg" /> -->
  6. <!-- <image class="bgImage" src="@/static/images/wt/bg.png" v-if="!bg" /> -->
  7. </view>
  8. <view class="middle login">
  9. <view class="top">
  10. <image class="logo" :src="logo" mode="widthFix" v-if="logo" />
  11. <text class="title">{{ title }}移动端登录</text>
  12. </view>
  13. <view id="login-input" style="margin-top: 20px" v-if="switchText == '账号密码登录'">
  14. <u-input v-model="phone" prefixIcon="phone" placeholder="请输入手机号码" />
  15. </view>
  16. <view id="login-input" style="display: flex; margin-top: 20px" v-if="switchText == '账号密码登录'">
  17. <u-input style="width: 60%" v-model="verify" prefixIcon="email" placeholder="请输入验证码" />
  18. <u-button @click="getVerifyCode">{{ !codeTime ? "获取验证码" : codeTime + "s" }}</u-button>
  19. </view>
  20. <view id="login-input" style="margin-top: 20px" v-if="switchText == '验证码登录'">
  21. <u-input type="text" v-model="username" prefixIcon="account" placeholder="请输入账号" />
  22. <u-input style="margin-top: 20px" v-model="password" prefixIcon="lock" placeholder="请输入密码" :password="inputIconBool">
  23. <template #suffix>
  24. <text class="iconfont ucicon-eye" @click="inputIcon()" v-if="!inputIconBool"></text>
  25. <text class="iconfont ucicon-eye-close" @click="inputIcon()" v-if="inputIconBool"></text>
  26. </template>
  27. </u-input>
  28. </view>
  29. <!-- -->
  30. <view class="switchText" @click="switchMode">
  31. <text>{{ switchText }}</text>
  32. </view>
  33. <button class="submit" @click="submitRes">登 录</button>
  34. </view>
  35. <view class="bottom">
  36. <div class="title">{{ bottomTitle }}</div>
  37. </view>
  38. </view>
  39. </template>
  40. <script setup>
  41. import { onLoad, onShow, onHide, onLaunch } from "@dcloudio/uni-app";
  42. import { reactive, getCurrentInstance, toRefs, inject } from "vue";
  43. import { getToken, setToken, removeToken } from "@/utils/auth";
  44. import { Lock, User, Cellphone } from "@element-plus/icons-vue";
  45. import { getCodeImg, getMobileTenantConfig } from "@/api/login";
  46. import useStores from "@/store/modules/user.js";
  47. const useStore = useStores();
  48. const { proxy } = getCurrentInstance();
  49. const data = reactive({
  50. /** saas数据 */
  51. bg: "/static/images/wt/bg.png",
  52. logo: "/static/logo.png",
  53. title: undefined,
  54. bottomTitle: undefined,
  55. /** login数据 */
  56. phone: undefined,
  57. verify: undefined,
  58. codeTime: 0,
  59. switchText: "验证码登录",
  60. tenantId: undefined,
  61. username: undefined,
  62. password: undefined,
  63. inputIconBool: true,
  64. // VerificationCodeOne
  65. });
  66. const { title, bg, bottomTitle, logo, phone, verify, codeTime, switchText, username, password, tenantId, inputIconBool } = toRefs(data);
  67. function goSeverConfig() {
  68. uni.navigateTo({
  69. url: '/pages/serveConfig'
  70. })
  71. }
  72. /**登录方式切换 */
  73. function switchMode() {
  74. if (switchText.value == "验证码登录") {
  75. switchText.value = "账号密码登录";
  76. } else {
  77. switchText.value = "验证码登录";
  78. }
  79. }
  80. /** 点击发送验证码 */
  81. function getVerifyCode() {
  82. if (!phone.value) {
  83. uni.showToast({
  84. title: "请输入手机号码...",
  85. icon: "none",
  86. });
  87. return;
  88. }
  89. if (!/^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/.test(phone.value)) {
  90. uni.showToast({
  91. title: "请输入正确的手机号码...",
  92. icon: "none",
  93. });
  94. return;
  95. }
  96. verify.value = undefined;
  97. getSendSms({
  98. phone: phone.value,
  99. });
  100. //
  101. if (codeTime.value > 0) {
  102. uni.showToast({
  103. title: "不能重复获取",
  104. icon: "none",
  105. });
  106. return;
  107. } else {
  108. codeTime.value = 60;
  109. let timer = setInterval(() => {
  110. codeTime.value--;
  111. if (codeTime.value < 1) {
  112. clearInterval(timer);
  113. codeTime.value = 0;
  114. }
  115. }, 1000);
  116. }
  117. }
  118. /** 获取验证码 */
  119. async function getSendSms(params) {
  120. getCodeImg(params).then((res) => {
  121. console.log(res);
  122. });
  123. }
  124. /**
  125. * 判断运行环境
  126. */
  127. function env() {
  128. // const u = navigator.userAgent
  129. // //https://blog.csdn.net/weixin_42659644/article/details/126294231
  130. // if(u.indexOf('saas')>-1){ //指定app内 获取app识别号
  131. // }else{
  132. // }
  133. let port = uni.getSystemInfoSync().platform;
  134. switch (port) {
  135. case "android":
  136. getMobileTenantConfigApi({ url: window.location.host });
  137. console.log("Android"); //android
  138. break;
  139. case "ios":
  140. console.log("iOS"); //ios
  141. getMobileTenantConfigApi({ url: window.location.host });
  142. break;
  143. case "windows":
  144. // console.log("H5"); //H5
  145. //getMobileTenantConfigApi({ url: "172.16.1.47:3000" })
  146. getMobileTenantConfigApi({ url: window.location.host });
  147. break;
  148. default: //devtools
  149. console.log("小程序");
  150. break;
  151. }
  152. }
  153. // #ifdef H5
  154. env();
  155. // #endif
  156. /** 点击提交按钮 */
  157. async function submitRes() {
  158. if (switchText.value == "账号密码登录") {
  159. if (!phone.value) {
  160. uni.showToast({
  161. title: "请输入手机号码...",
  162. icon: "none",
  163. });
  164. return;
  165. }
  166. if (!/^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/.test(phone.value)) {
  167. uni.showToast({
  168. title: "请输入正确的手机号码...",
  169. icon: "none",
  170. });
  171. return;
  172. }
  173. if (!verify.value) {
  174. uni.showToast({
  175. title: "请输入验证码...",
  176. icon: "none",
  177. });
  178. return;
  179. }
  180. login({
  181. phone: phone.value,
  182. verify: verify.value,
  183. tenantId: tenantId.value,
  184. });
  185. } else {
  186. if (!username.value) {
  187. uni.showToast({
  188. title: "请输入账户",
  189. icon: "none",
  190. });
  191. return;
  192. }
  193. if (!password.value) {
  194. uni.showToast({
  195. title: "请输入密码",
  196. icon: "none",
  197. });
  198. return;
  199. }
  200. login({
  201. username: username.value,
  202. password: password.value,
  203. tenantId: tenantId.value,
  204. });
  205. }
  206. }
  207. /** 获取登录数据 */
  208. async function login(data) {
  209. proxy.$modal.loading("登录中,请耐心等待...");
  210. useStore.Login(data).then(() => {
  211. info();
  212. });
  213. }
  214. /** 获取用户信息 */
  215. async function info() {
  216. proxy.$modal.closeLoading();
  217. useStore.GetInfo().then((res) => {
  218. proxy.$tab.reLaunch("/pages/index");
  219. });
  220. }
  221. /** 获取登录页数据 */
  222. async function getMobileTenantConfigApi(params) {
  223. getMobileTenantConfig(params).then((res) => {
  224. if (res.data.length > 0) {
  225. let data = res.data.data[0];
  226. bg.value = data.loginBackUrl;
  227. title.value = data.loginTitle;
  228. bottomTitle.value = data.loginFooter;
  229. tenantId.value = data.tenantId;
  230. uni.setStorageSync("homeTitle", data.loginTitle);
  231. }
  232. });
  233. }
  234. /**
  235. * @密码显示隐藏icon图标事件
  236. * @icon点击事件
  237. */
  238. function inputIcon() {
  239. inputIconBool.value = !inputIconBool.value;
  240. }
  241. /**
  242. * @onLoad
  243. */
  244. onLoad(() => {
  245. const token = getToken();
  246. //需要登录
  247. if (token) {
  248. uni.switchTab({
  249. url: "/pages/index",
  250. });
  251. }
  252. });
  253. </script>
  254. <style>
  255. .uni-input{
  256. color:red!important
  257. }</style>
  258. <style lang="scss" scoped>
  259. .section {
  260. display: flex;
  261. width: 100%;
  262. height: 100%;
  263. .bg {
  264. width: 100%;
  265. height: 100%;
  266. position: fixed;
  267. top: 0;
  268. left: 0;
  269. right: 0;
  270. z-index: 0;
  271. background: #fff;
  272. .bgImage {
  273. width: 100%;
  274. height: 100%;
  275. }
  276. }
  277. .middle {
  278. position: relative;
  279. z-index: 1;
  280. width: 100%;
  281. padding: 0 40px;
  282. margin: auto;
  283. margin-top: 30%;
  284. #login-input .u-input{
  285. border-color:gray!important
  286. }
  287. .uni-input-placeholder{
  288. color:gray!important
  289. }
  290. .top {
  291. display: flex;
  292. margin-bottom: 60px;
  293. .logo {
  294. width: 50px;
  295. height: 50px;
  296. margin: 0 20px 0 auto;
  297. }
  298. .title {
  299. font-size: 20px;
  300. text-align: center;
  301. color: #000;
  302. margin: auto auto auto 0;
  303. }
  304. }
  305. .switchText {
  306. color: gray;
  307. font-size: 16px;
  308. margin-top: 20px;
  309. width: 100%;
  310. display: block;
  311. }
  312. .submit {
  313. height: 45px;
  314. line-height: 45px;
  315. border-radius: 24px;
  316. background: #2a98ff;
  317. color: #fff;
  318. margin-top: 45px;
  319. border: 0px;
  320. }
  321. }
  322. .bottom {
  323. position: absolute;
  324. width: 100%;
  325. bottom: 10px;
  326. .title {
  327. text-align: center;
  328. color: #000;
  329. font-size: 14px;
  330. }
  331. }
  332. .iconfont {
  333. color: #999;
  334. }
  335. }
  336. </style>