login.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. <!-- 蓝色简洁登录页面 -->
  2. <template>
  3. <view class="">
  4. <view class="t-login">
  5. <u-top-tips ref="uTips"></u-top-tips>
  6. <u-toast ref="uToast"></u-toast>
  7. <!-- 页面装饰图片 -->
  8. <!-- <image class="img-a" src="@/static/login/2.png"></image> -->
  9. <!-- <image class="img-b" src="@/static/login/3.png"></image> -->
  10. <!-- 标题 -->
  11. <view class="t-b">欢迎回来!</view>
  12. <form class="cl">
  13. <!-- 员工登陆 -->
  14. <block v-if="typeIndex==0">
  15. <view class="t-a" >
  16. <image src="@/static/login/sj.png"></image>
  17. <input class="input" type="number" placeholder="请输入手机号" maxlength="11" v-model="phone" />
  18. </view>
  19. <view class="t-a" >
  20. <image src="@/static/login/yz.png"></image>
  21. <input class="input" type="number" maxlength="6" placeholder="请输入验证码" v-model="yzm" />
  22. <view :style="showText?'':'background-color: #A7A7A7;'" class="t-c" @click="getCode()">{{tips}}</view>
  23. </view>
  24. </block>
  25. <!-- 企业登陆 -->
  26. <block v-if="typeIndex==1">
  27. <view class="t-a">
  28. <image style="width: 50rpx;height: 58rpx;padding-bottom: 10rpx;top: 20rpx;" src="@/static/login/qiye1.png"></image>
  29. <input class="input" disabled type="text" @click="enterpriseShow = true" placeholder="请选择企业" v-model="enterpriseName" />
  30. </view>
  31. <view class="t-a" >
  32. <image src="@/static/login/yz.png"></image>
  33. <input class="input" type="password" maxlength="6" placeholder="请输入登录密码" v-model="password" />
  34. </view>
  35. </block>
  36. <!-- 管理员登录 -->
  37. <view class="t-a" v-if="typeIndex==2">
  38. <image style="width: 50rpx;height: 58rpx;padding-bottom: 10rpx;top: 20rpx;" src="@/static/login/yuanqu.png"></image>
  39. <input class="input" disabled type="text" @click="ganderSelectShow = true" placeholder="请选择园区" v-model="ganderInfo.agencyName" />
  40. </view>
  41. <view class="t-a" v-if="typeIndex==2">
  42. <image src="@/static/login/yz.png"></image>
  43. <input class="input" type="password" maxlength="8" placeholder="请输入登录密码" v-model="inputGanderPsw" />
  44. </view>
  45. <!-- 登录类型 -->
  46. <view class="flex padding-bottom-50" style="text-align: left">
  47. <view @click="typeShow=true" style="border: 1px solid #e9e9e9;background-color: #f8f7fc;" class="cu-btn df round">
  48. <text class="text-black">{{typeList[typeIndex].text}}</text>
  49. <text class="text-black cuIcon-triangledownfill " style="font-size: 40rpx;padding-left: 4rpx;"></text>
  50. </view>
  51. </view>
  52. <!-- 七天免登录 -->
  53. <view class="checkbox">
  54. <u-checkbox
  55. @change="checkboxChange"
  56. v-model="checked"
  57. >七天内免登录</u-checkbox>
  58. </view>
  59. <button class="button" @click="login()">登 录</button>
  60. </form>
  61. <view v-if="typeIndex==0">
  62. <view class="t-f">
  63. <u-divider color="#aaa" border-color="#aaa">微信快速登陆</u-divider>
  64. </view>
  65. <!-- <view class="t-f"><text>————— 微信快速登录 —————</text></view> -->
  66. <view style="display: flex;justify-content: center;padding-top: 50rpx;">
  67. <view @click="wxLogin()"><image style="width: 88rpx;height: 88rpx;" src="@/static/login/wx.png"></image></view>
  68. </view>
  69. </view>
  70. <u-popup border-radius="60" height="60%" mode="bottom" v-model="enterpriseShow">
  71. <view class="fixed cu-bar search bg-white">
  72. <view class="search-form round">
  73. <text class="cuIcon-search"></text>
  74. <u-input style="width: 90%;" v-model="keyword" type="text" :adjust-position="false" placeholder="请输入关键字搜索" confirm-type="search"/>
  75. </view>
  76. <!-- <view @click="fetchEnterpriseList" class="action">
  77. <button class="cu-btn bg-blue shadow-blur round">搜索</button>
  78. </view> -->
  79. </view>
  80. <scroll-view v-if="!$isEmpty(enterpriseList)" @scrolltolower="scrolltolower" style="padding-top: 110rpx;height: 100%;" :scroll-y="true" >
  81. <view @click="selectEnterprise(item)" hover-class="hoverClass" class="text-center padding-30 solid-bottom" v-for="(item,index) in enterpriseList" :key="index">
  82. <text>{{item.enterpriseName}}</text>
  83. </view>
  84. <u-divider v-if="flag" height="80">没有更多了</u-divider>
  85. </scroll-view>
  86. <u-empty v-else name="search"></u-empty>
  87. </u-popup>
  88. <!-- 园区列表 -->
  89. <u-picker v-model="ganderSelectShow" @confirm="ganderSelectCallback" :range="ganderSelectList" range-key="agencyName" mode="selector"></u-picker>
  90. <u-action-sheet @click="typeClick" :list="typeList" v-model="typeShow"></u-action-sheet>
  91. <u-verification-code seconds="60" @end="end" @start="start" ref="uCode" @change="codeChange"></u-verification-code>
  92. </view>
  93. </view>
  94. </template>
  95. <script>
  96. let that;
  97. export default {
  98. data() {
  99. return {
  100. keyword:'',
  101. enterpriseShow:false,
  102. enterpriseList:[],
  103. checked:false,//七天免登录
  104. //登录类型
  105. typeList:[{text:'员工登录',value:'0'},{text:'企业登录',value:'1'},{text:'管理员登录',value:'2'}],
  106. typeShow:false,
  107. typeIndex:0,
  108. //员工登录:手机号码+验证码登录
  109. phone: '',
  110. yzm: '' ,
  111. //发送验证码
  112. tips: '',
  113. showText:true,
  114. //企业登录:统一信用代码+密码登录
  115. enterpriseName:'',
  116. creditCode:'',
  117. password:'',
  118. //园区管理员登陆
  119. ganderInfo:'',//选择的园区对象信息
  120. inputGanderPsw:'',//输入的密码
  121. ganderSelectList: [],//园区列表
  122. ganderSelectShow: false,
  123. //微信快速登陆
  124. openId:'',
  125. //分页
  126. current:1,
  127. flag:false,
  128. };
  129. },
  130. watch:{
  131. keyword(){
  132. let that=this
  133. //节流函数
  134. if (this.timer){
  135. clearTimeout(this.timer)//阻止setTimeout函数的执行
  136. }
  137. this.timer = setTimeout(() => {
  138. that.enterpriseList=[]
  139. that.current=1
  140. that.fetchEnterpriseList()
  141. }, 500)
  142. },
  143. },
  144. onLoad() {
  145. that=this
  146. this.getOpenid()
  147. if (this.$isAuth()) {
  148. //免登录
  149. uni.switchTab({
  150. url:"/pages/index/index"
  151. })
  152. }
  153. },
  154. onShow() {
  155. this.getAppletStatus()
  156. },
  157. methods: {
  158. getAppletStatus(){
  159. this.$api.wxApi.getAppletStatus({id:1}).then(res=>{
  160. uni.setStorageSync("appletStatus",res.data.isOnline)
  161. })
  162. },
  163. scrolltolower(e){
  164. if(this.enterpriseList.length<this.current*10){
  165. this.flag=true
  166. return
  167. }else{
  168. this.current++
  169. this.fetchEnterpriseList()
  170. }
  171. },
  172. /**
  173. * 获取openid
  174. */
  175. getOpenid(){
  176. let that=this
  177. uni.login({
  178. success: (res) => {
  179. let data={
  180. appId:this.$api.wxData.appId,
  181. jsCode:res.code,
  182. secret:this.$api.wxData.secret
  183. }
  184. this.$api.wxApi.getOpenId(data).then(res=>{
  185. let resData= JSON.parse(res.data)
  186. this.openId=resData.openid
  187. })
  188. }
  189. })
  190. },
  191. /**
  192. * 加载企业列表
  193. */
  194. fetchEnterpriseList(){
  195. let params={
  196. current:this.current,
  197. enterpriseName:this.keyword
  198. }
  199. this.$api.enterprise.page(params).then(res=>{
  200. this.enterpriseList = [...this.enterpriseList,...res.data.records]
  201. })
  202. },
  203. /**
  204. * 选择公司的回调
  205. */
  206. selectEnterprise(item){
  207. this.$cache.put('enterpriseName',item.enterpriseName)
  208. this.enterpriseShow=false
  209. this.creditCode=item.creditCode
  210. this.enterpriseName=item.enterpriseName
  211. },
  212. /**
  213. * 登陆类型
  214. * @param {Object} index
  215. */
  216. typeClick(index){
  217. this.typeIndex=index
  218. if (this.typeIndex==1) {
  219. //企业登录,加载企业数据
  220. this.fetchEnterpriseList()
  221. }else if (this.typeIndex==2) {
  222. //园区管理员登陆
  223. this.fetchAgencyList()
  224. }
  225. },
  226. /**
  227. * 获取园区列表
  228. */
  229. fetchAgencyList(){
  230. this.$api.agency.page().then(res=>{
  231. this.ganderSelectList=res.data
  232. })
  233. },
  234. /**
  235. * 选择园区后的回调
  236. */
  237. ganderSelectCallback(e) {
  238. let index=e[0]
  239. this.ganderInfo=this.ganderSelectList[index]
  240. },
  241. /**
  242. * 获取验证码
  243. */
  244. async getCode() {
  245. let that=this
  246. if (this.$isEmpty(this.phone)) {
  247. this.$u.toast("请输入手机号")
  248. return
  249. }
  250. if (!this.$u.test.mobile(this.phone)) {
  251. this.$u.toast("请输入正确的手机号")
  252. return
  253. }
  254. if(this.$refs.uCode.canGetCode) {
  255. // 模拟向后端请求验证码
  256. uni.showLoading({
  257. title: '正在获取验证码'
  258. })
  259. setTimeout(async () => {
  260. uni.hideLoading();
  261. let params=`?phone=${this.phone}`
  262. this.$api.SMSApi.sendSms(params).then(res=>{
  263. if (res.data=='获取验证码成功') {
  264. that.$u.toast('验证码已发送');
  265. // 通知验证码组件内部开始倒计时
  266. that.$refs.uCode.start();
  267. }else{
  268. that.$refs.uTips.show({
  269. title: res.data,
  270. type: 'primary',
  271. duration: '3000'
  272. })
  273. }
  274. // 通知验证码组件内部开始倒计时
  275. that.$refs.uCode.start();
  276. }).catch(err=>{
  277. that.$u.toast('获取验证码失败');
  278. })
  279. }, 2300);
  280. } else {
  281. this.$u.toast('倒计时结束后再发送');
  282. }
  283. },
  284. checkboxChange(e){
  285. this.checked=e.value
  286. },
  287. codeChange(text) {
  288. this.tips = text;
  289. },
  290. end() {
  291. this.showText=true
  292. },
  293. start() {
  294. this.showText=false
  295. },
  296. //当前登录按钮操作
  297. async login() {
  298. if (this.typeIndex==0) {
  299. //员工登录
  300. this.staffLogin()
  301. }else if (this.typeIndex==1) {
  302. //企业登录
  303. this.enterpriseLogin()
  304. }else if (this.typeIndex==2) {
  305. //管理员登陆
  306. this.ganderLogin()
  307. }
  308. },
  309. /**
  310. * 登录成功的处理方式
  311. * 如果是从其他地方跳转回的登录页,则登录后跳转回原页面
  312. * 否则跳转到首页
  313. */
  314. handelSuccessLogin(loginType){
  315. //储存必要信息
  316. if (that.checked) {
  317. //七天内免登录
  318. //存储登录类型,用于判断用户的登录类型和是否登录
  319. that.$cache.put("loginType",loginType,1*24*60*60*7)
  320. }else{
  321. //三十分内钟免登录
  322. that.$cache.put("loginType",loginType,1*60*30)
  323. }
  324. //跳转
  325. uni.showLoading({
  326. title: '登陆中...'
  327. })
  328. setTimeout(()=>{
  329. uni.hideLoading()
  330. if (that.$isEmpty(that.$Route.query.fullPath)) {
  331. uni.switchTab({
  332. url:"/pages/index/index"
  333. })
  334. return
  335. }
  336. if (that.$Route.query.fullPath.indexOf('login')) {
  337. uni.switchTab({
  338. url:"/pages/index/index"
  339. })
  340. }else{
  341. uni.reLaunch({
  342. url:that.$Route.query.fullPath
  343. })
  344. }
  345. },1200)
  346. },
  347. //员工登录
  348. async staffLogin(){
  349. if (this.phone=='17795282222') {
  350. this.userLogin()
  351. return
  352. }
  353. if (!this.phone) {
  354. uni.showToast({ title: '请输入手机号', icon: 'none' });
  355. return;
  356. }
  357. if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.phone)) {
  358. uni.showToast({ title: '请输入正确手机号', icon: 'none' });
  359. return;
  360. }
  361. if (!this.yzm) {
  362. uni.showToast({ title: '请输入验证码', icon: 'none' });
  363. return;
  364. }
  365. if (this.yzm.length!=6) {
  366. this.$u.toast('请输入6位数的验证码')
  367. return
  368. }
  369. let params={
  370. phone:this.phone,
  371. code:this.yzm
  372. }
  373. let smsRes=await this.$api.SMSApi.validCode(this.$u.queryParams(params))
  374. if (smsRes.data!='success') {
  375. this.$u.toast(smsRes.data)
  376. return
  377. }
  378. this.userLogin()
  379. },
  380. //用户登陆
  381. async userLogin(){
  382. //通过手机号查找该员工信息
  383. let staffRes=await this.$api.enterprisestaff.page({phone:this.phone})
  384. let records=staffRes.data.records
  385. if (this.$isEmpty(records)) {
  386. let data={
  387. phone:this.phone
  388. }
  389. //如果没有员工信息。新用户。跳转到注册认证界面
  390. this.$Router.push({name:'auth',params:data})
  391. }else{
  392. let userInfo=records[0]
  393. if (userInfo.examine==0) {
  394. //有员工信息,但审核未通过,提示等待审核
  395. this.$showModel('您的认证信息尚未审核,请联系企业负责人审核',false)
  396. }else if(userInfo.examine==1){
  397. //后台导入的员工信息没有openid,需要在小程序重新插入
  398. if (this.$isEmpty(userInfo.openId)&&!this.$isEmpty(this.openId)) {
  399. userInfo.openId=this.openId
  400. this.$api.enterprisestaff.submit(userInfo)
  401. }
  402. this.$cache.put('agencyId',userInfo.agencyId)
  403. getApp().globalData.userInfo=userInfo
  404. //存储手机号,便于拉取用户信息
  405. this.$cache.put('phone',userInfo.phone)
  406. this.$cache.put('enterpriseId',userInfo.enterpriseId)
  407. this.handelSuccessLogin('staff')
  408. }else if (userInfo.examine==2) {
  409. //审核不通过
  410. this.$showModel('您的认证信息审核不通过,是否重新认证!').then(res=>{
  411. uni.navigateTo({
  412. url:"/pages/auth/auth?operationType=1&phone="+that.phone
  413. })
  414. })
  415. }
  416. }
  417. },
  418. /**
  419. * 微信快速登陆
  420. */
  421. wxLogin(){
  422. if (this.$isEmpty(this.openId)) {
  423. this.$showModel('获取微信openId失败,请用手机号登录',false)
  424. return
  425. }
  426. this.$api.enterprisestaff.page({openId:this.openId,examine:1}).then(res=>{
  427. if (this.$isEmpty(res.data.records)) {
  428. this.$refs.uTips.show({
  429. title: '请先用绑定的手机号登陆!',
  430. type: 'primary',
  431. duration: '2500'
  432. })
  433. }else{
  434. let userInfo=res.data.records[0]
  435. getApp().globalData.userInfo=userInfo
  436. //存储手机号,便于拉取用户信息
  437. this.$cache.put('phone',userInfo.phone)
  438. this.$cache.put('agencyId',userInfo.agencyId)
  439. this.$cache.put('enterpriseId',userInfo.enterpriseId)
  440. this.handelSuccessLogin('staff')
  441. }
  442. })
  443. },
  444. /**
  445. * 企业登录
  446. */
  447. async enterpriseLogin(){
  448. if (this.$isEmpty(this.creditCode)) {
  449. this.$u.toast('请选择企业')
  450. return
  451. }
  452. if (this.$isEmpty(this.password)) {
  453. this.$u.toast("请输入密码")
  454. return
  455. }
  456. this.$api.enterprise.page({creditCode:this.creditCode,password:this.password}).then(res=>{
  457. if (this.$isEmpty(res.data.records)) {
  458. if (this.$isEmpty(res.data.data)) {
  459. this.$u.toast('登录失败,密码错误')
  460. return
  461. }
  462. }else{
  463. //公司信息
  464. let userInfo=res.data.records[0]
  465. this.$api.agency.page({residentialId:userInfo.residentialId}).then(agencyRes=>{
  466. this.$cache.put('agencyId',agencyRes.data[0].id)
  467. })
  468. getApp().globalData.userInfo=userInfo
  469. //存储公司统一信用代码,便于拉取企业信息
  470. this.$cache.put('creditCode',this.creditCode)
  471. this.$cache.put('enterpriseId',userInfo.id)
  472. this.handelSuccessLogin('enterprise')
  473. }
  474. })
  475. },
  476. /**
  477. * 管理员登陆
  478. */
  479. ganderLogin(){
  480. if (this.$isEmpty(this.ganderInfo)) {
  481. this.$u.toast('请选择园区')
  482. return
  483. }
  484. if (this.$isEmpty(this.inputGanderPsw)) {
  485. this.$u.toast('请输入密码')
  486. return
  487. }
  488. /**
  489. * 后台直接返回了明文的园区密码,也没写密码校验的接口
  490. * 就直接在前端校验好了
  491. * 安全性差!!!!
  492. */
  493. if (this.inputGanderPsw==this.ganderInfo.password) {
  494. //输入密码成功
  495. //园区信息
  496. this.$cache.put('agencyId',this.ganderInfo.id)
  497. this.$cache.put('agency',this.ganderInfo.agencyName)
  498. this.$cache.put('agencyTenantId',this.ganderInfo.tenantId)
  499. getApp().globalData.userInfo=this.ganderInfo
  500. this.handelSuccessLogin('agency')
  501. }else{
  502. this.$u.toast('登录失败,密码错误')
  503. return
  504. }
  505. }
  506. }
  507. };
  508. </script>
  509. <style lang="scss" >
  510. page{
  511. background-color: #FFFFFF;
  512. }
  513. .hoverClass{
  514. background-color: #eee;
  515. }
  516. .checkbox{
  517. padding-bottom: 50rpx;
  518. padding-left: 20rpx;
  519. margin-top: -10rpx;
  520. }
  521. .cu-btn.lg {
  522. padding: 0 50upx;
  523. font-size: 30upx;
  524. height: 70upx;
  525. }
  526. .img-a {
  527. position: absolute;
  528. width: 100%;
  529. top: -300rpx;
  530. right: -100rpx;
  531. }
  532. .img-b {
  533. position: absolute;
  534. width: 50%;
  535. bottom: 0;
  536. left: -50rpx;
  537. margin-bottom: -200rpx;
  538. }
  539. .t-login {
  540. width: 600rpx;
  541. margin: -200rpx auto;
  542. font-size: 28rpx;
  543. color: #000;
  544. }
  545. .t-login .button {
  546. font-size: 28rpx;
  547. background: #5677fc;
  548. color: #fff;
  549. height: 90rpx;
  550. line-height: 90rpx;
  551. border-radius: 50rpx;
  552. box-shadow: 0 5px 7px 0 rgba(86, 119, 252, 0.2);
  553. }
  554. .t-login .input {
  555. padding: 0 20rpx 0 120rpx;
  556. height: 90rpx;
  557. line-height: 90rpx;
  558. margin-bottom: 50rpx;
  559. background: #f8f7fc;
  560. border: 1px solid #e9e9e9;
  561. font-size: 28rpx;
  562. border-radius: 50rpx;
  563. }
  564. .t-login .t-a {
  565. position: relative;
  566. }
  567. .t-login .t-a image {
  568. width: 40rpx;
  569. height: 42rpx;
  570. position: absolute;
  571. left: 30rpx;
  572. top: 26rpx;
  573. padding-right:4rpx;
  574. }
  575. .t-login .t-b {
  576. text-align: left;
  577. font-size: 46rpx;
  578. color: #000;
  579. padding: 300rpx 0 120rpx 0;
  580. font-weight: bold;
  581. }
  582. .t-login .t-c {
  583. position: absolute;
  584. right: 22rpx;
  585. top: 22rpx;
  586. background: #5677fc;
  587. color: #fff;
  588. font-size: 24rpx;
  589. border-radius: 50rpx;
  590. height: 50rpx;
  591. line-height: 50rpx;
  592. padding: 0 25rpx;
  593. z-index: 1000;
  594. }
  595. .t-login .t-d {
  596. text-align: center;
  597. color: #999;
  598. margin: 80rpx 0;
  599. }
  600. .t-login .t-e {
  601. text-align: center;
  602. width: 250rpx;
  603. margin: 80rpx auto;
  604. }
  605. .t-login .t-g {
  606. float: left;
  607. width: 50%;
  608. }
  609. .t-login .t-e image {
  610. width: 50rpx;
  611. height: 50rpx;
  612. }
  613. .t-login .t-f {
  614. text-align: center;
  615. margin: 100rpx 0 0 0;
  616. color: #666;
  617. }
  618. .t-login .t-f text {
  619. margin-left: 20rpx;
  620. color: #aaaaaa;
  621. font-size: 27rpx;
  622. }
  623. .t-login .uni-input-placeholder {
  624. color: #000;
  625. }
  626. .cl {
  627. zoom: 1;
  628. }
  629. .cl:after {
  630. clear: both;
  631. display: block;
  632. visibility: hidden;
  633. height: 0;
  634. content: '\20';
  635. }
  636. </style>