index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <template>
  2. <div class="login-wrapper">
  3. <div
  4. class="back_extend"
  5. v-if="appStore?.$wxConfig?.auditModeEnabled"
  6. @click="backHome"
  7. >
  8. {{ "<" }} 回到首页
  9. </div>
  10. <div class="shading">
  11. <image :src="logoUrl" />
  12. <!-- <image src="/static/images/logo2.png" v-if="!logoUrl" /> -->
  13. </div>
  14. <div class="whiteBg" v-if="formItem === 1">
  15. <div class="list" v-if="current !== 1">
  16. <form @submit.prevent="submit">
  17. <div class="item">
  18. <div class="acea-row row-middle">
  19. <image
  20. src="https://sb-admin.oss-cn-shenzhen.aliyuncs.com/crmebimage/public/maintain/2025/08/28/phone_1.png"
  21. style="width: 24rpx; height: 34rpx"
  22. ></image>
  23. <input
  24. type="text"
  25. class="texts"
  26. placeholder="输入手机号码"
  27. v-model="account"
  28. required
  29. />
  30. </div>
  31. </div>
  32. <div class="item">
  33. <div class="acea-row row-middle">
  34. <image
  35. src="https://sb-admin.oss-cn-shenzhen.aliyuncs.com/crmebimage/public/maintain/2025/08/28/1cfaad5f37a64884b3eef985d254606eqzlswf1m9g.png"
  36. style="width: 28rpx; height: 32rpx"
  37. ></image>
  38. <input
  39. type="password"
  40. class="texts"
  41. placeholder="填写登录密码"
  42. v-model="password"
  43. required
  44. />
  45. </div>
  46. </div>
  47. </form>
  48. </div>
  49. <div
  50. class="list"
  51. v-if="current !== 0 || appLoginStatus || appleLoginStatus"
  52. >
  53. <div class="item">
  54. <div class="acea-row row-middle">
  55. <image
  56. src="https://sb-admin.oss-cn-shenzhen.aliyuncs.com/crmebimage/public/maintain/2025/08/28/phone_1.png"
  57. style="width: 24rpx; height: 34rpx"
  58. ></image>
  59. <input
  60. type="text"
  61. class="texts"
  62. placeholder="输入手机号码"
  63. v-model="account"
  64. />
  65. </div>
  66. </div>
  67. <div class="item">
  68. <div class="acea-row row-middle">
  69. <image
  70. src="https://sb-admin.oss-cn-shenzhen.aliyuncs.com/crmebimage/public/maintain/2025/08/28/1cfaad5f37a64884b3eef985d254606eqzlswf1m9g.png"
  71. style="width: 28rpx; height: 32rpx"
  72. ></image>
  73. <input
  74. type="text"
  75. placeholder="填写验证码"
  76. class="codeIput"
  77. v-model="captcha"
  78. />
  79. <div
  80. class="code"
  81. :disabled="disabled"
  82. :class="disabled === true ? 'on' : ''"
  83. @click="getCode"
  84. >
  85. {{ text }}
  86. </div>
  87. </div>
  88. </div>
  89. <div class="item">
  90. <div class="text-row row-middle">
  91. 新用户可使用验证码登录,将自动注册账号
  92. </div>
  93. <div class="text-row row-middle" v-if="invite_code">
  94. 邀请码:{{ invite_code }}
  95. </div>
  96. </div>
  97. <div class="item" v-if="isShowCode">
  98. <div class="acea-row row-middle">
  99. <image
  100. src="https://sb-admin.oss-cn-shenzhen.aliyuncs.com/crmebimage/public/maintain/2025/08/28/1cfaad5f37a64884b3eef985d254606eqzlswf1m9g.png"
  101. style="width: 28rpx; height: 32rpx"
  102. ></image>
  103. <input
  104. type="text"
  105. placeholder="填写验证码"
  106. class="codeIput"
  107. v-model="codeVal"
  108. />
  109. <div class="code" @click="again"><img :src="codeUrl" /></div>
  110. </div>
  111. </div>
  112. </div>
  113. <div class="tips">
  114. <div v-if="current == 0" @click="current = 1">快速登录</div>
  115. <div v-if="current == 1" @click="current = 0">账号登录</div>
  116. <div @click="change_password">忘记密码</div>
  117. </div>
  118. <div class="logon" @click="loginMobile" v-if="current !== 0">登录</div>
  119. <div class="logon" @click="submit" v-if="current === 0">登录</div>
  120. </div>
  121. <div class="bottom"></div>
  122. </div>
  123. </template>
  124. <script setup>
  125. import { ref, watch, onMounted } from "vue";
  126. import { onLoad, onBackPress as uniOnBackPress } from "@dcloudio/uni-app";
  127. import { useAppStore } from "@/stores/app";
  128. import {
  129. loginH5,
  130. loginMobile as loginMobileApi,
  131. registerVerify,
  132. register,
  133. getUserInfo,
  134. getUserOpenId,
  135. } from "@/api/user";
  136. import { getLogo, appAuth, appleLogin } from "@/api/public";
  137. import { VUE_APP_API_URL } from "@/utils";
  138. import { useSendCode } from "@/hooks/useSendCode";
  139. import Cache from "@/utils/cache";
  140. import { EXPIRES_TIME } from "@/config/cache";
  141. import { useToast } from "@/hooks/useToast.js";
  142. const appStore = useAppStore();
  143. const { Toast } = useToast();
  144. const BACK_URL = "login_back_url";
  145. // Reactive state
  146. const navList = ref(["快速登录", "账号登录"]);
  147. const current = ref(1);
  148. const account = ref("");
  149. const password = ref("");
  150. const captcha = ref("");
  151. const formItem = ref(1);
  152. const type = ref("login");
  153. const logoUrl = ref("");
  154. const keyCode = ref("");
  155. const codeUrl = ref("");
  156. const codeVal = ref("");
  157. const isShowCode = ref(false);
  158. const platform = ref("");
  159. const appLoginStatus = ref(false);
  160. const appUserInfo = ref(null);
  161. const appleLoginStatus = ref(false);
  162. const appleUserInfo = ref(null);
  163. const appleShow = ref(false);
  164. const invite_code = ref("");
  165. // Watch formItem to update type
  166. watch(formItem, (newVal) => {
  167. type.value = newVal === 1 ? "login" : "register";
  168. });
  169. const { disabled, text, sendCode } = useSendCode();
  170. // Refresh captcha image
  171. const again = () => {
  172. codeUrl.value = `${VUE_APP_API_URL}/sms_captcha?key=${
  173. keyCode.value
  174. }${Date.parse(new Date())}`;
  175. };
  176. // Get logo image
  177. const getLogoImage = async () => {
  178. try {
  179. const res = await getLogo();
  180. console.log('=========')
  181. logoUrl.value = res.data.logoUrl || "/static/images/logo2.png";
  182. } catch (err) {
  183. console.error(err);
  184. }
  185. };
  186. const getWechatOpenid = async () => {
  187. try {
  188. const { code, errMsg } = await uni.login();
  189. if (!errMsg) return uni.showToast({ title: errMsg, icon: "none" }); // debug
  190. // if (!code) return uni.showToast({ title: "无code", icon: "none" }); // debug
  191. console.log(code)
  192. const res = await getUserOpenId({ code });
  193. if (res.data?.code === 200 && res.data?.data?.openid) {
  194. const openid = res.data.data.openid;
  195. Cache.set("wxOpenid", openid);
  196. appStore.SET_WX_OPENID(openid);
  197. }
  198. } catch (err) {
  199. console.error("获取openid过程出错:", err);
  200. }
  201. };
  202. // Mobile login
  203. const loginMobile = async () => {
  204. if (!account.value)
  205. return uni.showToast({ title: "请填写手机号码", icon: "none" });
  206. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(account.value))
  207. return uni.showToast({ title: "请输入正确的手机号码", icon: "none" });
  208. if (!captcha.value)
  209. return uni.showToast({ title: "请填写验证码", icon: "none" });
  210. if (!/^[\w\d]+$/i.test(captcha.value))
  211. return uni.showToast({ title: "请输入正确的验证码", icon: "none" });
  212. try {
  213. const res = await loginMobileApi({
  214. phone: account.value,
  215. captcha: captcha.value,
  216. spread_spid: Cache.get("spread"),
  217. invite_code: invite_code.value,
  218. });
  219. // 使用 LOGIN action 保存 token 到缓存
  220. appStore.LOGIN({ token: res.data.token });
  221. // 保存过期时间(7天后过期)
  222. const expiresTime = Math.round(new Date() / 1000) + 7 * 24 * 60 * 60;
  223. Cache.set(EXPIRES_TIME, expiresTime, 0);
  224. await getUserInfoFn(res.data);
  225. } catch (res) {
  226. uni.showToast({ title: res.message, icon: "none" });
  227. }
  228. };
  229. // Register
  230. const registerFn = async () => {
  231. if (!account.value)
  232. return uni.showToast({ title: "请填写手机号码", icon: "none" });
  233. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(account.value))
  234. return uni.showToast({ title: "请输入正确的手机号码", icon: "none" });
  235. if (!captcha.value)
  236. return uni.showToast({ title: "请填写验证码", icon: "none" });
  237. if (!/^[\w\d]+$/i.test(captcha.value))
  238. return uni.showToast({ title: "请输入正确的验证码", icon: "none" });
  239. if (!password.value)
  240. return uni.showToast({ title: "请填写密码", icon: "none" });
  241. if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/i.test(password.value))
  242. return uni.showToast({ title: "您输入的密码过于简单", icon: "none" });
  243. try {
  244. const res = await register({
  245. account: account.value,
  246. captcha: captcha.value,
  247. password: password.value,
  248. spread: Cache.get("spread"),
  249. });
  250. uni.showToast({ title: res.message, icon: "none" });
  251. formItem.value = 1;
  252. } catch (res) {
  253. uni.showToast({ title: res.message, icon: "none" });
  254. }
  255. };
  256. // Send verification code
  257. const getCode = async () => {
  258. if (!account.value)
  259. return uni.showToast({ title: "请填写手机号码", icon: "none" });
  260. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(account.value))
  261. return uni.showToast({ title: "请输入正确的手机号码", icon: "none" });
  262. if (formItem.value === 2) type.value = "register";
  263. try {
  264. const res = await registerVerify(account.value);
  265. uni.showToast({ title: res.message, icon: "none" });
  266. sendCode();
  267. } catch (err) {
  268. uni.showToast({ title: err.message, icon: "none" });
  269. }
  270. };
  271. // Navigation tab switch
  272. const navTap = (index) => {
  273. current.value = index;
  274. };
  275. // h5 login
  276. const submit = async () => {
  277. if (!account.value) return Toast({ title: "请填写账号", icon: "none" });
  278. if (!/^[\w\d]{5,16}$/i.test(account.value))
  279. return Toast({ title: "请输入正确的账号", icon: "none" });
  280. if (!password.value) return Toast({ title: "请填写密码", icon: "none" });
  281. try {
  282. const { data } = await loginH5({
  283. account: account.value,
  284. password: password.value,
  285. spread: Cache.get("spread"),
  286. });
  287. appStore.LOGIN({ token: data.token });
  288. // 保存过期时间(7天后过期)
  289. const expiresTime = Math.round(new Date() / 1000) + 7 * 24 * 60 * 60;
  290. Cache.set(EXPIRES_TIME, expiresTime, 0);
  291. await getUserInfoFn(data);
  292. } catch (err) {
  293. console.log("submit error", err);
  294. Toast({ title: err, icon: "none" });
  295. }
  296. };
  297. const getUserInfoFn = async (data) => {
  298. try {
  299. appStore.SETUID(data.uid);
  300. await getWechatOpenid();
  301. const res = await getUserInfo();
  302. appStore.UPDATE_USERINFO(res.data);
  303. Toast({ title: "登录成功" });
  304. backHome();
  305. } catch (err) {
  306. console.error(err);
  307. uni.showToast({ title: err.msg, icon: "none" });
  308. }
  309. };
  310. onLoad((options) => {
  311. if (options?.invite_code) {
  312. invite_code.value = options.invite_code;
  313. uni.setStorageSync("inviteCode", options.invite_code);
  314. } else {
  315. invite_code.value = uni.getStorageSync("inviteCode") || "";
  316. }
  317. getLogoImage();
  318. });
  319. onMounted(() => {
  320. uni.getDeviceInfo({
  321. success(res) {
  322. // 获取平台(如 "ios"、"android")
  323. platform.value = res.platform.toLowerCase();
  324. // 解析系统版本(以 iOS 为例,res.system 格式如 "iOS 16.5")
  325. if (platform.value === "ios") {
  326. const systemParts = res.system.split(" ");
  327. if (systemParts.length > 1) {
  328. const iosVersion = parseFloat(systemParts[1]);
  329. // iOS 版本 >= 13 时显示 Apple 登录相关内容
  330. if (iosVersion >= 13) {
  331. appleShow.value = true;
  332. }
  333. }
  334. }
  335. },
  336. fail(err) {
  337. console.error("获取设备信息失败:", err);
  338. },
  339. });
  340. });
  341. const backHome = () => {
  342. uni.switchTab({
  343. url: "/pages/index/index",
  344. });
  345. };
  346. const change_password = () => {
  347. uni.navigateTo({
  348. url: "/pages/change_password/change_password",
  349. });
  350. };
  351. </script>
  352. <style lang="scss" scoped>
  353. page {
  354. background: #fff;
  355. }
  356. .appLogin {
  357. margin-top: 60rpx;
  358. .hds {
  359. display: flex;
  360. justify-content: center;
  361. align-items: center;
  362. font-size: 24rpx;
  363. color: #b4b4b4;
  364. .line {
  365. width: 68rpx;
  366. height: 1rpx;
  367. background: #cccccc;
  368. }
  369. p {
  370. margin: 0 20rpx;
  371. }
  372. }
  373. .btn-wrapper {
  374. display: flex;
  375. align-items: center;
  376. justify-content: center;
  377. margin-top: 30rpx;
  378. .btn {
  379. display: flex;
  380. align-items: center;
  381. justify-content: center;
  382. width: 68rpx;
  383. height: 68rpx;
  384. border-radius: 50%;
  385. }
  386. .apple-btn {
  387. display: flex;
  388. align-items: center;
  389. justify-content: center;
  390. margin-left: 30rpx;
  391. background: #000;
  392. border-radius: 34rpx;
  393. font-size: 40rpx;
  394. .icon-s-pingguo {
  395. color: #fff;
  396. font-size: 40rpx;
  397. }
  398. }
  399. .iconfont {
  400. font-size: 40rpx;
  401. color: #fff;
  402. }
  403. .wx {
  404. margin-right: 30rpx;
  405. background-color: #61c64f;
  406. }
  407. .mima {
  408. background-color: #28b3e9;
  409. }
  410. .yanzheng {
  411. background-color: #f89c23;
  412. }
  413. }
  414. }
  415. .code img {
  416. width: 100%;
  417. height: 100%;
  418. }
  419. .acea-row.row-middle {
  420. input {
  421. margin-left: 20rpx;
  422. display: block;
  423. }
  424. }
  425. .text-row {
  426. font-size: 22rpx;
  427. color: #999;
  428. }
  429. .login-wrapper {
  430. padding: 0 30rpx;
  431. height: 100vh;
  432. .back_extend {
  433. position: fixed;
  434. top: 50px;
  435. left: 10px;
  436. font-size: 26rpx;
  437. color: #999;
  438. }
  439. .shading {
  440. display: flex;
  441. align-items: center;
  442. justify-content: center;
  443. width: 100%;
  444. /* #ifdef APP-VUE */
  445. margin-top: 50rpx;
  446. /* #endif */
  447. /* #ifndef APP-VUE */
  448. margin-top: 200rpx;
  449. /* #endif */
  450. image {
  451. width: 180rpx;
  452. height: 180rpx;
  453. }
  454. }
  455. .whiteBg {
  456. margin-top: 100rpx;
  457. .list {
  458. border-radius: 16rpx;
  459. overflow: hidden;
  460. .item {
  461. border-bottom: 1px solid #f0f0f0;
  462. background: #fff;
  463. .row-middle {
  464. position: relative;
  465. padding: 16rpx 45rpx;
  466. .texts {
  467. flex: 1;
  468. font-size: 28rpx;
  469. height: 80rpx;
  470. line-height: 80rpx;
  471. display: flex;
  472. justify-content: center;
  473. align-items: center;
  474. }
  475. input {
  476. flex: 1;
  477. font-size: 28rpx;
  478. height: 80rpx;
  479. line-height: 80rpx;
  480. display: flex;
  481. justify-content: center;
  482. align-items: center;
  483. }
  484. .code {
  485. position: absolute;
  486. right: 30rpx;
  487. top: 50%;
  488. color: #cd9933;
  489. font-size: 26rpx;
  490. z-index: 999;
  491. transform: translateY(-50%);
  492. border: 1px solid;
  493. border-radius: 14rpx;
  494. padding: 8rpx 10rpx;
  495. }
  496. }
  497. }
  498. }
  499. .logon {
  500. display: flex;
  501. align-items: center;
  502. justify-content: center;
  503. width: 100%;
  504. height: 86rpx;
  505. margin-top: 80rpx;
  506. background-color: $theme-color;
  507. border-radius: 120rpx;
  508. color: #ffffff;
  509. font-size: 30rpx;
  510. }
  511. .tips {
  512. margin: 0.9375rem;
  513. text-align: center;
  514. color: #999;
  515. display: flex;
  516. justify-content: space-between;
  517. align-items: center;
  518. }
  519. }
  520. }
  521. </style>