login.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <template>
  2. <view style="height: 100%;">
  3. <image mode="widthFix" src="/static/login_bg.jpg" class="full_img"></image>
  4. <u-navbar title="登录" :border-bottom="false" background="{ background: '#ffffff',opacity:0.4; }" title-color="#ffffff" back-icon-color="#ffffff"></u-navbar>
  5. <view style="padding: 30rpx;">
  6. <view style="width: 400rpx;">
  7. <u-subsection @change="sectionChange" :list="list" :current="current" active-color="#007AFF" inactive-color="#ffffff" bg-color="rgba(255,255,255,0.52)"></u-subsection>
  8. </view>
  9. <view class="login_box">
  10. <u-form :model="form" ref="uForm1" v-show="current==0">
  11. <u-form-item prop="account" ><u-input type="idcard" v-model="form.account" placeholder-style="color:#999999" placeholder="手机号/学员身份证"/></u-form-item>
  12. <u-form-item prop="pwd" ><u-input class="password" v-model="form.pwd" placeholder-style="color:#999999" type="password" placeholder="登录密码"/></u-form-item>
  13. </u-form>
  14. <u-form :model="form" ref="uForm2" v-show="current==1">
  15. <u-form-item prop="tel" ref="tel"><u-input type="number" maxlength="11" placeholder-style="color:#999999" v-model="form.tel" placeholder="手机号"/></u-form-item>
  16. <u-form-item prop="code" >
  17. <u-input v-model="form.code" type="number" placeholder-style="color:#999999" placeholder="验证码"/>
  18. <u-button slot="right" size="mini" @click="getCode">{{codeTips}}</u-button>
  19. </u-form-item>
  20. </u-form>
  21. </view>
  22. <button class="loginBtn" :class="{able:canLogin()}" @click="login" v-if="current==0">
  23. 登录
  24. </button>
  25. <button :disabled="isUse" :class="{able:canLogin()}" class="loginBtn" @click="sms_login" v-if="current==1">
  26. 登录
  27. </button>
  28. <view style="width: 100%;margin: 40rpx auto;">
  29. <view style="display: flex;align-items: center;color: #007AFF;font-size: 24rpx;justify-content:center;">
  30. <navigator hover-class="none" url="/pages2/register/register" style="margin: 0 40rpx;">立即注册</navigator>
  31. <view style="width: 3rpx;height: 20rpx;background-color: #007AFF;"></view>
  32. <navigator hover-class="none" url="/pages2/register/forget" style="margin: 0 40rpx;">忘记密码</navigator>
  33. </view>
  34. </view>
  35. <view class="wxBtn">
  36. <button type="default" open-type="getPhoneNumber" @getphonenumber="wxLogin" class="wxloginBtn"></button>
  37. <view style="text-align: center;color: #999999;font-size: 24rpx;margin-top: 10rpx;">微信快捷登录</view>
  38. </view>
  39. </view>
  40. <u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
  41. </view>
  42. </template>
  43. <script>
  44. export default {
  45. data() {
  46. return {
  47. code: '',
  48. form:{
  49. tel:'',
  50. code:'',
  51. account:'',
  52. pwd:''
  53. },
  54. list: [
  55. {
  56. name: '密码登录'
  57. },
  58. {
  59. name: '短信登录'
  60. }
  61. ],
  62. rules: {
  63. tel: [
  64. {
  65. required: true,
  66. message: '请输入手机号',
  67. // 可以单个或者同时写两个触发验证方式
  68. trigger: ['change'],
  69. },
  70. {
  71. validator: (rule, value, callback) => {
  72. // 上面有说,返回true表示校验通过,返回false表示不通过
  73. // this.$u.test.mobile()就是返回true或者false的
  74. return this.$u.test.mobile(value);
  75. },
  76. message: '手机号码格式不正确',
  77. // 触发器可以同时用blur和change
  78. trigger: ['change'],
  79. }
  80. ],
  81. account: [
  82. {
  83. required: true,
  84. message: '请输入手机号/学员身份证',
  85. // 可以单个或者同时写两个触发验证方式
  86. trigger: ['change'],
  87. }
  88. ],
  89. pwd: [
  90. {
  91. required: true,
  92. message: '请输入密码',
  93. // 可以单个或者同时写两个触发验证方式
  94. trigger: ['change'],
  95. }
  96. ],
  97. code: [
  98. {
  99. required: true,
  100. message: '请输入验证码',
  101. // 可以单个或者同时写两个触发验证方式
  102. trigger: ['change'],
  103. }
  104. ],
  105. },
  106. current: 0,
  107. codeTips: '',
  108. isUse:false,
  109. isBack:false
  110. };
  111. },
  112. onLoad(option) {
  113. this.$refs.uForm1.setRules(this.rules)
  114. this.$refs.uForm2.setRules(this.rules)
  115. if(option.isBack){
  116. this.isBack = option.isBack;
  117. }
  118. let that = this;
  119. // this.from = option.from;
  120. uni.login({
  121. provider: 'weixin',
  122. success: function(loginRes) {
  123. that.code = loginRes.code;
  124. }
  125. });
  126. },
  127. mounted() {},
  128. methods: {
  129. wxLogin(e) {
  130. console.log(e)
  131. this.$api.wxLogin({
  132. code: this.code,
  133. encryptedData: e.detail.encryptedData,
  134. iv: e.detail.iv
  135. }).then(res => {
  136. if(res.data.data.full_info){
  137. //信息完善,直接进入页面
  138. uni.setStorageSync('user_account', res.data.data.user_account);
  139. uni.setStorageSync('token', res.data.data.token);
  140. this.$api.getInfo().then(resdata => {
  141. if(resdata.data.code == 200){
  142. this.$store.state.userInfo = resdata.data.data;
  143. if(!this.isBack){
  144. uni.reLaunch({
  145. url:'/pages/index/index'
  146. })
  147. }else{
  148. uni.navigateBack();
  149. }
  150. }
  151. });
  152. }else{
  153. //未完善信息,存为临时信息
  154. uni.setStorageSync('user_account_temp', res.data.data.user_account);
  155. uni.setStorageSync('token_temp', res.data.data.token);
  156. this.$navTo.togo('/pages2/register/bind');
  157. }
  158. })
  159. },
  160. canLogin() {
  161. if(this.current == 0) {
  162. if(this.form.account && this.form.pwd) {
  163. return true;
  164. }
  165. return false;
  166. } else if(this.current == 1) {
  167. if(this.form.tel && this.form.code) {
  168. return true;
  169. }
  170. return false;
  171. }
  172. },
  173. sms_login(){
  174. this.$refs.uForm2.validate(valid => {
  175. if(valid) {
  176. let that = this
  177. if(!this.form.tel){
  178. this.$u.toast('请输入手机号码');
  179. return
  180. }
  181. if(!this.form.code){
  182. this.$u.toast('请输入验证码');
  183. return
  184. }
  185. that.isUse = true
  186. let datas = {
  187. tel:this.form.tel,
  188. code:this.form.code
  189. }
  190. that.$api.smsLogin(datas).then(
  191. res => {
  192. that.isUse = false
  193. if (res.data.code == 200) {
  194. if(res.data.data.full_info){
  195. //信息完善,直接进入页面
  196. uni.setStorageSync('user_account', res.data.data.user_account);
  197. uni.setStorageSync('token', res.data.data.token);
  198. that.$api.getInfo().then(resdata => {
  199. if(resdata.data.code == 200){
  200. that.$store.state.userInfo = resdata.data.data;
  201. if(!that.isBack){
  202. uni.reLaunch({
  203. url:'/pages/index/index'
  204. })
  205. }else{
  206. uni.navigateBack();
  207. }
  208. }
  209. });
  210. }else{
  211. //未完善信息,存为临时信息
  212. uni.setStorageSync('user_account_temp', res.data.data.user_account);
  213. uni.setStorageSync('token_temp', res.data.data.token);
  214. that.$navTo.togo('/pages2/register/bind');
  215. }
  216. } else {
  217. that.$u.toast(res.data.msg);
  218. }
  219. },
  220. err => {
  221. that.isUse = false
  222. }
  223. );
  224. }
  225. })
  226. },
  227. fakeLogin(){
  228. uni.setStorageSync('user_account', '123');
  229. uni.setStorageSync('token', '123');
  230. uni.switchTab({
  231. url:'/pages/index/index'
  232. })
  233. },
  234. login(){
  235. let that = this
  236. if(!this.form.account){
  237. this.$u.toast('请输入手机号码/身份证号');
  238. return
  239. }
  240. if(!this.form.pwd){
  241. this.$u.toast('请输入密码');
  242. return
  243. }
  244. //虚拟登录
  245. /* that.fakeLogin()
  246. return */
  247. that.isUse = true
  248. that.$api.accountLogin(this.form).then(
  249. res => {
  250. that.isUse = false
  251. if (res.data.code == 200) {
  252. if(res.data.data.full_info){
  253. //信息完善,直接进入页面
  254. uni.setStorageSync('user_account', res.data.data.user_account);
  255. uni.setStorageSync('token', res.data.data.token);
  256. that.$api.getInfo().then(resdata => {
  257. if(resdata.data.code == 200){
  258. that.$store.state.userInfo = resdata.data.data;
  259. if(!that.isBack){
  260. uni.reLaunch({
  261. url:'/pages/index/index'
  262. })
  263. }else{
  264. uni.navigateBack();
  265. }
  266. }
  267. });
  268. }else{
  269. //未完善信息,存为临时信息
  270. uni.setStorageSync('user_account_temp', res.data.data.user_account);
  271. uni.setStorageSync('token_temp', res.data.data.token);
  272. that.$navTo.togo('/pages2/register/bind');
  273. }
  274. } else {
  275. that.$u.toast(res.data.msg);
  276. }
  277. },
  278. err => {
  279. that.isUse = false
  280. }
  281. );
  282. },
  283. codeChange(text) {
  284. this.codeTips = text;
  285. },
  286. // 获取验证码
  287. getCode() {
  288. let that = this
  289. if(that.$refs.uCode.canGetCode) {
  290. if(that.$refs.tel.validateState == 'success') {
  291. let datas = {tel:this.form.tel}
  292. that.$api.loginSms(datas).then(
  293. res => {
  294. if (res.data.code == 200) {
  295. that.$u.toast('验证码已发送');
  296. // 通知验证码组件内部开始倒计时
  297. that.$refs.uCode.start();
  298. } else {
  299. that.$u.toast(res.data.msg);
  300. }
  301. },
  302. err => {
  303. console.log(err);
  304. }
  305. );
  306. } else {
  307. this.$refs.tel.onFieldChange()
  308. }
  309. }
  310. },
  311. sectionChange(index) {
  312. this.current = index;
  313. },
  314. getPhoneNumber(e) {
  315. let that = this;
  316. uni.checkSession({
  317. success () {
  318. //session_key 未过期,并且在本生命周期一直有效
  319. that.putInfo(e)
  320. },
  321. fail () {
  322. // session_key 已经失效,需要重新执行登录流程
  323. uni.login({
  324. provider: 'weixin',
  325. success: function(loginRes) {
  326. that.code = loginRes.code;
  327. that.putInfo(e)
  328. }
  329. });
  330. }
  331. })
  332. },
  333. putInfo(e){
  334. let that = this;
  335. if (e.detail.encryptedData) {
  336. let inviteCode = uni.getStorageSync("inviteCode")
  337. //用户同意授权
  338. var datas = {
  339. iv: e.detail.iv,
  340. encryptedData: e.detail.encryptedData,
  341. code: that.code
  342. };
  343. if(inviteCode){
  344. datas.inviteCode = inviteCode
  345. }
  346. that.$api.login(datas).then(
  347. res => {
  348. if (res.data.code == 200) {
  349. uni.setStorageSync('union_id', res.data.data.union_id);
  350. uni.setStorageSync('token', res.data.data.token);
  351. that.$api.getInfo().then(resdata => {
  352. if(resdata.data.code == 200){
  353. uni.navigateBack();
  354. that.$store.state.userInfo = resdata.data.data;
  355. }
  356. });
  357. } else {
  358. uni.showModal({
  359. title: '提示',
  360. content: res.data.msg,
  361. showCancel: false
  362. });
  363. }
  364. },
  365. err => {
  366. console.log(err);
  367. }
  368. );
  369. }
  370. }
  371. }
  372. };
  373. </script>
  374. <style scoped lang="scss">
  375. .wxBtn{
  376. position: fixed;
  377. bottom: 10%;
  378. width: 100%;
  379. left: 0;
  380. }
  381. /deep/ .wxBtn button::after{
  382. border: none;
  383. }
  384. .loginBtn{
  385. width: 526rpx;
  386. height: 80rpx;
  387. background: linear-gradient(90deg, #015EEA, #00C0FA);
  388. box-shadow: 0rpx 10rpx 16rpx 4rpx rgba(1, 99, 235, 0.04);
  389. opacity: 0.6;
  390. border-radius: 40rpx;
  391. color: #FFFFFF;
  392. text-align: center;
  393. line-height: 80rpx;
  394. margin: 40rpx auto;
  395. &.able {
  396. opacity: 1;
  397. }
  398. }
  399. .wxloginBtn{
  400. background: url("/static/loginBtn.png") no-repeat;
  401. background-size:100% 100%;
  402. border:none;
  403. width: 100rpx;
  404. height: 100rpx;
  405. }
  406. /deep/page {
  407. background-color: #FFFFFF;
  408. height: 100%;
  409. width: 100%;
  410. }
  411. .login_box{
  412. width: 100%;
  413. height: 360rpx;
  414. background: #FFFFFF;
  415. box-shadow: 0rpx 0rpx 16rpx 4rpx rgba(1, 99, 235, 0.1);
  416. border-radius: 24rpx;
  417. margin-top: 30rpx;
  418. padding:40rpx 35rpx;
  419. .password {
  420. /deep/.uicon-eye-fill {
  421. &::before{
  422. color:#007AFF;
  423. content:"\e613";
  424. }
  425. }
  426. }
  427. }
  428. /deep/ .u-item-bg{
  429. border-radius: 32px !important;
  430. }
  431. /deep/ .u-subsection{
  432. border-radius: 32px !important;
  433. }
  434. .full_img {
  435. position: absolute;
  436. left: 0;
  437. display: block;
  438. width: 100%;
  439. z-index: -999;
  440. top: 0;
  441. }
  442. .head {
  443. height: 96rpx;
  444. width: 100%;
  445. line-height: 96rpx;
  446. margin-top: 40rpx;
  447. text-align: center;
  448. display: flex;
  449. position: relative;
  450. justify-content: center;
  451. }
  452. .icon {
  453. position: absolute;
  454. left: 30rpx;
  455. }
  456. </style>