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