login.vue 11 KB

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