codeLogin.vue 7.0 KB

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