login.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <template>
  2. <view class="login-page">
  3. <view class="title">
  4. <view>欢迎使用WORKARK</view>
  5. </view>
  6. <view class="form-box">
  7. <view class="form-tab">
  8. <view :class="loginType == 1 ? 'tab-item select type1' : 'tab-item type1'" @click="loginType=1">
  9. 验证码登录
  10. </view>
  11. <view :class="loginType == 2 ? 'tab-item select type2' : 'tab-item type2'" @click="loginType=2">
  12. 账号密码登陆
  13. </view>
  14. </view>
  15. <view class="form-input">
  16. <view class="input-box">
  17. <view class="account">
  18. <input class="input-inner" type="number" v-model="form.phone" :adjust-position="false"
  19. placeholder="手机号" />
  20. </view>
  21. <view class="password" v-if="loginType==2">
  22. <input class="input-inner" type="text" :password="!passwordShow" v-model="form.password"
  23. :adjust-position="false" placeholder="密码" />
  24. <uv-icon :name="passwordShow ? 'eye-fill' : 'eye-off'" size="50rpx" color="#a5a8aa"
  25. @click="passwordShow = !passwordShow">
  26. </uv-icon>
  27. </view>
  28. <view class="vcode" v-if="loginType==1">
  29. <input class="input-inner" type="number" v-model="form.phoneCode" :adjust-position="false"
  30. placeholder="验证码" />
  31. <uv-button type="primary" :text="codeTips" @click="getImageCode" size="small"
  32. :disabled="codeDisabled">
  33. </uv-button>
  34. </view>
  35. </view>
  36. <view class="button">
  37. <view class="button-item" @click="loginSubmit">
  38. 登录
  39. </view>
  40. </view>
  41. <view class="login-bottom">
  42. <button class="wx-btn" open-type="getPhoneNumber" @getphonenumber="wxPhoneLogin">
  43. <uv-icon name="weixin-fill" size="60rpx" color="#43b156"></uv-icon>
  44. </button>
  45. </view>
  46. <uv-code ref="code" :seconds="seconds" @end="codeDisabled = false" @start="codeDisabled = true"
  47. @change="text => codeTips = text">
  48. </uv-code>
  49. </view>
  50. </view>
  51. <view class="pact">
  52. <view class="pact-box" v-if="isPact">
  53. <uv-checkbox-group v-model="pactChecked" shape="circle" placement="column"
  54. customStyle="maxWidth:208rpx;" labelSize="24">
  55. <uv-checkbox :name="true" label="我已阅读并同意" iconColor="#ffffff" size="32" iconSize="28"></uv-checkbox>
  56. </uv-checkbox-group>
  57. <uv-text text="《用户协议》" size="24rpx" type="primary" @click="toPact"></uv-text>
  58. <uv-text text="和" size="24rpx"></uv-text>
  59. <uv-text text="《隐私协议》" size="24rpx" type="primary" @click="toPact"></uv-text>
  60. </view>
  61. </view>
  62. <image-code :show="captchaShow" title="请填写图形验证码" @onConfirm="confirmHandle" @onCancel="captchaShow = false">
  63. <view class="code-input-container">
  64. <input type="text" class="code-input" placeholder="输入图形验证码" maxlength="6" v-model="imgCode" />
  65. <view class="code-canvas-wrapper">
  66. <image v-if="codeImg" :src="codeImg" alt="图片验证码" class="code-image" @click="imgCodeFunc"></image>
  67. </view>
  68. </view>
  69. </image-code>
  70. </view>
  71. </template>
  72. <script>
  73. import {
  74. login,
  75. getImgCode,
  76. sendPhoneCode,
  77. getUserInfo,
  78. getOpenId
  79. } from '@/request/api/login'
  80. import imageCode from "@/components/login/imageCode.vue";
  81. import {
  82. isPhoneNumber
  83. } from '@/uitls/validate'
  84. export default {
  85. components: {
  86. imageCode
  87. },
  88. data() {
  89. return {
  90. form: {
  91. phone: '',
  92. phoneCode: '',
  93. password: ''
  94. },
  95. loginType: 1,
  96. passwordShow: false,
  97. pactChecked: [],
  98. codeTips: '获取验证码',
  99. seconds: 60,
  100. codeDisabled: false,
  101. captchaShow: false,
  102. imgCode: '',
  103. codeImg: '',
  104. isPact: false
  105. }
  106. },
  107. onLoad() {
  108. },
  109. methods: {
  110. getImageCode() { //图片验证码显示
  111. if (!isPhoneNumber(this.form.phone)) return this.$toast('请输入正确的手机号');
  112. this.imgCodeFunc();
  113. },
  114. async imgCodeFunc() { //获取图片验证码
  115. let imageData = await getImgCode();
  116. if (imageData.state) {
  117. this.codeImg = imageData.data.pngBase64;
  118. this.captchaShow = true;
  119. }
  120. },
  121. async confirmHandle() { //发送手机验证码
  122. if (!this.imgCode) return this.$toast('请输入图片验证码');
  123. if (!this.$refs.code.canGetCode) return this.$toast('请倒计时结束后再发送');
  124. uni.showLoading();
  125. let phoneData = await sendPhoneCode(this.form.phone, this.imgCode);
  126. uni.hideLoading();
  127. if (phoneData.state) {
  128. this.$refs.code.start();
  129. this.$toast('发送成功');
  130. this.imgCode = '';
  131. this.captchaShow = false;
  132. }
  133. this.imgCodeFunc();
  134. },
  135. async wxPhoneLogin(e) { //微信手机号一键登录
  136. if (!e.detail.code) return;
  137. uni.login({
  138. provider: 'weixin',
  139. success: loginRes => {
  140. this.loginFunction({
  141. pCode: e.detail.code,
  142. code: loginRes.code
  143. })
  144. }
  145. })
  146. },
  147. loginSubmit() {
  148. if (!isPhoneNumber(this.form.phone)) return this.$toast('请输入正确的手机号');
  149. if (this.loginType === 1 && !this.form.phoneCode) return this.$toast('请输入验证码');
  150. if (this.loginType === 2 && !this.form.password) return this.$toast('请输入密码');
  151. let postData = this.loginType === 1 ? {
  152. phone: this.form.phone,
  153. phoneCode: this.form.phoneCode
  154. } : {
  155. phone: this.form.phone,
  156. password: this.form.password
  157. };
  158. this.loginFunction(postData);
  159. },
  160. loginFunction(postData) {
  161. // #ifdef MP-WEIXIN
  162. uni.login({
  163. provider: 'weixin',
  164. success: loginRes => {
  165. this.login(postData, loginRes.code);
  166. }
  167. })
  168. // #endif
  169. // #ifndef MP-WEIXIN
  170. this.login(postData);
  171. // #endif
  172. },
  173. async login(postData, wxCode) {
  174. uni.showLoading({
  175. title: '登录中...'
  176. })
  177. let loginData = await login(postData);
  178. if (!loginData.state) return;
  179. uni.setStorageSync('token', loginData.data.token);
  180. if (wxCode) {
  181. let wxData = await getOpenId(wxCode);
  182. if (wxData.state) uni.setStorageSync('openId', wxData.data.openid);
  183. }
  184. let userData = await getUserInfo();
  185. if (!userData.state) return;
  186. this.$store.dispatch('app/changeOrganization', userData.data.workarkOrganization);
  187. this.$store.dispatch('app/changeUser', userData.data);
  188. this.$chat.connect('workark' + userData.data.userId);
  189. uni.setStorageSync('vuex_state', this.$store.state);
  190. uni.hideLoading();
  191. this.$toast('登录成功');
  192. uni.navigateBack();
  193. },
  194. toPact() {
  195. uni.showToast({
  196. title: "协议",
  197. icon: 'none'
  198. })
  199. }
  200. }
  201. }
  202. </script>
  203. <style lang="scss" scoped>
  204. .login-page {
  205. min-height: 100vh;
  206. background: url("https://images.unsplash.com/photo-1519751138087-5bf79df62d5b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wyMDUzMDJ8MHwxfHNlYXJjaHwyMHx8YmFja2dyb3VuZHxlbnwxfHx8fDE3MjExOTc4NjJ8MA&ixlib=rb-4.0.3&q=80&w=1080") no-repeat top center;
  207. background-size: 100%;
  208. display: flex;
  209. flex-direction: column;
  210. justify-content: space-between;
  211. }
  212. //弹出输入框
  213. .code-input-container {
  214. display: flex;
  215. align-items: center;
  216. margin-top: 30rpx;
  217. .code-input {
  218. background: #F7F8FA;
  219. width: 306rpx;
  220. height: 84rpx;
  221. line-height: 84rpx;
  222. padding: 0 20rpx;
  223. border-radius: 20rpx;
  224. box-sizing: border-box;
  225. &::placeholder {
  226. line-height: 84rpx;
  227. }
  228. }
  229. .code-canvas-wrapper {
  230. flex: 1;
  231. .code-image {
  232. width: 100%;
  233. height: 84rpx;
  234. }
  235. }
  236. }
  237. .form-box {
  238. flex: 1;
  239. margin: 0 30rpx;
  240. border-radius: 40rpx;
  241. background-color: #fff;
  242. overflow: hidden;
  243. }
  244. .form-input {
  245. background-color: #fff;
  246. }
  247. .input-box {
  248. padding: 150rpx 32rpx 0;
  249. margin-bottom: 80rpx;
  250. }
  251. .account,
  252. .password,
  253. .vcode {
  254. height: 96rpx;
  255. border-radius: 20rpx;
  256. padding: 0 48rpx;
  257. display: flex;
  258. align-items: center;
  259. background-color: #f7fafc;
  260. .input-inner {
  261. flex: 1;
  262. }
  263. }
  264. .account {
  265. margin-bottom: 48rpx;
  266. }
  267. .vcode-text {
  268. text-wrap: nowrap;
  269. font-size: 26rpx;
  270. background-color: #fff;
  271. padding: 14rpx 30rpx;
  272. border-radius: 12rpx;
  273. }
  274. .button {
  275. padding: 0 32rpx;
  276. .button-item {
  277. line-height: 96rpx;
  278. border-radius: 20rpx;
  279. text-align: center;
  280. font-size: 32rpx;
  281. background-color: $uv-primary;
  282. color: #fff;
  283. }
  284. }
  285. .pact {
  286. padding-top: 20rpx;
  287. padding-bottom: 120rpx;
  288. height: 40rpx;
  289. }
  290. .pact-box {
  291. height: 40rpx;
  292. display: flex;
  293. align-items: center;
  294. justify-content: center;
  295. font-size: 24rpx;
  296. }
  297. .title {
  298. padding-top: 180rpx;
  299. padding-bottom: 40rpx;
  300. padding-left: 64rpx;
  301. font-size: 48rpx;
  302. font-weight: 700;
  303. color: #383838;
  304. }
  305. .form-tab {
  306. display: flex;
  307. align-items: center;
  308. justify-content: space-between;
  309. background-color: #ecf1fb;
  310. height: 100rpx;
  311. background: linear-gradient(180deg, #e3ebf9 0%, #ffffff 100%);
  312. .tab-item {
  313. width: 340rpx;
  314. line-height: 100rpx;
  315. text-align: center;
  316. color: #9e9e9e;
  317. position: relative;
  318. &::after {
  319. content: '';
  320. width: 50rpx;
  321. height: 8rpx;
  322. display: block;
  323. background-color: $uv-primary;
  324. border-radius: 4rpx;
  325. position: absolute;
  326. left: 50%;
  327. bottom: 14rpx;
  328. transform: translateX(-50%);
  329. opacity: 0;
  330. }
  331. &.select {
  332. background-color: #fff;
  333. color: $uv-primary;
  334. }
  335. &.type1.select {
  336. border-radius: 0 40rpx 0 0;
  337. }
  338. &.type2.select {
  339. border-radius: 40rpx 0 0;
  340. }
  341. &.select::after {
  342. opacity: 1;
  343. }
  344. }
  345. }
  346. .login-bottom {
  347. display: flex;
  348. flex-direction: column;
  349. justify-content: center;
  350. align-items: center;
  351. padding-bottom: 30px;
  352. .wx-btn {
  353. margin-top: 40rpx;
  354. height: 80rpx;
  355. width: 160rpx;
  356. background: #fff;
  357. display: flex;
  358. align-items: center;
  359. justify-content: center;
  360. &::after {
  361. border-radius: 80rpx;
  362. border-color: $uv-border-color;
  363. }
  364. }
  365. }
  366. </style>