clientIndex.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <!-- my/index.vue -->
  2. <template>
  3. <view class="my-page">
  4. <!-- 你的页面内容 -->
  5. <div class="captcha-container" v-if="showCaptcha">
  6. <view class="input-item">
  7. <text class="label">手机号:</text>
  8. <input v-model="userTel" type="number" placeholder="请输入手机号" class="input" maxlength="11" />
  9. </view>
  10. <view id="your-dom-id" class="nc-container"></view>
  11. <view class="code-input">
  12. <input v-model="verification" type="number" placeholder="请输入验证码" class="code-input-field" />
  13. <button @click="checkVerification" class="code-btn">
  14. {{btnText}}
  15. </button>
  16. </view>
  17. <button @click="save()" class="verify-phone-btn">绑定手机号</button>
  18. </div>
  19. </view>
  20. </template>
  21. <script setup>
  22. import {
  23. ref,
  24. onMounted,
  25. onUnmounted
  26. } from 'vue'
  27. import {
  28. bindApplet,
  29. SendCode
  30. } from "@/api/login.js"
  31. import {
  32. toast
  33. } from '@/utils/common'
  34. import {
  35. onLoad,
  36. onShow
  37. } from '@dcloudio/uni-app';
  38. // 从URL参数中获取code
  39. const wxCode = ref('')
  40. const showCaptcha = ref(false)
  41. const userTel = ref('')
  42. const verification = ref('')
  43. const btnTextDisabled = ref(false)
  44. const btnText = ref('获取验证码')
  45. const countdown = ref(60)
  46. const isValidate = ref(false)
  47. const sliderData = ref({})
  48. const timer = ref(null)
  49. // 生命周期
  50. onLoad((options) => {
  51. // 获取URL中的code参数
  52. const queryCode = options.code
  53. console.log('接收到code参数:', queryCode)
  54. if (queryCode) {
  55. wxCode.value = queryCode
  56. showCaptcha.value = true
  57. // 页面加载完成后初始化验证码
  58. initCaptcha()
  59. } else {
  60. toast('参数错误,请重新登录')
  61. }
  62. })
  63. onUnmounted(() => {
  64. clearTimer()
  65. })
  66. // 初始化验证码
  67. function initCaptcha() {
  68. try {
  69. window.initAliyunCaptcha({
  70. SceneId: "1xp14eyl",
  71. mode: "embed",
  72. element: "#your-dom-id",
  73. success: function(captchaVerifyParam) {
  74. console.log('验证成功:', captchaVerifyParam);
  75. sliderData.value.param = captchaVerifyParam
  76. isValidate.value = true;
  77. },
  78. fail: function(result) {
  79. console.error('验证失败:', result);
  80. isValidate.value = false;
  81. },
  82. slideStyle: {
  83. width: 350,
  84. height: 40,
  85. },
  86. language: 'cn',
  87. });
  88. } catch (error) {
  89. console.error('初始化验证码失败:', error)
  90. toast('验证码初始化失败')
  91. }
  92. }
  93. // 获取验证码
  94. async function checkVerification() {
  95. if (!userTel.value) {
  96. toast('请输入手机号!')
  97. return false
  98. }
  99. // if (!/^1[3-9]\d{9}$/.test(userTel.value)) {
  100. // toast('请输入正确的手机号!')
  101. // return false
  102. // }
  103. if (isValidate.value) {
  104. try {
  105. const res = await SendCode({
  106. 'param': sliderData.value.param,
  107. 'phone': userTel.value,
  108. 'sceneId': '1xp14eyl',
  109. })
  110. if (res.code === 0 && res.data) {
  111. setTime()
  112. } else {
  113. toast(res.msg || '发送失败')
  114. }
  115. } catch (error) {
  116. console.error('发送验证码失败:', error)
  117. isValidate.value = false
  118. initCaptcha()
  119. toast('发送失败,请重试')
  120. }
  121. } else {
  122. toast('请先完成滑块验证!')
  123. return false
  124. }
  125. }
  126. // 倒计时
  127. function setTime() {
  128. if (countdown.value === 0) {
  129. btnTextDisabled.value = false
  130. btnText.value = '获取验证码'
  131. countdown.value = 60
  132. isValidate.value = false
  133. initCaptcha()
  134. } else {
  135. btnTextDisabled.value = true
  136. btnText.value = countdown.value + 's'
  137. countdown.value--
  138. timer.value = setTimeout(() => {
  139. setTime()
  140. }, 1000)
  141. }
  142. }
  143. function clearTimer() {
  144. if (timer.value) {
  145. clearTimeout(timer.value)
  146. timer.value = null
  147. }
  148. }
  149. // 绑定手机号
  150. async function save() {
  151. if (!verification.value) {
  152. toast('请输入验证码')
  153. return
  154. }
  155. try {
  156. const params = {
  157. tel: userTel.value,
  158. code: verification.value,
  159. openId: wxCode.value // 传递微信的code
  160. }
  161. // 使用tijiao接口绑定手机号
  162. const response = await bindApplet(params)
  163. if (response.code === 0) {
  164. toast('绑定成功')
  165. uni.postMessage({
  166. data: {
  167. status: 'success',
  168. phone: userTel.value,
  169. captchaResult: sliderData.value.param,
  170. bindResult: response.data
  171. }
  172. })
  173. // 延迟返回
  174. setTimeout(() => {
  175. uni.navigateBack()
  176. }, 1000)
  177. } else {
  178. toast(response.msg || '绑定失败')
  179. }
  180. } catch (error) {
  181. console.error('绑定失败:', error)
  182. toast('绑定失败,请重试')
  183. }
  184. }
  185. </script>
  186. <style scoped>
  187. .my-page {
  188. padding: 30rpx;
  189. }
  190. .captcha-container {
  191. background: white;
  192. padding: 40rpx 30rpx;
  193. border-radius: 16rpx;
  194. box-shadow: 0 4rpx 24rpx rgba(0, 0, 0, 0.08);
  195. }
  196. .input-item {
  197. display: flex;
  198. align-items: center;
  199. margin-bottom: 30rpx;
  200. }
  201. .label {
  202. width: 150rpx;
  203. font-size: 28rpx;
  204. color: #333;
  205. }
  206. .input {
  207. flex: 1;
  208. height: 80rpx;
  209. padding: 0 20rpx;
  210. border: 1px solid #e5e5e5;
  211. border-radius: 8rpx;
  212. font-size: 28rpx;
  213. }
  214. .nc-container {
  215. margin: 30rpx 0;
  216. display: block !important;
  217. }
  218. .code-input {
  219. display: flex;
  220. align-items: center;
  221. height: 80rpx;
  222. margin-bottom: 30rpx;
  223. border: 1px solid #e5e5e5;
  224. border-radius: 8rpx;
  225. overflow: hidden;
  226. }
  227. .icon {
  228. width: 40rpx;
  229. height: 40rpx;
  230. margin: 0 20rpx;
  231. }
  232. .code-input-field {
  233. flex: 1;
  234. height: 100%;
  235. padding: 0 20rpx;
  236. font-size: 28rpx;
  237. }
  238. .code-btn {
  239. width: 200rpx;
  240. height: 100%;
  241. line-height: 80rpx;
  242. background-color: #17c05b;
  243. color: #fff;
  244. font-size: 26rpx;
  245. border: none;
  246. border-radius: 0;
  247. }
  248. .code-btn:disabled {
  249. background-color: #ccc;
  250. }
  251. .verify-phone-btn {
  252. width: 100%;
  253. height: 90rpx;
  254. line-height: 90rpx;
  255. background-color: #17c05b;
  256. color: #fff;
  257. font-size: 32rpx;
  258. border: none;
  259. border-radius: 45rpx;
  260. }
  261. </style>