login.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <template>
  2. <div class="sld_login">
  3. <div class="sld_login_header">
  4. <div class="content">
  5. <nuxt-link tag="a" class="l_logo" :to="'/'">
  6. <img
  7. class="img"
  8. :src="
  9. filtersStore.getSiteLogo ? filtersStore.getSiteLogo : defaultImg
  10. "
  11. :onerror="defaultImg"
  12. alt
  13. />
  14. </nuxt-link>
  15. <div class="r_register_wrap">
  16. {{ L["还没注册?"] }}
  17. <a
  18. class="go_register_btn"
  19. href="javascript:void(0)"
  20. @click="goToPage('/register')"
  21. >{{ L["去注册"] }}</a
  22. >
  23. </div>
  24. </div>
  25. </div>
  26. <div class="sld_login_content">
  27. <img
  28. class="bg"
  29. :src="ImgBG ? ImgBG : configInfo.main_user_logon_bg + ''"
  30. :onerror="defaultBgImg"
  31. alt
  32. />
  33. <div class="login">
  34. <div class="login_box">
  35. <div class="top">
  36. <div class="item1">{{ L["账号登录"] }}</div>
  37. </div>
  38. <div class="center">
  39. <div class="item account">
  40. <span
  41. style="color: #bbb; font-size: 19px; padding-top: 7px"
  42. class="icon iconfont icon-wode"
  43. ></span>
  44. <input
  45. type="text"
  46. v-model="name"
  47. :placeholder="L['请输入邮箱']"
  48. class="input"
  49. @blur="validateEmail"
  50. autocomplete="off"
  51. />
  52. <div
  53. data-type="userName"
  54. class="cancel"
  55. @click="clearInputVal('name')"
  56. >
  57. <span style="color: #bbb" class="iconfont icon-cuowu"></span>
  58. </div>
  59. </div>
  60. <div class="error" v-if="emailErrorMsg">
  61. <span
  62. style="color: #e1251b; font-size: 14px"
  63. class="iconfont icon-jubao"
  64. ></span>
  65. {{ emailErrorMsg }}
  66. </div>
  67. <div class="item password">
  68. <span
  69. style="color: #bbb; font-size: 21px; padding-top: 7px"
  70. class="icon iconfont icon-mima1"
  71. ></span>
  72. <input
  73. type="password"
  74. v-model="password"
  75. :placeholder="L['请输入密码']"
  76. class="input"
  77. autocomplete="new-password"
  78. />
  79. <div
  80. data-type="password"
  81. class="cancel"
  82. @click="clearInputVal('password')"
  83. >
  84. <span style="color: #bbb" class="iconfont icon-cuowu"></span>
  85. </div>
  86. </div>
  87. <div class="error" v-if="errorMsg">
  88. <span
  89. style="color: #e1251b; font-size: 14px"
  90. class="iconfont icon-jubao"
  91. ></span>
  92. {{ errorMsg }}
  93. </div>
  94. <el-button
  95. @click="login"
  96. :class="{ submit: true, disabled: loginDisabled }"
  97. :disabled="loginDisabled"
  98. :loading="loginLoding"
  99. >{{ L["登录"] }}</el-button
  100. >
  101. </div>
  102. <div
  103. :class="{
  104. bottom: true,
  105. flex_row_between_center: wxEnable == 0,
  106. flex_row_end_center: wxEnable != 0,
  107. }"
  108. >
  109. <a href="javascript:void(0)" @click="goToPage('/register')">{{
  110. L["立即注册"]
  111. }}</a>
  112. <router-link tag="a" :to="`/member/login/forget`">{{
  113. L["忘记密码"]
  114. }}</router-link>
  115. </div>
  116. </div>
  117. </div>
  118. </div>
  119. </div>
  120. </template>
  121. <script setup>
  122. import { ElMessage } from "element-plus";
  123. // import { lang_zn } from "@/assets/language/zh";
  124. import { getCurLanguage } from "@/composables/common.js";
  125. import { useUserInfo } from "@/store/user.js";
  126. import { useFiltersStore } from "@/store/filter.js";
  127. const configInfo = useUserInfo();
  128. const filtersStore = useFiltersStore();
  129. const isLoading = ref(false);
  130. // const L = lang_zn;
  131. const L = getCurLanguage();
  132. const router = useRouter();
  133. const route = useRoute();
  134. const { proxy } = getCurrentInstance();
  135. const keyEnter = ref(true);
  136. const errorMsg = ref(); //错误提示
  137. const name = ref(""); //账户
  138. const password = ref(""); //密码
  139. const loginType = ref(1); //登陆类型:1-账号密码登陆,2-手机验证码登陆
  140. const defaultImg = ref("/common_top_logo.png");
  141. const defaultBgImg = ref("/login_bg.png");
  142. const fromurl = ref("");
  143. const wxEnable = ref("");
  144. const pwdCalc = ref();
  145. const ImgBG = ref("");
  146. const loginLoding = ref(false);
  147. const emailCalc = ref();
  148. const emailErrorMsg = ref('')
  149. const loginDisabled = computed(() => !name.value || !password.value);
  150. useHead({
  151. title: "Login",
  152. meta: [
  153. {
  154. name: "description",
  155. content: "Login",
  156. },
  157. {
  158. name: "keywords",
  159. content: "Login",
  160. },
  161. ],
  162. });
  163. //由于这里的回车键触发事件和商品搜索框的回车键触发事件冲突,引入keyEnter变量判断
  164. if (process.client) {
  165. document.onkeydown = function () {
  166. var key = window.event.keyCode;
  167. if (key == 13 && keyEnter.value) {
  168. login();
  169. }
  170. };
  171. }
  172. //获取背景图
  173. const getBg = () => {
  174. get("v3/system/front/setting/getSettings?names=main_user_logon_bg").then(
  175. (res) => {
  176. ImgBG.value = res.data[0];
  177. }
  178. );
  179. };
  180. getBg();
  181. // 校验邮箱
  182. const validateEmail = () => {
  183. //邮箱非空的验证
  184. if (!name.value) {
  185. emailErrorMsg.value = L["请输入邮箱"];
  186. return false;
  187. }
  188. // 邮箱格式验证
  189. emailCalc.value = checkEmail(name.value);
  190. if (emailCalc.value !== true) {
  191. emailErrorMsg.value = emailCalc.value;
  192. return false;
  193. }
  194. emailErrorMsg.value = "";
  195. return true;
  196. };
  197. const login = () => {
  198. let param = {};
  199. param.username = name.value;
  200. param.password = password.value;
  201. param.loginType = loginType.value;
  202. //邮箱验证
  203. if (!validateEmail()) return;
  204. //密码校验
  205. if (!param.password) {
  206. errorMsg.value = L["请输入密码"];
  207. return false;
  208. } else {
  209. pwdCalc.value = checkPwd(param.password);
  210. if (pwdCalc.value !== true) {
  211. errorMsg.value = pwdCalc.value;
  212. return false;
  213. }
  214. }
  215. loginLoding.value = true;
  216. post("v3/frontLogin/oauth/token", param).then((res) => {
  217. if (res.state == 200) {
  218. //将用户信息存缓存,并跳转到首页
  219. filtersStore.setLoginStatus(true);
  220. filtersStore.setToken(res.data.access_token);
  221. filtersStore.setRefreshToken(res.data.refresh_token);
  222. filtersStore.setTime(new Date().getTime().toString()); //存储refresh_token更新时间
  223. //获取用户信息,并同步信息到pinia
  224. get("v3/member/front/member/getInfo")
  225. .then((res) => {
  226. if (res.state == 200) {
  227. filtersStore.setMemberInfo(res.data);
  228. router.replace({
  229. path: '/'
  230. })
  231. localStorage.setItem('isLoggedIn', 'true');
  232. setTimeout(() => {
  233. localStorage.removeItem('isLoggedIn');
  234. }, 200)
  235. // if (window.history.state.back) {
  236. // router.replace({
  237. // path: '/'
  238. // })
  239. // // if(window.history.state.back == '/register' || window.history.state.back == '/member/login/forget' || window.history.state.back == '/member/login/forget' || window.history.state.back == '/member/login/forget'){
  240. // // window.location.href = "/";
  241. // // }else{
  242. // // window.location.href = window.history.state.back;
  243. // // }
  244. // } else {
  245. // router.replace({ path: "/member/home" });
  246. // }
  247. }
  248. })
  249. } else {
  250. //提示错误
  251. errorMsg.value = res.msg;
  252. }
  253. }).finally(() => {
  254. loginLoding.value = false;
  255. })
  256. };
  257. //清空输入框内容
  258. const clearInputVal = (type) => {
  259. if (type == "name") {
  260. name.value = "";
  261. } else if (type == "password") {
  262. password.value = "";
  263. }
  264. };
  265. //登录方式切换
  266. const changeLoginType = (type) => {
  267. loginType.value = type;
  268. name.value = "";
  269. password.value = "";
  270. errorMsg.value = "";
  271. };
  272. //通过replace方式跳转页面
  273. const goToPage = (type) => {
  274. router.replace({
  275. path: type,
  276. });
  277. };
  278. watch([name, password], () => {
  279. if (loginType.value == 1) {
  280. password.value = password.value.substring(0, 20);
  281. // name.value = memberEmail.value.substring(0, 20)
  282. } else {
  283. password.value = password.value.substring(0, 6);
  284. // name.value = memberEmail.value.substring(0, .gitignore)
  285. }
  286. if (password.value || name.value) {
  287. errorMsg.value = "";
  288. }
  289. });
  290. onMounted(() => {
  291. if (route.query.redirectUrl) {
  292. fromurl.value =
  293. window.location.origin + decodeURIComponent(route.query.redirectUrl);
  294. } else {
  295. fromurl.value = window.location.origin;
  296. }
  297. if (route.query.error) {
  298. ElMessage.error(decodeURIComponent(route.query.error));
  299. }
  300. });
  301. // onBeforeRouteLeave(() => {
  302. // keyEnter.value = false;
  303. // });
  304. </script>
  305. <style lang="scss" scoped>
  306. @import "@/assets/style/register.scss";
  307. .center {
  308. padding: 30px 30px 40px;
  309. .item {
  310. position: relative;
  311. margin-top: 15px;
  312. border-radius: 2px;
  313. &:first-child {
  314. margin-top: 0;
  315. }
  316. .icon {
  317. position: absolute;
  318. left: 1px;
  319. top: 1px;
  320. width: 50px;
  321. text-align: center;
  322. height: 38px;
  323. background: #f8f8f8;
  324. .input {
  325. border: 1px solid #e8e8e8;
  326. height: 40px;
  327. padding: 0 44px 0 60px;
  328. width: 326px;
  329. }
  330. }
  331. .input {
  332. border: 1px solid #e8e8e8;
  333. height: 40px;
  334. padding: 0 44px 0 60px;
  335. width: 326px;
  336. }
  337. }
  338. .cancel {
  339. position: absolute;
  340. right: 0;
  341. top: 1px;
  342. width: 44px;
  343. height: 38px;
  344. cursor: pointer;
  345. :before {
  346. position: absolute;
  347. top: 9px;
  348. left: 14px;
  349. }
  350. }
  351. .error {
  352. margin-top: 10px;
  353. position: relative;
  354. color: $colorMain;
  355. height: 16px;
  356. line-height: 16px;
  357. }
  358. .submit {
  359. margin-top: 35px;
  360. background: $colorMain;
  361. color: #fff;
  362. text-align: center;
  363. border-radius: 2px;
  364. width: 100%;
  365. height: 45px;
  366. font-size: 18px;
  367. letter-spacing: 0px;
  368. &:hover {
  369. opacity: 0.9;
  370. }
  371. &.disabled {
  372. background-color: #909399;
  373. }
  374. }
  375. }
  376. </style>