login.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <template>
  2. <view class="loginpage">
  3. <view class="tab-box">
  4. <view
  5. v-for="tab in list"
  6. :key="tab.index"
  7. :class="current == tab.index ? 'actvie' : ''"
  8. @click="sectionChange(tab.index)"
  9. >
  10. <view>{{ tab.name }}</view>
  11. <image src="/static/image/selected.png" mode="" />
  12. </view>
  13. </view>
  14. <view>
  15. <view class="login_box">
  16. <u--form
  17. :model="form"
  18. ref="uForm1"
  19. v-show="current == 0"
  20. labelWidth="100"
  21. >
  22. <u-form-item prop="account" label="账号"
  23. ><u--input
  24. type="idcard"
  25. v-model="form.account"
  26. border="bottom"
  27. placeholder-style="color:#999999"
  28. placeholder="手机号/身份证号"
  29. /></u-form-item>
  30. <u-form-item prop="pwd" label="密码"
  31. ><u--input
  32. class="password"
  33. v-model="form.pwd"
  34. border="bottom"
  35. placeholder-style="color:#999999"
  36. type="password"
  37. placeholder="登录密码"
  38. /></u-form-item>
  39. </u--form>
  40. <u--form
  41. :model="form"
  42. ref="uForm2"
  43. v-show="current == 1"
  44. labelWidth="130"
  45. >
  46. <u-form-item prop="tel" ref="tel" label="手机号"
  47. ><u--input
  48. type="number"
  49. maxlength="11"
  50. border="bottom"
  51. placeholder-style="color:#999999"
  52. v-model="form.tel"
  53. placeholder="请输入手机号"
  54. /></u-form-item>
  55. <u-form-item prop="code" label="验证码">
  56. <u--input
  57. v-model="form.code"
  58. type="number"
  59. border="bottom"
  60. placeholder-style="color:#999999"
  61. placeholder="请输入验证码"
  62. >
  63. <template slot="suffix">
  64. <u-code
  65. ref="uCode"
  66. @change="codeChange"
  67. seconds="60"
  68. changeText="X秒重新获取"
  69. ></u-code>
  70. <u-button size="mini" @click="getCode">{{ codeTips }}</u-button>
  71. </template>
  72. </u--input>
  73. </u-form-item>
  74. </u--form>
  75. </view>
  76. <u-button
  77. :disabled="isUse"
  78. class="c_btn"
  79. text="登录"
  80. @click="handelLogin"
  81. ></u-button>
  82. <view style="width: 100%; margin: 40rpx auto">
  83. <view
  84. style="
  85. display: flex;
  86. align-items: center;
  87. color: #007aff;
  88. font-size: 24rpx;
  89. justify-content: center;
  90. "
  91. >
  92. <view style="margin: 0 40rpx" @click="toRegister">立即注册</view>
  93. <view
  94. style="width: 3rpx; height: 20rpx; background-color: #007aff"
  95. ></view>
  96. <navigator
  97. hover-class="none"
  98. url="/pages/login/forget"
  99. style="margin: 0 40rpx"
  100. >忘记密码</navigator
  101. >
  102. </view>
  103. </view>
  104. </view>
  105. </view>
  106. </template>
  107. <script>
  108. import { encryptor } from "@/common/jse";
  109. import { loginSms, accountLogin, smsLogin } from "@/utils/login";
  110. export default {
  111. data() {
  112. return {
  113. code: "",
  114. form: {
  115. tel: "",
  116. code: "",
  117. account: "18378140619",
  118. pwd: "123456Aa..",
  119. },
  120. list: [
  121. {
  122. name: "密码登录",
  123. index: 0,
  124. },
  125. {
  126. name: "短信登录",
  127. index: 1,
  128. },
  129. ],
  130. rules: {
  131. tel: [
  132. {
  133. required: true,
  134. message: "请输入手机号",
  135. // 可以单个或者同时写两个触发验证方式
  136. trigger: ["change", "blur"],
  137. },
  138. {
  139. validator: (rule, value, callback) => {
  140. return this.$u.test.mobile(value);
  141. },
  142. message: "手机号码格式不正确",
  143. // 触发器可以同时用blur和change
  144. trigger: ["change", "blur"],
  145. },
  146. ],
  147. account: [
  148. {
  149. required: true,
  150. message: "请输入手机号/学员身份证",
  151. // 可以单个或者同时写两个触发验证方式
  152. trigger: ["change"],
  153. },
  154. ],
  155. pwd: [
  156. {
  157. required: true,
  158. message: "请输入密码",
  159. // 可以单个或者同时写两个触发验证方式
  160. trigger: ["change"],
  161. },
  162. ],
  163. code: [
  164. {
  165. required: true,
  166. message: "请输入验证码",
  167. // 可以单个或者同时写两个触发验证方式
  168. trigger: ["change"],
  169. },
  170. ],
  171. },
  172. current: 0,
  173. codeTips: "",
  174. isUse: false,
  175. isBack: false,
  176. };
  177. },
  178. onLoad(option) {
  179. if (option.isBack) {
  180. this.isBack = option.isBack;
  181. }
  182. },
  183. onShow() {},
  184. onReady() {
  185. this.$refs.uForm1.setRules(this.rules);
  186. this.$refs.uForm2.setRules(this.rules);
  187. },
  188. methods: {
  189. toRegister() {
  190. uni.navigateTo({
  191. url: "/pages/login/register",
  192. });
  193. },
  194. handelLogin() {
  195. const isC1 = this.current == 1;
  196. this.$refs[isC1 ? "uForm2" : "uForm1"]
  197. .validate()
  198. .then((res) => {
  199. this.isUse = true;
  200. isC1 ? this.sms_login() : this.pwlogin();
  201. })
  202. .catch((errors) => {
  203. console.log("校验失败");
  204. });
  205. },
  206. sms_login() {
  207. smsLogin({
  208. tel: this.form.tel,
  209. code: this.form.code,
  210. })
  211. .then((data) => {
  212. this.$u.toast("登录成功");
  213. this.loginCallback(data);
  214. })
  215. .finally(() => {
  216. this.isUse = false;
  217. });
  218. },
  219. pwlogin() {
  220. let form = JSON.parse(JSON.stringify(this.form));
  221. form.pwd = encryptor(form.pwd);
  222. accountLogin(form)
  223. .then((data) => {
  224. this.$u.toast("登录成功");
  225. this.loginCallback(data);
  226. })
  227. .finally(() => {
  228. this.isUse = false;
  229. });
  230. },
  231. codeChange(text) {
  232. this.codeTips = text;
  233. },
  234. // 获取验证码
  235. getCode() {
  236. if (this.$refs.uCode.canGetCode) {
  237. this.$refs.uForm2.validateField("tel", (res) => {
  238. if (res.length) {
  239. return;
  240. }
  241. loginSms({ tel: this.form.tel }).then(
  242. (res) => {
  243. this.$u.toast("验证码已发送");
  244. // 通知验证码组件内部开始倒计时
  245. this.$refs.uCode.start();
  246. },
  247. (err) => {
  248. console.log(err);
  249. }
  250. );
  251. });
  252. }
  253. },
  254. sectionChange(index) {
  255. this.current = index;
  256. },
  257. loginCallback(data) {
  258. this.$store.commit("LOGIN_CB", data);
  259. this.$store.dispatch("getUserInfo").then((res) => {
  260. setTimeout(() => {
  261. uni.navigateTo({
  262. url: "/pages/index/index",
  263. });
  264. }, 600);
  265. });
  266. },
  267. },
  268. };
  269. </script>
  270. <style scoped lang="scss">
  271. .loginpage {
  272. padding: 0 48rpx;
  273. .loginpage-title {
  274. font-weight: bold;
  275. color: #222222;
  276. font-size: 48rpx;
  277. margin: 96rpx 0;
  278. }
  279. .tab-box {
  280. margin: 116rpx 0 90rpx;
  281. font-weight: bold;
  282. color: #666666;
  283. font-size: 32rpx;
  284. display: flex;
  285. align-items: center;
  286. & > view {
  287. margin-right: 52rpx;
  288. }
  289. .actvie {
  290. color: #222222;
  291. font-size: 48rpx;
  292. image {
  293. opacity: 1;
  294. }
  295. }
  296. image {
  297. display: block;
  298. width: 40rpx;
  299. height: 40rpx;
  300. margin: 0 auto;
  301. opacity: 0;
  302. }
  303. }
  304. }
  305. .wxBtn {
  306. position: fixed;
  307. bottom: 10%;
  308. width: 100%;
  309. left: 0;
  310. }
  311. /deep/ .wxBtn button::after {
  312. border: none;
  313. }
  314. .loginBtn {
  315. // width: 526rpx;
  316. height: 80rpx;
  317. background: linear-gradient(90deg, #015eea, #00c0fa);
  318. box-shadow: 0rpx 10rpx 16rpx 4rpx rgba(1, 99, 235, 0.04);
  319. border-radius: 40rpx;
  320. color: #ffffff;
  321. text-align: center;
  322. line-height: 80rpx;
  323. margin: 40rpx auto;
  324. &.able {
  325. opacity: 1;
  326. }
  327. }
  328. .wxloginBtn {
  329. margin: 0 auto;
  330. background: url("/static/loginBtn.png") no-repeat;
  331. background-size: 100% 100%;
  332. border: none;
  333. width: 100rpx;
  334. height: 100rpx;
  335. }
  336. /deep/page {
  337. background-color: #ffffff;
  338. height: 100%;
  339. width: 100%;
  340. }
  341. .login_box {
  342. margin-top: 30rpx;
  343. margin-bottom: 68rpx;
  344. .password {
  345. /deep/.uicon-eye-fill {
  346. &::before {
  347. color: #007aff;
  348. content: "\e613";
  349. }
  350. }
  351. }
  352. }
  353. /deep/ .u-item-bg {
  354. border-radius: 32px !important;
  355. }
  356. /deep/ .u-subsection {
  357. border-radius: 32px !important;
  358. .u-subsection__bar {
  359. border-radius: 32px !important;
  360. }
  361. }
  362. .full_img {
  363. position: absolute;
  364. left: 0;
  365. display: block;
  366. width: 100%;
  367. z-index: -999;
  368. top: 0;
  369. }
  370. .head {
  371. height: 96rpx;
  372. width: 100%;
  373. line-height: 96rpx;
  374. margin-top: 40rpx;
  375. text-align: center;
  376. display: flex;
  377. position: relative;
  378. justify-content: center;
  379. }
  380. .icon {
  381. position: absolute;
  382. left: 30rpx;
  383. }
  384. </style>