index.vue 15 KB

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