login.vue 11 KB

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