login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <template>
  2. <div class="login_content" style="background: #e6eaf0">
  3. <!-- <el-button type="primary" @click="login()">登录</el-button> -->
  4. <div class="login_box" v-show="islogin">
  5. <div style="padding: 0px 20px">
  6. <div class="login_title">
  7. <span>PBL学生平台</span>
  8. </div>
  9. <!-- <div class="userLogin">
  10. <img src="../assets/username.png" />
  11. <el-input v-model="phoneNum" placeholder="手机号码"></el-input>
  12. </div>
  13. <div class="userLogin">
  14. <img src="../assets/password.png" />
  15. <el-input
  16. v-model="password"
  17. placeholder="密码"
  18. type="password"
  19. ></el-input>
  20. </div> -->
  21. <el-form
  22. label-position="right"
  23. label-width="80px"
  24. style="margin-top: 30px"
  25. >
  26. <el-form-item label="手机号码">
  27. <el-input
  28. v-model="phoneNum"
  29. auto-complete="off"
  30. placeholder="请输入手机号码"
  31. style="width: 100%"
  32. @keyup.enter.native="findPhone"
  33. ></el-input>
  34. </el-form-item>
  35. <el-form-item label="密码">
  36. <el-input
  37. v-model="password"
  38. auto-complete="off"
  39. placeholder="请输入密码"
  40. type="password"
  41. style="width: 100%"
  42. @keyup.enter.native="findPhone"
  43. ></el-input>
  44. </el-form-item>
  45. </el-form>
  46. <div style="display: flex; justify-content: flex-end">
  47. <el-button type="text" @click="islogin = false">立即注册</el-button>
  48. </div>
  49. <div class="login_button">
  50. <button @click="findPhone()">登录</button>
  51. </div>
  52. </div>
  53. </div>
  54. <div class="login_box" v-show="!islogin">
  55. <div style="padding: 0px 20px">
  56. <div class="login_title">
  57. <span>PBL学生平台注册</span>
  58. </div>
  59. <!-- <div class="userLogin">
  60. <img src="../assets/username.png" />
  61. <el-input v-model="phoneNum" placeholder="手机号码"></el-input>
  62. </div>
  63. <div class="userLogin">
  64. <img src="../assets/password.png" />
  65. <el-input v-model="password" placeholder="密码" type="password"></el-input>
  66. </div> -->
  67. <el-form
  68. label-position="right"
  69. label-width="80px"
  70. style="margin-top: 30px"
  71. :rules="rules2"
  72. ref="ruleForm2"
  73. :model="registerForm"
  74. >
  75. <el-form-item label="手机号码" prop="phone">
  76. <el-input
  77. v-model="registerForm.rphoneNum"
  78. auto-complete="off"
  79. placeholder="请输入手机号码"
  80. style="width: 100%"
  81. ></el-input>
  82. </el-form-item>
  83. <el-form-item label="密码" prop="pass">
  84. <el-input
  85. v-model="registerForm.rpassword"
  86. auto-complete="off"
  87. placeholder="请输入密码"
  88. type="password"
  89. style="width: 100%"
  90. ></el-input>
  91. </el-form-item>
  92. <el-form-item label="确认密码" prop="checkPass">
  93. <el-input
  94. v-model="registerForm.repassword"
  95. auto-complete="off"
  96. placeholder="请再输入一遍密码"
  97. type="password"
  98. style="width: 100%"
  99. ></el-input>
  100. </el-form-item>
  101. <el-form-item label="学校" prop="school">
  102. <el-select
  103. v-model="registerForm.school"
  104. placeholder="请选择学校"
  105. @change="getClass"
  106. >
  107. <el-option
  108. v-for="(item, index) in schoolArray"
  109. :key="index"
  110. :label="item.name"
  111. :value="item.id"
  112. >
  113. </el-option>
  114. </el-select>
  115. </el-form-item>
  116. <el-form-item label="班级" prop="class">
  117. <el-select v-model="registerForm.class" placeholder="请先选择学校">
  118. <el-option
  119. v-for="(item, index) in classArray"
  120. :key="index"
  121. :label="item.name"
  122. :value="item.id"
  123. >
  124. </el-option>
  125. </el-select>
  126. </el-form-item>
  127. </el-form>
  128. <div style="display: flex; justify-content: flex-end">
  129. <el-button type="text" @click="islogin = true">返回登录</el-button>
  130. </div>
  131. <div class="login_button">
  132. <button @click="register()">注册</button>
  133. </div>
  134. </div>
  135. </div>
  136. </div>
  137. </template>
  138. <script>
  139. export default {
  140. data() {
  141. var validatePass = (rule, value, callback) => {
  142. var _this = this;
  143. if (_this.registerForm.rpassword === "") {
  144. callback(new Error("请输入密码"));
  145. } else {
  146. if (_this.registerForm.rpassword.length < 6) {
  147. callback(new Error("输入的密码需不少于6位"));
  148. } else if (_this.registerForm.repassword !== "") {
  149. _this.$refs.ruleForm2.validateField("checkPass");
  150. }
  151. callback();
  152. }
  153. };
  154. var validatePass2 = (rule, value, callback) => {
  155. var _this = this;
  156. if (_this.registerForm.repassword === "") {
  157. callback(new Error("请再次输入密码"));
  158. } else if (
  159. _this.registerForm.repassword !== _this.registerForm.rpassword
  160. ) {
  161. callback(new Error("两次输入密码不一致!"));
  162. } else {
  163. callback();
  164. }
  165. };
  166. // 验证手机
  167. var checkPhone = (rule, value, callback) => {
  168. var _this = this;
  169. let reg = /^1\d{10}$/;
  170. if (_this.registerForm.rphoneNum === "") {
  171. callback(new Error("请输入手机号码"));
  172. } else {
  173. if (!reg.test(_this.registerForm.rphoneNum)) {
  174. callback(new Error("请输入11位手机号"));
  175. } else {
  176. let params = { un: _this.registerForm.rphoneNum };
  177. this.ajax
  178. .get(this.$store.state.api + "findPhone", params)
  179. .then((res) => {
  180. if (res.data[0].length > 0) {
  181. callback(new Error("此手机号已注册"));
  182. } else {
  183. callback();
  184. }
  185. console.log(res.data[0]);
  186. })
  187. .catch((err) => {
  188. callback();
  189. console.error(err);
  190. });
  191. }
  192. }
  193. };
  194. return {
  195. islogin: true,
  196. phoneNum: "",
  197. password: "",
  198. sqlPassword: "",
  199. registerForm: {
  200. rphoneNum: "",
  201. rpassword: "",
  202. repassword: "",
  203. school: "",
  204. class: "",
  205. },
  206. schoolArray: [],
  207. classArray: [],
  208. now: "",
  209. rules2: {
  210. phone: [
  211. {
  212. required: true,
  213. type: "number",
  214. validator: checkPhone,
  215. trigger: ["blur", "change"],
  216. },
  217. ],
  218. pass: [{ required: true, validator: validatePass, trigger: "blur" }],
  219. checkPass: [
  220. { required: true, validator: validatePass2, trigger: "blur" },
  221. ],
  222. school: [
  223. { required: true, message: "请选择活动区域", trigger: "blur" },
  224. ],
  225. },
  226. studentInfo: [],
  227. };
  228. },
  229. created() {
  230. // this.getOrg();
  231. this.getSchool();
  232. },
  233. methods: {
  234. time() {
  235. if (!this.now) {
  236. this.now = new Date().getTime();
  237. return true;
  238. } else {
  239. let time = new Date().getTime();
  240. if (time - this.now > 3000) {
  241. this.now = time;
  242. return true;
  243. } else {
  244. return false;
  245. }
  246. }
  247. },
  248. register() {
  249. this.$refs.ruleForm2.validate((valid) => {
  250. if (valid) {
  251. if (this.time()) {
  252. let params = [
  253. {
  254. username: this.registerForm.rphoneNum,
  255. userpassword: this.registerForm.repassword,
  256. oid: this.registerForm.school,
  257. cid: this.registerForm.class,
  258. },
  259. ];
  260. this.ajax
  261. .post(this.$store.state.api + "register_student", params)
  262. .then((res) => {
  263. this.$message({
  264. message: "注册成功",
  265. type: "success",
  266. });
  267. this.registerForm = {
  268. rphoneNum: "",
  269. rpassword: "",
  270. repassword: "",
  271. school: "",
  272. class: "",
  273. };
  274. this.islogin = true;
  275. })
  276. .catch((err) => {
  277. this.$message.error("注册失败");
  278. console.error(err);
  279. });
  280. }
  281. } else {
  282. return false;
  283. }
  284. });
  285. },
  286. findPhone() {
  287. let params = { un: this.phoneNum };
  288. this.ajax
  289. .get(this.$store.state.api + "findPhone", params)
  290. .then((res) => {
  291. if (res.data[0].length > 0) {
  292. if (res.data[0][0].state == 0) {
  293. this.$message.error("此账号已被屏蔽登录,请咨询管理员");
  294. return;
  295. }
  296. if (res.data[0][0].type == 2) {
  297. // this.sqlPassword = res.data[0][0].userpassword;
  298. // this.userInfo = res.data[0][0]
  299. this.isU = 1;
  300. } else {
  301. this.isU = 2;
  302. }
  303. } else {
  304. this.isU = false;
  305. }
  306. this.login();
  307. console.log(res.data[0][0]);
  308. })
  309. .catch((err) => {
  310. console.error(err);
  311. });
  312. },
  313. login() {
  314. if (this.time()) {
  315. var _this = this;
  316. var isU = _this.isU;
  317. if (isU == 1) {
  318. let params = [
  319. {
  320. uname: this.phoneNum,
  321. upassword: this.password,
  322. },
  323. ];
  324. _this.ajax
  325. .post(_this.$store.state.api + "login", params)
  326. .then((res) => {
  327. if (res.data.status == "1") {
  328. _this.$message({
  329. message: "登录成功",
  330. type: "success",
  331. });
  332. _this.studentInfo = res.data[0][0];
  333. _this.$cookies.set("slogin", "1", -1);
  334. _this.$cookies.set("suserid", _this.studentInfo.userid, -1);
  335. _this.$cookies.set("studentInfo", _this.studentInfo, -1);
  336. // window.sessionStorage.setItem("login", true);
  337. // window.sessionStorage.setItem("userInfo", JSON.stringify(this.userInfo));
  338. _this.$store.commit("update", ["isLogin", true]);
  339. _this.$store.commit("update", [
  340. "studentInfo",
  341. _this.studentInfo,
  342. ]);
  343. // console.log(_this.$store.state);
  344. _this.$router.push("/index");
  345. } else {
  346. this.$message.error("密码错误");
  347. }
  348. console.log(res.data);
  349. })
  350. .catch((err) => {
  351. this.$message.error("登录失败");
  352. console.error(err);
  353. });
  354. } else if (isU == 2) {
  355. this.$message.error("此账号角色不是学生");
  356. } else {
  357. this.$message.error("此账号还未注册请去注册");
  358. }
  359. }
  360. // sessionStorage.removeItem("key");
  361. },
  362. getSchool() {
  363. this.ajax
  364. .get(this.$store.state.api + "selectOrg", "")
  365. .then((res) => {
  366. this.schoolArray = res.data[0];
  367. })
  368. .catch((err) => {
  369. console.error(err);
  370. });
  371. },
  372. getClass() {
  373. let params = {
  374. oid: this.registerForm.school,
  375. };
  376. this.ajax
  377. .get(this.$store.state.api + "selectClassBySchool", params)
  378. .then((res) => {
  379. this.classArray = res.data[0];
  380. })
  381. .catch((err) => {
  382. console.error(err);
  383. });
  384. },
  385. },
  386. };
  387. </script>
  388. <style scoped>
  389. html,body{
  390. height: 100%;
  391. }
  392. .login_content {
  393. display: flex;
  394. align-items: center;
  395. justify-content: center;
  396. height: calc(100% - (67.5px * 2));
  397. /* position: absolute; */
  398. width: 100%;
  399. }
  400. .login_box {
  401. width: 23%;
  402. min-width: 400px;
  403. margin: 0 auto;
  404. background: #fff;
  405. padding: 20px;
  406. box-shadow: 0 0 20px #eee;
  407. }
  408. /* .userLogin img {
  409. width: 50%;
  410. display: inline-block;
  411. width: 18px;
  412. padding-right: 5px;
  413. border-right: 1px solid #2a97ff;
  414. position: absolute;
  415. top: 8px;
  416. left: 10px;
  417. z-index: 999;
  418. } */
  419. /* .el-input >>> input {
  420. width: 100%;
  421. padding: 15px 0 15px 40px;
  422. outline: none;
  423. border: 1px solid #ccc;
  424. font-size: 14px;
  425. } */
  426. /* .userLogin {
  427. margin-top: 25px;
  428. position: relative;
  429. } */
  430. .login_button {
  431. margin-top: 10px;
  432. }
  433. .login_button button {
  434. width: 100%;
  435. color: #fff;
  436. background: #2a97ff;
  437. padding: 10px;
  438. border-radius: 10px;
  439. border: none;
  440. font-size: 18px;
  441. text-align: center;
  442. outline: none;
  443. cursor: pointer;
  444. }
  445. .login_title {
  446. text-align: center;
  447. font-size: 26px;
  448. font-weight: 600;
  449. }
  450. </style>