index.vue 15 KB

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