login.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <template>
  2. <view class="app">
  3. <!-- 左上角的 x 关闭 -->
  4. <view class="back-btn mix-icon icon-guanbi" @click="navBack"></view>
  5. <!-- 用户协议 -->
  6. <view class="agreement center">
  7. <text class="mix-icon icon-xuanzhong" :class="{active: agreement}" @click="checkAgreement"></text>
  8. <text @click="checkAgreement">请认真阅读并同意</text>
  9. <text class="title" @click="navToAgreementDetail(1)">《用户服务协议》</text>
  10. <text class="title" @click="navToAgreementDetail(2)">《隐私权政策》</text>
  11. </view>
  12. <!-- 登录表单 -->
  13. <view class="wrapper">
  14. <view class="left-top-sign">LOGIN</view>
  15. <view class="welcome">手机登录/注册</view>
  16. <!-- 手机验证码登录 -->
  17. <view class="input-content">
  18. <u--form labelPosition="left" :model="form" :rules="rules" ref="form" errorType="toast">
  19. <u-form-item prop="mobile" borderBottom>
  20. <u--input type="number" v-model="form.mobile" placeholder="请输入手机号" border="none"></u--input>
  21. </u-form-item>
  22. <!-- 判断使用验证码还是密码 -->
  23. <u-form-item prop="code" borderBottom v-if="loginType == 'code'">
  24. <u--input type="number" v-model="form.code" placeholder="请输入验证码" border="none"></u--input>
  25. <u-button slot="right" @tap="getCode" :text="uCode.tips" type="success" size="mini" :disabled="uCode.disabled"></u-button>
  26. <u-code ref="uCode" @change="codeChange" seconds="60" @start="uCode.disabled === true" @end="uCode.disabled === false"></u-code>
  27. </u-form-item>
  28. <u-form-item prop="password" borderBottom v-else>
  29. <u--input password v-model="form.password" placeholder="请输入密码" border="none"></u--input>
  30. </u-form-item>
  31. </u--form>
  32. <u-button class="login-button" text="立即登录" type="error" shape="circle" @click="mobileLogin"
  33. :loading="loading"></u-button>
  34. <!-- 切换登陆 -->
  35. <view class="login-type" v-if="loginType == 'code'" @click="setLoginType('password')">账号密码登录</view>
  36. <view class="login-type" v-else @click="setLoginType('code')">免密登录</view>
  37. </view>
  38. <!-- 快捷登录 -->
  39. <!-- #ifdef APP-PLUS || MP-WEIXIN -->
  40. <view class="other-wrapper">
  41. <view class="line center">
  42. <text class="title">快捷登录</text>
  43. </view>
  44. <view class="list row">
  45. <!-- #ifdef MP-WEIXIN -->
  46. <view class="item column center" @click="mpWxGetUserInfo">
  47. <image class="icon" src="/static/icon/login-wx.png"></image>
  48. </view>
  49. <!-- #endif -->
  50. <!-- #ifdef APP-PLUS -->
  51. <view class="item column center" style="width: 180rpx;" @click="loginByWxApp">
  52. <image class="icon" src="/static/icon/login-wx.png"></image>
  53. <text>微信登录</text>
  54. </view>
  55. <!-- #endif -->
  56. </view>
  57. </view>
  58. <!-- #endif -->
  59. </view>
  60. </view>
  61. </template>
  62. <script>
  63. import { checkStr } from '@/common/js/util'
  64. import { login, smsLogin, sendSmsCode } from '@/api/system/auth.js'
  65. import loginMpWx from './mixin/login-mp-wx.js'
  66. import loginAppWx from './mixin/login-app-wx.js'
  67. export default{
  68. mixins: [loginMpWx, loginAppWx],
  69. data() {
  70. return {
  71. agreement: true,
  72. loginType: 'code', // 登录方式,code 验证码;password 密码
  73. loading: false, // 表单提交
  74. rules: {
  75. mobile: [{
  76. required: true,
  77. message: '请输入手机号'
  78. }, {
  79. validator: (rule, value, callback) => {
  80. return uni.$u.test.mobile(value);
  81. },
  82. message: '手机号码不正确'
  83. }],
  84. code: [],
  85. password: []
  86. },
  87. form: {
  88. mobile: '',
  89. code: '',
  90. password: '',
  91. },
  92. uCode: {
  93. tips: '',
  94. disable: false,
  95. }
  96. }
  97. },
  98. onLoad() {
  99. this.setLoginType(this.loginType);
  100. },
  101. methods: {
  102. // 手机号登录
  103. mobileLogin() {
  104. if (!this.agreement) {
  105. this.$util.msg('请阅读并同意用户服务及隐私协议');
  106. return;
  107. }
  108. this.$refs.form.validate().then(() => {
  109. this.loading = true;
  110. // 执行登陆
  111. const { mobile, code, password} = this.form;
  112. const loginPromise = this.loginType == 'password' ? login(mobile, password) :
  113. smsLogin(mobile, code);
  114. loginPromise.then(data => {
  115. // 登陆成功
  116. this.loginSuccessCallBack(data);
  117. }).catch(errors => {
  118. }).finally(() => {
  119. this.loading = false;
  120. })
  121. }).catch(errors => {
  122. });
  123. },
  124. // 登陆成功的处理逻辑
  125. loginSuccessCallBack(data) {
  126. this.$util.msg('登录成功');
  127. this.$store.commit('setToken', data);
  128. // TODO 芋艿:如果当前页是第一页,则无法返回。期望是能够回到首页
  129. setTimeout(()=>{
  130. uni.navigateBack();
  131. }, 1000)
  132. },
  133. navBack() {
  134. uni.navigateBack({
  135. delta: 1
  136. });
  137. },
  138. setLoginType(loginType) {
  139. this.loginType = loginType;
  140. // 修改校验规则
  141. this.rules.code = [];
  142. this.rules.password = [];
  143. if (loginType == 'code') {
  144. this.rules.code = [{
  145. required: true,
  146. message: '请输入验证码'
  147. }, {
  148. min: 4,
  149. max: 6,
  150. message: '验证码不正确'
  151. }];
  152. } else {
  153. this.rules.password = [{
  154. required: true,
  155. message: '请输入密码'
  156. }, {
  157. min: 4,
  158. max: 16,
  159. message: '密码不正确'
  160. }]
  161. }
  162. },
  163. //同意协议
  164. checkAgreement(){
  165. this.agreement = !this.agreement;
  166. },
  167. //打开协议
  168. navToAgreementDetail(type){
  169. this.navTo('/pages/public/article?param=' + JSON.stringify({
  170. module: 'article',
  171. operation: 'getAgreement',
  172. data: {
  173. type
  174. }
  175. }))
  176. },
  177. codeChange(text) {
  178. this.uCode.tips = text;
  179. },
  180. getCode() {
  181. // 处于发送中,则跳过
  182. if (!this.$refs.uCode.canGetCode) {
  183. return;
  184. }
  185. // 校验手机号
  186. this.$refs.form.validateField('mobile', errors => {
  187. if (errors.length > 0) {
  188. uni.$u.toast(errors[0].message);
  189. return;
  190. }
  191. // 发送验证码 TODO 芋艿,枚举类
  192. sendSmsCode(this.form.mobile, 1).then(data => {
  193. uni.$u.toast('验证码已发送');
  194. // 通知验证码组件内部开始倒计时
  195. this.$refs.uCode.start();
  196. }).catch(erros => {
  197. })
  198. })
  199. },
  200. }
  201. }
  202. </script>
  203. <style>
  204. page {
  205. background: #fff;
  206. }
  207. </style>
  208. <style scoped lang='scss'>
  209. .app {
  210. padding-top: 15vh;
  211. position:relative;
  212. width: 100vw;
  213. height: 100vh;
  214. overflow: hidden;
  215. background: #fff;
  216. }
  217. .wrapper {
  218. position:relative;
  219. z-index: 90;
  220. padding-bottom: 40rpx;
  221. .welcome {
  222. position:relative;
  223. left: 50rpx;
  224. top: -90rpx;
  225. font-size: 46rpx;
  226. color: #555;
  227. text-shadow: 1px 0px 1px rgba(0,0,0,.3);
  228. }
  229. }
  230. .back-btn {
  231. position:absolute;
  232. left: 20rpx;
  233. top: calc(var(--status-bar-height) + 20rpx);
  234. z-index: 90;
  235. padding: 20rpx;
  236. font-size: 32rpx;
  237. color: #606266;
  238. }
  239. .left-top-sign {
  240. font-size: 120rpx;
  241. color: #f8f8f8;
  242. position: relative;
  243. left: -12rpx;
  244. }
  245. /** 手机登录部分 */
  246. .input-content {
  247. padding: 0 60rpx;
  248. .login-button {
  249. margin-top: 30rpx;
  250. }
  251. .login-type {
  252. display: flex;
  253. justify-content: flex-end;
  254. font-size: 13px;
  255. color: #40a2ff;
  256. margin-top: 20rpx;
  257. }
  258. }
  259. /* 其他登录方式 */
  260. .other-wrapper{
  261. display: flex;
  262. flex-direction: column;
  263. align-items: center;
  264. padding-top: 20rpx;
  265. margin-top: 80rpx;
  266. .line{
  267. margin-bottom: 40rpx;
  268. .title {
  269. margin: 0 32rpx;
  270. font-size: 24rpx;
  271. color: #606266;
  272. }
  273. &:before, &:after{
  274. content: '';
  275. width: 160rpx;
  276. height: 0;
  277. border-top: 1px solid #e0e0e0;
  278. }
  279. }
  280. .item{
  281. font-size: 24rpx;
  282. color: #606266;
  283. background-color: #fff;
  284. border: 0;
  285. &:after{
  286. border: 0;
  287. }
  288. }
  289. .icon{
  290. width: 90rpx;
  291. height: 90rpx;
  292. margin: 0 24rpx 16rpx;
  293. }
  294. }
  295. .agreement{
  296. position: absolute;
  297. left: 0;
  298. bottom: 6vh;
  299. z-index: 1;
  300. width: 750rpx;
  301. height: 90rpx;
  302. font-size: 24rpx;
  303. color: #999;
  304. .mix-icon{
  305. font-size: 36rpx;
  306. color: #ccc;
  307. margin-right: 8rpx;
  308. margin-top: 1px;
  309. &.active{
  310. color: $base-color;
  311. }
  312. }
  313. .title {
  314. color: #40a2ff;
  315. }
  316. }
  317. </style>