login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <template>
  2. <div class="login_content">
  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 v-model="registerForm.school" placeholder="请选择学校">
  103. <el-option
  104. v-for="(item, index) in schoolArray"
  105. :key="index"
  106. :label="item.name"
  107. :value="item.id"
  108. >
  109. </el-option>
  110. </el-select>
  111. </el-form-item>
  112. </el-form>
  113. <div style="display: flex; justify-content: flex-end">
  114. <el-button type="text" @click="islogin = true">返回登录</el-button>
  115. </div>
  116. <div class="login_button">
  117. <button @click="register()">注册</button>
  118. </div>
  119. </div>
  120. </div>
  121. </div>
  122. </template>
  123. <script>
  124. export default {
  125. data() {
  126. var validatePass = (rule, value, callback) => {
  127. var _this = this;
  128. if (_this.registerForm.rpassword === "") {
  129. callback(new Error("请输入密码"));
  130. } else {
  131. if (_this.registerForm.rpassword.length < 6) {
  132. callback(new Error("输入的密码需不少于6位"));
  133. } else if (_this.registerForm.repassword !== "") {
  134. _this.$refs.ruleForm2.validateField("checkPass");
  135. }
  136. callback();
  137. }
  138. };
  139. var validatePass2 = (rule, value, callback) => {
  140. var _this = this;
  141. if (_this.registerForm.repassword === "") {
  142. callback(new Error("请再次输入密码"));
  143. } else if (
  144. _this.registerForm.repassword !== _this.registerForm.rpassword
  145. ) {
  146. callback(new Error("两次输入密码不一致!"));
  147. } else {
  148. callback();
  149. }
  150. };
  151. // 验证手机
  152. var checkPhone = (rule, value, callback) => {
  153. var _this = this;
  154. let reg = /^1\d{10}$/;
  155. if (_this.registerForm.rphoneNum === "") {
  156. callback(new Error("请输入手机号码"));
  157. } else {
  158. if (!reg.test(_this.registerForm.rphoneNum)) {
  159. callback(new Error("请输入11位手机号"));
  160. } else {
  161. let params = { un: _this.registerForm.rphoneNum };
  162. this.ajax
  163. .get(this.$store.state.api + "findPhone", params)
  164. .then((res) => {
  165. if (res.data[0].length > 0) {
  166. callback(new Error("此手机号已注册"));
  167. } else {
  168. callback();
  169. }
  170. console.log(res.data[0]);
  171. })
  172. .catch((err) => {
  173. callback();
  174. console.error(err);
  175. });
  176. }
  177. }
  178. };
  179. return {
  180. islogin: true,
  181. phoneNum: "",
  182. password: "",
  183. sqlPassword: "",
  184. registerForm: {
  185. rphoneNum: "",
  186. rpassword: "",
  187. repassword: "",
  188. school: "",
  189. },
  190. schoolArray: [],
  191. now: "",
  192. rules2: {
  193. phone: [
  194. {
  195. required: true,
  196. type: "number",
  197. validator: checkPhone,
  198. trigger: ["blur", "change"],
  199. },
  200. ],
  201. pass: [{ required: true, validator: validatePass, trigger: "blur" }],
  202. checkPass: [
  203. { required: true, validator: validatePass2, trigger: "blur" },
  204. ],
  205. school: [
  206. { required: true, message: "请选择活动区域", trigger: "blur" },
  207. ],
  208. },
  209. userInfo: [],
  210. };
  211. },
  212. created() {
  213. // this.getOrg();
  214. this.getSchool();
  215. },
  216. methods: {
  217. time() {
  218. if (!this.now) {
  219. this.now = new Date().getTime();
  220. return true;
  221. } else {
  222. let time = new Date().getTime();
  223. if (time - this.now > 3000) {
  224. this.now = time;
  225. return true;
  226. } else {
  227. return false;
  228. }
  229. }
  230. },
  231. register() {
  232. this.$refs.ruleForm2.validate((valid) => {
  233. if (valid) {
  234. if (this.time()) {
  235. let params = [
  236. {
  237. username: this.registerForm.rphoneNum,
  238. userpassword: this.registerForm.repassword,
  239. oid: this.registerForm.school,
  240. },
  241. ];
  242. this.ajax
  243. .post(this.$store.state.api + "register", params)
  244. .then((res) => {
  245. this.$message({
  246. message: "注册成功",
  247. type: "success",
  248. });
  249. this.registerForm = {
  250. rphoneNum: "",
  251. rpassword: "",
  252. repassword: "",
  253. school: "",
  254. };
  255. this.islogin = true;
  256. })
  257. .catch((err) => {
  258. this.$message.error("注册失败");
  259. console.error(err);
  260. });
  261. }
  262. } else {
  263. return false;
  264. }
  265. });
  266. },
  267. findPhone() {
  268. let params = { un: this.phoneNum };
  269. this.ajax
  270. .get(this.$store.state.api + "findPhone", params)
  271. .then((res) => {
  272. if (res.data[0].length > 0) {
  273. if (res.data[0][0].state == 0) {
  274. this.$message.error("此账号已被屏蔽登录,请咨询管理员");
  275. return;
  276. }
  277. if (res.data[0][0].type == 1) {
  278. // this.sqlPassword = res.data[0][0].userpassword;
  279. // this.userInfo = res.data[0][0]
  280. this.isU = 1;
  281. } else {
  282. this.isU = 2;
  283. }
  284. } else {
  285. this.isU = false;
  286. }
  287. this.login();
  288. console.log(res.data[0][0]);
  289. })
  290. .catch((err) => {
  291. console.error(err);
  292. });
  293. },
  294. login() {
  295. if (this.time()) {
  296. var _this = this;
  297. var isU = _this.isU;
  298. if (isU == 1) {
  299. let params = [
  300. {
  301. uname: this.phoneNum,
  302. upassword: this.password,
  303. // sqlpassword: this.sqlPassword,
  304. },
  305. ];
  306. _this.ajax
  307. .post(_this.$store.state.api + "login", params)
  308. .then((res) => {
  309. if (res.data.status == "1") {
  310. _this.$message({
  311. message: "登录成功",
  312. type: "success",
  313. });
  314. _this.userInfo = res.data[0][0];
  315. _this.$cookies.set("tlogin", "1", -1);
  316. _this.$cookies.set("tuserid", _this.userInfo.userid, -1);
  317. _this.$cookies.set("teacherInfo", _this.userInfo, -1);
  318. // window.sessionStorage.setItem("login", true);
  319. // window.sessionStorage.setItem("userInfo", JSON.stringify(this.userInfo));
  320. _this.$store.commit("update", ["isLogin", true]);
  321. _this.$store.commit("update", ["userInfo", _this.userInfo]);
  322. // console.log(_this.$store.state);
  323. _this.$router.push("/course?userid=" + _this.userInfo.userid);
  324. } else {
  325. this.$message.error("密码错误");
  326. }
  327. console.log(res.data);
  328. })
  329. .catch((err) => {
  330. this.$message.error("登录失败");
  331. console.error(err);
  332. });
  333. } else if (isU == 2) {
  334. this.$message.error("此账号角色不是老师");
  335. } else {
  336. this.$message.error("此账号还未注册请去注册");
  337. }
  338. }
  339. // sessionStorage.removeItem("key");
  340. },
  341. //查询学校
  342. // getOrg() {
  343. // this.ajax
  344. // .get(this.$store.state.api + "getAllOrg", "")
  345. // .then((res) => {
  346. // this.schoolArray = res.data[0];
  347. // console.log(res.data[0]);
  348. // })
  349. // .catch((err) => {
  350. // console.error(err);
  351. // });
  352. // },
  353. getSchool() {
  354. this.ajax
  355. .get(this.$store.state.api + "selectOrg", "")
  356. .then((res) => {
  357. this.schoolArray = res.data[0];
  358. })
  359. .catch((err) => {
  360. console.error(err);
  361. });
  362. },
  363. },
  364. };
  365. </script>
  366. <style scoped>
  367. .login_content {
  368. display: flex;
  369. align-items: center;
  370. justify-content: center;
  371. height: calc(100% - (67.5px * 2));
  372. /* position: absolute; */
  373. width: 100%;
  374. }
  375. .login_box {
  376. width: 23%;
  377. min-width: 400px;
  378. margin: 0 auto;
  379. background: #fff;
  380. padding: 20px;
  381. box-shadow: 0 0 20px #eee;
  382. }
  383. /* .userLogin img {
  384. width: 50%;
  385. display: inline-block;
  386. width: 18px;
  387. padding-right: 5px;
  388. border-right: 1px solid #2a97ff;
  389. position: absolute;
  390. top: 8px;
  391. left: 10px;
  392. z-index: 999;
  393. } */
  394. /* .el-input >>> input {
  395. width: 100%;
  396. padding: 15px 0 15px 40px;
  397. outline: none;
  398. border: 1px solid #ccc;
  399. font-size: 14px;
  400. } */
  401. /* .userLogin {
  402. margin-top: 25px;
  403. position: relative;
  404. } */
  405. .login_button {
  406. margin-top: 10px;
  407. }
  408. .login_button button {
  409. width: 100%;
  410. color: #fff;
  411. background: #2a97ff;
  412. padding: 10px;
  413. border-radius: 10px;
  414. border: none;
  415. font-size: 18px;
  416. text-align: center;
  417. outline: none;
  418. cursor: pointer;
  419. }
  420. .login_title {
  421. text-align: center;
  422. font-size: 26px;
  423. font-weight: 600;
  424. }
  425. </style>