login.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <!-- 微信公众号的登录回调页 -->
  2. <template>
  3. <view />
  4. <!-- 积分确权 -->
  5. <su-popup :show="state.showProtocol" type="center" round="10" :isMaskClick="false" showClose @close="close">
  6. <view class="head-nav">
  7. <view class="head-box">
  8. <view class="ss-flex ss-m-b-20">
  9. <view class="isActive head-title">
  10. 请选择账号登录
  11. </view>
  12. </view>
  13. </view>
  14. </view>
  15. <view>
  16. {{socialUsers}}
  17. <view v-for="user in socialUsers" :key="user.username" class="ss-flex">
  18. <image :src="user.avatar" style="width:100rpx;height:100rpx" />
  19. <view>{{user.username}}</view>
  20. </view>
  21. </view>
  22. </su-popup>
  23. <su-popup :show="isPopup" round="10" :showClose="false" @close="closeAuthModal">
  24. <view>
  25. <view class="head-box">
  26. <view class="ss-flex ss-m-b-20">
  27. <view class="isActive head-title">
  28. 微信注册
  29. </view>
  30. </view>
  31. </view>
  32. <!-- 表单项 -->
  33. <uni-forms ref="smsLoginRef" v-model="state.model" :rules="state.rules" validateTrigger="bind"
  34. labelWidth="140" labelAlign="center" class="loginUniForm">
  35. <uni-forms-item name="mobile" label="手机号" class="loginUniFormItem">
  36. <uni-easyinput placeholder="请输入手机号" v-model="state.model.mobile" :inputBorder="false" type="number">
  37. <template v-slot:right>
  38. <button class="ss-reset-button code-btn code-btn-start" :disabled="state.isMobileEnd"
  39. :class="{ 'code-btn-end': state.isMobileEnd }"
  40. @tap="getSmsCode('smsLogin', state.model.mobile)">
  41. {{ getSmsTimer('smsLogin') }}
  42. </button>
  43. </template>
  44. </uni-easyinput>
  45. </uni-forms-item>
  46. <uni-forms-item name="code" label="验证码" class="loginUniFormItem">
  47. <uni-easyinput placeholder="请输入验证码" v-model="state.model.code" :inputBorder="false" type="number"
  48. maxlength="4">
  49. </uni-easyinput>
  50. </uni-forms-item>
  51. </uni-forms>
  52. <view style="display: flex;justify-content: space-between;padding: 40rpx;padding-bottom: 0rpx;">
  53. <button class="ss-reset-button login-btn-start" @tap="OfficialEnterLogin"> 继续登录 </button>
  54. </view>
  55. </view>
  56. </su-popup>
  57. </template>
  58. <script setup>
  59. import {
  60. ref,
  61. reactive,
  62. unref,
  63. computed,
  64. onBeforeMount
  65. } from 'vue';
  66. import sheep from '@/sheep';
  67. import {
  68. onLoad
  69. } from '@dcloudio/uni-app';
  70. import {
  71. showAuthModal,
  72. closeAuthModal,
  73. getSmsCode,
  74. getSmsTimer
  75. } from '@/sheep/hooks/useModal';
  76. import {
  77. code,
  78. mobile
  79. } from '@/sheep/validate/form';
  80. import AuthUtil from '@/sheep/api/member/auth';
  81. const smsLoginRef = ref(null);
  82. // 数据
  83. const state = reactive({
  84. socialUsers:[],
  85. loginReqVO: {
  86. type: 31,
  87. code: "",
  88. state: "",
  89. },
  90. isMobileEnd: false, // 手机号输入完毕
  91. codeText: '获取验证码',
  92. model: {
  93. mobile: '', // 手机号
  94. code: '', // 验证码
  95. scene: 1
  96. },
  97. rules: {
  98. code,
  99. mobile,
  100. },
  101. });
  102. const isPopup = ref(false);
  103. const selectSocialUsers = ref(false);
  104. // 定义修改状态的方法
  105. function updateIsPopup() {
  106. isPopup.value = true;
  107. }
  108. function updateSocialUsers(res){
  109. selectSocialUsers.value = true;
  110. state.socialUsers = res
  111. }
  112. defineExpose({
  113. updateIsPopup
  114. });
  115. async function OfficialEnterLogin() {
  116. // 看缓存中有没有linkId 如果有的话 加入model传给后台 即为绑定
  117. const linkId = uni.getStorageSync("linkId")
  118. if(linkId){
  119. state.loginReqVO.linkId = linkId
  120. }
  121. // 参数校验
  122. const validate = await unref(smsLoginRef)
  123. .validate()
  124. .catch((error) => {
  125. console.log('error: ', error);
  126. });
  127. if (!validate) {
  128. return;
  129. }
  130. // 提交数据
  131. const {
  132. code
  133. } = await AuthUtil.OfficialEnterLogin(state.model, state.loginReqVO);
  134. if (code === 0) {
  135. closeAuthModal();
  136. // 检测 H5 登录回调
  137. let returnUrl = uni.getStorageSync('returnUrl');
  138. if (returnUrl) {
  139. uni.removeStorage('returnUrl');
  140. location.replace(returnUrl);
  141. } else {
  142. uni.switchTab({
  143. url: '/',
  144. });
  145. }
  146. }
  147. }
  148. onBeforeMount(async () => {
  149. const options = {}
  150. new URLSearchParams(location.search).forEach((value, key) => {
  151. options[key] = value;
  152. });
  153. state.loginReqVO.code = options.code
  154. state.loginReqVO.state = options.state
  155. })
  156. onLoad(async (options) => {
  157. // #ifdef H5
  158. // 将 search 参数赋值到 options 中,方便下面解析
  159. new URLSearchParams(location.search).forEach((value, key) => {
  160. options[key] = value;
  161. });
  162. const event = options.event;
  163. const code = options.code;
  164. const state = options.state;
  165. if (event === 'login') { // 场景一:登录
  166. const res = await sheep.$platform.useProvider().login(code, state)
  167. console.log("login.vue的res",res)
  168. if (!res) {
  169. updateIsPopup()
  170. return false
  171. }
  172. if (res.data.socialUsers != null || res.data.socialUsers.length > 1){
  173. updateSocialUsers(res.data.socialUsers)
  174. return false
  175. }
  176. } else if (event === 'bind') { // 场景二:绑定
  177. sheep.$platform.useProvider().bind(code, state);
  178. }
  179. // 检测 H5 登录回调
  180. let returnUrl = uni.getStorageSync('returnUrl');
  181. if (returnUrl) {
  182. uni.removeStorage('returnUrl');
  183. location.replace(returnUrl);
  184. } else {
  185. uni.switchTab({
  186. url: '/',
  187. });
  188. }
  189. // #endif
  190. });
  191. </script>
  192. <style lang="scss" scoped>
  193. @keyframes title-animation {
  194. 0% {
  195. font-size: 32rpx;
  196. }
  197. 100% {
  198. font-size: 36rpx;
  199. }
  200. }
  201. .login-wrap {
  202. padding: 50rpx 34rpx;
  203. min-height: 500rpx;
  204. background-color: #fff;
  205. border-radius: 20rpx 20rpx 0 0;
  206. }
  207. .head-box {
  208. padding: 40rpx 40rpx 0;
  209. .head-title {
  210. min-width: 160rpx;
  211. font-size: 36rpx;
  212. font-weight: bold;
  213. color: #333333;
  214. line-height: 36rpx;
  215. }
  216. .head-title-active {
  217. width: 160rpx;
  218. font-size: 32rpx;
  219. font-weight: 600;
  220. color: #999;
  221. line-height: 36rpx;
  222. }
  223. .head-title-animation {
  224. animation-name: title-animation;
  225. animation-duration: 0.1s;
  226. animation-timing-function: ease-out;
  227. animation-fill-mode: forwards;
  228. }
  229. .head-title-line {
  230. position: relative;
  231. &::before {
  232. content: '';
  233. width: 1rpx;
  234. height: 34rpx;
  235. background-color: #e4e7ed;
  236. position: absolute;
  237. left: -30rpx;
  238. top: 50%;
  239. transform: translateY(-50%);
  240. }
  241. }
  242. .head-subtitle {
  243. font-size: 26rpx;
  244. font-weight: 400;
  245. color: #afb6c0;
  246. text-align: left;
  247. display: flex;
  248. }
  249. }
  250. // .code-btn[disabled] {
  251. // background-color: #fff;
  252. // }
  253. .code-btn-start {
  254. width: 160rpx;
  255. height: 56rpx;
  256. line-height: normal;
  257. border: 2rpx solid var(--ui-BG-Main);
  258. border-radius: 28rpx;
  259. font-size: 26rpx;
  260. font-weight: 400;
  261. color: var(--ui-BG-Main);
  262. opacity: 1;
  263. }
  264. .forgot-btn {
  265. width: 160rpx;
  266. line-height: 56rpx;
  267. font-size: 30rpx;
  268. font-weight: 500;
  269. color: #999;
  270. }
  271. .login-btn-start {
  272. width: 158rpx;
  273. height: 56rpx;
  274. line-height: normal;
  275. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  276. border-radius: 28rpx;
  277. font-size: 26rpx;
  278. font-weight: 500;
  279. color: #fff;
  280. }
  281. .type-btn {
  282. padding: 20rpx;
  283. margin: 40rpx auto;
  284. width: 200rpx;
  285. font-size: 30rpx;
  286. font-weight: 500;
  287. color: #999999;
  288. }
  289. .auto-login-box {
  290. width: 100%;
  291. .auto-login-btn {
  292. width: 68rpx;
  293. height: 68rpx;
  294. border-radius: 50%;
  295. margin: 0 30rpx;
  296. }
  297. .auto-login-img {
  298. width: 68rpx;
  299. height: 68rpx;
  300. border-radius: 50%;
  301. }
  302. }
  303. .agreement-box {
  304. margin: 80rpx auto 0;
  305. .protocol-check {
  306. transform: scale(0.7);
  307. }
  308. .agreement-text {
  309. font-size: 26rpx;
  310. font-weight: 500;
  311. color: #999999;
  312. .tcp-text {
  313. color: var(--ui-BG-Main);
  314. }
  315. }
  316. }
  317. // 修改密码
  318. .editPwd-btn-box {
  319. .save-btn {
  320. width: 690rpx;
  321. line-height: 70rpx;
  322. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  323. border-radius: 35rpx;
  324. font-size: 28rpx;
  325. font-weight: 500;
  326. color: #ffffff;
  327. }
  328. .forgot-btn {
  329. width: 690rpx;
  330. line-height: 70rpx;
  331. font-size: 28rpx;
  332. font-weight: 500;
  333. color: #999999;
  334. }
  335. }
  336. .code-btn-start {
  337. color: #55b774;
  338. border: 1px solid #55b774;
  339. }
  340. .agreement-box {
  341. margin: 20rpx 0;
  342. }
  343. .login-btn-start {
  344. background: rgb(14, 147, 46);
  345. width: 100%;
  346. height: 80rpx;
  347. font-size: 32rpx;
  348. }
  349. .loginUniForm {
  350. margin: 40rpx;
  351. border: 1rpx solid #d6d6d6;
  352. padding: 10rpx 15rpx;
  353. border-radius: 10rpx;
  354. }
  355. .loginUniFormItem:first-child {
  356. border-bottom: 1rpx solid #d6d6d6;
  357. padding-bottom: 10rpx;
  358. }
  359. .loginUniFormItem:last-child {
  360. // border-bottom: 1rpx solid #d6d6d6;
  361. padding-top: 10rpx;
  362. }
  363. ::v-deep .loginUniFormItem .uni-forms-item__inner {
  364. padding-bottom: 0;
  365. }
  366. ::v-deep .loginUniFormItem .uni-error-message {
  367. bottom: -20rpx;
  368. }
  369. </style>