login.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <template>
  2. <view class="regbox pregbox flexdc pr" v-if="loginflag" style="padding-top: 120rpx;">
  3. <image :src="headimg" class="headimg flex0"></image>
  4. <view class="flex1" style="position: relative;">
  5. <image :src="ldicon" class="mzimg flex0"></image>
  6. <!-- #ifdef APP-PLUS -->
  7. <!-- <view class="mb24 mt50">
  8. <view class="f25 c16 fw5 mb30 txc">133****0756</view>
  9. <view class="rbtn">本机号码一键登录</view>
  10. </view> -->
  11. <!-- #endif -->
  12. <!-- #ifdef MP-WEIXIN -->
  13. <!-- #endif -->
  14. <view class="tab flexcc">
  15. <block v-for="(ite,idx) in tablist" :key="idx">
  16. <view class="tabtit" :class="tabidx==ite.val?'act':''" @click="getTab(ite.val)">
  17. {{ite.tit}}</view>
  18. </block>
  19. </view>
  20. <!-- 手机号登录 -->
  21. <view class="login-form-content">
  22. <block v-if="tabidx==1">
  23. <view class="input-item">
  24. <view class="login_tit">账号</view>
  25. <view class="login_box">
  26. <view class="login_boxl" v-if="tabidx==2">+86</view>
  27. <input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
  28. </view>
  29. </view>
  30. <view class="input-item" >
  31. <view class="flexcj">
  32. <view class="login_tit">密码</view>
  33. <view class="login_jz" @click="jzflag=!jzflag">
  34. <image :src="pcicon" v-if="jzflag"></image>
  35. <image :src="pnicon" v-else></image>
  36. <view>记住密码</view>
  37. </view>
  38. </view>
  39. <view class="login_box">
  40. <input v-if="checkeye" v-model="loginForm.password" type="text" class="input" placeholder="请输入密码" maxlength="20" />
  41. <input v-else v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
  42. <view class="line flex0"></view>
  43. <view class="login_txt" @click="getForget"><text>忘记密码</text></view>
  44. </view>
  45. </view>
  46. <view class="input-item flex align-center" style="width: 60%;margin: 0px;margin-bottom: 24rpx;" v-if="captchaEnabled">
  47. <input v-model="tucode" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
  48. <view class="login-code">
  49. <image :src="codeUrl" @click="getCode" class="login-code-img"></image>
  50. </view >
  51. </view>
  52. <button v-if="captchaEnabled" type="primary" class=" rbtn mb24 mt50" :class="tucode&&loginForm.username&&loginForm.password?'btn2':'btn'" @click="getLogin">登录</button>
  53. <button v-else type="primary" class=" rbtn mb24 mt50" :class="loginForm.username&&loginForm.password?'btn2':'btn'" @click="getLogin">登录</button>
  54. </block>
  55. <block v-if="tabidx==2">
  56. <view class="input-item">
  57. <view class="login_tit">手机号码</view>
  58. <view class="login_box">
  59. <view class="login_boxl" v-if="tabidx==2">+86</view>
  60. <input v-model="loginForm.username" class="input" type="text" placeholder="请输入手机号" maxlength="30" />
  61. </view>
  62. </view>
  63. <view class="input-item" style="margin-bottom: 24rpx;" >
  64. <view class="login_tit">验证码</view>
  65. <view style="display: flex;align-items: center;">
  66. <input v-model="duancode" type="code" class="input" placeholder="请输入短信验证码" maxlength="20" />
  67. <view v-if="timefalg" class="codes">重新发送{{time}}s</view>
  68. <view v-else class="codess" @click="getsendCode">发送验证码</view>
  69. </view>
  70. </view>
  71. <button v-if="tabidx==2" type="primary" class=" rbtn mb24 mt50" :class="loginForm.username&&duancode?'btn2':'btn'" @click="getLogin">登录</button>
  72. </block>
  73. </view>
  74. <!-- 微信登录 -->
  75. <!-- <block v-if="tabidx==1">
  76. <button type="primary" class="rbtn mb24 mt50" open-type="getUserInfo" @getuserinfo="getuserinfo" >
  77. <text class="btn-txt">快捷登录</text>
  78. </button>
  79. </block> -->
  80. <!-- 账号密码登录 -->
  81. <view class="lread" @click="checkflag=!checkflag">
  82. <view class="lreadl">
  83. <image :src="checkimg" v-if="checkflag"></image>
  84. <image :src="ncheckimg" v-else></image>
  85. </view>
  86. <view class="tit">登录即代表您已阅读并同意<text @click.stop="handlePrivacy">用户协议</text>及<text @click.stop="handlePrivacy">隐私政策</text></view>
  87. </view>
  88. <!-- <view class="bbcode">版本号:ZXY_YY_1.3</view> -->
  89. </view>
  90. <!-- <view class="loginb" @click="getTab(1)" v-if="tabidx==2">账号密码登录</view>
  91. <view class="loginb" @click="getTab(2)" v-if="tabidx==1">手机登录</view> -->
  92. <view class="rtxt mt26" @click="getregister">还没有账号?去注册</view>
  93. <phone-btn :type="typeflag" @getPhoneNumber='getPhoneNumber' @getClose="getClose"></phone-btn>
  94. </view>
  95. </template>
  96. <script>
  97. // #ifdef APP-PLUS
  98. const jpushModule = uni.requireNativePlugin('JG-JPush')
  99. // #endif
  100. import * as base64 from "base-64"
  101. import {getweChatLogin,getInfo,getweChatOpenid,getCodeImg,getCode,sendSmsOnly} from "@/api/login.js"
  102. import {getUserProfile,updateUserProfile} from "@/api/system/user.js"
  103. import { getToken } from '@/utils/auth'
  104. import phoneBtn from "@/components/toptab/phonebtn.vue"
  105. export default {
  106. components:{
  107. phoneBtn
  108. },
  109. data(){
  110. return{
  111. headimg:require('@/static/logo.png'),
  112. ldicon:require('@/static/images/mine/ldicon.png'),
  113. checkimg:require('@/static/images/mine/lcicon.png'),
  114. ncheckimg:require('@/static/images/mine/lnicon.png'),
  115. rimg:require('@/static/images/mine/rimg.png'),
  116. pcicon:require('@/static/images/mine/pcicon.png'),
  117. pnicon:require('@/static/images/mine/pnicon.png'),
  118. checkflag:false,
  119. typeflag: false,
  120. loginflag:false,
  121. captchaEnabled: false,
  122. duancode:'',//短信验证码
  123. checkeye:false,
  124. tucode:'',
  125. codeUrl:'',
  126. time:'',
  127. timefalg:'',
  128. tabidx:1,
  129. tablist:[{tit:'账号登录',val:1},{tit:'手机登录',val:2},],
  130. loginForm: {
  131. username: "",
  132. code: "",
  133. password:'',
  134. type:1,//1:账号密码 2:手机登录
  135. },
  136. voList:[],
  137. voindex:0,
  138. jzflag:true
  139. }
  140. },
  141. methods:{
  142. getTab(idx){
  143. this.tabidx=idx;
  144. this.loginForm.type=idx;
  145. },
  146. // 获取图形验证码
  147. getCode() {
  148. getCodeImg().then(res => {
  149. this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
  150. if (this.captchaEnabled) {
  151. this.codeUrl = 'data:image/gif;base64,' + res.img
  152. this.loginForm.uuid = res.uuid
  153. }
  154. })
  155. },
  156. getForget(){
  157. this.$tab.navigateTo(`/pages/mine/pwd/forgetpwd`)
  158. },
  159. getregister(){
  160. this.$tab.navigateTo(`/pages/register`)
  161. // this.$tab.navigateTo(`/work/pages/prove/index`)
  162. // this.$tab.navigateTo(`/mine/pages/mine/auth`)
  163. },
  164. getLogin(){
  165. if(this.tabidx==1){
  166. if(!this.loginForm.username||!this.loginForm.password||(!this.tucode&& this.captchaEnabled)){
  167. return
  168. }
  169. this.loginForm.code=this.tucode;
  170. }else{
  171. let regphone = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
  172. // 座机号
  173. if (this.loginForm.username && !regphone.test(this.loginForm.username)) {
  174. this.$toast("请输入正确的手机号")
  175. return
  176. }
  177. if(!this.loginForm.username||!this.duancode){
  178. return
  179. }
  180. this.loginForm.code=this.duancode;
  181. }
  182. this.pwdLogin()
  183. // if(!this.checkflag){
  184. // this.$toast("请同意用户协议")
  185. // return
  186. // }
  187. },
  188. // 密码登录
  189. async pwdLogin() {
  190. var that=this;
  191. this.$store.dispatch('Login', this.loginForm).then((res) => {
  192. // 判断是否记住密码
  193. if(this.tabidx==1){
  194. if(that.jzflag){
  195. var newObj={
  196. username:that.loginForm.username,
  197. password:base64.encode(that.loginForm.password),
  198. captchaEnabled:that.captchaEnabled,
  199. }
  200. uni.setStorageSync('account', JSON.parse(JSON.stringify(newObj)))
  201. }else{
  202. uni.removeStorageSync('account')
  203. }
  204. }
  205. this.$modal.closeLoading()
  206. this.$toast('登录成功')
  207. setTimeout(function(){
  208. that.loginSuccess()
  209. },1500)
  210. }).catch((error) => {
  211. setTimeout(function(){
  212. if (that.captchaEnabled&&that.tabidx==2) {
  213. that.getCode()
  214. }
  215. },1000)
  216. })
  217. },
  218. // 获取短信验证码
  219. getsendCode(){
  220. var that=this;
  221. if (!this.loginForm.username ) {
  222. that.$toast("请输入手机号")
  223. return
  224. }
  225. let regphone = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
  226. // 座机号
  227. let reg1=/^(0\d{2,3})-?(\d{7,8})$/
  228. if (this.loginForm.username && !regphone.test(this.loginForm.username)) {
  229. that.$toast("请输入正确的手机号")
  230. return
  231. }
  232. var params={
  233. 'username':this.loginForm.username
  234. }
  235. // getSMS();
  236. // return
  237. sendSmsOnly(params).then(res=>{
  238. if (res.code == 200) {
  239. // 发送验证码
  240. that.$toast("发送成功")
  241. that.time=60;
  242. that.timefalg=true;
  243. that.setTimein()
  244. } else {
  245. that.$toast(res.msg)
  246. }
  247. })
  248. },
  249. // 验证码倒计时
  250. setTimein(){
  251. var that=this;
  252. clearInterval(that.timer)
  253. that.timer=setInterval(()=>{
  254. if(that.time<=1){
  255. that.timefalg=false;
  256. that.time=60;
  257. clearInterval(that.timer)
  258. }
  259. that.time=that.time-1;
  260. },1000)
  261. },
  262. handlePrivacy(){
  263. this.$tab.navigateTo(`/pages/agreement`)
  264. },
  265. getClose() {
  266. this.typeflag = false
  267. },
  268. btns() {
  269. },
  270. getPhoneNumber(data){
  271. var that=this;
  272. var params=data
  273. that.$store.dispatch('Login', params).then(() => {
  274. that.$modal.closeLoading()
  275. that.$toast('登录成功')
  276. that.typeflag=false;
  277. that.loginSuccess(data)
  278. }).catch(() => {
  279. // if (this.captchaEnabled) {
  280. // this.getCode()
  281. // }
  282. })
  283. },
  284. //获取推送ID
  285. getRegistrationID() {
  286. jpushModule.getRegistrationID(result => {
  287. let registerID = result.registerID
  288. console.log(registerID,7)
  289. var params={
  290. jgId:registerID,
  291. }
  292. updateUserProfile(params).then(res=>{
  293. if(res.code==200){
  294. console.log(res,9)
  295. }
  296. })
  297. })
  298. },
  299. // 登录成功后,处理函数
  300. loginSuccess(result) {
  301. // 设置用户信息
  302. var that=this;
  303. // #ifdef APP-PLUS
  304. // this.getRegistrationID()
  305. // #endif
  306. this.$store.dispatch('GetInfo').then(res => {
  307. this.$tab.reLaunch('/pages/index/index')
  308. // 判断有几个角色 进入选择页面
  309. // var roles=that.$store.state.user.roles;
  310. // var userroles=that.$store.state.user.userroles;
  311. // if(roles.length>0){
  312. // // 默认选择学校 老师 家长
  313. // var a,b,c,type;
  314. // if(roles.includes('school')){
  315. // type='school'
  316. // }else if(roles.includes('teacher')){
  317. // type='teacher'
  318. // }else if(roles.includes('parents')){
  319. // type='parents'
  320. // }else{
  321. // that.$tab.reLaunch('/pages/index/index')
  322. // return
  323. // }
  324. // Object.keys(userroles).some((key) => {
  325. // if (userroles[key].roleKey == ('' + type)) {
  326. // that.$store.dispatch('checkRole', userroles[key]).then(() => {
  327. // that.$tab.reLaunch('/pages/index/index')
  328. // }).catch(() => {
  329. // })
  330. // return true;
  331. // }
  332. // })
  333. // }else{
  334. // this.$tab.reLaunch('/pages/index/index')
  335. // }
  336. })
  337. },
  338. async getuserinfo(e) {
  339. // if(!this.checkflag){
  340. // this.$toast("请同意用户协议")
  341. // return
  342. // }
  343. // 判断是不是有学校
  344. const {iv,encryptedData} = e.detail
  345. const {nickName, avatarUrl, ...userInfo} = e.detail.userInfo
  346. // console.log(e.detail)
  347. try{
  348. uni.login({
  349. provider: 'weixin',
  350. success: (res) => {
  351. // 获取用户信息
  352. if(res.code){
  353. this.avatarUrl = avatarUrl
  354. this.nickName = nickName
  355. this.code = res.code
  356. this.userInfo =e.detail.userInfo
  357. uni.setStorageSync('userInfo', e.detail.userInfo)
  358. uni.setStorageSync('lcheckflag',true)
  359. this.typeflag=true;
  360. }
  361. }
  362. })
  363. } catch(e){
  364. //TODO handle the exception
  365. }
  366. },
  367. },
  368. onLoad: function() {
  369. this.getCode()
  370. if (getToken()) {
  371. this.$tab.reLaunch('/pages/index/index')
  372. // 判断有什么角色
  373. } else {
  374. this.loginflag=true;
  375. // this.getCode()
  376. if(uni.getStorageSync('lcheckflag')){
  377. this.checkflag=true
  378. }else{
  379. this.checkflag=false
  380. }
  381. if(uni.getStorageSync('account')){
  382. var newObj=JSON.parse(JSON.stringify(uni.getStorageSync('account')))
  383. this.loginForm.username=newObj.username;
  384. this.loginForm.password=base64.decode(newObj.password);
  385. this.jzflag=true;
  386. }
  387. }
  388. },
  389. }
  390. </script>
  391. <style lang="scss" scoped>
  392. .headimg{width: 154rpx;height: 160rpx;margin:0 auto 26rpx;}
  393. .mzimg{width: 100rpx;height: 44rpx;margin: 0 auto 40rpx;}
  394. .rtxt{font-size: 32rpx;font-weight: 500;
  395. color: $com-cd3;text-align: center;}
  396. .bbcode{position: absolute;width: 100%;text-align: center;left: 0;bottom: -80rpx;font-size: 24rpx;color: #666666;}
  397. .loginb{position: absolute;width: 100%;text-align: center;left: 0;bottom: 120rpx;font-size: 26rpx;color: #00A9F0;}
  398. .lread{
  399. display: flex;align-items: flex-start;justify-content: center;
  400. .lreadl{padding-top: 4rpx;
  401. image{width: 28rpx;height: 28rpx;margin-right: 14rpx;}
  402. }
  403. .tit{font-size: 26rpx;font-weight: 500;color: #666666;line-height: 40rpx;
  404. text{color:$com-cd3;text-decoration: underline;padding: 0 6rpx;}
  405. }
  406. }
  407. .tab{
  408. .tabtit{font-size: 32rpx;font-weight: 500;color: #666666;padding: 0 24rpx;position: relative;
  409. &.act{color: $com-cd3;font-weight: bold;}
  410. &::before{content: '';position: absolute;left: 0;top: 50%;margin-top: -10rpx;width: 2rpx;height: 20rpx;background: #DADADA;}
  411. &:first-child::before{display: none;}
  412. }
  413. }
  414. .rbtn {font-size: 30rpx;
  415. &.btn{background: #9a9c9e;color: #ffffff;}
  416. &.btn2{background: $com-cd3;color: #ffffff;}
  417. }
  418. .login-form-content {
  419. margin: 0 auto;
  420. width: 100%;
  421. .input-item {
  422. // margin-bottom:48rpx;
  423. border-bottom: 2rpx solid #CDCDCD;
  424. padding: 40rpx 0 0rpx;
  425. .icon {
  426. font-size: 40rpx;
  427. margin-left: 10px;
  428. color: #999;
  429. }
  430. .login_tit{
  431. font-size: 30rpx;
  432. font-weight: bold;
  433. color: #343434;margin-bottom: 20rpx;
  434. }
  435. .login_box{display: flex;align-items: center;
  436. .rimgs{width: 18rpx;height: 30rpx;flex: 0 0 auto;margin-left: 20rpx;}
  437. }
  438. .input {
  439. width: 100%;
  440. font-size: 30rpx;
  441. height: 84rpx;
  442. line-height: 84rpx;
  443. }
  444. }
  445. .login-code {
  446. height: 38px;
  447. float: right;
  448. .login-code-img {
  449. height: 38px;
  450. position: absolute;
  451. margin-left: 10px;
  452. width: 200rpx;
  453. }
  454. }
  455. }
  456. // .image{width: 32rpx;height: 32rpx;margin-right: 14rpx;}
  457. .input_ye image{width: 30rpx;height: 18rpx;}
  458. .codess{font-size: 30rpx;color: $com-cd3;flex: 0 0 auto;min-width: 200rpx;text-align: center;border-left: 2rpx solid #CDCDCD;}
  459. .codes{background: none;font-size: 28rpx;flex: 0 0 auto;width: 180rpx;text-align: center;border-left: 2rpx solid #CDCDCD;}
  460. .login_box{display: flex;align-items: center;}
  461. .login_boxl{width:100rpx;font-size: 30rpx;color: #343434;border-right: 2rpx solid #CDCDCD;margin-right: 40rpx;}
  462. .login_txt{text-align: right;flex: 0 0 auto; padding:0 24rpx;
  463. text{font-size: 30rrpx;color:$com-cd3;}
  464. }
  465. .line{width: 2rpx;height: 24rpx;background: #CDCDCD;margin: 0 10rpx;}
  466. .login_jz{display: flex;align-items: center;justify-content: flex-end;flex: 0 0 auto;
  467. image{width: 30rpx;height: 30rpx;margin-right: 12rpx;}
  468. view{color: #666666;font-size: 28rpx;}
  469. }
  470. // app
  471. </style>