AliyunCaptcha copy.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <template>
  2. <view class="phone-login-page">
  3. <view class="login-wrap-box">
  4. <view class="bjcx-head-box">
  5. <icon class="bjcx-logo-box"></icon>
  6. <view class="bjcx-logo-title">家政学</view>
  7. </view>
  8. <view v-if="fromAppShare" class="wechat-auth-box">
  9. <view class="auth-tips">欢迎使用小程序</view>
  10. <button class="wechat-auth-btn" open-type="getPhoneNumber"@getphonenumber="onWechatAuth">
  11. 微信一键登录
  12. </button>
  13. </view>
  14. </view>
  15. </view>
  16. </template>
  17. <script setup>
  18. import cacheManager from '@/utils/cacheManager.js'
  19. import * as httpApi from "@/api/login.js"
  20. import {
  21. ref,
  22. onMounted
  23. } from "vue"
  24. import {
  25. toast
  26. } from "@/utils/common";
  27. import {
  28. useIsCanBack
  29. } from "@/store/isCanBack.js"
  30. import {
  31. onLoad
  32. } from "@dcloudio/uni-app";
  33. // 原有变量
  34. const userName = ref('')
  35. const password = ref('')
  36. const showPassword = ref(true)
  37. const clearTelIcon = ref(false)
  38. const clearPwIcon = ref(false)
  39. const isAgreed = ref(false)
  40. const id = ref('')
  41. const code = ref('')
  42. const fromPage = ref('');
  43. // 新增变量
  44. const fromAppShare = ref(true) // 是否来自APP分享
  45. const store = useIsCanBack();
  46. onLoad((options) => {
  47. // uni.showLoading({
  48. // title: '登录中'
  49. // })
  50. const loginRes = uni.login().then(res=>{
  51. console.log('loginRes', loginRes);
  52. code.value = loginRes.code
  53. const result = httpApi.loginApplet({
  54. code: loginRes.code,
  55. }).then(res=>{
  56. console.log('result', result);
  57. console.log('result', result);
  58. })
  59. })
  60. // 调用后端微信登录接口
  61. // // 判断是否来自APP分享
  62. // if (options.from === 'appcx') {
  63. // fromAppShare.value = true
  64. // id.value = options.id
  65. // // APP分享过来直接显示微信授权界面,不需要自动弹窗
  66. // fromPage.value = options.from;
  67. // }
  68. getAllImg();
  69. })
  70. // 切换到普通登录
  71. function switchToNormalLogin() {
  72. fromAppShare.value = false
  73. }
  74. function phoneBind() {
  75. uni.navigateTo({
  76. url: '/pages/Login/AliyunCaptcha?code='+code.value,
  77. events: {
  78. getCaptchaVerifyParam: (captchaVerifyParam) => {
  79. // 接收到验证码返回结果
  80. console.log('验证码返回结果:', captchaVerifyParam)
  81. }
  82. },
  83. success: () => {
  84. console.log('跳转到验证码页面成功')
  85. },
  86. fail: (err) => {
  87. console.error('跳转到验证码页面失败:', err)
  88. toast('验证码加载失败,请重试')
  89. }
  90. })
  91. }
  92. // 微信授权登录
  93. async function onWechatAuth(e) {
  94. if (e.detail.errMsg.includes('fail')) {
  95. toast('授权失败')
  96. return
  97. }
  98. uni.showLoading({
  99. title: '登录中'
  100. })
  101. console.log('e123123123', e);
  102. try {
  103. // 获取微信code
  104. // const loginRes = await uni.login()
  105. // console.log('loginRes', loginRes);
  106. // code.value = loginRes.code
  107. // // 调用后端微信登录接口
  108. // const result = await httpApi.loginApplet({
  109. // code: loginRes.code,
  110. // userInfo: e.detail.userInfo
  111. // })
  112. // console.log('result', result);
  113. // console.log('result', result);
  114. //phoneBind()
  115. // if (result.data.bind) {
  116. // // 保存用户信息
  117. // if (result.data.loginVo.type == 4) {
  118. // cacheManager.set('auth', result.data.loginVo)
  119. // store.setIsCanBack(false)
  120. // gotoPage();
  121. // } else if (result.data.loginVo.type == 6) {
  122. // cacheManager.set('auth', result.data.loginVo)
  123. // store.setIsCanBack(false)
  124. // gotoPage2();
  125. // } else {
  126. // toast('登录失败,您的身份有误,请联系管理员。')
  127. // }
  128. // } else {
  129. // // bind 为 false 未绑定
  130. // phoneBind()
  131. // }
  132. } catch (error) {
  133. toast('微信登录失败')
  134. } finally {
  135. uni.hideLoading()
  136. }
  137. }
  138. // 你原有的所有方法都不变
  139. function handleChange() {
  140. isAgreed.value = !isAgreed.value
  141. }
  142. function agreeBtn(code) {
  143. if (code === 'yhxy') {
  144. uni.navigateTo({
  145. url: "/pages/client/my/xieyi"
  146. })
  147. } else {
  148. uni.navigateTo({
  149. url: "/pages/client/my/zhengce"
  150. })
  151. }
  152. }
  153. function userInputChange(event) {
  154. if (event.detail.value.length > 0) {
  155. clearTelIcon.value = true;
  156. } else {
  157. clearTelIcon.value = false;
  158. }
  159. }
  160. function passwordInputChange(event) {
  161. if (event.detail.value.length > 0) {
  162. clearPwIcon.value = true;
  163. } else {
  164. clearPwIcon.value = false;
  165. }
  166. }
  167. function clearTel() {
  168. userName.value = '';
  169. clearTelIcon.value = false;
  170. }
  171. function clearPw() {
  172. password.value = '';
  173. clearPwIcon.value = false;
  174. }
  175. function changePassword() {
  176. showPassword.value = !showPassword.value;
  177. }
  178. function handleLogin() {
  179. if (userName.value.length === 0) {
  180. toast('请输入手机号!')
  181. return
  182. }
  183. if (password.value.length === 0) {
  184. toast('请输入密码!')
  185. return
  186. }
  187. const trimmedUserName = userName.value;
  188. const trimmedPassword = password.value;
  189. uni.showLoading({
  190. title: '登录中'
  191. })
  192. httpApi.loginTemp({
  193. type: 2,
  194. userName: trimmedUserName,
  195. password: trimmedPassword,
  196. }).then(res => {
  197. if (res.data.type === 4) {
  198. cacheManager.set('auth', res.data)
  199. store.setIsCanBack(false)
  200. gotoPage();
  201. } else if (res.data.type === 6) {
  202. cacheManager.set('auth', res.data)
  203. store.setIsCanBack(false)
  204. gotoPage2();
  205. } else {
  206. toast('登录失败,您的身份有误,请联系管理员。')
  207. }
  208. }).catch(err => {
  209. store.setIsCanBack(true)
  210. }).finally(err => {
  211. uni.hideLoading()
  212. })
  213. }
  214. function gotoPage() {
  215. console.log('ididid',id.value);
  216. if (id.value) {
  217. uni.navigateTo({
  218. url: `/pages/client/hetong/hetongInfo?id=` + id.value + `&from=${fromPage.value}`
  219. })
  220. } else {
  221. uni.navigateTo({
  222. url: `/pages/client/ShouYe/shouye`
  223. })
  224. }
  225. }
  226. function gotoPage2() {
  227. console.log('ididid222222',id.value);
  228. if (id.value) {
  229. uni.navigateTo({
  230. url: `/pages/kehu/hetong/hetongInfo?id=` + id.value + `&from=${fromPage.value}`
  231. })
  232. } else {
  233. uni.navigateTo({
  234. url: `/pages/kehu/shouYe/shouye`
  235. })
  236. }
  237. }
  238. function getAllImg() {
  239. httpApi.getAllImgList({}).then(res => {
  240. cacheManager.set('projectImg', res.data)
  241. });
  242. }
  243. </script>
  244. <style scoped>
  245. /* 新增样式 */
  246. .wechat-auth-box {
  247. text-align: center;
  248. padding: 40rpx 0;
  249. }
  250. .auth-tips {
  251. font-size: 32rpx;
  252. color: #333;
  253. margin-bottom: 60rpx;
  254. }
  255. .wechat-auth-btn {
  256. background-color: #07C160;
  257. color: white;
  258. border-radius: 50rpx;
  259. margin-bottom: 30rpx;
  260. }
  261. .switch-login {
  262. color: #007AFF;
  263. font-size: 28rpx;
  264. }
  265. /* 你原有的所有样式都保持不变 */
  266. </style>