login.vue 10.0 KB

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