login.vue 9.7 KB

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