login.vue 5.2 KB

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