login.vue 12 KB

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