index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. import { getSceneInfo } from '@/utils/util.js';
  143. import { footprintScan } from "@/api/merchant.js";
  144. const appStore = useAppStore();
  145. const { Toast } = useToast();
  146. const BACK_URL = "login_back_url";
  147. // Reactive state
  148. const navList = ref(["快速登录", "账号登录"]);
  149. const current = ref(1);
  150. const account = ref("");
  151. const password = ref("");
  152. const captcha = ref("");
  153. const formItem = ref(1);
  154. const type = ref("login");
  155. const logoUrl = ref("");
  156. const keyCode = ref("");
  157. const codeUrl = ref("");
  158. const codeVal = ref("");
  159. const isShowCode = ref(false);
  160. const platform = ref("");
  161. const appLoginStatus = ref(false);
  162. const appUserInfo = ref(null);
  163. const appleLoginStatus = ref(false);
  164. const appleUserInfo = ref(null);
  165. const appleShow = ref(false);
  166. const invite_code = ref("");
  167. const merchantId = ref('');
  168. const userInfo = ref({});
  169. // Watch formItem to update type
  170. watch(formItem, (newVal) => {
  171. type.value = newVal === 1 ? "login" : "register";
  172. });
  173. const { disabled, text, sendCode } = useSendCode();
  174. // Refresh captcha image
  175. const again = () => {
  176. codeUrl.value = `${VUE_APP_API_URL}/sms_captcha?key=${
  177. keyCode.value
  178. }${Date.parse(new Date())}`;
  179. };
  180. // Get logo image
  181. const getLogoImage = async () => {
  182. try {
  183. const res = await getLogo();
  184. console.log('=========')
  185. logoUrl.value = res.data.logoUrl || "/static/images/logo2.png";
  186. } catch (err) {
  187. console.error(err);
  188. }
  189. };
  190. const getWechatOpenid = async () => {
  191. try {
  192. const { code, errMsg } = await uni.login();
  193. if (!errMsg) return uni.showToast({ title: errMsg, icon: "none" }); // debug
  194. // if (!code) return uni.showToast({ title: "无code", icon: "none" }); // debug
  195. console.log(code)
  196. const res = await getUserOpenId({ code });
  197. if (res.data?.code === 200 && res.data?.data?.openid) {
  198. const openid = res.data.data.openid;
  199. Cache.set("wxOpenid", openid);
  200. appStore.SET_WX_OPENID(openid);
  201. }
  202. } catch (err) {
  203. console.error("获取openid过程出错:", err);
  204. }
  205. };
  206. // Mobile login
  207. const loginMobile = async () => {
  208. if (!account.value)
  209. return uni.showToast({ title: "请填写手机号码", icon: "none" });
  210. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(account.value))
  211. return uni.showToast({ title: "请输入正确的手机号码", icon: "none" });
  212. if (!captcha.value)
  213. return uni.showToast({ title: "请填写验证码", icon: "none" });
  214. if (!/^[\w\d]+$/i.test(captcha.value))
  215. return uni.showToast({ title: "请输入正确的验证码", icon: "none" });
  216. try {
  217. const res = await loginMobileApi({
  218. phone: account.value,
  219. captcha: captcha.value,
  220. spread_spid: Cache.get("spread"),
  221. invite_code: invite_code.value,
  222. });
  223. // 使用 LOGIN action 保存 token 到缓存
  224. appStore.LOGIN({ token: res.data.token });
  225. // 保存过期时间(7天后过期)
  226. const expiresTime = Math.round(new Date() / 1000) + 7 * 24 * 60 * 60;
  227. Cache.set(EXPIRES_TIME, expiresTime, 0);
  228. await getUserInfoFn(res.data);
  229. } catch (res) {
  230. uni.showToast({ title: res.message, icon: "none" });
  231. }
  232. };
  233. // Register
  234. const registerFn = async () => {
  235. if (!account.value)
  236. return uni.showToast({ title: "请填写手机号码", icon: "none" });
  237. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(account.value))
  238. return uni.showToast({ title: "请输入正确的手机号码", icon: "none" });
  239. if (!captcha.value)
  240. return uni.showToast({ title: "请填写验证码", icon: "none" });
  241. if (!/^[\w\d]+$/i.test(captcha.value))
  242. return uni.showToast({ title: "请输入正确的验证码", icon: "none" });
  243. if (!password.value)
  244. return uni.showToast({ title: "请填写密码", icon: "none" });
  245. if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/i.test(password.value))
  246. return uni.showToast({ title: "您输入的密码过于简单", icon: "none" });
  247. try {
  248. const res = await register({
  249. account: account.value,
  250. captcha: captcha.value,
  251. password: password.value,
  252. spread: Cache.get("spread"),
  253. });
  254. uni.showToast({ title: res.message, icon: "none" });
  255. formItem.value = 1;
  256. } catch (res) {
  257. uni.showToast({ title: res.message, icon: "none" });
  258. }
  259. };
  260. // Send verification code
  261. const getCode = async () => {
  262. if (!account.value)
  263. return uni.showToast({ title: "请填写手机号码", icon: "none" });
  264. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(account.value))
  265. return uni.showToast({ title: "请输入正确的手机号码", icon: "none" });
  266. if (formItem.value === 2) type.value = "register";
  267. try {
  268. const res = await registerVerify(account.value);
  269. uni.showToast({ title: res.message, icon: "none" });
  270. sendCode();
  271. } catch (err) {
  272. uni.showToast({ title: err.message, icon: "none" });
  273. }
  274. };
  275. // Navigation tab switch
  276. const navTap = (index) => {
  277. current.value = index;
  278. };
  279. // h5 login
  280. const submit = async () => {
  281. if (!account.value) return Toast({ title: "请填写账号", icon: "none" });
  282. if (!/^[\w\d]{5,16}$/i.test(account.value))
  283. return Toast({ title: "请输入正确的账号", icon: "none" });
  284. if (!password.value) return Toast({ title: "请填写密码", icon: "none" });
  285. try {
  286. const { data } = await loginH5({
  287. account: account.value,
  288. password: password.value,
  289. spread: Cache.get("spread"),
  290. });
  291. appStore.LOGIN({ token: data.token });
  292. // 保存过期时间(7天后过期)
  293. const expiresTime = Math.round(new Date() / 1000) + 7 * 24 * 60 * 60;
  294. Cache.set(EXPIRES_TIME, expiresTime, 0);
  295. await getUserInfoFn(data);
  296. } catch (err) {
  297. console.log("submit error", err);
  298. Toast({ title: err, icon: "none" });
  299. }
  300. };
  301. const getUserInfoFn = async (data) => {
  302. try {
  303. appStore.SETUID(data.uid);
  304. // await getWechatOpenid();
  305. const res = await getUserInfo();
  306. appStore.UPDATE_USERINFO(res.data);
  307. userInfo.value = res.data;
  308. if(merchantId.value != ''){
  309. let obj ={
  310. merchantId:merchantId.value,
  311. userId:res.data.userId
  312. }
  313. await footprintScanFn(obj)
  314. }else{
  315. appStore.UPDATE_MERCHANT_ID('')
  316. Toast({ title: "登录成功" });
  317. backHome();
  318. }
  319. } catch (err) {
  320. console.error(err);
  321. uni.showToast({ title: err.msg, icon: "none" });
  322. }
  323. };
  324. onLoad((options) => {
  325. const params = getSceneInfo(options);
  326. merchantId.value = params.merchantId || '';
  327. getLogoImage();
  328. });
  329. onMounted(() => {
  330. uni.getDeviceInfo({
  331. success(res) {
  332. // 获取平台(如 "ios"、"android")
  333. platform.value = res.platform.toLowerCase();
  334. // 解析系统版本(以 iOS 为例,res.system 格式如 "iOS 16.5")
  335. if (platform.value === "ios") {
  336. const systemParts = res.system.split(" ");
  337. if (systemParts.length > 1) {
  338. const iosVersion = parseFloat(systemParts[1]);
  339. // iOS 版本 >= 13 时显示 Apple 登录相关内容
  340. if (iosVersion >= 13) {
  341. appleShow.value = true;
  342. }
  343. }
  344. }
  345. },
  346. fail(err) {
  347. console.error("获取设备信息失败:", err);
  348. },
  349. });
  350. });
  351. const backHome = () => {
  352. if(userInfo.value.merchant && userInfo.value.merchant.id){
  353. uni.navigateTo({
  354. url:"/pages/merchantCenter/index"
  355. })
  356. }else{
  357. uni.switchTab({
  358. url: "/pages/index/index",
  359. });
  360. }
  361. };
  362. const change_password = () => {
  363. uni.navigateTo({
  364. url: "/pages/change_password/change_password",
  365. });
  366. };
  367. const footprintScanFn =async (data) => {
  368. const res = await footprintScan(data);
  369. Toast({ title: "登录成功" });
  370. backHome();
  371. }
  372. </script>
  373. <style>
  374. page {
  375. background: #fff !important;
  376. }
  377. </style>
  378. <style lang="scss" scoped>
  379. .appLogin {
  380. margin-top: 60rpx;
  381. .hds {
  382. display: flex;
  383. justify-content: center;
  384. align-items: center;
  385. font-size: 24rpx;
  386. color: #b4b4b4;
  387. .line {
  388. width: 68rpx;
  389. height: 1rpx;
  390. background: #cccccc;
  391. }
  392. p {
  393. margin: 0 20rpx;
  394. }
  395. }
  396. .btn-wrapper {
  397. display: flex;
  398. align-items: center;
  399. justify-content: center;
  400. margin-top: 30rpx;
  401. .btn {
  402. display: flex;
  403. align-items: center;
  404. justify-content: center;
  405. width: 68rpx;
  406. height: 68rpx;
  407. border-radius: 50%;
  408. }
  409. .apple-btn {
  410. display: flex;
  411. align-items: center;
  412. justify-content: center;
  413. margin-left: 30rpx;
  414. background: #000;
  415. border-radius: 34rpx;
  416. font-size: 40rpx;
  417. .icon-s-pingguo {
  418. color: #fff;
  419. font-size: 40rpx;
  420. }
  421. }
  422. .iconfont {
  423. font-size: 40rpx;
  424. color: #fff;
  425. }
  426. .wx {
  427. margin-right: 30rpx;
  428. background-color: #61c64f;
  429. }
  430. .mima {
  431. background-color: #28b3e9;
  432. }
  433. .yanzheng {
  434. background-color: #f89c23;
  435. }
  436. }
  437. }
  438. .code img {
  439. width: 100%;
  440. height: 100%;
  441. }
  442. .acea-row.row-middle {
  443. input {
  444. margin-left: 20rpx;
  445. display: block;
  446. }
  447. }
  448. .text-row {
  449. font-size: 22rpx;
  450. color: #999;
  451. }
  452. .login-wrapper {
  453. padding: 0 30rpx;
  454. height: 100vh;
  455. .back_extend {
  456. position: fixed;
  457. top: 50px;
  458. left: 10px;
  459. font-size: 26rpx;
  460. color: #999;
  461. }
  462. .shading {
  463. display: flex;
  464. align-items: center;
  465. justify-content: center;
  466. width: 100%;
  467. /* #ifdef APP-VUE */
  468. margin-top: 50rpx;
  469. /* #endif */
  470. /* #ifndef APP-VUE */
  471. margin-top: 200rpx;
  472. /* #endif */
  473. image {
  474. width: 180rpx;
  475. height: 180rpx;
  476. border-radius: 50%;
  477. }
  478. }
  479. .whiteBg {
  480. margin-top: 100rpx;
  481. .list {
  482. border-radius: 16rpx;
  483. overflow: hidden;
  484. .item {
  485. border-bottom: 1px solid #f0f0f0;
  486. background: #fff;
  487. .row-middle {
  488. position: relative;
  489. padding: 16rpx 45rpx;
  490. .texts {
  491. flex: 1;
  492. font-size: 28rpx;
  493. height: 80rpx;
  494. line-height: 80rpx;
  495. display: flex;
  496. justify-content: center;
  497. align-items: center;
  498. }
  499. input {
  500. flex: 1;
  501. font-size: 28rpx;
  502. height: 80rpx;
  503. line-height: 80rpx;
  504. display: flex;
  505. justify-content: center;
  506. align-items: center;
  507. }
  508. .code {
  509. position: absolute;
  510. right: 30rpx;
  511. top: 50%;
  512. color: #cd9933;
  513. font-size: 26rpx;
  514. z-index: 999;
  515. transform: translateY(-50%);
  516. border: 1px solid;
  517. border-radius: 14rpx;
  518. padding: 8rpx 10rpx;
  519. }
  520. }
  521. }
  522. }
  523. .logon {
  524. display: flex;
  525. align-items: center;
  526. justify-content: center;
  527. width: 100%;
  528. height: 86rpx;
  529. margin-top: 80rpx;
  530. background-color: $theme-color;
  531. border-radius: 120rpx;
  532. color: #ffffff;
  533. font-size: 30rpx;
  534. }
  535. .tips {
  536. margin: 0.9375rem;
  537. text-align: center;
  538. color: #999;
  539. display: flex;
  540. justify-content: space-between;
  541. align-items: center;
  542. }
  543. }
  544. }
  545. </style>