login.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <view class="normal-login-container">
  3. <view class="logo-content align-center justify-center flex">
  4. <!-- <image style="width: 100rpx;height: 100rpx;" :src="globalConfig.appInfo.logo" mode="widthFix" /> -->
  5. <image :src="imagePath" mode="aspectFit" style="width: 100rpx;height: 100rpx;"></image>
  6. <text class="title">乐融融</text>
  7. </view>
  8. <!-- <view class="login-form-content">
  9. <view class="action-btn">
  10. <button @click="handleLogin" class="login-btn cu-btn block bg-green lg round">微信授权登录</button>
  11. </view>
  12. <view class="reg text-center" v-if="register">
  13. <text class="text-grey1">没有账号?</text>
  14. <text @click="handleUserRegister" class="text-blue">立即注册</text>
  15. </view>
  16. <view class="xieyi text-center">
  17. <text class="text-grey1">登录即代表同意</text>
  18. <text @click="handleUserAgrement" class="text-blue">《用户协议》</text>
  19. <text @click="handlePrivacy" class="text-blue">《隐私协议》</text>
  20. </view>
  21. </view> -->
  22. <view>
  23. </view>
  24. <up-popup :show="show" mode="bottom" @close="close" @open="open">
  25. <view class="popup-content">
  26. <!-- 上方头像 -->
  27. <up-avatar :src="src" class="avatar"></up-avatar>
  28. <!-- 下方按钮 -->
  29. <up-button type="success" shape="circle" class="button" @click="handleLogin">获取微信授权登录</up-button>
  30. </view>
  31. <view class="xieyi text-center">
  32. <text class="text-grey1">登录即代表同意</text>
  33. <text @click="handleUserAgrement" class="text-blue">《用户协议》</text>
  34. <text @click="handlePrivacy" class="text-blue">《隐私协议》</text>
  35. </view>
  36. </up-popup>
  37. </view>
  38. </template>
  39. <script setup>
  40. import {
  41. ref,
  42. reactive,
  43. onMounted
  44. } from 'vue';
  45. import {
  46. useRouter,
  47. useRoute
  48. } from 'vue-router'; // 根据实际情况选择路由库
  49. import {
  50. getCodeImg,
  51. login
  52. } from '@/api/login';
  53. import store from "@/store"
  54. const imagePath = '/static/logo.png';
  55. // 获取全局配置
  56. // const globalConfig = getApp().globalData.config; // 注意:getApp()是特定于小程序的方法
  57. const register = ref(false); // 用户注册开关
  58. const loginForm = reactive({
  59. username: "",
  60. password: "",
  61. code: "",
  62. uuid: ''
  63. });
  64. const codeUrl = ref("");
  65. const captchaEnabled = ref(true);
  66. const router = useRouter();
  67. // 创建响应式数据
  68. const show = ref(false);
  69. // 定义方法
  70. function open() {
  71. // 打开逻辑,比如设置 show 为 true
  72. show.value = true;
  73. }
  74. function close() {
  75. // 关闭逻辑,设置 show 为 false
  76. show.value = false;
  77. }
  78. // 获取图形验证码
  79. // const getCode = () => {
  80. // getCodeImg().then(res => {
  81. // captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled;
  82. // if (captchaEnabled.value) {
  83. // codeUrl.value = 'data:image/gif;base64,' + res.img;
  84. // loginForm.uuid = res.uuid;
  85. // }
  86. // });
  87. // };
  88. // 用户注册
  89. const handleUserRegister = () => {
  90. router.push('/pages/register'); // 替换为实际的路由跳转逻辑
  91. };
  92. // 隐私协议
  93. const handlePrivacy = () => {
  94. // let site = globalConfig.appInfo.agreements[0];
  95. router.push(`/pages/common/webview/index?title=${site.title}&url=${site.url}`);
  96. };
  97. // 用户协议
  98. const handleUserAgrement = () => {
  99. // let site = globalConfig.appInfo.agreements[1];
  100. router.push(`/pages/common/webview/index?title=${site.title}&url=${site.url}`);
  101. };
  102. // 登录方法
  103. const handleLogin = async () => {
  104. uni.showLoading({
  105. title: "登录中,请耐心等待..."
  106. }); // 使用uni-app的loading方法替代$modal.loading
  107. // 获取服务商信息
  108. uni.getProvider({
  109. service: "oauth",
  110. success: (res) => {
  111. console.log(res);
  112. }
  113. });
  114. // 获取code
  115. uni.login({
  116. provider: 'weixin',
  117. success: (res) => {
  118. loginForm.code = res.code;
  119. // 获取用户信息
  120. uni.getUserInfo({
  121. success: (res) => {
  122. console.log("用户信息", res);
  123. }
  124. });
  125. pwdLogin();
  126. uni.hideLoading()
  127. },
  128. });
  129. };
  130. // 密码登录
  131. const pwdLogin = async () => {
  132. if (!store) {
  133. console.error("Store is not defined");
  134. return;
  135. }
  136. store.dispatch('Login', loginForm).then(() => {
  137. // uni.hideLoading(); // 关闭加载提示
  138. loginSuccess();
  139. })
  140. };
  141. // 登录成功后,处理函数
  142. const loginSuccess = () => {
  143. store.dispatch('GetInfo').then(() => {
  144. uni.switchTab({// 跳转 tabBar 中的页面
  145. url: '/pages/index'
  146. });
  147. });
  148. };
  149. // 页面加载时获取验证码
  150. onMounted(() => {
  151. open();
  152. });
  153. </script>
  154. <style lang="scss">
  155. page {
  156. background-color: #ffffff;
  157. }
  158. .normal-login-container {
  159. width: 100%;
  160. .logo-content {
  161. width: 100%;
  162. font-size: 21px;
  163. text-align: center;
  164. padding-top: 15%;
  165. image {
  166. border-radius: 4px;
  167. }
  168. .title {
  169. margin-left: 10px;
  170. }
  171. }
  172. .login-form-content {
  173. text-align: center;
  174. margin: 20px auto;
  175. margin-top: 15%;
  176. width: 80%;
  177. .input-item {
  178. margin: 20px auto;
  179. background-color: #f5f6f7;
  180. height: 45px;
  181. border-radius: 20px;
  182. .icon {
  183. font-size: 38rpx;
  184. margin-left: 10px;
  185. color: #999;
  186. }
  187. .input {
  188. width: 100%;
  189. font-size: 14px;
  190. line-height: 20px;
  191. text-align: left;
  192. padding-left: 15px;
  193. }
  194. }
  195. .login-btn {
  196. margin-top: 40px;
  197. height: 45px;
  198. }
  199. .reg {
  200. margin-top: 15px;
  201. }
  202. .xieyi {
  203. color: #333;
  204. margin-top: 20px;
  205. }
  206. .login-code {
  207. height: 38px;
  208. float: right;
  209. .login-code-img {
  210. height: 38px;
  211. position: absolute;
  212. margin-left: 10px;
  213. width: 200rpx;
  214. }
  215. }
  216. }
  217. }
  218. /* 弹性容器:垂直方向排列 */
  219. .popup-content {
  220. display: flex;
  221. flex-direction: column;
  222. /* 上下布局 */
  223. align-items: center;
  224. /* 水平居中 */
  225. padding: 30rpx;
  226. gap: 30rpx;
  227. /* 元素间距 */
  228. }
  229. /* 头像样式 */
  230. .avatar {
  231. width: 120rpx;
  232. height: 160rpx;
  233. }
  234. /* 按钮样式 */
  235. .button {
  236. width: 170rpx !important;
  237. /* 覆盖默认宽度 */
  238. margin-left: 0 !important;
  239. /* 清除原代码中的 margin-left */
  240. }
  241. </style>