initial.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <view class="app-login-page">
  3. <view class="app-login">
  4. <view class="app-icon u-m-b-30 ">
  5. <image src="http://www.gzrea.org.cn:8543/icon/wxmp/login-icon.png"
  6. mode="aspectFit" style="height: 80rpx;"></image>
  7. </view>
  8. <view class="app-title u-m-b-60">
  9. 欢迎使用会员中心系统小程序
  10. </view>
  11. <view style="height: 25vh;">
  12. <u-button v-if="userBook" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber" class="custom-button" style="width: 40px;" plain :hair-line="false">
  13. <view class="login-button">
  14. 本机号码一键登录
  15. </view>
  16. </u-button>
  17. <u-button v-else class="custom-button" style="width: 40px;" plain :hair-line="false" @click="check">
  18. <view class="login-button">
  19. 本机号码一键登录
  20. </view>
  21. </u-button>
  22. <view class="login-button other-button u-m-b-50 u-m-t-50" @click="toLogin">
  23. 其他手机号/账号登录
  24. </view>
  25. </view>
  26. <view class="app-user-book mb-30">
  27. <u-checkbox-group style="display: flex;justify-content: center;">
  28. <u-checkbox v-model="userBook" name="yes" shape="circle">我已阅读&#x300A;运营商一键登录服务协议&#x300B;</u-checkbox>
  29. </u-checkbox-group>
  30. </view>
  31. </view>
  32. <u-modal v-model="userBookModal" mask-close-able class="modal" show-cancel-button
  33. :title-style="{fontWeight: 'bold'}" @confirm="confirmModal">
  34. <view class="modal-content">
  35. 为了更好地保障你的合法权益,进行下一步前,请阅读并同意
  36. <text class="content-a">&#x300A;运营商一键登录服务协议&#x300B;</text>
  37. </view>
  38. </u-modal>
  39. </view>
  40. </template>
  41. <script setup>
  42. import { wxLoginOpenid, wxloginPhone } from '@/api/login.js'
  43. import {
  44. msg,
  45. msgSuccess
  46. } from "@/utils/common.js"
  47. import { ref } from 'vue'
  48. import {
  49. useAuthStore
  50. } from '@/store/authStore'
  51. import {
  52. me
  53. } from '@/api/user.js'
  54. const authStore = useAuthStore()
  55. const userBook = ref(false)
  56. const userBookModal = ref(false)
  57. function toLogin() {
  58. uni.navigateTo({
  59. url: "/pages/login/login"
  60. })
  61. }
  62. async function wxlogin(){
  63. const loginRes = await wx.login();
  64. const code = loginRes.code;
  65. wxLoginOpenid({code}).then(res=>{
  66. if(res?.data){
  67. const { openid, sessionKey } = res.data
  68. }
  69. })
  70. }
  71. function confirmModal() {
  72. userBook.value = true
  73. // this.wxlogin()
  74. }
  75. function checkUserBook() {
  76. if (!userBook.value) {
  77. userBookModal.value = true
  78. return false;
  79. }
  80. return true
  81. }
  82. function check(){
  83. if (!checkUserBook()) return;
  84. }
  85. async function decryptPhoneNumber(e){
  86. if (e.detail.encryptedData) {
  87. const {
  88. encryptedData,
  89. iv,
  90. code
  91. } = e.detail;
  92. const loginRes = await wx.login();
  93. const wxcode = loginRes.code;
  94. try{
  95. const openidRes = await wxLoginOpenid({code : wxcode})
  96. if(openidRes?.data){
  97. const { openid, sessionKey } = openidRes.data
  98. authStore.setOpenid(openid);
  99. const phoneRes = await wxloginPhone({ openid, phoneCode: code})
  100. const { phone, isNewUser } = phoneRes.data;
  101. authStore.setPhone(phone);
  102. // uni.reLaunch({
  103. // url: "/pages/register/register?mode=refine"
  104. // })
  105. if(isNewUser){
  106. //新用户,跳转:完善用户信息
  107. // console.log("完善用户信息")
  108. uni.reLaunch({
  109. url: "/pages/register/register?mode=refine"
  110. })
  111. }else{
  112. const { token } = phoneRes.data;
  113. authStore.setAuthToken(token);
  114. // 老用户,跳转至:首页
  115. msgSuccess("授权成功!")
  116. let userRes = await me();
  117. if(userRes && userRes?.code===0){
  118. authStore.setUserInfo(userRes.data)
  119. setTimeout(()=>{
  120. uni.switchTab({
  121. url: "/pages/index/index"
  122. })
  123. }, 1000)
  124. }
  125. }
  126. }
  127. }catch(err){
  128. console.log("ee", err)
  129. }
  130. // 将 encryptedData 和 iv 发送到后端进行解密
  131. } else {
  132. msg('取消授权手机号');
  133. }
  134. }
  135. </script>
  136. <style lang="scss">
  137. .form-item .u-input--border {
  138. border-radius: 16rpx !important;
  139. }
  140. .custom-button {
  141. .u-btn {
  142. display: block !important;
  143. width: fit-content;
  144. padding: 0 !important;
  145. border-radius: 50rpx;
  146. }
  147. .u-btn--default {
  148. border: none !important;
  149. background-color: transparent !important;
  150. }
  151. }
  152. </style>
  153. <style lang="scss" scoped>
  154. .app-login-page {
  155. background-color: #ffffff;
  156. width: 100vw;
  157. height: 100vh;
  158. display: flex;
  159. justify-content: center;
  160. align-items: center;
  161. }
  162. .app-login {
  163. width: 90%;
  164. }
  165. .app-icon {
  166. display: flex;
  167. justify-content: center;
  168. align-items: center;
  169. height: 90rpx;
  170. text-align: center;
  171. }
  172. .app-title {
  173. text-align: center;
  174. font-size: 44rpx;
  175. letter-spacing: 2px;
  176. font-weight: 700;
  177. }
  178. .mb-30 {
  179. margin-bottom: 30rpx;
  180. }
  181. .login-button {
  182. background: linear-gradient(90deg, #006af5, #21b0fc);
  183. border-radius: 20rpx;
  184. line-height: 80rpx;
  185. text-align: center;
  186. color: #fff;
  187. margin-bottom: 50rpx;
  188. width: 550rpx;
  189. margin: 0 auto;
  190. font-size: 32rpx;
  191. letter-spacing: 4rpx;
  192. }
  193. .other-button{
  194. color: #006af5;
  195. background: #e5e5e5;
  196. }
  197. // 用户手册模拟态
  198. .modal {
  199. .modal-content {
  200. padding: 50rpx;
  201. .content-a {
  202. color: #2979ff;
  203. }
  204. }
  205. }
  206. </style>