login.vue 12 KB

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