index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. Toast({ title: "登录成功" });
  316. backHome();
  317. }
  318. } catch (err) {
  319. console.error(err);
  320. uni.showToast({ title: err.msg, icon: "none" });
  321. }
  322. };
  323. onLoad((options) => {
  324. getSceneInfo(options)
  325. getLogoImage();
  326. });
  327. onMounted(() => {
  328. uni.getDeviceInfo({
  329. success(res) {
  330. // 获取平台(如 "ios"、"android")
  331. platform.value = res.platform.toLowerCase();
  332. // 解析系统版本(以 iOS 为例,res.system 格式如 "iOS 16.5")
  333. if (platform.value === "ios") {
  334. const systemParts = res.system.split(" ");
  335. if (systemParts.length > 1) {
  336. const iosVersion = parseFloat(systemParts[1]);
  337. // iOS 版本 >= 13 时显示 Apple 登录相关内容
  338. if (iosVersion >= 13) {
  339. appleShow.value = true;
  340. }
  341. }
  342. }
  343. },
  344. fail(err) {
  345. console.error("获取设备信息失败:", err);
  346. },
  347. });
  348. });
  349. const backHome = () => {
  350. if(userInfo.value.merchant){
  351. uni.navigateTo({
  352. url:"/pages/merchantCenter/index"
  353. })
  354. }else{
  355. uni.switchTab({
  356. url: "/pages/index/index",
  357. });
  358. }
  359. };
  360. const change_password = () => {
  361. uni.navigateTo({
  362. url: "/pages/change_password/change_password",
  363. });
  364. };
  365. const footprintScanFn =async (data) => {
  366. const res = await footprintScan(data);
  367. Toast({ title: "登录成功" });
  368. backHome();
  369. }
  370. </script>
  371. <style>
  372. page {
  373. background: #fff !important;
  374. }
  375. </style>
  376. <style lang="scss" scoped>
  377. .appLogin {
  378. margin-top: 60rpx;
  379. .hds {
  380. display: flex;
  381. justify-content: center;
  382. align-items: center;
  383. font-size: 24rpx;
  384. color: #b4b4b4;
  385. .line {
  386. width: 68rpx;
  387. height: 1rpx;
  388. background: #cccccc;
  389. }
  390. p {
  391. margin: 0 20rpx;
  392. }
  393. }
  394. .btn-wrapper {
  395. display: flex;
  396. align-items: center;
  397. justify-content: center;
  398. margin-top: 30rpx;
  399. .btn {
  400. display: flex;
  401. align-items: center;
  402. justify-content: center;
  403. width: 68rpx;
  404. height: 68rpx;
  405. border-radius: 50%;
  406. }
  407. .apple-btn {
  408. display: flex;
  409. align-items: center;
  410. justify-content: center;
  411. margin-left: 30rpx;
  412. background: #000;
  413. border-radius: 34rpx;
  414. font-size: 40rpx;
  415. .icon-s-pingguo {
  416. color: #fff;
  417. font-size: 40rpx;
  418. }
  419. }
  420. .iconfont {
  421. font-size: 40rpx;
  422. color: #fff;
  423. }
  424. .wx {
  425. margin-right: 30rpx;
  426. background-color: #61c64f;
  427. }
  428. .mima {
  429. background-color: #28b3e9;
  430. }
  431. .yanzheng {
  432. background-color: #f89c23;
  433. }
  434. }
  435. }
  436. .code img {
  437. width: 100%;
  438. height: 100%;
  439. }
  440. .acea-row.row-middle {
  441. input {
  442. margin-left: 20rpx;
  443. display: block;
  444. }
  445. }
  446. .text-row {
  447. font-size: 22rpx;
  448. color: #999;
  449. }
  450. .login-wrapper {
  451. padding: 0 30rpx;
  452. height: 100vh;
  453. .back_extend {
  454. position: fixed;
  455. top: 50px;
  456. left: 10px;
  457. font-size: 26rpx;
  458. color: #999;
  459. }
  460. .shading {
  461. display: flex;
  462. align-items: center;
  463. justify-content: center;
  464. width: 100%;
  465. /* #ifdef APP-VUE */
  466. margin-top: 50rpx;
  467. /* #endif */
  468. /* #ifndef APP-VUE */
  469. margin-top: 200rpx;
  470. /* #endif */
  471. image {
  472. width: 180rpx;
  473. height: 180rpx;
  474. border-radius: 50%;
  475. }
  476. }
  477. .whiteBg {
  478. margin-top: 100rpx;
  479. .list {
  480. border-radius: 16rpx;
  481. overflow: hidden;
  482. .item {
  483. border-bottom: 1px solid #f0f0f0;
  484. background: #fff;
  485. .row-middle {
  486. position: relative;
  487. padding: 16rpx 45rpx;
  488. .texts {
  489. flex: 1;
  490. font-size: 28rpx;
  491. height: 80rpx;
  492. line-height: 80rpx;
  493. display: flex;
  494. justify-content: center;
  495. align-items: center;
  496. }
  497. input {
  498. flex: 1;
  499. font-size: 28rpx;
  500. height: 80rpx;
  501. line-height: 80rpx;
  502. display: flex;
  503. justify-content: center;
  504. align-items: center;
  505. }
  506. .code {
  507. position: absolute;
  508. right: 30rpx;
  509. top: 50%;
  510. color: #cd9933;
  511. font-size: 26rpx;
  512. z-index: 999;
  513. transform: translateY(-50%);
  514. border: 1px solid;
  515. border-radius: 14rpx;
  516. padding: 8rpx 10rpx;
  517. }
  518. }
  519. }
  520. }
  521. .logon {
  522. display: flex;
  523. align-items: center;
  524. justify-content: center;
  525. width: 100%;
  526. height: 86rpx;
  527. margin-top: 80rpx;
  528. background-color: $theme-color;
  529. border-radius: 120rpx;
  530. color: #ffffff;
  531. font-size: 30rpx;
  532. }
  533. .tips {
  534. margin: 0.9375rem;
  535. text-align: center;
  536. color: #999;
  537. display: flex;
  538. justify-content: space-between;
  539. align-items: center;
  540. }
  541. }
  542. }
  543. </style>