login.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <template>
  2. <view class="normal-login-container">
  3. <view class="logo-content align-center justify-center flex">
  4. <image :src="imagePath" mode="aspectFit" style="width: 100rpx;height: 100rpx;"></image>
  5. <text class="title">金邻助家</text>
  6. </view>
  7. <view>
  8. </view>
  9. <up-popup :show="show" mode="bottom" @close="close" @open="open">
  10. <view class="popup-content">
  11. <!-- 上方头像 -->
  12. <up-avatar :src="src" class="avatar"></up-avatar>
  13. <!-- 下方按钮 -->
  14. <up-button type="success" shape="circle" class="button" @click="handleLogin">获取微信授权登录</up-button>
  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. </up-popup>
  22. </view>
  23. </template>
  24. <script setup>
  25. import {
  26. ref,
  27. reactive,
  28. onMounted
  29. } from 'vue';
  30. import {
  31. useRouter,
  32. useRoute
  33. } from 'vue-router'; // 根据实际情况选择路由库
  34. import {
  35. getCodeImg,
  36. login
  37. } from '@/api/login';
  38. import store from "@/store"
  39. const imagePath = '/static/9efd1.png';
  40. // 获取全局配置
  41. // const globalConfig = getApp().globalData.config;
  42. const register = ref(false); // 用户注册开关
  43. const loginForm = reactive({
  44. username: "",
  45. password: "",
  46. code: "",
  47. uuid: ''
  48. });
  49. const codeUrl = ref("");
  50. const captchaEnabled = ref(true);
  51. const router = useRouter();
  52. // 创建响应式数据
  53. const show = ref(false);
  54. // 定义方法
  55. function open() {
  56. // 打开逻辑,比如设置 show 为 true
  57. show.value = true;
  58. }
  59. function close() {
  60. // 关闭逻辑,设置 show 为 false
  61. show.value = false;
  62. }
  63. // 用户注册
  64. const handleUserRegister = () => {
  65. router.push('/pages/register'); // 替换为实际的路由跳转逻辑
  66. };
  67. // 隐私协议
  68. const handlePrivacy = () => {
  69. uni.navigateTo({
  70. url: '/pages_home/pages/login/index'
  71. })
  72. };
  73. // 用户协议
  74. const handleUserAgrement = () => {
  75. uni.navigateTo({
  76. url: '/pages_home/pages/login/user'
  77. })
  78. };
  79. // 登录方法
  80. const handleLogin = async () => {
  81. uni.showLoading({
  82. title: "登录中,请耐心等待..."
  83. }); // 使用uni-app的loading方法替代$modal.loading
  84. // 获取服务商信息
  85. uni.getProvider({
  86. service: "oauth",
  87. success: (res) => {
  88. console.log(res);
  89. }
  90. });
  91. // 获取code
  92. uni.login({
  93. provider: 'weixin',
  94. success: (res) => {
  95. if (res.code == 500) {
  96. uni.showLoading({
  97. title: "系统暂未开发,敬请期待"
  98. });
  99. } else if (res.code == 400) {
  100. uni.showLoading({
  101. title: "系统暂未开放"
  102. });
  103. }
  104. loginForm.code = res.code;
  105. // 获取用户信息
  106. uni.getUserInfo({
  107. success: (res) => {
  108. console.log("用户信息", res);
  109. }
  110. });
  111. pwdLogin();
  112. uni.hideLoading()
  113. },
  114. });
  115. };
  116. // 密码登录
  117. const pwdLogin = async () => {
  118. if (!store) {
  119. console.error("Store is not defined");
  120. return;
  121. }
  122. store.dispatch('Login', loginForm).then(() => {
  123. // uni.hideLoading(); // 关闭加载提示
  124. loginSuccess();
  125. })
  126. };
  127. // 登录成功后,处理函数
  128. const loginSuccess = () => {
  129. store.dispatch('GetInfo').then((res) => {
  130. console.log(store.state.user.userOrWorker, '>>>>99');
  131. switch (store.state.user.userOrWorker) {
  132. case 0:
  133. uni.reLaunch({
  134. url: '/pages/UserSelection'
  135. });
  136. break;
  137. case 1: // 跳转到用户页面
  138. uni.setStorageSync('userType', 1);
  139. case 2: // 跳转到志愿者页
  140. console.log('跳转到页面')
  141. uni.setStorageSync('userType', 2);
  142. uni.switchTab({
  143. url: '/pages/index'
  144. })
  145. break;
  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>