index.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <view v-if="isUp">
  3. <view class="mobile-bg" v-if="isShow" @click="close"></view>
  4. <view
  5. class="mobile-mask animated"
  6. :class="{ slideInUp: isUp }"
  7. :style="{ position: isPos ? 'fixed' : 'static' }"
  8. >
  9. <view class="input-item">
  10. <input type="text" v-model="account" placeholder="输入手机号" />
  11. </view>
  12. <view class="input-item">
  13. <input type="text" v-model="codeNum" placeholder="输入验证码" />
  14. <button class="code" :disabled="disabled" @click="code">
  15. {{ text }}
  16. </button>
  17. </view>
  18. <view class="sub_btn" @click="loginBtn">{{
  19. (!appStore.userInfo.phone && isLogin) ||
  20. (appStore.userInfo.phone && isLogin)
  21. ? "立即绑定"
  22. : "立即登录"
  23. }}</view>
  24. </view>
  25. </view>
  26. </template>
  27. <script setup>
  28. import { ref } from "vue";
  29. import { useAppStore } from "@/stores/app.js";
  30. import { onMounted } from "vue";
  31. import { useSendCode } from "@/hooks/useSendCode";
  32. import { useToast } from "@/hooks/useToast";
  33. import { registerVerify, getCodeApi, getUserInfo } from "@/api/user";
  34. import { getUserPhone, iosBinding } from "@/api/public";
  35. const app = getApp();
  36. const appStore = useAppStore();
  37. const { Toast } = useToast();
  38. const emit = defineEmits(["close", "wechatPhone"]);
  39. const props = defineProps({
  40. isUp: { type: Boolean, default: false },
  41. authKey: { type: String, default: "" },
  42. isShow: { type: Boolean, default: true },
  43. isPos: { type: Boolean, default: true },
  44. appleShow: { type: String, default: "" },
  45. platform: { type: String, default: "" },
  46. });
  47. // 状态
  48. const keyCode = ref("");
  49. const account = ref("");
  50. const codeNum = ref("");
  51. const isApp = ref(0);
  52. // 验证码倒计时等逻辑
  53. const { disabled, text, sendCode } = useSendCode();
  54. // 获取验证码
  55. async function code() {
  56. if (!account.value) return Toast({ title: "请填写手机号码" });
  57. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(account.value))
  58. return Toast({ title: "请输入正确的手机号码" });
  59. try {
  60. const res = await registerVerify(account.value);
  61. Toast({ title: res.msg });
  62. sendCode();
  63. } catch (err) {
  64. Toast({ title: err });
  65. }
  66. }
  67. // 获取验证码api
  68. function getCode() {
  69. getCodeApi()
  70. .then((res) => {
  71. keyCode.value = res.data.key;
  72. })
  73. .catch((res) => {
  74. Toast({ title: res });
  75. });
  76. }
  77. // 关闭弹窗
  78. function close() {
  79. emit("close", false);
  80. }
  81. // 登录/绑定
  82. function loginBtn() {
  83. if (!account.value) return Toast({ title: "请填写手机号码" });
  84. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(account.value))
  85. return Toast({ title: "请输入正确的手机号码" });
  86. if (!codeNum.value) return Toast({ title: "请填写验证码" });
  87. if (!/^[\w\d]+$/i.test(codeNum.value))
  88. return Toast({ title: "请输入正确的验证码" });
  89. uni.showLoading({
  90. title:
  91. !appStore.userInfo.phone && appStore.isLogin
  92. ? "正在绑定中"
  93. : "正在登录中",
  94. });
  95. if (!appStore.userInfo.phone && appStore.isLogin) {
  96. iosBinding({
  97. captcha: codeNum.value,
  98. phone: account.value,
  99. })
  100. .then((res) => {
  101. Toast({ title: "绑定手机号成功" });
  102. isApp.value = 1;
  103. getUserInfoFn();
  104. })
  105. .catch((error) => {
  106. uni.hideLoading();
  107. Toast({ title: error });
  108. });
  109. } else {
  110. getUserPhone({
  111. captcha: codeNum.value,
  112. phone: account.value,
  113. // #ifdef H5
  114. type: "public",
  115. // #endif
  116. key: props.authKey,
  117. })
  118. .then((res) => {
  119. appStore.LOGIN({ token: res.data.token });
  120. appStore.SETUID(res.data.uid);
  121. getUserInfoFn();
  122. })
  123. .catch((error) => {
  124. uni.hideLoading();
  125. Toast({ title: error });
  126. });
  127. }
  128. }
  129. // 获取用户信息
  130. function getUserInfoFn() {
  131. getUserInfo().then((res) => {
  132. console.log("getUserInfoFn", res.data);
  133. uni.hideLoading();
  134. appStore.UPDATE_USERINFO(res.data);
  135. // #ifdef MP
  136. Toast({ title: "登录成功" });
  137. close();
  138. // #endif
  139. // #ifdef H5
  140. emit("wechatPhone", true);
  141. // #endif
  142. });
  143. }
  144. onMounted(() => {
  145. // getCode() // 如需自动获取验证码可打开
  146. });
  147. </script>
  148. <style lang="scss" scoped>
  149. .mobile-bg {
  150. position: fixed;
  151. left: 0;
  152. top: 0;
  153. width: 100%;
  154. height: 100%;
  155. background: rgba(0, 0, 0, 0.5);
  156. }
  157. .isPos {
  158. position: static;
  159. }
  160. .mobile-mask {
  161. z-index: 20;
  162. // position: fixed;
  163. left: 0;
  164. bottom: 0;
  165. width: 100%;
  166. padding: 67rpx 30rpx;
  167. background: #fff;
  168. .input-item {
  169. display: flex;
  170. justify-content: space-between;
  171. width: 100%;
  172. height: 86rpx;
  173. margin-bottom: 38rpx;
  174. input {
  175. flex: 1;
  176. display: block;
  177. height: 100%;
  178. padding-left: 40rpx;
  179. border-radius: 43rpx;
  180. border: 1px solid #dcdcdc;
  181. }
  182. .code {
  183. display: flex;
  184. align-items: center;
  185. justify-content: center;
  186. width: 220rpx;
  187. height: 86rpx;
  188. margin-left: 30rpx;
  189. background: rgba(233, 51, 35, 0.05);
  190. font-size: 28rpx;
  191. color: $theme-color;
  192. border-radius: 43rpx;
  193. &[disabled] {
  194. background: rgba(0, 0, 0, 0.05);
  195. color: #999;
  196. }
  197. }
  198. }
  199. .sub_btn {
  200. width: 690rpx;
  201. height: 86rpx;
  202. line-height: 86rpx;
  203. margin-top: 60rpx;
  204. background: #e93323;
  205. border-radius: 43rpx;
  206. color: #fff;
  207. font-size: 28rpx;
  208. text-align: center;
  209. }
  210. }
  211. .animated {
  212. animation-duration: 0.4s;
  213. }
  214. </style>