index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <template>
  2. <div class="login-wrapper">
  3. <div class="shading">
  4. <image src="/static/img/logo.png" />
  5. </div>
  6. <div class="whiteBg">
  7. <div class="list" v-if="current !== 1">
  8. <form @submit.prevent="submit">
  9. <div class="item">
  10. <div class="acea-row row-middle">
  11. <image src="/static/images/phone_1.png" style="width: 24rpx; height: 34rpx;" />
  12. <input type="text" class="texts" placeholder="输入手机号码" v-model="account" required/>
  13. </div>
  14. </div>
  15. <div class="item">
  16. <div class="acea-row row-middle">
  17. <image src="/static/images/code_2.png" style="width: 28rpx; height: 32rpx;"></image>
  18. <input type="password" class="texts" placeholder="填写登录密码" v-model="password" required />
  19. </div>
  20. </div>
  21. </form>
  22. </div>
  23. <div class="list" v-if="current !== 0 || appLoginStatus || appleLoginStatus">
  24. <div class="item">
  25. <div class="acea-row row-middle">
  26. <image src="/static/images/phone_1.png" style="width: 24rpx; height: 34rpx;"></image>
  27. <input type="text" class="texts" placeholder="输入手机号码" v-model="account" />
  28. </div>
  29. </div>
  30. <div class="item">
  31. <div class="acea-row row-middle">
  32. <image src="/static/images/code_2.png" style="width: 28rpx; height: 32rpx;"></image>
  33. <input type="text" placeholder="填写验证码" class="codeIput" v-model="captcha" />
  34. <button class="code" :disabled="disabled" :class="disabled === true ? 'on' : ''" @click="code">
  35. {{ text }}
  36. </button>
  37. </div>
  38. </div>
  39. <div class="item" v-if="isShowCode">
  40. <div class="acea-row row-middle">
  41. <image src="/static/images/code_2.png" style="width: 28rpx; height: 32rpx;"></image>
  42. <input type="text" placeholder="填写验证码" class="codeIput" v-model="codeVal" />
  43. </div>
  44. </div>
  45. </div>
  46. <!-- 登录方式一:验证码登录 -->
  47. <div class="logon" @click="loginMobile" v-if="current !== 0">登录</div>
  48. <!-- 登录方式二:密码登录 -->
  49. <div class="logon" @click="submit" v-if="current === 0">登录</div>
  50. <div class="tips">
  51. <div v-if="current === 0" @click="current = 1">快速登录</div>
  52. <div v-if="current === 1" @click="current = 0">账号登录</div>
  53. </div>
  54. </div>
  55. <div class="bottom"></div>
  56. </div>
  57. </template>
  58. <script>
  59. import sendVerifyCode from "@/mixins/SendVerifyCode";
  60. import * as AuthApi from "@/api/member/auth";
  61. import * as UserApi from "@/api/member/user";
  62. import { appAuth, appleLogin } from "@/api/public";
  63. const BACK_URL = "login_back_url";
  64. import * as BrokerageAPI from '@/api/trade/brokerage.js'
  65. import Routine from '@/libs/routine.js';
  66. export default {
  67. name: "Login",
  68. mixins: [sendVerifyCode],
  69. data: function() {
  70. return {
  71. current: 1, // 1:快速登录;2:账号登录
  72. account: "",
  73. password: "",
  74. captcha: "",
  75. type: "login",
  76. codeVal: "",
  77. isShowCode: false,
  78. platform: '',
  79. appLoginStatus: false, // 微信登录强制绑定手机号码状态
  80. appUserInfo: null, // 微信登录保存的用户信息
  81. appleLoginStatus: false, // 苹果登录强制绑定手机号码状态
  82. appleUserInfo: null,
  83. appleShow: false // 苹果登录版本必须要求ios13以上的
  84. };
  85. },
  86. onLoad() {
  87. let self = this
  88. uni.getSystemInfo({
  89. success: function(res) {
  90. if (res.platform.toLowerCase() === 'ios' && res.system.split(' ')[1] >= 13) {
  91. self.appleShow = true
  92. }
  93. }
  94. });
  95. },
  96. methods: {
  97. // 苹果登录
  98. appleLogin() {
  99. let self = this
  100. this.account = ''
  101. this.captcha = ''
  102. uni.showLoading({
  103. title: '登录中'
  104. })
  105. uni.login({
  106. provider: 'apple',
  107. timeout: 10000,
  108. success(loginRes) {
  109. uni.getUserInfo({
  110. provider: 'apple',
  111. success: function(infoRes) {
  112. self.appleUserInfo = infoRes.userInfo
  113. self.appleLoginApi()
  114. },
  115. fail() {
  116. uni.hideLoading()
  117. uni.showToast({
  118. title: '获取用户信息失败',
  119. icon: 'none',
  120. duration: 2000
  121. })
  122. },
  123. complete() {
  124. uni.hideLoading()
  125. }
  126. });
  127. },
  128. fail(error) {
  129. uni.hideLoading()
  130. console.log(error)
  131. }
  132. })
  133. },
  134. // 苹果登录Api
  135. appleLoginApi() {
  136. let self = this
  137. appleLogin({
  138. openId: self.appleUserInfo.openId,
  139. email: self.appleUserInfo.email == undefined ? '' :self.appleUserInfo.email,
  140. identityToken: self.appleUserInfo.identityToken || ''
  141. }).then((res) => {
  142. this.$store.commit("LOGIN", {
  143. 'token': res.data.token
  144. });
  145. this.getUserInfo(res.data);
  146. }).catch(error => {
  147. uni.hideLoading();
  148. uni.showModal({
  149. title: '提示',
  150. content: `错误信息${error}`,
  151. success: function(res) {
  152. if (res.confirm) {
  153. console.log('用户点击确定');
  154. } else if (res.cancel) {
  155. console.log('用户点击取消');
  156. }
  157. }
  158. });
  159. })
  160. },
  161. // App微信登录
  162. wxLogin() {
  163. let self = this
  164. this.account = ''
  165. this.captcha = ''
  166. uni.showLoading({
  167. title: '登录中'
  168. })
  169. uni.login({
  170. provider: 'weixin',
  171. success: function(loginRes) {
  172. // 获取用户信息
  173. uni.getUserInfo({
  174. provider: 'weixin',
  175. success: function(infoRes) {
  176. uni.hideLoading();
  177. self.appUserInfo = infoRes.userInfo
  178. self.appUserInfo.type = self.platform === 'ios' ? 'iosWx' : 'androidWx'
  179. self.wxLoginGo(self.appUserInfo)
  180. },
  181. fail() {
  182. uni.hideLoading();
  183. uni.showToast({
  184. title: '获取用户信息失败',
  185. icon: 'none',
  186. duration: 2000
  187. })
  188. },
  189. complete() {
  190. uni.hideLoading()
  191. }
  192. });
  193. },
  194. fail() {
  195. uni.hideLoading()
  196. uni.showToast({
  197. title: '登录失败',
  198. icon: 'none',
  199. duration: 2000
  200. })
  201. }
  202. });
  203. },
  204. wxLoginGo(userInfo) {
  205. appAuth(userInfo).then(res => {
  206. if (res.data.type === 'register') {
  207. uni.navigateTo({
  208. url: '/pages/users/app_login/index?authKey='+res.data.key
  209. })
  210. }
  211. if (res.data.type === 'login') {
  212. this.$store.commit("LOGIN", {
  213. 'token': res.data.token
  214. });
  215. this.getUserInfo(res.data);
  216. }
  217. }).catch(res => {
  218. that.$util.Tips({
  219. title: res
  220. });
  221. });
  222. },
  223. /**
  224. * 手机 + 验证码登录
  225. */
  226. async loginMobile() {
  227. if (!this.account) {
  228. return this.$util.Tips({
  229. title: '请填写手机号码'
  230. });
  231. }
  232. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(this.account)) {
  233. return this.$util.Tips({
  234. title: '请输入正确的手机号码'
  235. });
  236. }
  237. if (!this.captcha) {
  238. return this.$util.Tips({
  239. title: '请填写验证码'
  240. });
  241. }
  242. if (!/^[\w\d]+$/i.test(this.captcha)) {
  243. return this.$util.Tips({
  244. title: '请输入正确的验证码'
  245. });
  246. }
  247. // 1. 三方登录的特殊逻辑
  248. let socialType = undefined;
  249. let socialCode = undefined;
  250. let socialState = undefined;
  251. // 1.1 微信小程序的情况
  252. // #ifdef MP
  253. socialType = 34;
  254. socialCode = await Routine.getCode();
  255. socialState = 'default'
  256. // #endif
  257. // 2. 短信登录
  258. AuthApi.smsLogin({
  259. mobile: this.account,
  260. code: this.captcha,
  261. socialType: socialType,
  262. socialCode: socialCode,
  263. socialState: socialState
  264. }).then(res => {
  265. // TODO 芋艿:refreshToken 机制
  266. let data = res.data;
  267. this.$store.commit("LOGIN", {
  268. 'token': res.data.accessToken
  269. });
  270. this.getUserInfo(data);
  271. this.bindBrokerUser();
  272. }).catch(res => {
  273. this.$util.Tips({
  274. title: res
  275. });
  276. });
  277. },
  278. async code() {
  279. if (!this.account) {
  280. return this.$util.Tips({
  281. title: '请填写手机号码'
  282. });
  283. }
  284. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(this.account)) {
  285. return this.$util.Tips({
  286. title: '请输入正确的手机号码'
  287. });
  288. }
  289. await AuthApi.sendSmsCode(this.account, 1)
  290. .then(res => {
  291. this.$util.Tips({title:res.message});
  292. this.sendCode();
  293. })
  294. .catch(err => {
  295. return this.$util.Tips({
  296. title: err
  297. });
  298. });
  299. },
  300. /**
  301. * 手机 + 密码登录
  302. */
  303. async submit() {
  304. if (!this.account) {
  305. return this.$util.Tips({
  306. title: '请填写账号'
  307. });
  308. }
  309. if (!/^[\w\d]{5,16}$/i.test(this.account)) {
  310. return this.$util.Tips({
  311. title: '请输入正确的账号'
  312. });
  313. }
  314. if (!this.password) {
  315. return this.$util.Tips({
  316. title: '请填写密码'
  317. });
  318. }
  319. // 1. 三方登录的特殊逻辑
  320. let socialType = undefined;
  321. let socialCode = undefined;
  322. let socialState = undefined;
  323. // 1.1 微信小程序的情况
  324. // #ifdef MP
  325. socialType = 34;
  326. socialCode = await Routine.getCode();
  327. socialState = 'default'
  328. // #endif
  329. // 2. 执行登录
  330. AuthApi.login({
  331. mobile: this.account,
  332. password: this.password,
  333. socialType: socialType,
  334. socialCode: socialCode,
  335. socialState: socialState
  336. }).then(({ data }) => {
  337. // TODO 芋艿:refreshToken 机制
  338. this.$store.commit("LOGIN", {
  339. 'token': data.accessToken
  340. });
  341. this.getUserInfo(data);
  342. this.bindBrokerUser();
  343. }).catch(e => {
  344. this.$util.Tips({
  345. title: e
  346. });
  347. });
  348. },
  349. getUserInfo(data) {
  350. this.$store.commit("SETUID", data.userId);
  351. this.$store.commit("OPENID", data.openid);
  352. UserApi.getUserInfo().then(res => {
  353. this.$store.commit("UPDATE_USERINFO", res.data);
  354. // 调回登录前页面
  355. let backUrl = this.$Cache.get(BACK_URL) || "/pages/index/index";
  356. if (backUrl.indexOf('/') !== 0) {
  357. backUrl = '/' + backUrl;
  358. }
  359. if (backUrl.indexOf('/pages/users/login/index') === 0) {
  360. backUrl = '/pages/index/index';
  361. }
  362. uni.reLaunch({
  363. url: backUrl
  364. });
  365. })
  366. },
  367. bindBrokerUser() {
  368. const spread = this.$Cache.get("spread");
  369. if (spread > 0) {
  370. BrokerageAPI.bindBrokerageUser(spread)
  371. }
  372. }
  373. }
  374. };
  375. </script>
  376. <style lang="scss" scoped>
  377. page {
  378. background: #fff;
  379. }
  380. .appLogin {
  381. margin-top: 60rpx;
  382. .hds {
  383. display: flex;
  384. justify-content: center;
  385. align-items: center;
  386. font-size: 24rpx;
  387. color: #B4B4B4;
  388. .line {
  389. width: 68rpx;
  390. height: 1rpx;
  391. background: #CCCCCC;
  392. }
  393. p {
  394. margin: 0 20rpx;
  395. }
  396. }
  397. .btn-wrapper {
  398. display: flex;
  399. align-items: center;
  400. justify-content: center;
  401. margin-top: 30rpx;
  402. .btn {
  403. display: flex;
  404. align-items: center;
  405. justify-content: center;
  406. width: 68rpx;
  407. height: 68rpx;
  408. border-radius: 50%;
  409. }
  410. .apple-btn {
  411. display: flex;
  412. align-items: center;
  413. justify-content: center;
  414. margin-left: 30rpx;
  415. background: #000;
  416. border-radius: 34rpx;
  417. font-size: 40rpx;
  418. .icon-s-pingguo {
  419. color: #fff;
  420. font-size: 40rpx;
  421. }
  422. }
  423. .iconfont {
  424. font-size: 40rpx;
  425. color: #fff;
  426. }
  427. .wx {
  428. margin-right: 30rpx;
  429. background-color: #61C64F;
  430. }
  431. .mima {
  432. background-color: #28B3E9;
  433. }
  434. .yanzheng {
  435. background-color: #F89C23;
  436. }
  437. }
  438. }
  439. .code img {
  440. width: 100%;
  441. height: 100%;
  442. }
  443. .acea-row.row-middle {
  444. input {
  445. margin-left: 20rpx;
  446. display: block;
  447. }
  448. }
  449. .login-wrapper {
  450. padding: 30rpx;
  451. .shading {
  452. display: flex;
  453. align-items: center;
  454. justify-content: center;
  455. width: 100%;
  456. /* #ifdef APP-VUE */
  457. margin-top: 150rpx;
  458. /* #endif */
  459. /* #ifndef APP-VUE */
  460. margin-top: 200rpx;
  461. /* #endif */
  462. image {
  463. width: 180rpx;
  464. height: 180rpx;
  465. }
  466. }
  467. .whiteBg {
  468. margin-top: 100rpx;
  469. .list {
  470. border-radius: 16rpx;
  471. overflow: hidden;
  472. .item {
  473. border-bottom: 1px solid #F0F0F0;
  474. background: #fff;
  475. .row-middle {
  476. position: relative;
  477. padding: 16rpx 45rpx;
  478. .texts{
  479. flex: 1;
  480. font-size: 28rpx;
  481. height: 80rpx;
  482. line-height: 80rpx;
  483. display: flex;
  484. justify-content: center;
  485. align-items: center;
  486. }
  487. input {
  488. flex: 1;
  489. font-size: 28rpx;
  490. height: 80rpx;
  491. line-height: 80rpx;
  492. display: flex;
  493. justify-content: center;
  494. align-items: center;
  495. }
  496. .code {
  497. position: absolute;
  498. right: 30rpx;
  499. top: 50%;
  500. color: $theme-color;
  501. font-size: 26rpx;
  502. transform: translateY(-50%);
  503. }
  504. }
  505. }
  506. }
  507. .logon {
  508. display: flex;
  509. align-items: center;
  510. justify-content: center;
  511. width: 100%;
  512. height: 86rpx;
  513. margin-top: 80rpx;
  514. background-color: $theme-color;
  515. border-radius: 120rpx;
  516. color: #FFFFFF;
  517. font-size: 30rpx;
  518. }
  519. .tips {
  520. margin: 30rpx;
  521. text-align: center;
  522. color: #999;
  523. }
  524. }
  525. }
  526. </style>