login.vue 6.6 KB

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