login.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <template>
  2. <view class="normal-login-container">
  3. <view class="back-icon" :style="`margin-top: ${globalData.statusBarHeight}px`">
  4. <up-icon name="arrow-left" size="24" @click="backClick"></up-icon>
  5. </view>
  6. <image src="/static/login13785@1x.png" mode="widthFix" class="wave-img"></image>
  7. <view class="logo-content" :style="`margin-top: ${globalData.navBarHeight}px`">
  8. <image :src="imagePath" mode="aspectFit" class="logo-image"></image>
  9. <text class="title">金邻助家</text>
  10. </view>
  11. <view class="slogan-content">
  12. <text class="slogan-text">着力打造全国居家服务行业标准</text>
  13. <text class="slogan-text">和建立综合信用评价体系</text>
  14. </view>
  15. <image src="/static/13779@1x.png" mode="widthFix" class="house-illustration"></image>
  16. <view class="actions-container">
  17. <up-button @click="onLogin" :customStyle="WeChat">微信授权登录</up-button>
  18. <view class="xieyi text-center">
  19. <up-checkbox-group v-model="checkboxValue">
  20. <up-checkbox label="我已认真阅读、理解并同意" name="agree" usedAlone @change="onCheckboxChange" class="text-grey1">
  21. </up-checkbox>
  22. </up-checkbox-group>
  23. <text @click="handleUserAgrement" class="text-blue">《用户协议》</text>
  24. <text @click="handlePrivacy" class="text-blue">《隐私协议》</text>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29. <script setup>
  30. import { ref, reactive, onMounted } from 'vue'
  31. import { useRouter } from 'vue-router' // 根据实际情况选择路由库
  32. import store from '@/store'
  33. import { onShow, onLoad } from '@dcloudio/uni-app'
  34. import {
  35. userOrWorker,
  36. } from '@/api/login';
  37. import { getUnlimitedQRCode } from '@/api/qrcode'
  38. const imagePath = '/static/9efd1.png'
  39. // 获取全局配置
  40. // const globalConfig = getApp().globalData.config;
  41. const loginForm = reactive({
  42. username: '',
  43. password: '',
  44. code: '',
  45. uuid: '',
  46. referrerType: '',
  47. referrerId: '',
  48. referrerQrCodeVersion: '',
  49. latitude: 0,
  50. longitude: 0,
  51. })
  52. const checkboxValue = ref(false)
  53. const globalData = ref({
  54. statusBarHeight: 47,
  55. navBarHeight: 91,
  56. })
  57. const router = useRouter()
  58. // 获取小程序码参数
  59. onLoad((options) => {
  60. console.log('登录页面接收到的参数:', options);
  61. // 处理从二维码扫描进入的场景
  62. if (options.scene) {
  63. // 解码场景值
  64. const scene = decodeURIComponent(options.scene);
  65. // const scene = "2:122:2"
  66. console.log('解码后的场景值:', scene);
  67. // 解析场景值参数,格式:推荐人类型:推荐人ID:二维码版本号
  68. const params = scene.split(':');
  69. console.log('解析后的参数:', params);
  70. // 设置推荐人类型
  71. loginForm.referrerType = params[0];
  72. // 设置推荐人ID
  73. loginForm.referrerId = params[1];
  74. // 设置二维码版本号
  75. if (params.length >= 3) {
  76. loginForm.referrerQrCodeVersion = params[2];
  77. }
  78. }
  79. // 处理直接传入的参数
  80. if (options.referrerType) {
  81. loginForm.referrerType = options.referrerType;
  82. }
  83. if (options.referrerId) {
  84. loginForm.referrerId = options.referrerId;
  85. }
  86. })
  87. // 用户注册
  88. const handleUserRegister = () => {
  89. router.push('/pages/register') // 替换为实际的路由跳转逻辑
  90. }
  91. const WeChat = {
  92. width: '530rpx',
  93. height: '96rpx',
  94. opacity: 1,
  95. background: '#8B4F2D',
  96. color: '#FFFFFF',
  97. borderRadius: '48rpx',
  98. fontWeight: '700px',
  99. fontSize: '32rpx',
  100. marginLeft: 'auto',
  101. marginRight: 'auto',
  102. marginBottom: '10rpx',
  103. display: 'flex',
  104. justifyContent: 'center',
  105. alignItems: 'center',
  106. border: 'none',
  107. boxShadow: 'none',
  108. }
  109. // 隐私协议
  110. const handlePrivacy = () => {
  111. uni.navigateTo({
  112. url: '/pages_home/pages/login/index',
  113. })
  114. }
  115. // 用户协议
  116. const handleUserAgrement = () => {
  117. uni.navigateTo({
  118. url: '/pages_home/pages/login/user',
  119. })
  120. }
  121. // 获取模糊位置信息
  122. const getFuzzyLocation = () => {
  123. return new Promise((resolve, reject) => {
  124. wx.getFuzzyLocation({
  125. type: 'gcj02',
  126. success: (res) => {
  127. console.log('模糊位置获取成功:', res)
  128. // 使用字符串格式保留6位小数,避免parseFloat去除末尾的0
  129. const lat = res.latitude.toFixed(6)
  130. const lng = res.longitude.toFixed(6)
  131. // 在表单中保存为数字格式
  132. loginForm.latitude = Number(lat)
  133. loginForm.longitude = Number(lng)
  134. console.log('处理后的经纬度:', lat, lng)
  135. resolve({
  136. latitude: lat,
  137. longitude: lng
  138. })
  139. },
  140. fail: (err) => {
  141. console.error('模糊位置获取失败:', err)
  142. // 位置获取失败时,设为默认值0,也保留6位小数
  143. loginForm.latitude = 0
  144. loginForm.longitude = 0
  145. resolve({
  146. latitude: "0.000000",
  147. longitude: "0.000000"
  148. })
  149. }
  150. })
  151. })
  152. }
  153. // 登录方法
  154. const onLogin = () => {
  155. uni.$u.debounce(handleLogin, 300)
  156. }
  157. const handleLogin = async () => {
  158. if (!checkboxValue.value) {
  159. uni.showToast({
  160. title: '请先阅读并同意相关协议',
  161. icon: 'none',
  162. })
  163. return
  164. }
  165. uni.showLoading({
  166. title: '登录中,请耐心等待...',
  167. }) // 使用uni-app的loading方法替代$modal.loading
  168. try {
  169. // 获取模糊位置信息
  170. await getFuzzyLocation()
  171. console.log('登录位置信息:', loginForm.latitude, loginForm.longitude)
  172. } catch (err) {
  173. console.log('位置获取失败,继续登录流程')
  174. }
  175. // 获取code
  176. uni.login({
  177. provider: 'weixin',
  178. success: (res) => {
  179. if (res.code == 500) {
  180. uni.showLoading({
  181. title: '系统暂未开发,敬请期待',
  182. })
  183. } else if (res.code == 400) {
  184. uni.showLoading({
  185. title: '系统暂未开放',
  186. })
  187. }
  188. loginForm.code = res.code
  189. pwdLogin()
  190. uni.hideLoading()
  191. },
  192. })
  193. }
  194. // 密码登录
  195. const pwdLogin = async () => {
  196. if (!store) {
  197. console.error('Store is not defined')
  198. return
  199. }
  200. // 确保经纬度已添加到loginForm
  201. console.log('发送登录请求,经纬度:', loginForm.latitude, loginForm.longitude)
  202. store.dispatch('Login', loginForm).then(() => {
  203. loginSuccess()
  204. })
  205. }
  206. // 登录成功后,处理函数
  207. const loginSuccess = () => {
  208. store.dispatch('GetInfo').then((res) => {
  209. uni.setStorageSync('userType', store.state.user.userOrWorker)
  210. uni.setStorageSync('userId', res.user.userId)
  211. userOrWorker({ userType: store.state.user.userOrWorker })
  212. .then(res => {
  213. uni.switchTab({
  214. url: '/pages/index',
  215. })
  216. })
  217. .catch(err => {
  218. console.error('设置失败:', err);
  219. });
  220. })
  221. }
  222. // 获取胶囊位置信息
  223. const getSystemInfo = () => {
  224. uni.getSystemInfo({
  225. success: (res) => {
  226. const statusBarHeight = res.statusBarHeight
  227. const navBarHeight =
  228. (res.platform === 'android') ? 48 : 44 + res.statusBarHeight
  229. globalData.value = { statusBarHeight, navBarHeight }
  230. },
  231. })
  232. }
  233. onShow(() => {
  234. getSystemInfo()
  235. })
  236. // 页面加载时
  237. onMounted(() => {
  238. uni.setStorageSync('userType', 1)
  239. // 设置状态栏为透明
  240. uni.setNavigationBarColor({
  241. frontColor: '#ffffff',
  242. backgroundColor: 'rgba(0,0,0,0)',
  243. animation: {
  244. duration: 0,
  245. timingFunc: 'easeIn',
  246. },
  247. })
  248. getSystemInfo()
  249. })
  250. const onCheckboxChange = (value) => {
  251. console.log('复选框状态改变:', value)
  252. checkboxValue.value = value
  253. }
  254. const backClick = () => {
  255. console.log('back');
  256. wx.navigateBack({
  257. delta: 1, // 返回上一页
  258. });
  259. }
  260. </script>
  261. <style lang="scss" scoped>
  262. .back-icon {
  263. height: 108rpx;
  264. position: fixed;
  265. width: 100%;
  266. padding: 0 34rpx;
  267. display: flex;
  268. align-items: center;
  269. justify-content: space-between;
  270. z-index: 99;
  271. }
  272. .normal-login-container {
  273. width: 100vw;
  274. height: 100vh;
  275. border-radius: 48rpx;
  276. background: linear-gradient(156deg, #f5c954 -4%, #f66c44 110%);
  277. position: relative;
  278. overflow-x: hidden;
  279. .wave-img {
  280. position: absolute;
  281. top: 0;
  282. left: 0;
  283. width: 100vw;
  284. height: 100vh;
  285. }
  286. display: flex;
  287. flex-direction: column;
  288. align-items: center;
  289. }
  290. .logo-content {
  291. display: flex;
  292. flex-direction: column;
  293. align-items: center;
  294. position: relative;
  295. z-index: 1;
  296. .logo-image {
  297. width: 232rpx;
  298. height: 201rpx;
  299. margin-top: 47rpx;
  300. margin-bottom: -10rpx;
  301. margin-right: 300rpx;
  302. margin-left: 300rpx;
  303. }
  304. .title {
  305. width: 360rpx;
  306. height: 128rpx;
  307. font-family: Poppins;
  308. font-size: 90rpx;
  309. font-weight: 600;
  310. line-height: 127.81rpx;
  311. letter-spacing: normal;
  312. color: #FFFFFF;
  313. z-index: 1;
  314. margin-top: 0;
  315. margin-bottom: 82rpx;
  316. margin-right: 195rpx;
  317. margin-left: 195rpx;
  318. }
  319. }
  320. .house-illustration {
  321. width: 720rpx;
  322. height: 334rpx;
  323. margin-top: 0;
  324. margin-bottom: 78rpx;
  325. margin-right: 136rpx;
  326. margin-left: 136rpx;
  327. }
  328. .slogan-content {
  329. margin-top: 0;
  330. margin-bottom: 235rpx;
  331. margin-right: 300rpx;
  332. margin-left: 300rpx;
  333. z-index: 1;
  334. .slogan-text {
  335. display: flex;
  336. flex-direction: column;
  337. align-items: center;
  338. width: 448rpx;
  339. height: 52rpx;
  340. font-family: Poppins;
  341. font-size: 32rpx;
  342. font-weight: 600;
  343. line-height: 52rpx;
  344. text-align: center;
  345. letter-spacing: normal;
  346. color: #824727;
  347. }
  348. }
  349. .actions-container {
  350. display: flex;
  351. flex-direction: column;
  352. align-items: center;
  353. width: 100%;
  354. z-index: 1;
  355. }
  356. .xieyi {
  357. display: flex;
  358. align-items: center;
  359. justify-content: center;
  360. flex-wrap: wrap;
  361. }
  362. .text-blue {
  363. color: #1976d2;
  364. margin: 0 4rpx;
  365. }
  366. .text-grey1 {
  367. color: #333333;
  368. }
  369. .text-center {
  370. text-align: center;
  371. }
  372. </style>