login.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <!-- 微信公众号的登录回调页 -->
  2. <template>
  3. <!-- 空登陆页 -->
  4. <view />
  5. <su-popup :show="state.isPopup" round="10" :showClose="false" @close="closeAuthModal">
  6. <view>
  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. <!-- 表单项 -->
  15. <uni-forms ref="smsLoginRef" v-model="state.model" :rules="state.rules" validateTrigger="bind"
  16. labelWidth="140" labelAlign="center" class="loginUniForm">
  17. <uni-forms-item name="mobile" label="手机号" class="loginUniFormItem">
  18. <uni-easyinput placeholder="请输入手机号" v-model="state.model.mobile" :inputBorder="false" type="number">
  19. <template v-slot:right>
  20. <button class="ss-reset-button code-btn code-btn-start" :disabled="state.isMobileEnd"
  21. :class="{ 'code-btn-end': state.isMobileEnd }"
  22. @tap="getSmsCode('smsLogin', state.model.mobile)">
  23. {{ getSmsTimer('smsLogin') }}
  24. </button>
  25. </template>
  26. </uni-easyinput>
  27. </uni-forms-item>
  28. <uni-forms-item name="code" label="验证码" class="loginUniFormItem">
  29. <uni-easyinput placeholder="请输入验证码" v-model="state.model.code" :inputBorder="false" type="number"
  30. maxlength="4">
  31. </uni-easyinput>
  32. </uni-forms-item>
  33. </uni-forms>
  34. <view style="display: flex;justify-content: space-between;padding: 40rpx;padding-bottom: 0rpx;">
  35. <button class="ss-reset-button login-btn-start" @tap="OfficialEnterLogin"> 继续登录 </button>
  36. </view>
  37. </view>
  38. </su-popup>
  39. </template>
  40. <script setup>
  41. import {
  42. ref,
  43. reactive,
  44. unref,
  45. computed,
  46. onBeforeMount
  47. } from 'vue';
  48. import sheep from '@/sheep';
  49. import {
  50. getRootUrl
  51. } from '@/sheep/helper';
  52. import {
  53. onLoad
  54. } from '@dcloudio/uni-app';
  55. import {
  56. showAuthModal,
  57. closeAuthModal,
  58. getSmsCode,
  59. getSmsTimer
  60. } from '@/sheep/hooks/useModal';
  61. import {
  62. code,
  63. mobile
  64. } from '@/sheep/validate/form';
  65. import AuthUtil from '@/sheep/api/member/auth';
  66. const smsLoginRef = ref(null);
  67. // 数据
  68. const state = reactive({
  69. isPopup: true,
  70. loginReqVO: {
  71. type: 31,
  72. code: "",
  73. state: "",
  74. },
  75. isMobileEnd: false, // 手机号输入完毕
  76. codeText: '获取验证码',
  77. model: {
  78. mobile: '', // 手机号
  79. code: '', // 验证码
  80. scene: 1
  81. },
  82. rules: {
  83. code,
  84. mobile,
  85. },
  86. });
  87. // 获取公众号登陆地址
  88. async function getLoginUrl(event = 'login') {
  89. const page = getRootUrl() + 'pages/index/login' +
  90. '?event=' + event; // event 目的,区分是 login 还是 bind
  91. const {
  92. code,
  93. data
  94. } = await AuthUtil.socialAuthRedirect(31, page);
  95. if (code !== 0) {
  96. return undefined;
  97. }
  98. return data;
  99. }
  100. // 微信公众号登陆
  101. async function login(code = '', state = '') {
  102. // 情况一:没有 code 时,去获取 code
  103. if (!code) {
  104. // 情况二:有 code 时,使用 code 去自动登录
  105. } else {
  106. // 解密 code 发起登陆
  107. const loginResult = await AuthUtil.socialLogin(31, code, state);
  108. console.log("loginResult真正的登录结果:",loginResult)
  109. if (loginResult.code === 0) {
  110. // TODO 非繁人:shareLog
  111. setOpenid(loginResult.data.openid);
  112. return loginResult;
  113. }
  114. }
  115. return false;
  116. }
  117. // 短信登录
  118. async function OfficialEnterLogin() {
  119. // 参数校验
  120. const validate = await unref(smsLoginRef)
  121. .validate()
  122. .catch((error) => {
  123. console.log('error: ', error);
  124. });
  125. if (!validate) {
  126. return;
  127. }
  128. const loginUrl = await getLoginUrl();
  129. if (loginUrl) {
  130. uni.setStorageSync('returnUrl', location.href);
  131. window.location = loginUrl;
  132. }
  133. // 提交数据
  134. // const {
  135. // code
  136. // } = await AuthUtil.OfficialEnterLogin(state.model, state.loginReqVO);
  137. if (code === 0) {
  138. closeAuthModal();
  139. // 检测 H5 登录回调
  140. let returnUrl = uni.getStorageSync('returnUrl');
  141. if (returnUrl) {
  142. uni.removeStorage('returnUrl');
  143. location.replace(returnUrl);
  144. } else {
  145. uni.switchTab({
  146. url: '/',
  147. });
  148. }
  149. }
  150. }
  151. onBeforeMount(async () => {
  152. const options = {}
  153. new URLSearchParams(location.search).forEach((value, key) => {
  154. options[key] = value;
  155. });
  156. state.loginReqVO.code = options.code
  157. state.loginReqVO.state = options.state
  158. })
  159. onLoad(async (options) => {
  160. // #ifdef H5
  161. // 将 search 参数赋值到 options 中,方便下面解析
  162. new URLSearchParams(location.search).forEach((value, key) => {
  163. options[key] = value;
  164. });
  165. const event = options.event;
  166. const code = options.code;
  167. const state = options.state;
  168. if (event === 'login') { // 场景一:登录
  169. const res = await sheep.$platform.useProvider().login(code, state)
  170. if (!res) {
  171. state.isPopup = true
  172. return false
  173. }
  174. } else if (event === 'bind') { // 场景二:绑定
  175. sheep.$platform.useProvider().bind(code, state);
  176. }
  177. // 检测 H5 登录回调
  178. let returnUrl = uni.getStorageSync('returnUrl');
  179. if (returnUrl) {
  180. uni.removeStorage('returnUrl');
  181. location.replace(returnUrl);
  182. } else {
  183. uni.switchTab({
  184. url: '/',
  185. });
  186. }
  187. // #endif
  188. });
  189. </script>
  190. <style lang="scss" scoped>
  191. @keyframes title-animation {
  192. 0% {
  193. font-size: 32rpx;
  194. }
  195. 100% {
  196. font-size: 36rpx;
  197. }
  198. }
  199. .login-wrap {
  200. padding: 50rpx 34rpx;
  201. min-height: 500rpx;
  202. background-color: #fff;
  203. border-radius: 20rpx 20rpx 0 0;
  204. }
  205. .head-box {
  206. padding: 40rpx 40rpx 0;
  207. .head-title {
  208. min-width: 160rpx;
  209. font-size: 36rpx;
  210. font-weight: bold;
  211. color: #333333;
  212. line-height: 36rpx;
  213. }
  214. .head-title-active {
  215. width: 160rpx;
  216. font-size: 32rpx;
  217. font-weight: 600;
  218. color: #999;
  219. line-height: 36rpx;
  220. }
  221. .head-title-animation {
  222. animation-name: title-animation;
  223. animation-duration: 0.1s;
  224. animation-timing-function: ease-out;
  225. animation-fill-mode: forwards;
  226. }
  227. .head-title-line {
  228. position: relative;
  229. &::before {
  230. content: '';
  231. width: 1rpx;
  232. height: 34rpx;
  233. background-color: #e4e7ed;
  234. position: absolute;
  235. left: -30rpx;
  236. top: 50%;
  237. transform: translateY(-50%);
  238. }
  239. }
  240. .head-subtitle {
  241. font-size: 26rpx;
  242. font-weight: 400;
  243. color: #afb6c0;
  244. text-align: left;
  245. display: flex;
  246. }
  247. }
  248. // .code-btn[disabled] {
  249. // background-color: #fff;
  250. // }
  251. .code-btn-start {
  252. width: 160rpx;
  253. height: 56rpx;
  254. line-height: normal;
  255. border: 2rpx solid var(--ui-BG-Main);
  256. border-radius: 28rpx;
  257. font-size: 26rpx;
  258. font-weight: 400;
  259. color: var(--ui-BG-Main);
  260. opacity: 1;
  261. }
  262. .forgot-btn {
  263. width: 160rpx;
  264. line-height: 56rpx;
  265. font-size: 30rpx;
  266. font-weight: 500;
  267. color: #999;
  268. }
  269. .login-btn-start {
  270. width: 158rpx;
  271. height: 56rpx;
  272. line-height: normal;
  273. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  274. border-radius: 28rpx;
  275. font-size: 26rpx;
  276. font-weight: 500;
  277. color: #fff;
  278. }
  279. .type-btn {
  280. padding: 20rpx;
  281. margin: 40rpx auto;
  282. width: 200rpx;
  283. font-size: 30rpx;
  284. font-weight: 500;
  285. color: #999999;
  286. }
  287. .auto-login-box {
  288. width: 100%;
  289. .auto-login-btn {
  290. width: 68rpx;
  291. height: 68rpx;
  292. border-radius: 50%;
  293. margin: 0 30rpx;
  294. }
  295. .auto-login-img {
  296. width: 68rpx;
  297. height: 68rpx;
  298. border-radius: 50%;
  299. }
  300. }
  301. .agreement-box {
  302. margin: 80rpx auto 0;
  303. .protocol-check {
  304. transform: scale(0.7);
  305. }
  306. .agreement-text {
  307. font-size: 26rpx;
  308. font-weight: 500;
  309. color: #999999;
  310. .tcp-text {
  311. color: var(--ui-BG-Main);
  312. }
  313. }
  314. }
  315. // 修改密码
  316. .editPwd-btn-box {
  317. .save-btn {
  318. width: 690rpx;
  319. line-height: 70rpx;
  320. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  321. border-radius: 35rpx;
  322. font-size: 28rpx;
  323. font-weight: 500;
  324. color: #ffffff;
  325. }
  326. .forgot-btn {
  327. width: 690rpx;
  328. line-height: 70rpx;
  329. font-size: 28rpx;
  330. font-weight: 500;
  331. color: #999999;
  332. }
  333. }
  334. .code-btn-start {
  335. color: #55b774;
  336. border: 1px solid #55b774;
  337. }
  338. .agreement-box {
  339. margin: 20rpx 0;
  340. }
  341. .login-btn-start {
  342. background: rgb(14, 147, 46);
  343. width: 100%;
  344. height: 80rpx;
  345. font-size: 32rpx;
  346. }
  347. .loginUniForm {
  348. margin: 40rpx;
  349. border: 1rpx solid #d6d6d6;
  350. padding: 10rpx 15rpx;
  351. border-radius: 10rpx;
  352. }
  353. .loginUniFormItem:first-child {
  354. border-bottom: 1rpx solid #d6d6d6;
  355. padding-bottom: 10rpx;
  356. }
  357. .loginUniFormItem:last-child {
  358. // border-bottom: 1rpx solid #d6d6d6;
  359. padding-top: 10rpx;
  360. }
  361. ::v-deep .loginUniFormItem .uni-forms-item__inner {
  362. padding-bottom: 0;
  363. }
  364. ::v-deep .loginUniFormItem .uni-error-message {
  365. bottom: -20rpx;
  366. }
  367. </style>