codeLogin.vue 7.0 KB

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