login.vue 8.5 KB

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