clientIndex.vue 5.9 KB

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