login.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <template>
  2. <z-paging ref="paging" bgColor="rgba(0,0,0,0)">
  3. <template #top>
  4. <up-navbar :fixed="false" title="登录">
  5. <template #left>
  6. <!-- <image @click="$u.route({ type: 'switchTab', url: '/pages/list/index' })" class="home_icon" src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images/common/home.png" mode="widthFix" /> -->
  7. <up-icon @click="homeBack()" name="arrow-left" color="#333" size="40rpx"></up-icon>
  8. </template>
  9. </up-navbar>
  10. </template>
  11. <view class="login-centent">
  12. <view class="login-logo-wrap p-rtv d-flex j-c a-c">
  13. <image class="bg-circle" src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images/login/bg_circle.png" mode="aspectFit" />
  14. <image class="login-logo" src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images/login/logo.png" mode="aspectFit" />
  15. </view>
  16. <view class="login-form pl-20 pd-25">
  17. <up-button type="primary" shape="circle" @click="$u.route({ url: '/pages/login/phone/phone', params: { redirect: encodeURIComponent(redirect) } })">
  18. <view class="d-flex a-c j-c">
  19. <!-- <image class="base-icon mr-20"
  20. src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images/login-phone/phone.png"
  21. mode="widthFix" /> -->
  22. 手机号验证码登录
  23. </view>
  24. </up-button>
  25. <view class="pd-20"></view>
  26. <template v-if="isBindPhone">
  27. <up-button class="mb-40" @click="handleLogin" color="#28A94B" type="primary" shape="circle">
  28. <view class="d-flex a-c j-c">
  29. <!-- <image class="base-icon mr-20"
  30. src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images/login/weixin_logo.png"
  31. mode="widthFix" /> -->
  32. 手机号快捷登录
  33. </view>
  34. </up-button>
  35. </template>
  36. <template v-else>
  37. <up-button class="mb-40" color="#28A94B" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" type="primary" shape="circle">
  38. <view class="d-flex a-c j-c">
  39. <!-- <image class="base-icon mr-20"
  40. src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images/login/weixin_logo.png"
  41. mode="widthFix" /> -->
  42. 手机号快捷登录
  43. </view>
  44. </up-button>
  45. </template>
  46. <view class="d-flex a-c pd-10 mb-20" @click="clickAgree">
  47. <up-checkbox activeColor="#2A6D52" size="32rpx" name="agree" usedAlone v-model:checked="form.aloneChecked"></up-checkbox>
  48. <view class="d-flex a-c f-s-24">
  49. <text class="c-999">我已阅读并同意</text>
  50. <text class="c-primary" @click="$u.route({ url: '/views/tool/agreement/agreement' })">《用户协议》</text>
  51. <text class="c-666">、</text>
  52. <text class="c-primary" @click="$u.route({ url: '/views/tool/privacy/privacy' })">《隐私政策》</text>
  53. </view>
  54. </view>
  55. <official-account></official-account>
  56. </view>
  57. </view>
  58. <image class="login-bttom-bg" src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images/login/bg_bottom.png" mode="widthFix" />
  59. </z-paging>
  60. </template>
  61. <script setup>
  62. import { ref, getCurrentInstance } from 'vue';
  63. import { useInfoStore } from '@/store';
  64. import config from '@/config';
  65. import { useRouter } from 'vue-router';
  66. import { getCurrentPage } from '@/utils/public';
  67. import { onLoad } from '@dcloudio/uni-app';
  68. import { updateUserInfo } from '@/utils/common';
  69. import { recursiveDecodeURIComponent } from '@/utils/ruoyi';
  70. import { useClientRequest } from '@/utils/request';
  71. const pages = ref(getCurrentPages());
  72. const infoStore = useInfoStore();
  73. const paging = ref(null);
  74. const redirect = ref('');
  75. // 判断是否绑定手机号
  76. const isBindPhone = ref(0);
  77. const form = ref({
  78. aloneChecked: false,
  79. phoneCode: '',
  80. xcxcode: '',
  81. grantType: 'xcx',
  82. clientId: config.clientId,
  83. // tenantId: config.tenantId,
  84. });
  85. const clickAgree = () => {
  86. form.value.aloneChecked = !form.value.aloneChecked;
  87. };
  88. const checkUserRegist = (data) => {
  89. return useClientRequest.post('/auth/checkUserRegist', data, false);
  90. };
  91. const getPhoneNumber = (e) => {
  92. console.log(e);
  93. // 判读是否获取成功
  94. if (!e.detail.code) {
  95. uni.showToast({
  96. title: '获取手机号失败',
  97. icon: 'none',
  98. });
  99. return;
  100. }
  101. form.value.phoneCode = e.detail.code;
  102. // 判断是否勾选隐私协议
  103. if (!form.value.aloneChecked) {
  104. uni.showToast({
  105. title: '请阅读并同意用户协议和隐私政策',
  106. icon: 'none',
  107. });
  108. return;
  109. }
  110. // 手机号登录方法
  111. handleLogin();
  112. };
  113. const handleLogin = () => {
  114. // 判断是否勾选隐私协议
  115. if (!form.value.aloneChecked) {
  116. uni.showToast({
  117. title: '请阅读并同意用户协议和隐私政策',
  118. icon: 'none',
  119. });
  120. return;
  121. }
  122. // 微信小程序登录获取的code
  123. uni.login({
  124. provider: 'weixin',
  125. success: function (loginRes) {
  126. // 获取用户信息
  127. if (loginRes.errMsg === 'login:ok') {
  128. form.value.xcxcode = loginRes.code;
  129. // 请求登录接口
  130. weixinLogin();
  131. }
  132. },
  133. });
  134. };
  135. const weixinLogin = async () => {
  136. try {
  137. // 请求登录接口
  138. uni.showLoading({
  139. title: '登录中...',
  140. mask: true,
  141. });
  142. await infoStore.wxLogin(form.value);
  143. // 调用获取用户信息接口
  144. infoStore.getUserInfo();
  145. console.log('调用获取用户信息接口');
  146. infoStore.getCompanyInfo();
  147. uni.hideLoading();
  148. // 重定向或跳转首页
  149. // 获取redirect参数
  150. const redirectUrl = redirect.value || '/pages/plant/index';
  151. // switchTab 页面
  152. const switchTabs = ['/pages/plant/index'];
  153. if (switchTabs.includes(redirectUrl)) {
  154. uni.$u.route({
  155. type: 'switchTab',
  156. url: redirectUrl,
  157. });
  158. return;
  159. }
  160. uni.$u.route({
  161. type: 'redirect',
  162. url: redirectUrl,
  163. });
  164. } catch (error) {
  165. uni.showToast({
  166. title: error.message || '登录失败,请稍后重试',
  167. icon: 'none',
  168. });
  169. }
  170. };
  171. // 判断用户是否绑定手机号
  172. const hasBindPhone = async () => {
  173. // 微信小程序登录获取的code
  174. uni.login({
  175. provider: 'weixin',
  176. success: function (loginRes) {
  177. // 获取用户信息
  178. if (loginRes.errMsg === 'login:ok') {
  179. console.log(loginRes, 'loginRes');
  180. checkUserRegist({
  181. code: loginRes.code,
  182. clientId: config.clientId,
  183. }).then((res) => {
  184. if (res.code === 200) {
  185. isBindPhone.value = +res.data;
  186. }
  187. });
  188. }
  189. },
  190. });
  191. };
  192. const homeBack = () => {
  193. const pages = getCurrentPages();
  194. if (pages.length > 1) {
  195. uni.navigateBack();
  196. } else {
  197. const redirectUrl = redirect.value || '/pages/list/index';
  198. const switchTabs = ['/pages/index/index', '/pages/find/index/index', '/pages/cart/index', '/pages/list/index', '/pages/user/index'];
  199. if (switchTabs.includes(redirectUrl)) {
  200. uni.$u.route({
  201. type: 'switchTab',
  202. url: redirectUrl,
  203. });
  204. return;
  205. } else {
  206. uni.$u.route({
  207. type: 'switchTab',
  208. url: '/pages/list/index',
  209. });
  210. }
  211. }
  212. };
  213. onLoad((options) => {
  214. // 获取redirect参数
  215. const redirectStr = options.redirect ? recursiveDecodeURIComponent(options.redirect) : '';
  216. console.log(redirectStr);
  217. redirect.value = redirectStr;
  218. // 判断用户是否绑定手机号
  219. hasBindPhone();
  220. });
  221. </script>
  222. <style lang="scss" scoped>
  223. .login-logo-wrap {
  224. width: 750rpx;
  225. height: 750rpx;
  226. margin: auto;
  227. box-sizing: border-box;
  228. }
  229. .bg-circle {
  230. position: absolute;
  231. left: 0;
  232. top: 0;
  233. bottom: 0;
  234. right: 0;
  235. margin: auto;
  236. width: 770rpx;
  237. height: 770rpx;
  238. // 360度旋转
  239. animation: rotate 10s linear infinite;
  240. }
  241. @keyframes rotate {
  242. from {
  243. transform: rotate(0deg);
  244. }
  245. to {
  246. transform: rotate(360deg);
  247. }
  248. }
  249. .login-logo {
  250. width: 493rpx;
  251. height: 284rpx;
  252. }
  253. .login-bttom-bg {
  254. position: absolute;
  255. left: 0;
  256. right: 0;
  257. bottom: 0;
  258. width: 687rpx;
  259. margin: auto;
  260. // 不能有事件
  261. pointer-events: none;
  262. }
  263. </style>