login.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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="error" 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. if (store.state.user.userOrWorker == 0) {
  130. uni.reLaunch({
  131. url: '/pages/UserSelection'
  132. });
  133. return
  134. }
  135. uni.setStorageSync('userType', store.state.user.userOrWorker);
  136. uni.switchTab({
  137. url: '/pages/index'
  138. })
  139. });
  140. };
  141. // 页面加载时获取验证码
  142. onMounted(() => {
  143. uni.setStorageSync('userType', 0);
  144. open();
  145. });
  146. </script>
  147. <style lang="scss">
  148. page {
  149. background-color: #ffffff;
  150. }
  151. .normal-login-container {
  152. width: 100%;
  153. .logo-content {
  154. width: 100%;
  155. font-size: 21px;
  156. text-align: center;
  157. padding-top: 25%;
  158. image {
  159. border-radius: 4px;
  160. }
  161. .title {
  162. margin-left: 10px;
  163. }
  164. }
  165. .login-form-content {
  166. text-align: center;
  167. margin: 20px auto;
  168. margin-top: 15%;
  169. width: 80%;
  170. .input-item {
  171. margin: 20px auto;
  172. background-color: #f5f6f7;
  173. height: 45px;
  174. border-radius: 20px;
  175. .icon {
  176. font-size: 38rpx;
  177. margin-left: 10px;
  178. color: #999;
  179. }
  180. .input {
  181. width: 100%;
  182. font-size: 14px;
  183. line-height: 20px;
  184. text-align: left;
  185. padding-left: 15px;
  186. }
  187. }
  188. .login-btn {
  189. margin-top: 40px;
  190. height: 45px;
  191. }
  192. .reg {
  193. margin-top: 15px;
  194. }
  195. .xieyi {
  196. color: #333;
  197. margin-top: 20px;
  198. }
  199. .login-code {
  200. height: 38px;
  201. float: right;
  202. .login-code-img {
  203. height: 38px;
  204. position: absolute;
  205. margin-left: 10px;
  206. width: 200rpx;
  207. }
  208. }
  209. }
  210. }
  211. /* 弹性容器:垂直方向排列 */
  212. .popup-content {
  213. display: flex;
  214. flex-direction: column;
  215. /* 上下布局 */
  216. align-items: center;
  217. /* 水平居中 */
  218. padding: 30rpx;
  219. /* 元素间距 */
  220. gap: 30rpx;
  221. }
  222. /* 头像样式 */
  223. .avatar {
  224. width: 120rpx;
  225. height: 160rpx;
  226. }
  227. /* 按钮样式 */
  228. .button {
  229. // width: 170rpx !important;
  230. /* 覆盖默认宽度 */
  231. margin-left: 0 !important;
  232. /* 清除原代码中的 margin-left */
  233. }
  234. </style>