login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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" v-if="false">
  36. <button type="default" 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. canLogin() {
  130. if(this.current == 0) {
  131. if(this.form.account && this.form.pwd) {
  132. return true;
  133. }
  134. return false;
  135. } else if(this.current == 1) {
  136. if(this.form.tel && this.form.code) {
  137. return true;
  138. }
  139. return false;
  140. }
  141. },
  142. sms_login(){
  143. this.$refs.uForm2.validate(valid => {
  144. if(valid) {
  145. let that = this
  146. if(!this.form.tel){
  147. this.$u.toast('请输入手机号码');
  148. return
  149. }
  150. if(!this.form.code){
  151. this.$u.toast('请输入验证码');
  152. return
  153. }
  154. that.isUse = true
  155. let datas = {
  156. tel:this.form.tel,
  157. code:this.form.code
  158. }
  159. that.$api.smsLogin(datas).then(
  160. res => {
  161. that.isUse = false
  162. if (res.data.code == 200) {
  163. if(res.data.data.full_info){
  164. //信息完善,直接进入页面
  165. uni.setStorageSync('user_account', res.data.data.user_account);
  166. uni.setStorageSync('token', res.data.data.token);
  167. that.$api.getInfo().then(resdata => {
  168. if(resdata.data.code == 200){
  169. that.$store.state.userInfo = resdata.data.data;
  170. if(!that.isBack){
  171. uni.reLaunch({
  172. url:'/pages/index/index'
  173. })
  174. }else{
  175. uni.navigateBack();
  176. }
  177. }
  178. });
  179. }else{
  180. //未完善信息,存为临时信息
  181. uni.setStorageSync('user_account_temp', res.data.data.user_account);
  182. uni.setStorageSync('token_temp', res.data.data.token);
  183. that.$navTo.togo('/pages2/register/bind');
  184. }
  185. } else {
  186. that.$u.toast(res.data.msg);
  187. }
  188. },
  189. err => {
  190. that.isUse = false
  191. }
  192. );
  193. }
  194. })
  195. },
  196. fakeLogin(){
  197. uni.setStorageSync('user_account', '123');
  198. uni.setStorageSync('token', '123');
  199. uni.switchTab({
  200. url:'/pages/index/index'
  201. })
  202. },
  203. login(){
  204. let that = this
  205. if(!this.form.account){
  206. this.$u.toast('请输入手机号码/身份证号');
  207. return
  208. }
  209. if(!this.form.pwd){
  210. this.$u.toast('请输入密码');
  211. return
  212. }
  213. //虚拟登录
  214. /* that.fakeLogin()
  215. return */
  216. that.isUse = true
  217. that.$api.accountLogin(this.form).then(
  218. res => {
  219. that.isUse = false
  220. if (res.data.code == 200) {
  221. if(res.data.data.full_info){
  222. //信息完善,直接进入页面
  223. uni.setStorageSync('user_account', res.data.data.user_account);
  224. uni.setStorageSync('token', res.data.data.token);
  225. that.$api.getInfo().then(resdata => {
  226. if(resdata.data.code == 200){
  227. that.$store.state.userInfo = resdata.data.data;
  228. if(!that.isBack){
  229. uni.reLaunch({
  230. url:'/pages/index/index'
  231. })
  232. }else{
  233. uni.navigateBack();
  234. }
  235. }
  236. });
  237. }else{
  238. //未完善信息,存为临时信息
  239. uni.setStorageSync('user_account_temp', res.data.data.user_account);
  240. uni.setStorageSync('token_temp', res.data.data.token);
  241. that.$navTo.togo('/pages2/register/bind');
  242. }
  243. } else {
  244. that.$u.toast(res.data.msg);
  245. }
  246. },
  247. err => {
  248. that.isUse = false
  249. }
  250. );
  251. },
  252. codeChange(text) {
  253. this.codeTips = text;
  254. },
  255. // 获取验证码
  256. getCode() {
  257. let that = this
  258. if(that.$refs.uCode.canGetCode) {
  259. if(that.$refs.tel.validateState == 'success') {
  260. let datas = {tel:this.form.tel}
  261. that.$api.loginSms(datas).then(
  262. res => {
  263. if (res.data.code == 200) {
  264. that.$u.toast('验证码已发送');
  265. // 通知验证码组件内部开始倒计时
  266. that.$refs.uCode.start();
  267. } else {
  268. that.$u.toast(res.data.msg);
  269. }
  270. },
  271. err => {
  272. console.log(err);
  273. }
  274. );
  275. } else {
  276. this.$refs.tel.onFieldChange()
  277. }
  278. }
  279. },
  280. sectionChange(index) {
  281. this.current = index;
  282. },
  283. getPhoneNumber(e) {
  284. let that = this;
  285. uni.checkSession({
  286. success () {
  287. //session_key 未过期,并且在本生命周期一直有效
  288. that.putInfo(e)
  289. },
  290. fail () {
  291. // session_key 已经失效,需要重新执行登录流程
  292. uni.login({
  293. provider: 'weixin',
  294. success: function(loginRes) {
  295. that.code = loginRes.code;
  296. that.putInfo(e)
  297. }
  298. });
  299. }
  300. })
  301. },
  302. putInfo(e){
  303. let that = this;
  304. if (e.detail.encryptedData) {
  305. let inviteCode = uni.getStorageSync("inviteCode")
  306. //用户同意授权
  307. var datas = {
  308. iv: e.detail.iv,
  309. encryptedData: e.detail.encryptedData,
  310. code: that.code
  311. };
  312. if(inviteCode){
  313. datas.inviteCode = inviteCode
  314. }
  315. that.$api.login(datas).then(
  316. res => {
  317. if (res.data.code == 200) {
  318. uni.setStorageSync('union_id', res.data.data.union_id);
  319. uni.setStorageSync('token', res.data.data.token);
  320. that.$api.getInfo().then(resdata => {
  321. if(resdata.data.code == 200){
  322. uni.navigateBack();
  323. that.$store.state.userInfo = resdata.data.data;
  324. }
  325. });
  326. } else {
  327. uni.showModal({
  328. title: '提示',
  329. content: res.data.msg,
  330. showCancel: false
  331. });
  332. }
  333. },
  334. err => {
  335. console.log(err);
  336. }
  337. );
  338. }
  339. }
  340. }
  341. };
  342. </script>
  343. <style scoped lang="scss">
  344. .wxBtn{
  345. position: fixed;
  346. bottom: 10%;
  347. width: 100%;
  348. left: 0;
  349. }
  350. /deep/ .wxBtn button::after{
  351. border: none;
  352. }
  353. .loginBtn{
  354. width: 526rpx;
  355. height: 80rpx;
  356. background: linear-gradient(90deg, #015EEA, #00C0FA);
  357. box-shadow: 0rpx 10rpx 16rpx 4rpx rgba(1, 99, 235, 0.04);
  358. opacity: 0.6;
  359. border-radius: 40rpx;
  360. color: #FFFFFF;
  361. text-align: center;
  362. line-height: 80rpx;
  363. margin: 40rpx auto;
  364. &.able {
  365. opacity: 1;
  366. }
  367. }
  368. .wxloginBtn{
  369. background: url("/static/loginBtn.png") no-repeat;
  370. background-size:100% 100%;
  371. border:none;
  372. width: 100rpx;
  373. height: 100rpx;
  374. }
  375. /deep/page {
  376. background-color: #FFFFFF;
  377. height: 100%;
  378. width: 100%;
  379. }
  380. .login_box{
  381. width: 100%;
  382. height: 360rpx;
  383. background: #FFFFFF;
  384. box-shadow: 0rpx 0rpx 16rpx 4rpx rgba(1, 99, 235, 0.1);
  385. border-radius: 24rpx;
  386. margin-top: 30rpx;
  387. padding:40rpx 35rpx;
  388. .password {
  389. /deep/.uicon-eye-fill {
  390. &::before{
  391. color:#007AFF;
  392. content:"\e613";
  393. }
  394. }
  395. }
  396. }
  397. /deep/ .u-item-bg{
  398. border-radius: 32px !important;
  399. }
  400. /deep/ .u-subsection{
  401. border-radius: 32px !important;
  402. }
  403. .full_img {
  404. position: absolute;
  405. left: 0;
  406. display: block;
  407. width: 100%;
  408. z-index: -999;
  409. top: 0;
  410. }
  411. .head {
  412. height: 96rpx;
  413. width: 100%;
  414. line-height: 96rpx;
  415. margin-top: 40rpx;
  416. text-align: center;
  417. display: flex;
  418. position: relative;
  419. justify-content: center;
  420. }
  421. .icon {
  422. position: absolute;
  423. left: 30rpx;
  424. }
  425. </style>