index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <template>
  2. <view class="page_login" v-if="!state.isLoading">
  3. <view class="login_weclome">
  4. Hi~欢迎使用
  5. </view>
  6. <view class="login_title">
  7. 大数据精准教学诊断平台
  8. </view>
  9. <view class="login_content">
  10. <view class="user_name">
  11. <input class="input_name" v-model="state.username" placeholder="请输入账号" @blur="inputBlur"
  12. @focus="inputFocus" />
  13. <text class="clear_icon" v-if="state.showClearIcon" @click="clearUserName"></text>
  14. </view>
  15. <view class="user_pass">
  16. <input class="input_name" v-model="state.password" :password="!state.isShowPassWord"
  17. placeholder="请输入密码" />
  18. <text v-if="!state.isShowPassWord" class="eyes_icon" @click="showUserPass"></text>
  19. <text v-else class="eye_icon" @click="hideUserPass"></text>
  20. </view>
  21. <view class="login_button" @click="handleLogin">
  22. 登录
  23. </view>
  24. <view class="agreement">
  25. <radio :checked="isCheck" @click="isCheck = !isCheck" value="radio" activeBackgroundColor="#2E64FA"
  26. activeBorderColor="#2E64FA" />
  27. <view class="text">
  28. <text class="color_999">我已阅读并同意</text>
  29. <text class="color_blue" @click="AgreementDetails">用户协议</text>
  30. <text class="color_999">和</text>
  31. <text class="color_blue" @click="AgreementDetails">隐私改策</text>
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. <!-- 提示框 -->
  37. <popupDialog ref="popupDialogRef" popType="custom" title="选择学校" @DialogConfirm="DialogConfirm">
  38. <view class="school_list">
  39. <view :class="['list_item', { active: state.schoolId == item.schoolId }]" @click="SelectSchool(item.schoolId)"
  40. v-for="item in state.schoolsList" :key="item.schoolId">
  41. <view class="school_logo">
  42. <image class="logo" v-if="item.schoolLogoUrl" :src="item.schoolLogoUrl"></image>
  43. <image class="logo" v-else src="@/static/image/bg/default_user.png"></image>
  44. </view>
  45. <text class="school_name">{{ item.schoolName }}</text>
  46. <uni-icons class="item_right" type="right"></uni-icons>
  47. </view>
  48. </view>
  49. </popupDialog>
  50. <!-- 同意协议并登录 -->
  51. <popupDialog class="agreement_dialog" ref="agreementPopupDialog" popType="custom" title="用户协议及隐私改策" :showDialogClose="false" :dialogWidth="550" @DialogConfirm="DialogAgreementConfirm">
  52. <view class="agreement_popup_content">
  53. <text class="color_999">我已阅读并同意</text>
  54. <text class="color_blue" @click="AgreementDetails">用户协议</text>
  55. <text class="color_999">和</text>
  56. <text class="color_blue" @click="AgreementDetails">隐私改策</text>
  57. </view>
  58. </popupDialog>
  59. <custom-dialog ref="popupSlideCode" title="滑动验证">
  60. <view class="slide_code_content">
  61. <slide-code ref="slideRef" :sliderBgUrl="state.sliderBgUrl" :sliderUUID="state.sliderUUID"
  62. :blockBgUrl="state.blockBgUrl" :sliderVerified="state.sliderVerified"
  63. :sliderPositionY="state.sliderPositionY" @Refresh="GetObtainSliderImg" @CheckResult="CheckResult" />
  64. </view>
  65. <view class="login_code_message" v-if="state.sliderPhone">
  66. 验证码已发送至手机:{{ state.sliderPhone }}
  67. </view>
  68. <view class="login_code_input">
  69. <input class="input_name" v-model="state.loginCode" placeholder="请输入验证码" maxlength="6" @blur="inputBlur"
  70. @focus="inputFocus" />
  71. </view>
  72. <view class="login_code_button" @click="ConfirmEnter">
  73. 确定
  74. </view>
  75. </custom-dialog>
  76. </template>
  77. <script setup>
  78. import user from '@/reqApi/user.js';
  79. import { encrypt } from '@/common/crypto.js';
  80. import uniIcons from '@/uni_modules/uni-icons/components/uni-icons/uni-icons.vue';
  81. import popupDialog from '@/components/popupDialog.vue';
  82. import customDialog from '@/components/customDialog.vue';
  83. import slideCode from '@/components/slideCode.vue';
  84. import {
  85. reactive,
  86. ref,
  87. onMounted,
  88. onUnmounted
  89. } from 'vue';
  90. import { useStore } from 'vuex';
  91. import { useRoute, useRouter } from 'vue-router'
  92. const store = useStore();
  93. const route = useRoute() // 获取当前路由信息
  94. const popupDialogRef = ref(null);
  95. const popupSlideCode = ref(null);
  96. const agreementPopupDialog = ref(null);
  97. const isCheck = ref(false);
  98. const state = reactive({
  99. username: '',
  100. password: '',
  101. schoolId: '',
  102. schoolsList: [],
  103. showClearIcon: false,
  104. isShowPassWord: false,
  105. isLoading: false,
  106. sliderBgUrl: '',//滑动验证码背景图
  107. blockBgUrl: '',//滑动验证码块切块图
  108. sliderUUID: '',//滑动验证码唯一标识
  109. sliderPositionY: 0,//滑动验证码块切块图位置y坐标
  110. sliderPositionX: 0,//滑动验证码块切块图位置x坐标
  111. loginCode: '',//登录验证码
  112. sliderVerified: 0,//滑动验证码是否验证成功 0 未验证 1 验证成功 -1 验证失败
  113. sliderPhone: '',//滑动验证码显示的手机号
  114. deviceFinger: '',//设备指纹
  115. });
  116. onMounted(() => {
  117. // #ifdef H5
  118. const options = route.query;
  119. if (options.schoolId && options.ticket) {
  120. state.isLoading = true;
  121. uni.showLoading({
  122. title: '登录中,请稍等!'
  123. });
  124. user.adminLoginTeach({
  125. id: options.schoolId,
  126. ticket: options.ticket
  127. }).then(res => {
  128. if (res.code == 200) {
  129. //先清除所有缓存
  130. store.dispatch('logout', false);
  131. const token = res.data.tokenValue;
  132. store.dispatch('setToken', token);
  133. uni.setStorageSync('token', token); // 存储 token 到本地
  134. store.dispatch('setUserInfo');
  135. uni.redirectTo({
  136. url: '/pages/workspace/index'
  137. });
  138. } else {
  139. uni.showToast({
  140. title: res.msg || '账号不存在或者密码错误!',
  141. icon: 'none'
  142. });
  143. }
  144. }).finally(() => {
  145. state.isLoading = false;
  146. uni.hideLoading();
  147. })
  148. }
  149. const systemInfo = uni.getSystemInfoSync();
  150. state.deviceFinger = systemInfo.deviceId;//设备指纹
  151. console.log('设备标识id', systemInfo);
  152. // #endif
  153. });
  154. const hideUserPass = () => {
  155. state.isShowPassWord = false;
  156. }
  157. const showUserPass = () => {
  158. state.isShowPassWord = true;
  159. }
  160. //清除用户名
  161. const clearUserName = () => {
  162. state.username = "";
  163. state.showClearIcon = false;
  164. }
  165. //滑动验证码验证结果返回
  166. const CheckResult = (result) => {
  167. console.log("滑动验证码结果", result);
  168. state.sliderPhone = result;
  169. state.sliderVerified = 1;
  170. // VerifySlideCaptcha();
  171. }
  172. //保存用户设备指纹信息
  173. const SaveUserDeviceFinge = () => {
  174. if (state.deviceFinger) {
  175. let param = {
  176. username: state.username.trim(),
  177. deviceFinger: state.deviceFinger,//设备指纹
  178. };
  179. user.saveUserDeviceFinge(param).then(res => {
  180. console.log("保存用户设备指纹信息结果", res);
  181. })
  182. }
  183. else {
  184. console.log("设备指纹为空");
  185. }
  186. }
  187. //验证滑动验证码位置是否正确
  188. const VerifySlideCaptcha = () => {
  189. let params = {
  190. sliderUUID: state.sliderUUID,
  191. x: Math.round(state.sliderPositionX),//滑动验证码块切块图位置x坐标
  192. };
  193. user.checkSlideCaptcha(params).then(res => {
  194. console.log("333验证滑动验证码结果", res);
  195. if (res.code == 200) {
  196. //验证成功
  197. state.sliderVerified = 1;
  198. state.sliderPhone = res.data;//手机号
  199. }
  200. else if (res.code == 601) {
  201. //验证成功
  202. state.sliderVerified = 1;
  203. uni.showToast({
  204. title: res.msg || '滑块验证通过,您上次的验证码还在有效期,请输入上次发送的验证码进行登录!',
  205. icon: 'none', // 可选值:'success', 'loading', 'none'
  206. duration: 2000 // 提示持续时间,单位毫秒
  207. });
  208. }
  209. else if (res.code == 602) {
  210. //滑动验证码过期 重新获取验证码
  211. uni.showToast({
  212. title: '验证码已过期,已重新获取,请重新拖动滑块验证',
  213. icon: 'none', // 可选值:'success', 'loading', 'none'
  214. duration: 2000 // 提示持续时间,单位毫秒
  215. });
  216. GetObtainSliderImg();//重新获取验证码
  217. }
  218. else {
  219. state.sliderVerified = 0;//验证失败
  220. state.sliderPositionX = 0;//重置滑块位置
  221. }
  222. });
  223. }
  224. //重新获取验证码
  225. const GetObtainSliderImg = () => {
  226. let params = {
  227. sliderUUID: state.sliderUUID,
  228. };
  229. user.obtainSliderImg(params).then(res => {
  230. console.log("重新获取滑动验证码结果", res);
  231. if (res.code == 200) {
  232. state.sliderVerified = 0;//未验证
  233. state.sliderUUID = res.data.sliderUUID;//滑动验证码唯一标识
  234. state.sliderBgUrl = res.data.backgroundBase64;//滑动验证码背景图
  235. state.blockBgUrl = res.data.blockBase64;//滑动验证码块
  236. state.sliderPositionY = Math.round(res.data.y);
  237. }
  238. })
  239. }
  240. // 失去焦点
  241. const inputBlur = () => {
  242. if (state.username != '') {
  243. state.showClearIcon = true;
  244. } else {
  245. state.showClearIcon = false;
  246. }
  247. }
  248. //获取焦点
  249. const inputFocus = (event) => {
  250. if (state.username != '') {
  251. state.showClearIcon = true;
  252. } else {
  253. state.showClearIcon = false;
  254. }
  255. }
  256. //选择学校
  257. const SelectSchool = (schoolId) => {
  258. state.schoolId = schoolId;
  259. }
  260. //点击确定按钮
  261. const DialogConfirm = () => {
  262. user.chooseBelongSchool({
  263. schoolId: state.schoolId,
  264. deviceType: 'APP'
  265. }).then(res => {
  266. if (res.code == 200) {
  267. LoginSuccess();
  268. }
  269. })
  270. }
  271. //登录验证点击确定
  272. const ConfirmEnter = () => {
  273. if (state.sliderVerified != 1) {
  274. uni.showToast({
  275. title: '请拖动滑块进行验证!',
  276. icon: 'none', // 可选值:'success', 'loading', 'none'
  277. duration: 2000 // 提示持续时间,单位毫秒
  278. });
  279. return;
  280. }
  281. if (state.loginCode == '') {
  282. uni.showToast({
  283. title: '请输入验证码',
  284. icon: 'none', // 可选值:'success', 'loading', 'none'
  285. duration: 2000 // 提示持续时间,单位毫秒
  286. });
  287. return;
  288. }
  289. let param = {
  290. username: state.username.trim(),
  291. password: encrypt(state.password.trim()),
  292. phoneCode: state.loginCode.trim(),//手机验证码
  293. sliderUUID: state.sliderUUID,//验证码唯一id
  294. deviceType: 'APP'
  295. };
  296. user.teacherLoginByCode(param).then(res => {
  297. console.log("教师端手机验证码登录返回结果", res);
  298. if (res.code == 200) {
  299. //登录成功
  300. console.log("打印登录结果", res);
  301. popupSlideCode.value.close();//关闭滑动验证码弹窗
  302. const token = res.data.tokenValue;
  303. store.dispatch('setToken', token);
  304. uni.setStorageSync('token', token); // 存储 token 到本地
  305. uni.showModal({
  306. title: '信任此设备',
  307. content: '是否将此设备设为信任设备?未来30天内登录可免滑动验证',
  308. confirmText: '信任',
  309. cancelText: '不信任',
  310. success: function (modalResult) {
  311. if (modalResult.confirm) {
  312. //用户信任此设备
  313. SaveUserDeviceFinge();
  314. }
  315. else if (modalResult.cancel) {
  316. //用户选择不信任
  317. }
  318. HandleSuccessfulLogin(res);//登录成功
  319. }
  320. })
  321. } else {
  322. //登录失败
  323. uni.showToast({
  324. title: res.msg,
  325. icon: 'none', // 可选值:'success', 'loading', 'none'
  326. duration: 2000 // 提示持续时间,单位毫秒
  327. });
  328. }
  329. })
  330. }
  331. const handleLogin = () => {
  332. state.schoolsList = [];
  333. // 登录逻辑
  334. // console.log('用户名:', state.username)
  335. // console.log('密码:', state.password)
  336. let params = {
  337. username: state.username.trim(),
  338. password: encrypt(state.password.trim()),
  339. deviceFinger: state.deviceFinger,//设备唯一指纹id
  340. deviceType: 'APP'
  341. }
  342. if (!params.username) {
  343. console.log("没有输入用户名");
  344. uni.showToast({
  345. title: '请输入用户名',
  346. icon: 'none', // 可选值:'success', 'loading', 'none'
  347. duration: 2000 // 提示持续时间,单位毫秒
  348. });
  349. return;
  350. }
  351. if (!params.password) {
  352. uni.showToast({
  353. title: '请输入密码',
  354. icon: 'none', // 可选值:'success', 'loading', 'none'
  355. duration: 2000 // 提示持续时间,单位毫秒
  356. });
  357. return;
  358. }
  359. if (!isCheck.value) {
  360. agreementPopupDialog.value.OpenDialog();
  361. // uni.showToast({
  362. // title: '请先阅读并同意相关协议',
  363. // icon: 'none', // 可选值:'success', 'loading', 'none'
  364. // duration: 2000 // 提示持续时间,单位毫秒
  365. // });
  366. return;
  367. }
  368. user.login(params).then(res => {
  369. console.log("打印登录结果", res);
  370. if (res.code == 200) {
  371. //先判断是否有token 有token 直接登录成功
  372. if (res.data.tokenValue != null) {
  373. HandleSuccessfulLogin(res);//登录成功
  374. }
  375. else {
  376. //没有token 判断是否绑定了手机号
  377. if (res.data.bindPhone == 1) {
  378. //有手机号 弹出滑动验证码
  379. state.sliderVerified = 0;//未验证
  380. popupSlideCode.value.open();//打开滑动验证码弹窗
  381. state.sliderBgUrl = res.data.backgroundBase64;
  382. state.blockBgUrl = res.data.blockBase64;
  383. state.sliderUUID = res.data.sliderUUID;
  384. state.sliderPositionY = Math.round(res.data.y);
  385. }
  386. else {
  387. //没有手机号 弹出登录验证码弹窗
  388. uni.showToast({
  389. title: '您的账号未绑定手机号,请先去pc端绑定手机号!',
  390. icon: 'none', // 可选值:'success', 'loading', 'none'
  391. duration: 2000 // 提示持续时间,单位毫秒
  392. });
  393. }
  394. }
  395. }
  396. else {
  397. uni.showToast({
  398. title: res.msg,
  399. icon: 'none', // 可选值:'success', 'loading', 'none'
  400. duration: 2000 // 提示持续时间,单位毫秒
  401. });
  402. }
  403. })
  404. }
  405. //统一登录处理
  406. const HandleSuccessfulLogin = (res) => {
  407. const token = res.data.tokenValue;
  408. store.dispatch('setToken', token);
  409. uni.setStorageSync('token', token); // 存储 token 到本地
  410. const schoolsList = res?.data?.schools || [];
  411. state.schoolsList = schoolsList;
  412. store.dispatch('setSchoolsList', schoolsList);
  413. uni.setStorageSync('schoolsList', schoolsList); // 存储 学校 到本地
  414. if (schoolsList.length > 1) {
  415. popupDialogRef.value.OpenDialog();
  416. } else {
  417. // state.schoolInfo = schoolsList?.[0];
  418. LoginSuccess();
  419. }
  420. }
  421. const LoginSuccess = () => {
  422. uni.showToast({
  423. title: '登录成功',
  424. icon: 'success',
  425. mask: true,
  426. duration: 2000
  427. });
  428. store.dispatch('setUserInfo');
  429. uni.redirectTo({
  430. url: '/pages/workspace/index'
  431. });
  432. }
  433. const DialogAgreementConfirm = () => {
  434. isCheck.value = true;
  435. handleLogin();
  436. }
  437. const AgreementDetails = () => {
  438. uni.navigateTo({
  439. url: '/pages/login/agreement'
  440. })
  441. }
  442. </script>
  443. <style lang="scss">
  444. :deep(.school_list) {
  445. padding: 0 30rpx;
  446. width: 100%;
  447. box-sizing: border-box;
  448. .list_item {
  449. margin-top: 20rpx;
  450. display: flex;
  451. align-items: center;
  452. width: 100%;
  453. height: 108rpx;
  454. background-color: #FFFFFF;
  455. border-radius: 12rpx;
  456. border: 1px solid #E4E7ED;
  457. padding: 20rpx 38rpx 20rpx 24rpx;
  458. box-sizing: border-box;
  459. &.active {
  460. border: 1px solid #2E64FA;
  461. background-color: rgba(46, 100, 250, 0.1);
  462. }
  463. .school_logo {
  464. width: 68rpx;
  465. height: 68rpx;
  466. background: #C2D2FF;
  467. border-radius: 50%;
  468. overflow: hidden;
  469. margin-right: 28rpx;
  470. .logo {
  471. width: 100%;
  472. height: 100%;
  473. border-radius: 50%;
  474. }
  475. }
  476. .school_name {
  477. flex: 1;
  478. height: 68rpx;
  479. display: inline-flex;
  480. align-items: center;
  481. justify-content: flex-start;
  482. font-weight: 500;
  483. font-size: 24rpx;
  484. color: #000000;
  485. span {
  486. display: -webkit-box;
  487. -webkit-line-clamp: 2;
  488. -webkit-box-orient: vertical;
  489. overflow: hidden;
  490. word-break: break-all;
  491. }
  492. }
  493. .item_right {
  494. margin-left: 28rpx;
  495. font-size: 36rpx !important;
  496. color: #999999 !important;
  497. }
  498. }
  499. }
  500. :deep(.login_code_input) {
  501. width: 100%;
  502. height: 40px;
  503. border-radius: 10px 10px 10px 10px;
  504. border: 1px solid #EBEEF5;
  505. line-height: 40px;
  506. margin-top: 20rpx;
  507. display: flex;
  508. align-items: center;
  509. justify-content: left;
  510. text-indent: 20rpx;
  511. }
  512. .login_code_message {
  513. width: 100%;
  514. font-size: 24rpx;
  515. margin-top: 20rpx;
  516. }
  517. .login_code_button {
  518. width: 100%;
  519. height: 40px;
  520. border-radius: 8px;
  521. background-color: #2E64FA;
  522. color: #FFFFFF;
  523. text-align: center;
  524. line-height: 40px;
  525. margin-top: 20rpx;
  526. }
  527. :deep(.agreement_dialog){
  528. .dialog_title{
  529. background-color: #FFFFFF;
  530. justify-content: center;
  531. }
  532. .dialog_content{
  533. min-height: auto;
  534. }
  535. .agreement_popup_content{
  536. font-size: 24rpx;
  537. display: flex;
  538. width: 100%;
  539. justify-content: center;
  540. gap: 6rpx;
  541. .color_999 {
  542. color: #999999;
  543. margin: 0 4rpx;
  544. }
  545. .color_blue {
  546. color: #2e64fa;
  547. }
  548. }
  549. }
  550. </style>