login.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. // if (window.history.state.back) {
  232. // router.replace({
  233. // path: '/'
  234. // })
  235. // // 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'){
  236. // // window.location.href = "/";
  237. // // }else{
  238. // // window.location.href = window.history.state.back;
  239. // // }
  240. // } else {
  241. // router.replace({ path: "/member/home" });
  242. // }
  243. }
  244. })
  245. } else {
  246. //提示错误
  247. errorMsg.value = res.msg;
  248. }
  249. }).finally(() => {
  250. loginLoding.value = false;
  251. })
  252. };
  253. //清空输入框内容
  254. const clearInputVal = (type) => {
  255. if (type == "name") {
  256. name.value = "";
  257. } else if (type == "password") {
  258. password.value = "";
  259. }
  260. };
  261. //登录方式切换
  262. const changeLoginType = (type) => {
  263. loginType.value = type;
  264. name.value = "";
  265. password.value = "";
  266. errorMsg.value = "";
  267. };
  268. //通过replace方式跳转页面
  269. const goToPage = (type) => {
  270. router.replace({
  271. path: type,
  272. });
  273. };
  274. watch([name, password], () => {
  275. if (loginType.value == 1) {
  276. password.value = password.value.substring(0, 20);
  277. // name.value = memberEmail.value.substring(0, 20)
  278. } else {
  279. password.value = password.value.substring(0, 6);
  280. // name.value = memberEmail.value.substring(0, .gitignore)
  281. }
  282. if (password.value || name.value) {
  283. errorMsg.value = "";
  284. }
  285. });
  286. onMounted(() => {
  287. if (route.query.redirectUrl) {
  288. fromurl.value =
  289. window.location.origin + decodeURIComponent(route.query.redirectUrl);
  290. } else {
  291. fromurl.value = window.location.origin;
  292. }
  293. if (route.query.error) {
  294. ElMessage.error(decodeURIComponent(route.query.error));
  295. }
  296. });
  297. // onBeforeRouteLeave(() => {
  298. // keyEnter.value = false;
  299. // });
  300. </script>
  301. <style lang="scss" scoped>
  302. @import "@/assets/style/register.scss";
  303. .center {
  304. padding: 30px 30px 40px;
  305. .item {
  306. position: relative;
  307. margin-top: 15px;
  308. border-radius: 2px;
  309. &:first-child {
  310. margin-top: 0;
  311. }
  312. .icon {
  313. position: absolute;
  314. left: 1px;
  315. top: 1px;
  316. width: 50px;
  317. text-align: center;
  318. height: 38px;
  319. background: #f8f8f8;
  320. .input {
  321. border: 1px solid #e8e8e8;
  322. height: 40px;
  323. padding: 0 44px 0 60px;
  324. width: 326px;
  325. }
  326. }
  327. .input {
  328. border: 1px solid #e8e8e8;
  329. height: 40px;
  330. padding: 0 44px 0 60px;
  331. width: 326px;
  332. }
  333. }
  334. .cancel {
  335. position: absolute;
  336. right: 0;
  337. top: 1px;
  338. width: 44px;
  339. height: 38px;
  340. cursor: pointer;
  341. :before {
  342. position: absolute;
  343. top: 9px;
  344. left: 14px;
  345. }
  346. }
  347. .error {
  348. margin-top: 10px;
  349. position: relative;
  350. color: $colorMain;
  351. height: 16px;
  352. line-height: 16px;
  353. }
  354. .submit {
  355. margin-top: 35px;
  356. background: $colorMain;
  357. color: #fff;
  358. text-align: center;
  359. border-radius: 2px;
  360. width: 100%;
  361. height: 45px;
  362. font-size: 18px;
  363. letter-spacing: 0px;
  364. &:hover {
  365. opacity: 0.9;
  366. }
  367. &.disabled {
  368. background-color: #909399;
  369. }
  370. }
  371. }
  372. </style>