login.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. this.loginFunction({
  138. pCode: e.detail.code
  139. })
  140. },
  141. loginSubmit() {
  142. if (!isPhoneNumber(this.form.phone)) return this.$toast('请输入正确的手机号');
  143. if (this.loginType === 1 && !this.form.phoneCode) return this.$toast('请输入验证码');
  144. if (this.loginType === 2 && !this.form.password) return this.$toast('请输入密码');
  145. let postData = this.loginType === 1 ? {
  146. phone: this.form.phone,
  147. phoneCode: this.form.phoneCode
  148. } : {
  149. phone: this.form.phone,
  150. password: this.form.password
  151. };
  152. this.loginFunction(postData);
  153. },
  154. loginFunction(postData) {
  155. // #ifdef MP-WEIXIN
  156. uni.login({
  157. provider: 'weixin',
  158. success: loginRes => {
  159. if (postData.pCode) postData['code'] = loginRes.code;
  160. this.login(postData, loginRes.code);
  161. }
  162. })
  163. // #endif
  164. // #ifndef MP-WEIXIN
  165. this.login(postData);
  166. // #endif
  167. },
  168. async login(postData, wxCode) {
  169. uni.showLoading({
  170. title: '登录中...'
  171. })
  172. let loginData = await login(postData);
  173. if (!loginData.state) return;
  174. uni.setStorageSync('token', loginData.data.token);
  175. if (wxCode) {
  176. let wxData = await getOpenId(wxCode);
  177. if (wxData.state) uni.setStorageSync('openId', wxData.data.openid);
  178. }
  179. let userData = await getUserInfo();
  180. if (!userData.state) return;
  181. this.$store.dispatch('app/changeOrganization', userData.data.workarkOrganization);
  182. this.$store.dispatch('app/changeUser', userData.data);
  183. this.$chat.connect('workark' + userData.data.userId);
  184. uni.setStorageSync('vuex_state', this.$store.state);
  185. uni.hideLoading();
  186. this.$toast('登录成功');
  187. uni.navigateBack();
  188. },
  189. toPact() {
  190. uni.showToast({
  191. title: "协议",
  192. icon: 'none'
  193. })
  194. }
  195. }
  196. }
  197. </script>
  198. <style lang="scss" scoped>
  199. .login-page {
  200. min-height: 100vh;
  201. 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;
  202. background-size: 100%;
  203. display: flex;
  204. flex-direction: column;
  205. justify-content: space-between;
  206. }
  207. //弹出输入框
  208. .code-input-container {
  209. display: flex;
  210. align-items: center;
  211. margin-top: 30rpx;
  212. .code-input {
  213. background: #F7F8FA;
  214. width: 306rpx;
  215. height: 84rpx;
  216. line-height: 84rpx;
  217. padding: 0 20rpx;
  218. border-radius: 20rpx;
  219. box-sizing: border-box;
  220. &::placeholder {
  221. line-height: 84rpx;
  222. }
  223. }
  224. .code-canvas-wrapper {
  225. flex: 1;
  226. .code-image {
  227. width: 100%;
  228. height: 84rpx;
  229. }
  230. }
  231. }
  232. .form-box {
  233. flex: 1;
  234. margin: 0 30rpx;
  235. border-radius: 40rpx;
  236. background-color: #fff;
  237. overflow: hidden;
  238. }
  239. .form-input {
  240. background-color: #fff;
  241. }
  242. .input-box {
  243. padding: 150rpx 32rpx 0;
  244. margin-bottom: 80rpx;
  245. }
  246. .account,
  247. .password,
  248. .vcode {
  249. height: 96rpx;
  250. border-radius: 20rpx;
  251. padding: 0 48rpx;
  252. display: flex;
  253. align-items: center;
  254. background-color: #f7fafc;
  255. .input-inner {
  256. flex: 1;
  257. }
  258. }
  259. .account {
  260. margin-bottom: 48rpx;
  261. }
  262. .vcode-text {
  263. text-wrap: nowrap;
  264. font-size: 26rpx;
  265. background-color: #fff;
  266. padding: 14rpx 30rpx;
  267. border-radius: 12rpx;
  268. }
  269. .button {
  270. padding: 0 32rpx;
  271. .button-item {
  272. line-height: 96rpx;
  273. border-radius: 20rpx;
  274. text-align: center;
  275. font-size: 32rpx;
  276. background-color: $uv-primary;
  277. color: #fff;
  278. }
  279. }
  280. .pact {
  281. padding-top: 20rpx;
  282. padding-bottom: 120rpx;
  283. height: 40rpx;
  284. }
  285. .pact-box {
  286. height: 40rpx;
  287. display: flex;
  288. align-items: center;
  289. justify-content: center;
  290. font-size: 24rpx;
  291. }
  292. .title {
  293. padding-top: 180rpx;
  294. padding-bottom: 40rpx;
  295. padding-left: 64rpx;
  296. font-size: 48rpx;
  297. font-weight: 700;
  298. color: #383838;
  299. }
  300. .form-tab {
  301. display: flex;
  302. align-items: center;
  303. justify-content: space-between;
  304. background-color: #ecf1fb;
  305. height: 100rpx;
  306. background: linear-gradient(180deg, #e3ebf9 0%, #ffffff 100%);
  307. .tab-item {
  308. width: 340rpx;
  309. line-height: 100rpx;
  310. text-align: center;
  311. color: #9e9e9e;
  312. position: relative;
  313. &::after {
  314. content: '';
  315. width: 50rpx;
  316. height: 8rpx;
  317. display: block;
  318. background-color: $uv-primary;
  319. border-radius: 4rpx;
  320. position: absolute;
  321. left: 50%;
  322. bottom: 14rpx;
  323. transform: translateX(-50%);
  324. opacity: 0;
  325. }
  326. &.select {
  327. background-color: #fff;
  328. color: $uv-primary;
  329. }
  330. &.type1.select {
  331. border-radius: 0 40rpx 0 0;
  332. }
  333. &.type2.select {
  334. border-radius: 40rpx 0 0;
  335. }
  336. &.select::after {
  337. opacity: 1;
  338. }
  339. }
  340. }
  341. .login-bottom {
  342. display: flex;
  343. flex-direction: column;
  344. justify-content: center;
  345. align-items: center;
  346. padding-bottom: 30px;
  347. .wx-btn {
  348. margin-top: 40rpx;
  349. height: 80rpx;
  350. width: 160rpx;
  351. background: #fff;
  352. display: flex;
  353. align-items: center;
  354. justify-content: center;
  355. &::after {
  356. border-radius: 80rpx;
  357. border-color: $uv-border-color;
  358. }
  359. }
  360. }
  361. </style>