SignIn.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <!-- 登录页面 -->
  2. <template>
  3. <el-container class="bgStyle">
  4. <el-main class="main">
  5. <el-row
  6. ><el-col :span="24">
  7. <h1 class="title">
  8. <img :src="imgSrc" />
  9. </h1>
  10. </el-col>
  11. </el-row>
  12. <el-row class="content">
  13. <el-col :span="10" class="contentChild" :offset="7">
  14. <el-row>
  15. <el-col :span="24">
  16. <h2>账号登录</h2>
  17. <el-form
  18. label-position="top"
  19. :model="ruleForm"
  20. status-icon
  21. :rules="rules"
  22. ref="ruleForm"
  23. label-width="100px"
  24. class="demo-ruleForm"
  25. >
  26. <el-form-item prop="telephone">
  27. <el-input
  28. prefix-icon="el-icon-phone"
  29. type="text"
  30. v-model.trim="ruleForm.telephone"
  31. maxlength="11"
  32. placeholder="请输入手机号"
  33. ></el-input>
  34. </el-form-item>
  35. <el-form-item prop="password">
  36. <el-input
  37. prefix-icon="el-icon-menu"
  38. type="password"
  39. v-model="ruleForm.password"
  40. placeholder="请输入密码"
  41. ></el-input>
  42. </el-form-item>
  43. <el-form-item prop="randYZM">
  44. <el-input
  45. prefix-icon="el-icon-picture"
  46. v-model="ruleForm.randYZM"
  47. class="randFirst"
  48. placeholder="请输入验证码"
  49. ></el-input>
  50. <input
  51. type="button"
  52. v-model="ruleForm.imageCode"
  53. class="rand"
  54. @click="createCode"
  55. />
  56. </el-form-item>
  57. <el-form-item class="noMarginBottom">
  58. <el-button
  59. class="btnSty"
  60. type="primary"
  61. @click="submitForm('ruleForm')"
  62. >登录</el-button
  63. >
  64. </el-form-item>
  65. <!-- <el-form-item class="textLeft">
  66. <el-checkbox v-model="ruleForm.checked"
  67. >下次自动登录</el-checkbox
  68. >
  69. </el-form-item> -->
  70. </el-form>
  71. </el-col>
  72. </el-row>
  73. </el-col>
  74. </el-row>
  75. </el-main>
  76. </el-container>
  77. </template>
  78. <script>
  79. import md5 from "js-md5";
  80. import { JSEncrypt } from "jsencrypt";
  81. import { login } from "../../api/api";
  82. import axios from 'axios'
  83. import qs from "qs";
  84. export default {
  85. name: "SignIn",
  86. data() {
  87. const validatePhone = (rule, value, callback) => {
  88. let patter = new RegExp("^1[3|4|5|7|8][0-9]\\d{8}$");
  89. if (!patter.test(value)) {
  90. return callback(new Error("请输入正确联系人手机号!"));
  91. } else {
  92. callback();
  93. }
  94. };
  95. const validateYZM = (rule, value, callback) => {
  96. if (value.toUpperCase() != this.ruleForm.imageCode.toUpperCase()) {
  97. return callback(new Error("请输入正确验证码!"));
  98. } else {
  99. callback();
  100. }
  101. };
  102. return {
  103. imgSrc: require("../../assets/logo.png"), // logo
  104. ruleForm: {
  105. telephone: '13000000002', // 手机号
  106. password: '123456', // 密码
  107. randYZM: "", // 验证码
  108. // checked: false, // 下次自动登录
  109. imageCode: "", // 图片验证码
  110. },
  111. secretKey: "6w3lacP9gmjbMgor",
  112. rules: {
  113. telephone: [
  114. { required: true, message: "请输入手机号", trigger: "blur" },
  115. { validator: validatePhone, trigger: "blur" },
  116. ],
  117. password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  118. randYZM: [
  119. { required: true, message: "请输入验证码", trigger: "blur" },
  120. { validator: validateYZM, trigger: "blur" },
  121. ],
  122. },
  123. id: 1,
  124. };
  125. },
  126. methods: {
  127. // 加密
  128. encryptedData(publicKey, data) {
  129. // 新建JSEncrypt对象+
  130. var val = "";
  131. data = encodeURIComponent(data);
  132. let arr = Array.apply(null, { length: data.length }).map(
  133. (item, index) => {
  134. return index;
  135. }
  136. );
  137. for (var i = 0, j = 0; i < data.length, j < data.length; i++, j++) {
  138. if (j == publicKey.length) {
  139. j = 0;
  140. }
  141. arr[i] = data.charCodeAt(i) ^ publicKey.charCodeAt(j);
  142. var strCipher = arr[i].toString(16);
  143. if (strCipher.length == 1) {
  144. val += "0" + strCipher;
  145. } else {
  146. val += strCipher;
  147. }
  148. }
  149. return val;
  150. },
  151. // 解密
  152. decryptData(privateKey, data) {
  153. // 新建JSEncrypt对象
  154. let decrypt = new JSEncrypt();
  155. // 设置私钥
  156. decrypt.setPrivateKey(privateKey);
  157. // 解密数据
  158. return decrypt.decrypt(secretWord);
  159. },
  160. //登录
  161. submitForm(formName) {
  162. // var a = this.encryptedData("6w3lacP9gmjbMgor", "13312341234");
  163. // console.log(a);
  164. // 验证
  165. var _this = this;
  166. _this.$refs[formName].validate((valid) => {
  167. if (valid) {
  168. var newsForm = {
  169. telephone: _this.encryptedData(
  170. _this.secretKey,
  171. _this.ruleForm.telephone
  172. ),
  173. // telephone: _this.ruleForm.telephone,
  174. password: md5(_this.ruleForm.password),
  175. // password: _this.ruleForm.password,
  176. // pushToken:"6e1c1be58ab4c75f4322963b4b40b7f73bb6c56eea2f0a9fcfdc406e78efbbeb",
  177. pushToken: _this.encryptedData(
  178. _this.secretKey,
  179. _this.ruleForm.telephon
  180. ),
  181. phoneType: "1",
  182. phonemodel: "iPhone12,1",
  183. code:_this.ruleForm.randYZM
  184. };
  185. login(qs.stringify(newsForm)).then(
  186. function (data) {
  187. if (data.code == 200) {
  188. sessionStorage.setItem("token", data.data.token);
  189. // 记住密码/不记住密码
  190. // _this.$cookies.set("id", data.data.id);
  191. // _this.$cookies.set("telephone", _this.ruleForm.telephone);
  192. // _this.$cookies.set("password", _this.ruleForm.password);
  193. // _this.$cookies.set("userName", data.data.userName);
  194. // if (_this.ruleForm.checked) {
  195. // //_this.$cookies.set('remPassword',true,{expires:7});
  196. // _this.$cookies.set("remPassword", true);
  197. // } else {
  198. // _this.$cookies.set("remPassword", false, { expires: -1 });
  199. // }
  200. // 提示信息
  201. _this.$message({
  202. message: "登录成功!",
  203. type: "success",
  204. });
  205. if (_this.id != null || _this.id != undefined) {
  206. let token = data.data.token;
  207. sessionStorage.setItem("token", token);
  208. sessionStorage.setItem("userinfo", JSON.stringify(data.data));
  209. // _this.$cookies.set("factoryList", data.data);
  210. // localStorage.setItem("factoryId", data.data.factoryId);
  211. // roleId 1:超级管理员 19:厂家管理员
  212. if (
  213. data.data.roleId == 3 ||
  214. data.data.roleId == 4 ||
  215. data.data.roleId == 2
  216. ) {
  217. _this.$router.push({
  218. path: "/cardHome",
  219. // query: { roleId: data.data.roleId },
  220. });
  221. } else {
  222. _this.$message({
  223. message: "请确定该用户是否有登录系统权限!",
  224. type: "error",
  225. });
  226. }
  227. }
  228. } else {
  229. _this.$message({
  230. message: data.body.msg,
  231. type: "error",
  232. });
  233. }
  234. },
  235. function () {
  236. _this.$alert("请求失败,请稍后再试", "提示", {
  237. confirmButtonText: "确定",
  238. });
  239. }
  240. );
  241. } else {
  242. return false;
  243. }
  244. });
  245. },
  246. createCode() {
  247. let code = "";
  248. let codeLength = 4;
  249. let random = new Array(
  250. 0,
  251. 1,
  252. 2,
  253. 3,
  254. 4,
  255. 5,
  256. 6,
  257. 7,
  258. 8,
  259. 9,
  260. "A",
  261. "B",
  262. "C",
  263. "D",
  264. "E",
  265. "F",
  266. "G",
  267. "H",
  268. "I",
  269. "J",
  270. "K",
  271. "L",
  272. "M",
  273. "N",
  274. "O",
  275. "P",
  276. "Q",
  277. "R",
  278. "S",
  279. "T",
  280. "U",
  281. "V",
  282. "W",
  283. "X",
  284. "Y",
  285. "Z"
  286. ); //随机数
  287. for (let i = 0; i < codeLength; i++) {
  288. let index = Math.floor(Math.random() * 36);
  289. code += random[index];
  290. }
  291. this.ruleForm.imageCode = code;
  292. },
  293. },
  294. mounted() {
  295. // 获取图片验证码
  296. this.createCode();
  297. // 判断初始化时cookies时有效的
  298. let telephone = this.$cookies.get("telephone");
  299. let password = this.$cookies.get("password");
  300. if (telephone != "" && this.$cookies.get("remPassword") == "true") {
  301. this.ruleForm.telephone = telephone;
  302. this.ruleForm.password = password;
  303. this.ruleForm.checked = true;
  304. this.ruleForm.randYZM = this.ruleForm.imageCode;
  305. this.submitForm("ruleForm"); // 执行登录
  306. } else {
  307. this.ruleForm.telephone = "";
  308. this.ruleForm.password = "";
  309. this.ruleForm.randYZM = "";
  310. //this.ruleForm.telephone = '13682152685';
  311. //this.ruleForm.password = '123456';
  312. this.ruleForm.checked = false;
  313. }
  314. // window.addEventListener('pywebviewready',function(){
  315. // },)
  316. },
  317. };
  318. </script>
  319. <!-- Add "scoped" attribute to limit CSS to this component only -->
  320. <style scoped>
  321. .bgStyle {
  322. background: #3b3b3b;
  323. position: fixed;
  324. width: 100%;
  325. height: 100%;
  326. }
  327. .title {
  328. color: #ffffff;
  329. font-size: 40px;
  330. margin: 90px 0 60px 0;
  331. text-align:center;
  332. }
  333. .title img {
  334. display: inline-block;
  335. vertical-align: middle;
  336. height: 70px;
  337. }
  338. .contentChild {
  339. background: #e6e6e6;
  340. border-radius: 3px;
  341. padding: 60px 50px;
  342. }
  343. .contentChild h2{
  344. padding: 10px 0;
  345. }
  346. .btnSty {
  347. width: 100%;
  348. }
  349. .noMarginBottom {
  350. margin-bottom: 0;
  351. }
  352. .textLeft {
  353. text-align: left;
  354. }
  355. .randFirst {
  356. float: left;
  357. width: 70%;
  358. }
  359. .rand {
  360. float: left;
  361. width: 30%;
  362. border-radius: 4px;
  363. border: 1px solid #dcdfe6;
  364. line-height: 40px;
  365. height: 40px;
  366. background: #ffffff;
  367. letter-spacing: 4px;
  368. }
  369. </style>