clientIndex.vue 7.9 KB

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