codeLogin.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <template>
  2. <div class="form">
  3. <u-form :model="codeForm" ref="validateCodeForm">
  4. <u-form-item class="cell" label-width="120" label="手机号" prop="mobile">
  5. <u-input maxlength="11" v-model="codeForm.mobile" placeholder="请输入您的手机号" />
  6. </u-form-item>
  7. <u-form-item class="cell code" label-width="120" prop="code" label="验证码">
  8. <div style="display:flex; with:100%;">
  9. <u-input v-model="codeForm.code" placeholder="请输入验证码" />
  10. <u-verification-code keep-running unique-key="page-login" :seconds="seconds" @end="end" @start="start" ref="uCode" @change="codeChange"></u-verification-code>
  11. <view @tap="getCode" class="text-tips">{{ tips }}</view>
  12. </div>
  13. </u-form-item>
  14. <view class="submit" @click="submit">登录</view>
  15. <view class="text-tips cell" @click="clickLogin">一键登录</view>
  16. <myVerification v-if="codeFlag" @send="verification" class="verification" ref="verification" business="LOGIN" />
  17. </u-form>
  18. </div>
  19. </template>
  20. <script>
  21. import { sendMobile, smsLogin } from "@/api/login";
  22. import { getUserInfo } from "@/api/members";
  23. import storage from "@/utils/storage.js"; //缓存
  24. import { whetherNavigate } from "@/utils/Foundation"; //登录跳转
  25. import myVerification from "@/components/verification/verification.vue"; //验证码模块
  26. import uuid from "@/utils/uuid.modified.js"; // uuid
  27. export default {
  28. components: {
  29. myVerification,
  30. },
  31. props: ["status"], //是否勾选 《用户隐私》和《隐私政策》
  32. data() {
  33. return {
  34. uuid,
  35. flage: false, //是否验证码验证
  36. codeFlag: true, //验证开关
  37. codeForm: {
  38. mobile: "", //手机号
  39. code: "", //验证码
  40. },
  41. tips: "", //提示
  42. clientType: "", // 客户端类型
  43. seconds: 60,
  44. codeRules: {
  45. // 手机号验证
  46. mobile: [
  47. {
  48. validator: (rule, value, callback) => {
  49. return this.$u.test.mobile(value);
  50. },
  51. message: "手机号码不正确",
  52. trigger: ["blur"],
  53. },
  54. ],
  55. // 验证码验证
  56. code: [
  57. {
  58. min: 4,
  59. max: 6,
  60. required: true,
  61. message: "请输入验证码",
  62. trigger: ["blur"],
  63. },
  64. ],
  65. },
  66. };
  67. },
  68. // 必须要在onReady生命周期,因为onLoad生命周期组件可能尚未创建完毕
  69. mounted() {
  70. this.$refs.validateCodeForm.setRules(this.codeRules);
  71. /**
  72. * 条件编译判断当前客户端类型
  73. */
  74. //#ifdef H5
  75. this.clientType = "H5";
  76. //#endif
  77. //#ifdef APP-PLUS
  78. this.clientType = "APP";
  79. //#endif
  80. },
  81. watch: {
  82. flage(val) {
  83. if (val) {
  84. if (this.$refs.uCode.canGetCode) {
  85. // 模拟向后端请求验证码
  86. uni.showLoading({
  87. title: "正在获取验证码",
  88. });
  89. sendMobile(this.codeForm.mobile).then((res) => {
  90. uni.hideLoading();
  91. // 这里此提示会被this.start()方法中的提示覆盖
  92. if (res.data.success) {
  93. this.$refs.uCode.start();
  94. } else {
  95. uni.showToast({
  96. title: res.data.message,
  97. duration: 2000,
  98. icon: "none",
  99. });
  100. }
  101. });
  102. } else {
  103. this.$u.toast("请倒计时结束后再发送");
  104. }
  105. }
  106. },
  107. },
  108. methods: {
  109. // 验证码验证
  110. verification(val) {
  111. this.flage = val == this.$store.state.verificationKey ? true : false;
  112. },
  113. // 登录
  114. submit() {
  115. if (!this.status) {
  116. /**
  117. * 用户必须了解
  118. * 用户协议以及隐私政策
  119. */
  120. uni.showToast({
  121. title: "请您阅读并同意用户协议以及隐私政策",
  122. duration: 2000,
  123. icon: "none",
  124. });
  125. return false;
  126. }
  127. let _this = this;
  128. this.$refs.validateCodeForm.validate((valid) => {
  129. if (valid) {
  130. /**
  131. * 清空当前账号信息
  132. */
  133. storage.setHasLogin(false);
  134. storage.setAccessToken("");
  135. storage.setRefreshToken("");
  136. storage.setUuid(this.uuid.v1());
  137. storage.setUserInfo({});
  138. /**
  139. * 执行登录
  140. */
  141. smsLogin(this.codeForm, _this.clientType).then((res) => {
  142. if (res.data.success) {
  143. storage.setAccessToken(res.data.result.accessToken);
  144. storage.setRefreshToken(res.data.result.refreshToken);
  145. /**
  146. * 登录成功后获取个人信息
  147. */
  148. getUserInfo().then((user) => {
  149. if (user.data.success) {
  150. /**
  151. * 个人信息存储到缓存userInfo中
  152. */
  153. storage.setUserInfo(user.data.result);
  154. storage.setHasLogin(true);
  155. // 登录成功
  156. uni.showToast({
  157. title: "登录成功!",
  158. icon: "none",
  159. });
  160. /**
  161. * 计算出当前router路径
  162. * 1.如果跳转的链接为登录页面或跳转的链接为空页面。则会重新跳转到首页
  163. * 2.都不满足返回跳转页面
  164. */
  165. whetherNavigate();
  166. } else {
  167. uni.switchTab({
  168. url: "/pages/tabbar/home/index",
  169. });
  170. }
  171. });
  172. }
  173. });
  174. }
  175. });
  176. },
  177. // 跳转到一键登录
  178. clickLogin() {
  179. this.$emit("open", "click");
  180. },
  181. /**点击验证码*/
  182. codeChange(text) {
  183. this.tips = text;
  184. },
  185. /** 结束验证码后执行 */
  186. end() {},
  187. /**获取验证码 */
  188. getCode() {
  189. if (this.tips == "重新获取") {
  190. this.codeFlag = true;
  191. uni.showLoading({
  192. title: "加载中",
  193. });
  194. setTimeout(() => {
  195. this.$refs.verification.hide();
  196. uni.hideLoading();
  197. }, 2000);
  198. }
  199. if (!this.$u.test.mobile(this.codeForm.mobile)) {
  200. uni.showToast({
  201. title: "请输入正确手机号",
  202. icon: "none",
  203. });
  204. return false;
  205. }
  206. if (!this.flage) {
  207. this.$refs.verification.hide();
  208. return false;
  209. }
  210. },
  211. // 点击获取验证码
  212. start() {
  213. this.$u.toast("验证码已发送");
  214. this.flage = false;
  215. this.codeFlag = false;
  216. this.$refs.verification.hide();
  217. },
  218. },
  219. };
  220. </script>
  221. <style lang="scss" scoped>
  222. @import url("./login.scss");
  223. // #ifdef MP-WEIXIN
  224. @import url("./mp-codeLogin.scss");
  225. // #endif
  226. </style>