index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. const modules = res?.data?.modules || [];
  268. store.dispatch('setModulesList', modules);
  269. uni.setStorageSync('modulesList', modules);
  270. LoginSuccess();
  271. }
  272. })
  273. }
  274. //登录验证点击确定
  275. const ConfirmEnter = () => {
  276. if (state.sliderVerified != 1) {
  277. uni.showToast({
  278. title: '请拖动滑块进行验证!',
  279. icon: 'none', // 可选值:'success', 'loading', 'none'
  280. duration: 2000 // 提示持续时间,单位毫秒
  281. });
  282. return;
  283. }
  284. if (state.loginCode == '') {
  285. uni.showToast({
  286. title: '请输入验证码',
  287. icon: 'none', // 可选值:'success', 'loading', 'none'
  288. duration: 2000 // 提示持续时间,单位毫秒
  289. });
  290. return;
  291. }
  292. let param = {
  293. username: state.username.trim(),
  294. password: encrypt(state.password.trim()),
  295. phoneCode: state.loginCode.trim(),//手机验证码
  296. sliderUUID: state.sliderUUID,//验证码唯一id
  297. deviceType: 'APP'
  298. };
  299. user.teacherLoginByCode(param).then(res => {
  300. console.log("教师端手机验证码登录返回结果", res);
  301. if (res.code == 200) {
  302. //登录成功
  303. console.log("打印登录结果", res);
  304. popupSlideCode.value.close();//关闭滑动验证码弹窗
  305. const token = res.data.tokenValue;
  306. store.dispatch('setToken', token);
  307. uni.setStorageSync('token', token); // 存储 token 到本地
  308. uni.showModal({
  309. title: '信任此设备',
  310. content: '是否将此设备设为信任设备?未来30天内登录可免滑动验证',
  311. confirmText: '信任',
  312. cancelText: '不信任',
  313. success: function (modalResult) {
  314. if (modalResult.confirm) {
  315. //用户信任此设备
  316. SaveUserDeviceFinge();
  317. }
  318. else if (modalResult.cancel) {
  319. //用户选择不信任
  320. }
  321. HandleSuccessfulLogin(res);//登录成功
  322. }
  323. })
  324. } else {
  325. //登录失败
  326. uni.showToast({
  327. title: res.msg,
  328. icon: 'none', // 可选值:'success', 'loading', 'none'
  329. duration: 2000 // 提示持续时间,单位毫秒
  330. });
  331. }
  332. })
  333. }
  334. const handleLogin = () => {
  335. state.schoolsList = [];
  336. // 登录逻辑
  337. // console.log('用户名:', state.username)
  338. // console.log('密码:', state.password)
  339. let params = {
  340. username: state.username.trim(),
  341. password: encrypt(state.password.trim()),
  342. deviceFinger: state.deviceFinger,//设备唯一指纹id
  343. deviceType: 'APP'
  344. }
  345. if (!params.username) {
  346. console.log("没有输入用户名");
  347. uni.showToast({
  348. title: '请输入用户名',
  349. icon: 'none', // 可选值:'success', 'loading', 'none'
  350. duration: 2000 // 提示持续时间,单位毫秒
  351. });
  352. return;
  353. }
  354. if (!params.password) {
  355. uni.showToast({
  356. title: '请输入密码',
  357. icon: 'none', // 可选值:'success', 'loading', 'none'
  358. duration: 2000 // 提示持续时间,单位毫秒
  359. });
  360. return;
  361. }
  362. if (!isCheck.value) {
  363. agreementPopupDialog.value.OpenDialog();
  364. // uni.showToast({
  365. // title: '请先阅读并同意相关协议',
  366. // icon: 'none', // 可选值:'success', 'loading', 'none'
  367. // duration: 2000 // 提示持续时间,单位毫秒
  368. // });
  369. return;
  370. }
  371. user.login(params).then(res => {
  372. console.log("打印登录结果", res);
  373. if (res.code == 200) {
  374. //先判断是否有token 有token 直接登录成功
  375. if (res.data.tokenValue != null) {
  376. HandleSuccessfulLogin(res);//登录成功
  377. }
  378. else {
  379. //没有token 判断是否绑定了手机号
  380. if (res.data.bindPhone == 1) {
  381. //有手机号 弹出滑动验证码
  382. state.sliderVerified = 0;//未验证
  383. popupSlideCode.value.open();//打开滑动验证码弹窗
  384. state.sliderBgUrl = res.data.backgroundBase64;
  385. state.blockBgUrl = res.data.blockBase64;
  386. state.sliderUUID = res.data.sliderUUID;
  387. state.sliderPositionY = Math.round(res.data.y);
  388. }
  389. else {
  390. //没有手机号 弹出登录验证码弹窗
  391. uni.showToast({
  392. title: '您的账号未绑定手机号,请先去pc端绑定手机号!',
  393. icon: 'none', // 可选值:'success', 'loading', 'none'
  394. duration: 2000 // 提示持续时间,单位毫秒
  395. });
  396. }
  397. }
  398. }
  399. else {
  400. uni.showToast({
  401. title: res.msg,
  402. icon: 'none', // 可选值:'success', 'loading', 'none'
  403. duration: 2000 // 提示持续时间,单位毫秒
  404. });
  405. }
  406. })
  407. }
  408. //统一登录处理
  409. const HandleSuccessfulLogin = (res) => {
  410. const token = res.data.tokenValue;
  411. store.dispatch('setToken', token);
  412. uni.setStorageSync('token', token); // 存储 token 到本地
  413. const schoolsList = res?.data?.schools || [];
  414. state.schoolsList = schoolsList;
  415. store.dispatch('setSchoolsList', schoolsList);
  416. uni.setStorageSync('schoolsList', schoolsList); // 存储 学校 到本地
  417. if (schoolsList.length > 1) {
  418. popupDialogRef.value.OpenDialog();
  419. } else {
  420. // state.schoolInfo = schoolsList?.[0];
  421. LoginSuccess();
  422. }
  423. }
  424. const LoginSuccess = () => {
  425. uni.showToast({
  426. title: '登录成功',
  427. icon: 'success',
  428. mask: true,
  429. duration: 2000
  430. });
  431. store.dispatch('setUserInfo');
  432. uni.redirectTo({
  433. url: '/pages/workspace/index'
  434. });
  435. }
  436. const DialogAgreementConfirm = () => {
  437. isCheck.value = true;
  438. handleLogin();
  439. }
  440. const AgreementDetails = () => {
  441. uni.navigateTo({
  442. url: '/pages/login/agreement'
  443. })
  444. }
  445. </script>
  446. <style lang="scss">
  447. :deep(.school_list) {
  448. padding: 0 30rpx;
  449. width: 100%;
  450. box-sizing: border-box;
  451. .list_item {
  452. margin-top: 20rpx;
  453. display: flex;
  454. align-items: center;
  455. width: 100%;
  456. height: 108rpx;
  457. background-color: #FFFFFF;
  458. border-radius: 12rpx;
  459. border: 1px solid #E4E7ED;
  460. padding: 20rpx 38rpx 20rpx 24rpx;
  461. box-sizing: border-box;
  462. &.active {
  463. border: 1px solid #2E64FA;
  464. background-color: rgba(46, 100, 250, 0.1);
  465. }
  466. .school_logo {
  467. width: 68rpx;
  468. height: 68rpx;
  469. background: #C2D2FF;
  470. border-radius: 50%;
  471. overflow: hidden;
  472. margin-right: 28rpx;
  473. .logo {
  474. width: 100%;
  475. height: 100%;
  476. border-radius: 50%;
  477. }
  478. }
  479. .school_name {
  480. flex: 1;
  481. height: 68rpx;
  482. display: inline-flex;
  483. align-items: center;
  484. justify-content: flex-start;
  485. font-weight: 500;
  486. font-size: 24rpx;
  487. color: #000000;
  488. span {
  489. display: -webkit-box;
  490. -webkit-line-clamp: 2;
  491. -webkit-box-orient: vertical;
  492. overflow: hidden;
  493. word-break: break-all;
  494. }
  495. }
  496. .item_right {
  497. margin-left: 28rpx;
  498. font-size: 36rpx !important;
  499. color: #999999 !important;
  500. }
  501. }
  502. }
  503. :deep(.login_code_input) {
  504. width: 100%;
  505. height: 40px;
  506. border-radius: 10px 10px 10px 10px;
  507. border: 1px solid #EBEEF5;
  508. line-height: 40px;
  509. margin-top: 20rpx;
  510. display: flex;
  511. align-items: center;
  512. justify-content: left;
  513. text-indent: 20rpx;
  514. }
  515. .login_code_message {
  516. width: 100%;
  517. font-size: 24rpx;
  518. margin-top: 20rpx;
  519. }
  520. .login_code_button {
  521. width: 100%;
  522. height: 40px;
  523. border-radius: 8px;
  524. background-color: #2E64FA;
  525. color: #FFFFFF;
  526. text-align: center;
  527. line-height: 40px;
  528. margin-top: 20rpx;
  529. }
  530. :deep(.agreement_dialog){
  531. .dialog_title{
  532. background-color: #FFFFFF;
  533. justify-content: center;
  534. }
  535. .dialog_content{
  536. min-height: auto;
  537. }
  538. .agreement_popup_content{
  539. font-size: 24rpx;
  540. display: flex;
  541. width: 100%;
  542. justify-content: center;
  543. gap: 6rpx;
  544. .color_999 {
  545. color: #999999;
  546. margin: 0 4rpx;
  547. }
  548. .color_blue {
  549. color: #2e64fa;
  550. }
  551. }
  552. }
  553. </style>