index.vue 15 KB

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