login.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <template>
  2. <Theme>
  3. <view class="wrap">
  4. <Navbar autoBack fixed @leftClick="leftClick" />
  5. <view class="content">
  6. <view class="logo">
  7. <image
  8. class="logo_img"
  9. src="@/static/login/login_logo.png"
  10. mode="widthFix"
  11. ></image>
  12. </view>
  13. <!-- Tab 切换栏 -->
  14. <view class="tab-bar">
  15. <view
  16. class="tab-item"
  17. :class="{ active: loginType === 'phone' }"
  18. @click="changeTab('phone')"
  19. >
  20. <trans _t="手机登录" />
  21. </view>
  22. <view
  23. class="tab-item"
  24. :class="{ active: loginType === 'email' }"
  25. @click="changeTab('email')"
  26. >
  27. <trans _t="邮箱登录" />
  28. </view>
  29. </view>
  30. <up-form
  31. labelPosition="left"
  32. :model="form"
  33. :rules="getRules()"
  34. ref="formRef"
  35. labelWidth="0"
  36. >
  37. <up-form-item prop="userphone">
  38. <up-input
  39. type="text"
  40. shape="circle"
  41. :placeholder="t(loginType === 'phone' ? '手机号' : '电子邮件')"
  42. v-model="form.userphone"
  43. >
  44. <template #prefix>
  45. <view
  46. @click="areaClick"
  47. v-if="useSystem.getCountryCodes.length > 0"
  48. >
  49. <Area
  50. codeNum
  51. v-model="areacode"
  52. ref="areaRef"
  53. @confirm="areaConfirm"
  54. v-show="loginType === 'phone'"
  55. />
  56. </view>
  57. <view class="prefix">
  58. <image
  59. src="@/static/login/email.png"
  60. class="img"
  61. mode="widthFix"
  62. v-show="loginType === 'email'"
  63. ></image>
  64. </view>
  65. </template>
  66. </up-input>
  67. </up-form-item>
  68. <up-form-item prop="userpass">
  69. <up-input
  70. type="password"
  71. shape="circle"
  72. :placeholder="t('密码')"
  73. v-model="form.userpass"
  74. >
  75. <template #prefix>
  76. <view class="prefix">
  77. <image
  78. src="@/static/login/password.png"
  79. class="img"
  80. mode="widthFix"
  81. ></image>
  82. </view>
  83. </template>
  84. </up-input>
  85. </up-form-item>
  86. </up-form>
  87. <view class="confirm_box">
  88. <up-checkbox
  89. activeColor="var(--black)"
  90. labelSize="14"
  91. labelColor="#676969"
  92. iconSize="16"
  93. name="agree"
  94. usedAlone
  95. v-model:checked="selectAllChecked"
  96. class="right-check"
  97. >
  98. <template #label>
  99. <trans class="text" _t="我已阅读并同意" />
  100. <trans
  101. _t="用户协议"
  102. class="agent"
  103. @click.stop="openAgent('agree')"
  104. />
  105. <trans _t="和" />
  106. <trans
  107. _t="隐私协议"
  108. class="agent"
  109. @click.stop="openAgent('private')"
  110. />
  111. </template>
  112. </up-checkbox>
  113. </view>
  114. <view class="label_texts" @click="goTo('/pages/login/forget')">
  115. <trans _t="忘记密码" />
  116. </view>
  117. <up-button
  118. :text="t('登录')"
  119. class="btn"
  120. color="var(--light)"
  121. @click="submit"
  122. ></up-button>
  123. <view class="tips">
  124. <view class="line"></view>
  125. <trans class="tips_text" _t="或者" />
  126. <view class="line"></view>
  127. </view>
  128. <template v-if="!isAppIOS">
  129. <view class="tip_text">
  130. <trans _t="tips_text" />
  131. </view>
  132. <view class="btns">
  133. <FacebookLogin @success="facebookLoginSuccess" />
  134. <!-- #ifdef H5 -->
  135. <GoogleLogin @success="handleGoogleSuccess" />
  136. <!-- #endif -->
  137. </view>
  138. </template>
  139. <view class="labels">
  140. <view class="label_text" @click="goTo('/pages/login/register')">
  141. <trans _t="没有账号" />
  142. <trans class="register" _t="注册" />
  143. </view>
  144. </view>
  145. </view>
  146. </view>
  147. <SimpleAgreementModal ref="agreementModal" />
  148. </Theme>
  149. </template>
  150. <script setup>
  151. import { reactive, ref, onMounted, computed } from "vue";
  152. import { t } from "@/locale";
  153. import Navbar from "@/components/navbar";
  154. import plaCard from "@/components/placard";
  155. import Area from "@/components/area";
  156. import { useUserStore, useSystemStore, useTabbarStore } from "@/store";
  157. import { USERS_THIRD_LOGIN } from "@/api";
  158. import { Toast, setStorage, removeStorage, getStorage } from "@/utils";
  159. import verConfig from "@/ver.config";
  160. import { getCurrentInstance } from "vue";
  161. import FacebookLogin from "./components/facebook-login.vue";
  162. import GoogleLogin from "./components/google-login.vue";
  163. import SimpleAgreementModal from "@/components/simpleAgreementModal";
  164. const { proxy } = getCurrentInstance();
  165. const adjustPosition = proxy.$adjustPosition;
  166. const isAppIOS = ref(false);
  167. const useUser = useUserStore();
  168. const useSystem = useSystemStore();
  169. const useTabbar = useTabbarStore();
  170. const formRef = ref(null);
  171. const selectAllChecked = ref(false);
  172. const agreementModal = ref(null);
  173. const loginType = ref("phone");
  174. const areaRef = ref(null);
  175. const areacode = ref("");
  176. const areaClick = () => {
  177. areaRef.value && areaRef.value.open();
  178. };
  179. const form = reactive({
  180. userphone: "",
  181. userpass: "",
  182. });
  183. const areaConfirm = (item) => {
  184. // console.log(item, areacode.value, "areaConfirm");
  185. };
  186. const getRules = () => ({
  187. userphone: {
  188. required: true,
  189. message: t(
  190. loginType.value === "phone" ? "手机号为必填项" : "电子邮件为必填项"
  191. ),
  192. trigger: ["change"],
  193. },
  194. userpass: {
  195. required: true,
  196. message: t("密码是必需的"),
  197. trigger: ["change"],
  198. },
  199. });
  200. const handleGoogleSuccess = (userInfo) => {
  201. getUserInfo(userInfo, "google");
  202. };
  203. const facebookLoginSuccess = (userInfo) => {
  204. getUserInfo(userInfo, "facebook");
  205. };
  206. const getUserInfo = async (info, platform) => {
  207. try {
  208. let para = {
  209. platform: platform,
  210. };
  211. if (platform === "facebook") {
  212. para.accessToken = info.accessToken;
  213. para.userID = info.userID;
  214. } else if (platform === "google") {
  215. para.id_token = info.credential;
  216. }
  217. const res = await USERS_THIRD_LOGIN(para);
  218. setStorage(verConfig.tokenName, res.data.token);
  219. setStorage(verConfig.infoName, res.data);
  220. Toast(res.msg, 1000).then(() => {
  221. let timer = setTimeout(() => {
  222. const backUrl = getStorage("beforeLoginPage");
  223. if (backUrl) {
  224. removeStorage("beforeLoginPage");
  225. const arr = [
  226. "/pages/index/index",
  227. "/pages/shop/cart",
  228. "/pages/community/index",
  229. "/pages/order/index",
  230. "/pages/user/index",
  231. "/pages/chat/index",
  232. ];
  233. const hasAny = arr.some((s) => backUrl.includes(s));
  234. if (hasAny) {
  235. uni.switchTab({ url: backUrl });
  236. } else {
  237. uni.redirectTo({ url: backUrl });
  238. }
  239. } else {
  240. uni.switchTab({ url: "/pages/index/index" });
  241. useTabbar.getPageCur("index");
  242. }
  243. clearTimeout(timer);
  244. }, 1000);
  245. });
  246. } catch (error) {
  247. Toast(error.msg);
  248. close();
  249. }
  250. };
  251. const openAgent = (type) => {
  252. agreementModal.value && agreementModal.value.open(type);
  253. };
  254. const leftClick = () => {
  255. uni.switchTab({ url: "/pages/index/index" });
  256. useTabbar.getPageCur("index");
  257. };
  258. const changeTab = (type) => {
  259. loginType.value = type;
  260. form.userphone = "";
  261. formRef.value.clearValidate();
  262. };
  263. onMounted(() => {
  264. // #ifdef APP-PLUS
  265. const sys = uni.getSystemInfoSync();
  266. isAppIOS.value = sys.platform === "ios";
  267. // #endif
  268. useUser.loginOut(true);
  269. // initGoogleSignIn()
  270. });
  271. const service = () => {
  272. useSystem.service();
  273. };
  274. const goTo = (url) => {
  275. uni.navigateTo({ url });
  276. };
  277. const submit = () => {
  278. formRef.value
  279. .validate()
  280. .then((valid) => {
  281. if (!selectAllChecked.value) {
  282. return Toast(t("请阅读并同意"));
  283. }
  284. const para = JSON.parse(JSON.stringify(form));
  285. if (loginType.value == "phone") {
  286. para.userphone = areacode.value + para.userphone;
  287. }
  288. useUser.login(para);
  289. })
  290. .catch(() => {});
  291. };
  292. </script>
  293. <style lang="less" scoped>
  294. @import url("@/style.less");
  295. @cut-size: 24rpx;
  296. .wrap {
  297. min-height: 100vh;
  298. background: var(--light);
  299. .content {
  300. padding: 40rpx 60rpx;
  301. .logo {
  302. .flex_center();
  303. padding-bottom: 48rpx;
  304. &_img {
  305. width: 284rpx;
  306. }
  307. }
  308. .login_title {
  309. color: var(--black);
  310. .size();
  311. position: relative;
  312. width: fit-content;
  313. line-height: 44rpx;
  314. padding-bottom: 10rpx;
  315. &::before {
  316. position: absolute;
  317. content: "";
  318. left: 0;
  319. bottom: 0;
  320. width: 75%;
  321. height: 2px;
  322. background-color: var(--black);
  323. }
  324. }
  325. .tab-bar {
  326. display: flex;
  327. margin-bottom: 48rpx;
  328. .tab-item {
  329. position: relative;
  330. width: 50%;
  331. height: 100rpx;
  332. line-height: 100rpx;
  333. text-align: center;
  334. font-size: 28rpx;
  335. color: var(--black);
  336. background: url("@/static/login/left.png");
  337. background-size: 100% 100%;
  338. &.active {
  339. background: url("@/static/login/left_active.png");
  340. background-size: 100% 100%;
  341. color: #fff;
  342. }
  343. &:not(:first-child) {
  344. background: url("@/static/login/right.png");
  345. background-size: 100% 100%;
  346. &.active {
  347. background: url("@/static/login/right_active.png");
  348. background-size: 100% 100%;
  349. color: #fff;
  350. }
  351. }
  352. }
  353. }
  354. /deep/ .u-form {
  355. margin-top: 16px;
  356. }
  357. /deep/ .u-form-item {
  358. padding-bottom: 10px;
  359. .u-form-item__body {
  360. padding: 0;
  361. }
  362. .u-form-item__body__right__message {
  363. margin-top: 10rpx;
  364. }
  365. .u-form-item__body__right__content {
  366. .u-input {
  367. padding: 16rpx 24rpx 16rpx !important;
  368. // border-radius: 16rpx !important;
  369. background-color: var(--bg);
  370. border-color: #d8d8d8 !important;
  371. // &:hover {
  372. // box-shadow: 0 0 0 1px var(--primary) inset;
  373. // }
  374. }
  375. .u-input__content__field-wrapper__field {
  376. height: 32px;
  377. line-height: 32px;
  378. .uni-input-input {
  379. font-size: 28rpx !important;
  380. }
  381. .input-placeholder {
  382. color: var(--text-01) !important;
  383. font-size: 24rpx;
  384. }
  385. }
  386. }
  387. }
  388. .phone_box {
  389. display: flex;
  390. align-items: center;
  391. margin-right: 18rpx;
  392. }
  393. .prefix {
  394. display: flex;
  395. align-items: center;
  396. width: 40rpx;
  397. margin-right: 18rpx;
  398. .img {
  399. width: inherit;
  400. display: block;
  401. }
  402. }
  403. .btn {
  404. color: var(--light);
  405. background-color: var(--black) !important;
  406. border: 1px solid var(--black) !important;
  407. padding: 16rpx 30rpx;
  408. height: 100rpx;
  409. border-radius: 100px;
  410. margin-bottom: 24rpx;
  411. color: var(--light);
  412. /deep/ .u-button__text {
  413. font-size: 32rpx !important;
  414. font-weight: 700;
  415. }
  416. }
  417. .label_texts {
  418. text-align: right;
  419. padding: 4px 0 14px;
  420. font-size: 24rpx;
  421. line-height: 44rpx;
  422. color: var(--text-01);
  423. }
  424. .confirm_box {
  425. display: flex;
  426. .agent {
  427. margin: 0 6rpx;
  428. color: var(--red);
  429. text-decoration: underline;
  430. }
  431. }
  432. .tips {
  433. display: flex;
  434. align-items: center;
  435. .line {
  436. flex: 1;
  437. border: 1px solid #d8d8d8;
  438. }
  439. .tips_text {
  440. .size(24rpx);
  441. font-weight: 500;
  442. color: #d8d8d8;
  443. margin: 0 10rpx;
  444. line-height: 44rpx;
  445. }
  446. }
  447. .tip_text {
  448. color: var(--text-01);
  449. .size(20rpx);
  450. line-height: 44rpx;
  451. text-align: center;
  452. }
  453. .btns {
  454. .ver();
  455. flex-direction: column;
  456. &_item {
  457. width: 400rpx;
  458. border: 1px solid var(--black);
  459. border-radius: 100px;
  460. padding: 14rpx 52rpx;
  461. margin-top: 18rpx;
  462. .ver();
  463. color: var(--text-02);
  464. .size(24rpx);
  465. .btn_icon {
  466. width: 48rpx;
  467. margin-right: 52rpx;
  468. .img {
  469. width: inherit;
  470. display: block;
  471. }
  472. }
  473. }
  474. }
  475. .labels {
  476. margin-top: 24rpx;
  477. padding-bottom: 40rpx;
  478. .flex_center();
  479. .label_text {
  480. cursor: pointer;
  481. color: var(--text-01);
  482. .size(24rpx);
  483. line-height: 44rpx;
  484. .register {
  485. .size(28rpx);
  486. color: var(--black);
  487. }
  488. }
  489. }
  490. }
  491. }
  492. </style>