editBankCard.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <template>
  2. <view class="container">
  3. <!-- 表单区域 -->
  4. <scroll-view class="content" scroll-y>
  5. <view class="form-section">
  6. <view class="form-item">
  7. <view class="form-label">开户行</view>
  8. <!-- <input
  9. class="form-input"
  10. v-model="formData.bankName"
  11. placeholder="请输入开户行"
  12. placeholder-class="text-placeholder"
  13. /> -->
  14. <view style="flex:1" @click="openBankPicker">
  15. <view class="form-input picker-input" :class="{ placeholder: !formData.bankName }">
  16. {{ getBankLabel() || '请选择开户行' }}
  17. <image class="w32 h32" src="/static/image/icon_my_more.png" mode=""></image>
  18. </view>
  19. </view>
  20. </view>
  21. <view class="divider"></view>
  22. <view class="form-item">
  23. <view class="form-label">支行</view>
  24. <input
  25. class="form-input"
  26. v-model="formData.bankBranch"
  27. placeholder="请输入支行"
  28. placeholder-class="text-placeholder"
  29. />
  30. </view>
  31. <view class="divider"></view>
  32. <view class="form-item">
  33. <view class="form-label">银行卡号</view>
  34. <input
  35. class="form-input"
  36. :value="isEditing ? formData.bankCardNo : formatBankCard(formData.bankCardNo)"
  37. @input="handleCardInput"
  38. @focus="isEditing = true"
  39. @blur="handleCardBlur"
  40. placeholder="请输入银行卡号"
  41. type="number"
  42. maxlength="19"
  43. placeholder-class="text-placeholder"
  44. />
  45. </view>
  46. </view>
  47. <!-- 注意事项 -->
  48. <view class="notes-section">
  49. <view class="notes-title">注意:</view>
  50. <view class="notes-item">1、只能绑定认证用户本人的银行卡;</view>
  51. <view class="notes-item">2、每次更换次数不得超过三次。</view>
  52. </view>
  53. </scroll-view>
  54. <!-- 银行搜索弹窗 -->
  55. <view class="search-picker-popup" v-if="showBankPicker" @click="closeBankPicker">
  56. <view class="popup-content" @click.stop>
  57. <view class="popup-header">
  58. <text class="popup-title">选择开户行</text>
  59. <view class="popup-close" @click="closeBankPicker">×</view>
  60. </view>
  61. <view class="search-box">
  62. <input
  63. class="search-input"
  64. v-model="bankSearchKeyword"
  65. placeholder="请输入银行名称搜索"
  66. @blur="onBankSearch"
  67. />
  68. </view>
  69. <scroll-view class="search-list" scroll-y>
  70. <view
  71. class="search-item"
  72. v-for="(item, index) in filteredBankList"
  73. :key="index"
  74. :class="{ active: formData.bankName === item.label }"
  75. @click="selectBank(item)"
  76. >
  77. <text>{{ item.label }}</text>
  78. <text v-if="formData.bankName === item.label" class="check-icon">✓</text>
  79. </view>
  80. <view class="search-empty" v-if="filteredBankList.length === 0">
  81. <text>暂无匹配结果</text>
  82. </view>
  83. </scroll-view>
  84. </view>
  85. </view>
  86. <!-- 底部按钮 -->
  87. <view class="bottom-bar">
  88. <view class="save-btn" @click="handleSave">保存</view>
  89. <!-- <view class="unbind-btn" @click="handleUnbind">解除绑定</view> -->
  90. </view>
  91. </view>
  92. </template>
  93. <script>
  94. import {getBankTypeList} from '@/api/certification'
  95. import { addBankCard, deleteBankCard, getBankCardInfo } from '@/api/bankCard'
  96. export default {
  97. data() {
  98. return {
  99. cardId: '',
  100. isEditing: false,
  101. formData: {
  102. "bankBranch": "",
  103. "bankCardNo": "",
  104. "bankName": ""
  105. },
  106. showBankPicker: false,
  107. bankSearchKeyword: '',
  108. filteredBankList: []
  109. }
  110. },
  111. onLoad(options) {
  112. // if (options.id) {
  113. // this.cardId = options.id
  114. // }
  115. this.loadBankCardDetail()
  116. this.loadBankList()
  117. },
  118. methods: {
  119. getBankLabel() {
  120. return this.formData.bankName || ''
  121. },
  122. openBankPicker() {
  123. this.showBankPicker = true
  124. this.bankSearchKeyword = ''
  125. },
  126. openBankPicker() {
  127. this.showBankPicker = true
  128. this.bankSearchKeyword = ''
  129. },
  130. closeBankPicker() {
  131. this.showBankPicker = false
  132. },
  133. async onBankSearch() {
  134. const keyword = this.bankSearchKeyword.trim()
  135. this.loadBankList(keyword)
  136. },
  137. selectBank(item) {
  138. this.formData.bankName = item.label
  139. this.closeBankPicker()
  140. },
  141. // 加载银行列表
  142. async loadBankList(keyword) {
  143. const params = {}
  144. // 如果传入了关键词,就用传入的
  145. if (keyword) {
  146. params.bankName = keyword.trim()
  147. }
  148. try {
  149. const res = await getBankTypeList(params)
  150. if (res.code === 200) {
  151. const arr = res.data
  152. this.filteredBankList = arr.map(item => ({
  153. label: item.bankName,
  154. value: item.id
  155. })).filter(opt => opt.label)
  156. }
  157. } catch (err) {
  158. console.log('银行类型加载失败', err)
  159. }
  160. },
  161. formatBankCard(cardNo) {
  162. if (!cardNo || cardNo.length < 8) return cardNo;
  163. const prefix = cardNo.substring(0, 4);
  164. const suffix = cardNo.substring(cardNo.length - 4);
  165. return `${prefix}****${suffix}`;
  166. },
  167. handleCardInput(e) {
  168. // 实时更新原始卡号数据
  169. this.formData.bankCardNo = e.detail.value;
  170. },
  171. handleCardBlur() {
  172. // 延迟切换状态,避免输入完成瞬间的视觉闪烁
  173. setTimeout(() => {
  174. this.isEditing = false;
  175. }, 100);
  176. // 可选:失去焦点后自动格式化卡号(每4位加空格,如需保留数字格式可注释)
  177. // this.formData.bankCardNo = this.formData.bankCardNo.replace(/\D/g, '');
  178. },
  179. async loadBankCardDetail() {
  180. try {
  181. uni.showLoading({ title: '加载中...' })
  182. const res = await getBankCardInfo({ id: this.cardId })
  183. uni.hideLoading()
  184. if (res.code === 200 && res.data) {
  185. // 根据接口返回的数据结构映射字段
  186. const data = res.data
  187. this.formData = {
  188. ...this.formData,
  189. bankBranch: data.bankBranch,
  190. bankCardNo: data.bankCardNo || '',
  191. bankName: data.bankName
  192. }
  193. }
  194. } catch (e) {
  195. uni.hideLoading()
  196. console.error('加载银行卡详情失败', e)
  197. }
  198. },
  199. goBack() {
  200. uni.navigateBack()
  201. },
  202. async handleSave() {
  203. // 表单验证
  204. if (!this.formData.bankName || !this.formData.bankName.trim()) {
  205. uni.showToast({
  206. icon: 'none',
  207. title: '请选择开户行'
  208. })
  209. return
  210. }
  211. if (!this.formData.bankBranch || !this.formData.bankBranch.trim()) {
  212. uni.showToast({
  213. icon: 'none',
  214. title: '请输入支行'
  215. })
  216. return
  217. }
  218. if (!this.formData.bankCardNo) {
  219. uni.showToast({
  220. icon: 'none',
  221. title: '请输入银行卡号'
  222. })
  223. return
  224. }
  225. // 银行卡号验证(简单验证)
  226. if (this.formData.bankCardNo.length < 16) {
  227. uni.showToast({
  228. icon: 'none',
  229. title: '请输入正确的银行卡号'
  230. })
  231. return
  232. }
  233. try {
  234. uni.showLoading({ title: '保存中...' })
  235. // 构建提交数据,按照接口字段要求
  236. const submitData = {
  237. bankName: this.formData.bankName,
  238. bankBranch: this.formData.bankBranch,
  239. bankCardNo: this.formData.bankCardNo
  240. }
  241. // 如果有ID(编辑时),添加id字段
  242. if (this.cardId || this.formData.id) {
  243. submitData.id = this.cardId || this.formData.id
  244. }
  245. const res = await addBankCard(submitData)
  246. uni.hideLoading()
  247. if (res.code === 200) {
  248. uni.showToast({
  249. icon: 'success',
  250. title: '保存成功'
  251. })
  252. setTimeout(() => {
  253. uni.navigateBack()
  254. }, 1500)
  255. } else {
  256. uni.showToast({
  257. icon: 'none',
  258. title: res.msg || '保存失败'
  259. })
  260. }
  261. } catch (e) {
  262. uni.hideLoading()
  263. console.error('保存银行卡失败', e)
  264. uni.showToast({
  265. icon: 'none',
  266. title: e.message || '保存失败'
  267. })
  268. }
  269. },
  270. handleUnbind() {
  271. uni.showModal({
  272. title: '提示',
  273. content: '确定要解除绑定该银行卡吗?',
  274. success: async (res) => {
  275. if (res.confirm) {
  276. try {
  277. uni.showLoading({ title: '解除绑定中...' })
  278. const result = await deleteBankCard({ id: this.cardId })
  279. uni.hideLoading()
  280. if (result.code === 200) {
  281. uni.showToast({
  282. icon: 'success',
  283. title: '解除绑定成功'
  284. })
  285. setTimeout(() => {
  286. uni.navigateBack()
  287. }, 1500)
  288. } else {
  289. uni.showToast({
  290. icon: 'none',
  291. title: result.msg || '解除绑定失败'
  292. })
  293. }
  294. } catch (e) {
  295. uni.hideLoading()
  296. uni.showToast({
  297. icon: 'none',
  298. title: '解除绑定失败'
  299. })
  300. }
  301. }
  302. }
  303. })
  304. }
  305. }
  306. }
  307. </script>
  308. <style lang="stylus">
  309. .text-placeholder{
  310. color: #C8C9CC !important;
  311. }
  312. </style>
  313. <style lang="scss" scoped>
  314. .container {
  315. // min-height: 100vh;
  316. background: #f5f5f5;
  317. display: flex;
  318. flex-direction: column;
  319. }
  320. .navbar {
  321. display: flex;
  322. align-items: center;
  323. justify-content: space-between;
  324. padding: 20rpx 24rpx;
  325. background: #fff;
  326. border-bottom: 1rpx solid #f0f0f0;
  327. .nav-left {
  328. width: 60rpx;
  329. .back-icon {
  330. font-size: 36rpx;
  331. color: #333;
  332. font-weight: bold;
  333. }
  334. }
  335. .nav-title {
  336. flex: 1;
  337. text-align: center;
  338. font-size: 36rpx;
  339. font-weight: bold;
  340. color: #333;
  341. }
  342. .nav-right {
  343. display: flex;
  344. align-items: center;
  345. gap: 24rpx;
  346. width: 60rpx;
  347. justify-content: flex-end;
  348. .more-icon {
  349. font-size: 32rpx;
  350. color: #333;
  351. }
  352. .eye-icon {
  353. font-size: 32rpx;
  354. color: #333;
  355. }
  356. }
  357. }
  358. .content {
  359. // flex: 1;
  360. // padding-bottom: 140rpx;
  361. }
  362. .form-section {
  363. background: #fff;
  364. // margin: 24rpx;
  365. // border-radius: 16rpx;
  366. overflow: hidden;
  367. .form-item {
  368. display: flex;
  369. align-items: center;
  370. justify-content: space-between;
  371. padding: 28rpx 32rpx;
  372. // min-height: 100rpx
  373. .form-label {
  374. font-family: PingFang SC, PingFang SC;
  375. font-weight: 400;
  376. font-size: 28rpx;
  377. color: #666666;
  378. width: 150rpx;
  379. }
  380. .form-input {
  381. flex: 1;
  382. font-size: 28rpx;
  383. color: #333;
  384. text-align: right;
  385. margin-left: 24rpx;
  386. &.picker-input {
  387. display: flex;
  388. align-items: center;
  389. justify-content: flex-end;
  390. .arrow-right {
  391. font-size: 32rpx;
  392. color: #999;
  393. margin-left: 8rpx;
  394. }
  395. }
  396. }
  397. }
  398. // placeholder 样式(需要独立定义,不能嵌套)
  399. .placeholder {
  400. //color: #C8C9CC !important;
  401. }
  402. .divider {
  403. height: 1rpx;
  404. background: #EBEDF0;
  405. margin-left: 24rpx;
  406. }
  407. }
  408. .notes-section {
  409. // background: #fff;
  410. // border-radius: 16rpx;
  411. padding: 22rpx 32rpx;
  412. // margin: 0 24rpx 24rpx;
  413. .notes-title {
  414. font-family: PingFang SC, PingFang SC;
  415. font-weight: 400;
  416. font-size: 28rpx;
  417. color: #999999;
  418. line-height: 40rpx;
  419. }
  420. .notes-item {
  421. font-family: PingFang SC, PingFang SC;
  422. font-weight: 400;
  423. font-size: 28rpx;
  424. color: #999999;
  425. line-height: 40rpx;
  426. }
  427. }
  428. .bottom-bar {
  429. display: flex;
  430. align-items: center;
  431. flex-direction: column;
  432. padding: 32rpx;
  433. .save-btn {
  434. width: 100%;
  435. height: 88rpx;
  436. background: #388BFF;
  437. border-radius: 44rpx;
  438. display: flex;
  439. align-items: center;
  440. justify-content: center;
  441. font-size: 32rpx;
  442. color: #fff;
  443. }
  444. .unbind-btn{
  445. margin-top: 20rpx;
  446. width: 100%;
  447. height: 88rpx;
  448. color: #388BFF;
  449. display: flex;
  450. align-items: center;
  451. justify-content: center;
  452. font-size: 32rpx;
  453. }
  454. }
  455. .search-picker-popup {
  456. position: fixed;
  457. top: 0;
  458. left: 0;
  459. right: 0;
  460. bottom: 0;
  461. background: rgba(0, 0, 0, 0.5);
  462. z-index: 1000;
  463. display: flex;
  464. align-items: flex-end;
  465. .popup-content {
  466. width: 100%;
  467. height: 80vh;
  468. background: #fff;
  469. border-radius: 24rpx 24rpx 0 0;
  470. display: flex;
  471. flex-direction: column;
  472. }
  473. .popup-header {
  474. display: flex;
  475. align-items: center;
  476. justify-content: space-between;
  477. padding: 32rpx;
  478. border-bottom: 1rpx solid #f0f0f0;
  479. .popup-title {
  480. font-size: 32rpx;
  481. font-weight: bold;
  482. color: #333;
  483. }
  484. .popup-close {
  485. font-size: 48rpx;
  486. color: #999;
  487. line-height: 1;
  488. }
  489. }
  490. .search-box {
  491. padding: 24rpx 32rpx;
  492. .search-input {
  493. //width: 100%;
  494. height: 72rpx;
  495. background: #f5f5f5;
  496. border-radius: 36rpx;
  497. padding: 0 32rpx;
  498. font-size: 28rpx;
  499. }
  500. }
  501. .search-list {
  502. flex: 1;
  503. height: 60%;
  504. padding: 0 32rpx 40rpx;
  505. box-sizing: border-box;
  506. .search-item {
  507. display: flex;
  508. align-items: center;
  509. justify-content: space-between;
  510. padding: 28rpx 0;
  511. border-bottom: 1rpx solid #f5f5f5;
  512. &:last-child {
  513. border-bottom: none;
  514. }
  515. &.active {
  516. color: #388BFF;
  517. text {
  518. color: #388BFF;
  519. }
  520. }
  521. .check-icon {
  522. color: #388BFF;
  523. font-size: 32rpx;
  524. font-weight: bold;
  525. }
  526. text {
  527. font-size: 28rpx;
  528. color: #333;
  529. }
  530. }
  531. .search-empty {
  532. display: flex;
  533. align-items: center;
  534. justify-content: center;
  535. padding: 60rpx 0;
  536. text {
  537. font-size: 28rpx;
  538. color: #999;
  539. }
  540. }
  541. }
  542. }
  543. </style>