login.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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;
  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. if(res.code == 500){
  119. uni.showLoading({
  120. title: "系统暂未开发,敬请期待"
  121. });
  122. }else if(res.code == 400){
  123. uni.showLoading({
  124. title: "系统暂未开放"
  125. });
  126. }
  127. loginForm.code = res.code;
  128. // 获取用户信息
  129. uni.getUserInfo({
  130. success: (res) => {
  131. console.log("用户信息", res);
  132. }
  133. });
  134. pwdLogin();
  135. uni.hideLoading()
  136. },
  137. });
  138. };
  139. // 密码登录
  140. const pwdLogin = async () => {
  141. if (!store) {
  142. console.error("Store is not defined");
  143. return;
  144. }
  145. store.dispatch('Login', loginForm).then(() => {
  146. // uni.hideLoading(); // 关闭加载提示
  147. loginSuccess();
  148. })
  149. };
  150. // 登录成功后,处理函数
  151. const loginSuccess = () => {
  152. store.dispatch('GetInfo').then((res) => {
  153. console.log(store.state.user.userOrWorker, '>>>>99');
  154. switch(store.state.user.userOrWorker) {
  155. case 0:
  156. uni.reLaunch({
  157. url: '/pages/UserSelection'
  158. });
  159. break;
  160. case 1: // 跳转到用户页面
  161. uni.setStorageSync('userType', 1);
  162. case 2: // 跳转到志愿者页
  163. console.log('跳转到页面')
  164. uni.setStorageSync('userType', 2);
  165. uni.switchTab({
  166. url: '/pages/index'
  167. })
  168. break;
  169. }
  170. });
  171. };
  172. // 页面加载时获取验证码
  173. onMounted(() => {
  174. open();
  175. });
  176. </script>
  177. <style lang="scss">
  178. page {
  179. background-color: #ffffff;
  180. }
  181. .normal-login-container {
  182. width: 100%;
  183. .logo-content {
  184. width: 100%;
  185. font-size: 21px;
  186. text-align: center;
  187. padding-top: 15%;
  188. image {
  189. border-radius: 4px;
  190. }
  191. .title {
  192. margin-left: 10px;
  193. }
  194. }
  195. .login-form-content {
  196. text-align: center;
  197. margin: 20px auto;
  198. margin-top: 15%;
  199. width: 80%;
  200. .input-item {
  201. margin: 20px auto;
  202. background-color: #f5f6f7;
  203. height: 45px;
  204. border-radius: 20px;
  205. .icon {
  206. font-size: 38rpx;
  207. margin-left: 10px;
  208. color: #999;
  209. }
  210. .input {
  211. width: 100%;
  212. font-size: 14px;
  213. line-height: 20px;
  214. text-align: left;
  215. padding-left: 15px;
  216. }
  217. }
  218. .login-btn {
  219. margin-top: 40px;
  220. height: 45px;
  221. }
  222. .reg {
  223. margin-top: 15px;
  224. }
  225. .xieyi {
  226. color: #333;
  227. margin-top: 20px;
  228. }
  229. .login-code {
  230. height: 38px;
  231. float: right;
  232. .login-code-img {
  233. height: 38px;
  234. position: absolute;
  235. margin-left: 10px;
  236. width: 200rpx;
  237. }
  238. }
  239. }
  240. }
  241. /* 弹性容器:垂直方向排列 */
  242. .popup-content {
  243. display: flex;
  244. flex-direction: column;
  245. /* 上下布局 */
  246. align-items: center;
  247. /* 水平居中 */
  248. padding: 30rpx;
  249. gap: 30rpx;
  250. /* 元素间距 */
  251. }
  252. /* 头像样式 */
  253. .avatar {
  254. width: 120rpx;
  255. height: 160rpx;
  256. }
  257. /* 按钮样式 */
  258. .button {
  259. width: 170rpx !important;
  260. /* 覆盖默认宽度 */
  261. margin-left: 0 !important;
  262. /* 清除原代码中的 margin-left */
  263. }
  264. </style>