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. console.log('走到这')
  258. // 2. 短信登录
  259. AuthApi.smsLogin({
  260. mobile: this.account,
  261. code: this.captcha,
  262. socialType: socialType,
  263. socialCode: socialCode,
  264. socialState: socialState
  265. }).then(res => {
  266. // TODO 芋艿:refreshToken 机制
  267. let data = res.data;
  268. this.$store.commit("LOGIN", {
  269. 'token': res.data.accessToken
  270. });
  271. this.getUserInfo(data);
  272. this.bindBrokerUser();
  273. }).catch(res => {
  274. this.$util.Tips({
  275. title: res
  276. });
  277. });
  278. },
  279. async code() {
  280. if (!this.account) {
  281. return this.$util.Tips({
  282. title: '请填写手机号码'
  283. });
  284. }
  285. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(this.account)) {
  286. return this.$util.Tips({
  287. title: '请输入正确的手机号码'
  288. });
  289. }
  290. await AuthApi.sendSmsCode(this.account, 1)
  291. .then(res => {
  292. this.$util.Tips({title:res.message});
  293. this.sendCode();
  294. })
  295. .catch(err => {
  296. return this.$util.Tips({
  297. title: err
  298. });
  299. });
  300. },
  301. /**
  302. * 手机 + 密码登录
  303. */
  304. async submit() {
  305. if (!this.account) {
  306. return this.$util.Tips({
  307. title: '请填写账号'
  308. });
  309. }
  310. if (!/^[\w\d]{5,16}$/i.test(this.account)) {
  311. return this.$util.Tips({
  312. title: '请输入正确的账号'
  313. });
  314. }
  315. if (!this.password) {
  316. return this.$util.Tips({
  317. title: '请填写密码'
  318. });
  319. }
  320. // 1. 三方登录的特殊逻辑
  321. let socialType = undefined;
  322. let socialCode = undefined;
  323. let socialState = undefined;
  324. // 1.1 微信小程序的情况
  325. // #ifdef MP
  326. socialType = 34;
  327. socialCode = await Routine.getCode();
  328. socialState = 'default'
  329. // #endif
  330. // 2. 执行登录
  331. AuthApi.login({
  332. mobile: this.account,
  333. password: this.password,
  334. socialType: socialType,
  335. socialCode: socialCode,
  336. socialState: socialState
  337. }).then(({ data }) => {
  338. // TODO 芋艿:refreshToken 机制
  339. this.$store.commit("LOGIN", {
  340. 'token': data.accessToken
  341. });
  342. this.getUserInfo(data);
  343. this.bindBrokerUser();
  344. }).catch(e => {
  345. this.$util.Tips({
  346. title: e
  347. });
  348. });
  349. },
  350. getUserInfo(data) {
  351. this.$store.commit("SETUID", data.userId);
  352. this.$store.commit("OPENID", data.openid);
  353. UserApi.getUserInfo().then(res => {
  354. this.$store.commit("UPDATE_USERINFO", res.data);
  355. // 调回登录前页面
  356. let backUrl = this.$Cache.get(BACK_URL) || "/pages/index/index";
  357. if (backUrl.indexOf('/') !== 0) {
  358. backUrl = '/' + backUrl;
  359. }
  360. if (backUrl.indexOf('/pages/users/login/index') === 0) {
  361. backUrl = '/pages/index/index';
  362. }
  363. uni.reLaunch({
  364. url: backUrl
  365. });
  366. })
  367. },
  368. bindBrokerUser() {
  369. const spread = this.$Cache.get("spread");
  370. if (spread > 0) {
  371. BrokerageAPI.bindBrokerageUser(spread)
  372. }
  373. }
  374. }
  375. };
  376. </script>
  377. <style lang="scss" scoped>
  378. page {
  379. background: #fff;
  380. }
  381. .appLogin {
  382. margin-top: 60rpx;
  383. .hds {
  384. display: flex;
  385. justify-content: center;
  386. align-items: center;
  387. font-size: 24rpx;
  388. color: #B4B4B4;
  389. .line {
  390. width: 68rpx;
  391. height: 1rpx;
  392. background: #CCCCCC;
  393. }
  394. p {
  395. margin: 0 20rpx;
  396. }
  397. }
  398. .btn-wrapper {
  399. display: flex;
  400. align-items: center;
  401. justify-content: center;
  402. margin-top: 30rpx;
  403. .btn {
  404. display: flex;
  405. align-items: center;
  406. justify-content: center;
  407. width: 68rpx;
  408. height: 68rpx;
  409. border-radius: 50%;
  410. }
  411. .apple-btn {
  412. display: flex;
  413. align-items: center;
  414. justify-content: center;
  415. margin-left: 30rpx;
  416. background: #000;
  417. border-radius: 34rpx;
  418. font-size: 40rpx;
  419. .icon-s-pingguo {
  420. color: #fff;
  421. font-size: 40rpx;
  422. }
  423. }
  424. .iconfont {
  425. font-size: 40rpx;
  426. color: #fff;
  427. }
  428. .wx {
  429. margin-right: 30rpx;
  430. background-color: #61C64F;
  431. }
  432. .mima {
  433. background-color: #28B3E9;
  434. }
  435. .yanzheng {
  436. background-color: #F89C23;
  437. }
  438. }
  439. }
  440. .code img {
  441. width: 100%;
  442. height: 100%;
  443. }
  444. .acea-row.row-middle {
  445. input {
  446. margin-left: 20rpx;
  447. display: block;
  448. }
  449. }
  450. .login-wrapper {
  451. padding: 30rpx;
  452. .shading {
  453. display: flex;
  454. align-items: center;
  455. justify-content: center;
  456. width: 100%;
  457. /* #ifdef APP-VUE */
  458. margin-top: 150rpx;
  459. /* #endif */
  460. /* #ifndef APP-VUE */
  461. margin-top: 200rpx;
  462. /* #endif */
  463. image {
  464. width: 180rpx;
  465. height: 180rpx;
  466. }
  467. }
  468. .whiteBg {
  469. margin-top: 100rpx;
  470. .list {
  471. border-radius: 16rpx;
  472. overflow: hidden;
  473. .item {
  474. border-bottom: 1px solid #F0F0F0;
  475. background: #fff;
  476. .row-middle {
  477. position: relative;
  478. padding: 16rpx 45rpx;
  479. .texts{
  480. flex: 1;
  481. font-size: 28rpx;
  482. height: 80rpx;
  483. line-height: 80rpx;
  484. display: flex;
  485. justify-content: center;
  486. align-items: center;
  487. }
  488. input {
  489. flex: 1;
  490. font-size: 28rpx;
  491. height: 80rpx;
  492. line-height: 80rpx;
  493. display: flex;
  494. justify-content: center;
  495. align-items: center;
  496. }
  497. .code {
  498. position: absolute;
  499. right: 30rpx;
  500. top: 50%;
  501. color: $theme-color;
  502. font-size: 26rpx;
  503. transform: translateY(-50%);
  504. }
  505. }
  506. }
  507. }
  508. .logon {
  509. display: flex;
  510. align-items: center;
  511. justify-content: center;
  512. width: 100%;
  513. height: 86rpx;
  514. margin-top: 80rpx;
  515. background-color: $theme-color;
  516. border-radius: 120rpx;
  517. color: #FFFFFF;
  518. font-size: 30rpx;
  519. }
  520. .tips {
  521. margin: 30rpx;
  522. text-align: center;
  523. color: #999;
  524. }
  525. }
  526. }
  527. </style>