clientIndex.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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分享过来的微信授权 -->
  9. <view v-if="fromAppShare && !showNormalLogin" class="wechat-auth-box">
  10. <view class="auth-tips">欢迎使用小程序</view>
  11. <button
  12. class="wechat-auth-btn"
  13. open-type="getUserInfo"
  14. @getuserinfo="onWechatAuth"
  15. >
  16. 微信一键登录
  17. </button>
  18. <view class="switch-login" @click="showNormalLogin = true">使用账号登录</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} 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(false) // 是否来自APP分享
  67. const showNormalLogin = ref(false) // 是否显示普通登录
  68. const store = useIsCanBack();
  69. onLoad((options) => {
  70. // 判断是否来自APP分享
  71. if (options.from === 'appcx') {
  72. fromAppShare.value = true
  73. }
  74. getAllImg();
  75. })
  76. // 微信授权登录
  77. async function onWechatAuth(e) {
  78. if (e.detail.errMsg.includes('fail')) {
  79. toast('授权失败')
  80. return
  81. }
  82. uni.showLoading({ title: '登录中' })
  83. try {
  84. // 获取微信code
  85. const loginRes = await uni.login()
  86. // 调用后端微信登录接口
  87. const result = await httpApi.wechatLogin({
  88. code: loginRes.code,
  89. userInfo: e.detail.userInfo
  90. })
  91. if (result.data.success) {
  92. // 保存用户信息
  93. cacheManager.set('auth', result.data)
  94. // 检查手机号绑定状态
  95. const bindResult = await httpApi.checkPhoneBind()
  96. if (bindResult.data.hasPhone) {
  97. // 已绑定手机号,进入首页
  98. store.setIsCanBack(false)
  99. gotoPage()
  100. } else {
  101. // 未绑定手机号,跳转绑定页面
  102. uni.navigateTo({
  103. url: '/pages/bind-phone/bind-phone'
  104. })
  105. }
  106. }
  107. } catch (error) {
  108. toast('微信登录失败')
  109. } finally {
  110. uni.hideLoading()
  111. }
  112. }
  113. // 你原有的所有方法都不变
  114. function handleChange() {
  115. isAgreed.value = !isAgreed.value
  116. }
  117. function agreeBtn(code) {
  118. if (code === 'yhxy') {
  119. uni.navigateTo({
  120. url:"/pages/client/my/xieyi"
  121. })
  122. } else {
  123. uni.navigateTo({
  124. url:"/pages/client/my/zhengce"
  125. })
  126. }
  127. }
  128. function userInputChange(event){
  129. if (event.detail.value.length > 0) {
  130. clearTelIcon.value = true;
  131. } else {
  132. clearTelIcon.value = false;
  133. }
  134. }
  135. function passwordInputChange(event){
  136. if (event.detail.value.length > 0) {
  137. clearPwIcon.value = true;
  138. } else {
  139. clearPwIcon.value = false;
  140. }
  141. }
  142. function clearTel(){
  143. userName.value = '';
  144. clearTelIcon.value = false;
  145. }
  146. function clearPw(){
  147. password.value = '';
  148. clearPwIcon.value = false;
  149. }
  150. function changePassword() {
  151. showPassword.value = !showPassword.value;
  152. }
  153. function handleLogin() {
  154. if(userName.value.length ===0){
  155. toast('请输入手机号!')
  156. return
  157. }
  158. if(password.value.length ===0){
  159. toast('请输入密码!')
  160. return
  161. }
  162. const trimmedUserName = userName.value;
  163. const trimmedPassword = password.value;
  164. uni.showLoading({
  165. title: '登录中'
  166. })
  167. httpApi.loginTemp({
  168. type:2,
  169. userName: trimmedUserName,
  170. password:trimmedPassword,
  171. }).then(res => {
  172. if(res.data.type ===4){
  173. cacheManager.set('auth', res.data)
  174. store.setIsCanBack(false)
  175. gotoPage();
  176. }else{
  177. toast('登录失败,您的身份有误,请联系管理员。')
  178. }
  179. }).catch(err => {
  180. store.setIsCanBack(true)
  181. }).finally(err => {
  182. uni.hideLoading()
  183. })
  184. }
  185. function gotoPage(){
  186. uni.navigateTo({
  187. url: `/pages/client/ShouYe/shouye`
  188. })
  189. }
  190. function getAllImg() {
  191. httpApi.getAllImgList({}).then(res => {
  192. cacheManager.set('projectImg', res.data)
  193. });
  194. }
  195. </script>
  196. <style scoped>
  197. /* 新增样式 */
  198. .wechat-auth-box {
  199. text-align: center;
  200. padding: 40rpx 0;
  201. }
  202. .auth-tips {
  203. font-size: 32rpx;
  204. color: #333;
  205. margin-bottom: 60rpx;
  206. }
  207. .wechat-auth-btn {
  208. background-color: #07C160;
  209. color: white;
  210. border-radius: 50rpx;
  211. margin-bottom: 30rpx;
  212. }
  213. .switch-login {
  214. color: #007AFF;
  215. font-size: 28rpx;
  216. }
  217. /* 你原有的所有样式都保持不变 */
  218. </style>