index.vue 16 KB

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