login.vue 15 KB

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