login.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <template>
  2. <view class="page-box">
  3. <!--
  4. <uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="button"
  5. activeColor="#007aff">
  6. </uni-segmented-control> -->
  7. <view style="margin-top: 30px;">
  8. <uni-forms v-if="current==0" :modelValue="postForm" ref="postForm" :rules="rules" :label-width="100">
  9. <uni-forms-item name="username" label="登录账号" required>
  10. <uni-easyinput type="text" v-model="postForm.username" placeholder="用户名/手机号" />
  11. </uni-forms-item>
  12. <uni-forms-item name="password" label="登录密码" required>
  13. <uni-easyinput type="password" v-model="postForm.password" placeholder="请输入密码" />
  14. </uni-forms-item>
  15. <uni-forms-item name="captchaValue" label="图形验证" required>
  16. <view class="ca-box">
  17. <uni-easyinput class="input" type="text" v-model="postForm.captchaValue"
  18. placeholder="输入验证码" />
  19. <image class="img" @click="refreshCaptcha" :src="captchaUrl"></image>
  20. </view>
  21. </uni-forms-item>
  22. </uni-forms>
  23. <uni-forms v-if="current==1" :modelValue="postForm" ref="postForm" :rules="rules">
  24. <sms-box v-model="postForm" ref="smsBox" :type="2"></sms-box>
  25. </uni-forms>
  26. <view>
  27. <button type="primary" @tap="handleLogin">登录</button>
  28. </view>
  29. <view class="login-opt">
  30. <a @tap="$navs.toReg">立即注册</a>
  31. <a @tap="$navs.toForgot">忘记密码?</a>
  32. </view>
  33. </view>
  34. <view class="other-login">
  35. <!-- <view class="tt">其它登录方式</view> -->
  36. <!-- <view style="display: flex; align-items: center;"> -->
  37. <!-- #ifdef MP-WEIXIN -->
  38. <!-- <view class="types">
  39. <image src="../../static/images/we-chat.png" style="width: 35px; height: 35px;" @click="appLoginWx">
  40. </image>
  41. <view>微信登录</view>
  42. </view> -->
  43. <!-- #endif -->
  44. <!-- <view class="types">
  45. <image src="../../static/images/face-id.png" style="width: 35px; height: 35px;"
  46. @click="appLoginFace"></image>
  47. <view>人脸登录</view>
  48. </view> -->
  49. <!-- </view> -->
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. import SmsBox from './components/SmsBox.vue'
  55. import {
  56. login,
  57. mobileLogin
  58. } from '@/api/user.js'
  59. import {
  60. wxLogin,
  61. apiGetWechatUrl
  62. } from '@/api/ability/login/wx.js'
  63. export default {
  64. components: {
  65. SmsBox
  66. },
  67. data() {
  68. return {
  69. captchaUrl: '',
  70. current: 0,
  71. items: ['账号登录', '手机登录'],
  72. rules: {
  73. username: {
  74. rules: [{
  75. required: true,
  76. errorMessage: '用户名不能为空!'
  77. }]
  78. },
  79. password: {
  80. rules: [{
  81. required: true,
  82. errorMessage: '密码不能为空!'
  83. }]
  84. },
  85. captchaValue: {
  86. rules: [{
  87. required: true,
  88. errorMessage: '图形验证码不能为空!'
  89. }]
  90. }
  91. },
  92. postForm: {
  93. captchaKey: '',
  94. captchaValue: '',
  95. smsCode: '',
  96. smsType: 2,
  97. username: '',
  98. password: ''
  99. }
  100. }
  101. },
  102. onBackPress(e) {
  103. this.backIndex()
  104. return true;
  105. },
  106. created() {
  107. this.refreshCaptcha()
  108. },
  109. methods: {
  110. backIndex(){
  111. uni.switchTab({
  112. url: '/pages/index/index'
  113. })
  114. },
  115. onClickItem(e) {
  116. if (this.current != e.currentIndex) {
  117. this.current = e.currentIndex;
  118. }
  119. },
  120. handleWxLogin(code, nickName, avatar) {
  121. uni.showLoading({
  122. title: '登录中...'
  123. });
  124. wxLogin({
  125. code: code,
  126. nickName: nickName,
  127. avatar: avatar
  128. }).then(data => {
  129. // 保存本地token
  130. this.loginBack(data)
  131. uni.hideLoading();
  132. })
  133. },
  134. appLoginWx() {
  135. let that = this
  136. // #ifdef MP-WEIXIN
  137. uni.showLoading({
  138. title: '请稍后...'
  139. });
  140. let code = '';
  141. // 获取微信code
  142. uni.login({
  143. provider: 'weixin',
  144. success: (res) => {
  145. code = res.code
  146. }
  147. })
  148. // 获取用户信息-新版
  149. if (uni.getUserProfile) {
  150. uni.getUserProfile({
  151. desc: "获取你的昵称,头像,地区以及姓名",
  152. success: (profile) => {
  153. if (profile.errMsg === 'getUserProfile:ok') {
  154. const data = JSON.parse(profile.rawData);
  155. that.handleWxLogin(code, data.nickName, data.avatarUrl);
  156. }
  157. }
  158. })
  159. }
  160. // #endif
  161. // #ifdef H5
  162. apiGetWechatUrl({
  163. state: 'h5'
  164. }).then(data => {
  165. window.location = data.url
  166. })
  167. //#endif
  168. },
  169. thirdLogin() {
  170. uni.login({
  171. success: function(loginRes) {
  172. console.log(loginRes.authResult);
  173. }
  174. });
  175. },
  176. refreshCaptcha() {
  177. this.postForm.captchaKey = new Date().getTime()
  178. this.captchaUrl = `${this.$urls.api}/api/common/captcha/gen?key=${this.postForm.captchaKey}`
  179. },
  180. quickLogin() {
  181. this.postForm.username = 'student'
  182. this.postForm.password = 'student'
  183. this.doLogin()
  184. },
  185. loginBack(data) {
  186. // 关闭加载
  187. uni.hideLoading()
  188. // 保存本地token
  189. uni.setStorageSync('token', data.token);
  190. if(!data.idCard){
  191. //打印请求返回的数据
  192. uni.redirectTo({
  193. url: '/pages/user/info/info'
  194. });
  195. }else{
  196. uni.setStorageSync('idCard', data.idCard);
  197. //打印请求返回的数据
  198. uni.switchTab({
  199. url: '/pages/index/index'
  200. });
  201. }
  202. },
  203. async mobileLogin() {
  204. // 消息框
  205. const smsCheck = await this.$refs.smsBox.validate().then(res => {
  206. return res
  207. });
  208. console.log('smsCheck', smsCheck)
  209. if (!smsCheck) {
  210. return;
  211. }
  212. // 对整个表单进行校验,返回一个 Promise
  213. this.$refs.postForm.validate().then((res) => {
  214. uni.showLoading({
  215. title: '登录中...'
  216. });
  217. // 手机登录
  218. mobileLogin(this.postForm).then(data => {
  219. this.loginBack(data)
  220. })
  221. }).catch(() => {
  222. })
  223. },
  224. accountLogin() {
  225. this.$refs.postForm.validate().then((res) => {
  226. uni.showLoading({
  227. title: '登录中...'
  228. });
  229. // 快速注册后开始考试
  230. login(this.postForm).then(data => {
  231. this.loginBack(data)
  232. });
  233. }).catch(() => {
  234. })
  235. },
  236. // 登录入口
  237. handleLogin() {
  238. if (this.current === 0) {
  239. this.accountLogin()
  240. } else {
  241. this.mobileLogin()
  242. }
  243. },
  244. // 人脸登录
  245. appLoginFace() {
  246. uni.redirectTo({
  247. url: '/pages/login/face'
  248. })
  249. }
  250. }
  251. }
  252. </script>
  253. <style>
  254. .other-login {
  255. flex-grow: 1;
  256. display: flex;
  257. align-items: center;
  258. justify-content: center;
  259. flex-direction: column;
  260. }
  261. .other-login .tt {
  262. text-align: center;
  263. border-bottom: #eee 1px solid;
  264. padding-bottom: 10px;
  265. width: 100%;
  266. color: #666;
  267. }
  268. .other-login .types {
  269. display: flex;
  270. align-items: center;
  271. flex-direction: column;
  272. justify-content: center;
  273. margin-top: 20px;
  274. font-size: 12px;
  275. color: #333;
  276. padding: 0px 10px 0px 10px;
  277. }
  278. .other-login .types uni-view,
  279. view {
  280. font-size: 12px;
  281. color: #666;
  282. }
  283. </style>