login.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. };
  76. // 登录方法
  77. const handleLogin = async () => {
  78. uni.showLoading({
  79. title: "登录中,请耐心等待..."
  80. }); // 使用uni-app的loading方法替代$modal.loading
  81. // 获取服务商信息
  82. uni.getProvider({
  83. service: "oauth",
  84. success: (res) => {
  85. console.log(res);
  86. }
  87. });
  88. // 获取code
  89. uni.login({
  90. provider: 'weixin',
  91. success: (res) => {
  92. if (res.code == 500) {
  93. uni.showLoading({
  94. title: "系统暂未开发,敬请期待"
  95. });
  96. } else if (res.code == 400) {
  97. uni.showLoading({
  98. title: "系统暂未开放"
  99. });
  100. }
  101. loginForm.code = res.code;
  102. // 获取用户信息
  103. uni.getUserInfo({
  104. success: (res) => {
  105. console.log("用户信息", res);
  106. }
  107. });
  108. pwdLogin();
  109. uni.hideLoading()
  110. },
  111. });
  112. };
  113. // 密码登录
  114. const pwdLogin = async () => {
  115. if (!store) {
  116. console.error("Store is not defined");
  117. return;
  118. }
  119. store.dispatch('Login', loginForm).then(() => {
  120. // uni.hideLoading(); // 关闭加载提示
  121. loginSuccess();
  122. })
  123. };
  124. // 登录成功后,处理函数
  125. const loginSuccess = () => {
  126. store.dispatch('GetInfo').then((res) => {
  127. console.log(store.state.user.userOrWorker, '>>>>99');
  128. switch (store.state.user.userOrWorker) {
  129. case 0:
  130. uni.reLaunch({
  131. url: '/pages/UserSelection'
  132. });
  133. break;
  134. case 1: // 跳转到用户页面
  135. uni.setStorageSync('userType', 1);
  136. case 2: // 跳转到志愿者页
  137. console.log('跳转到页面')
  138. uni.setStorageSync('userType', 2);
  139. uni.switchTab({
  140. url: '/pages/index'
  141. })
  142. break;
  143. }
  144. });
  145. };
  146. // 页面加载时获取验证码
  147. onMounted(() => {
  148. open();
  149. });
  150. </script>
  151. <style lang="scss">
  152. page {
  153. background-color: #ffffff;
  154. }
  155. .normal-login-container {
  156. width: 100%;
  157. .logo-content {
  158. width: 100%;
  159. font-size: 21px;
  160. text-align: center;
  161. padding-top: 15%;
  162. image {
  163. border-radius: 4px;
  164. }
  165. .title {
  166. margin-left: 10px;
  167. }
  168. }
  169. .login-form-content {
  170. text-align: center;
  171. margin: 20px auto;
  172. margin-top: 15%;
  173. width: 80%;
  174. .input-item {
  175. margin: 20px auto;
  176. background-color: #f5f6f7;
  177. height: 45px;
  178. border-radius: 20px;
  179. .icon {
  180. font-size: 38rpx;
  181. margin-left: 10px;
  182. color: #999;
  183. }
  184. .input {
  185. width: 100%;
  186. font-size: 14px;
  187. line-height: 20px;
  188. text-align: left;
  189. padding-left: 15px;
  190. }
  191. }
  192. .login-btn {
  193. margin-top: 40px;
  194. height: 45px;
  195. }
  196. .reg {
  197. margin-top: 15px;
  198. }
  199. .xieyi {
  200. color: #333;
  201. margin-top: 20px;
  202. }
  203. .login-code {
  204. height: 38px;
  205. float: right;
  206. .login-code-img {
  207. height: 38px;
  208. position: absolute;
  209. margin-left: 10px;
  210. width: 200rpx;
  211. }
  212. }
  213. }
  214. }
  215. /* 弹性容器:垂直方向排列 */
  216. .popup-content {
  217. display: flex;
  218. flex-direction: column;
  219. /* 上下布局 */
  220. align-items: center;
  221. /* 水平居中 */
  222. padding: 30rpx;
  223. gap: 30rpx;
  224. /* 元素间距 */
  225. }
  226. /* 头像样式 */
  227. .avatar {
  228. width: 120rpx;
  229. height: 160rpx;
  230. }
  231. /* 按钮样式 */
  232. .button {
  233. width: 170rpx !important;
  234. /* 覆盖默认宽度 */
  235. margin-left: 0 !important;
  236. /* 清除原代码中的 margin-left */
  237. }
  238. </style>