login.vue 9.7 KB

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