index.vue 15 KB

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