login.vue 10 KB

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