editBankCard.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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>
  15. <view class="divider"></view>
  16. <view class="form-item">
  17. <view class="form-label">支行</view>
  18. <input
  19. class="form-input"
  20. v-model="formData.bankBranch"
  21. placeholder="请输入支行"
  22. placeholder-class="text-placeholder"
  23. />
  24. </view>
  25. <view class="divider"></view>
  26. <view class="form-item">
  27. <view class="form-label">银行卡号</view>
  28. <input
  29. class="form-input"
  30. :value="isEditing ? formData.bankCardNo : formatBankCard(formData.bankCardNo)"
  31. @input="handleCardInput"
  32. @focus="isEditing = true"
  33. @blur="handleCardBlur"
  34. placeholder="请输入银行卡号"
  35. type="number"
  36. maxlength="19"
  37. placeholder-class="text-placeholder"
  38. />
  39. </view>
  40. </view>
  41. <!-- 注意事项 -->
  42. <view class="notes-section">
  43. <view class="notes-title">注意:</view>
  44. <view class="notes-item">1、只能绑定认证用户本人的银行卡;</view>
  45. <view class="notes-item">2、每次更换次数不得超过三次。</view>
  46. </view>
  47. </scroll-view>
  48. <!-- 底部按钮 -->
  49. <view class="bottom-bar">
  50. <view class="save-btn" @click="handleSave">保存</view>
  51. <!-- <view class="unbind-btn" @click="handleUnbind">解除绑定</view> -->
  52. </view>
  53. </view>
  54. </template>
  55. <script>
  56. import { addBankCard, deleteBankCard, getBankCardInfo } from '@/api/bankCard'
  57. export default {
  58. data() {
  59. return {
  60. cardId: '',
  61. isEditing: false,
  62. formData: {
  63. "bankBranch": "",
  64. "bankCardNo": "",
  65. "bankName": ""
  66. }
  67. }
  68. },
  69. onLoad(options) {
  70. // if (options.id) {
  71. // this.cardId = options.id
  72. // }
  73. this.loadBankCardDetail()
  74. },
  75. methods: {
  76. formatBankCard(cardNo) {
  77. if (!cardNo || cardNo.length < 8) return cardNo;
  78. const prefix = cardNo.substring(0, 4);
  79. const suffix = cardNo.substring(cardNo.length - 4);
  80. return `${prefix}****${suffix}`;
  81. },
  82. handleCardInput(e) {
  83. // 实时更新原始卡号数据
  84. this.formData.bankCardNo = e.detail.value;
  85. },
  86. handleCardBlur() {
  87. // 延迟切换状态,避免输入完成瞬间的视觉闪烁
  88. setTimeout(() => {
  89. this.isEditing = false;
  90. }, 100);
  91. // 可选:失去焦点后自动格式化卡号(每4位加空格,如需保留数字格式可注释)
  92. // this.formData.bankCardNo = this.formData.bankCardNo.replace(/\D/g, '');
  93. },
  94. async loadBankCardDetail() {
  95. try {
  96. uni.showLoading({ title: '加载中...' })
  97. const res = await getBankCardInfo({ id: this.cardId })
  98. uni.hideLoading()
  99. if (res.code === 200 && res.data) {
  100. // 根据接口返回的数据结构映射字段
  101. const data = res.data
  102. this.formData = {
  103. ...this.formData,
  104. bankBranch: data.bankBranch,
  105. bankCardNo: data.bankCardNo || '',
  106. bankName: data.bankName
  107. }
  108. }
  109. } catch (e) {
  110. uni.hideLoading()
  111. console.error('加载银行卡详情失败', e)
  112. }
  113. },
  114. goBack() {
  115. uni.navigateBack()
  116. },
  117. async handleSave() {
  118. // 表单验证
  119. if (!this.formData.bankName || !this.formData.bankName.trim()) {
  120. uni.showToast({
  121. icon: 'none',
  122. title: '请输入开户行'
  123. })
  124. return
  125. }
  126. if (!this.formData.bankBranch || !this.formData.bankBranch.trim()) {
  127. uni.showToast({
  128. icon: 'none',
  129. title: '请输入支行'
  130. })
  131. return
  132. }
  133. if (!this.formData.bankCardNo) {
  134. uni.showToast({
  135. icon: 'none',
  136. title: '请输入银行卡号'
  137. })
  138. return
  139. }
  140. // 银行卡号验证(简单验证)
  141. if (this.formData.bankCardNo.length < 16) {
  142. uni.showToast({
  143. icon: 'none',
  144. title: '请输入正确的银行卡号'
  145. })
  146. return
  147. }
  148. try {
  149. uni.showLoading({ title: '保存中...' })
  150. // 构建提交数据,按照接口字段要求
  151. const submitData = {
  152. bankName: this.formData.bankName,
  153. bankBranch: this.formData.bankBranch,
  154. bankCardNo: this.formData.bankCardNo
  155. }
  156. // 如果有ID(编辑时),添加id字段
  157. if (this.cardId || this.formData.id) {
  158. submitData.id = this.cardId || this.formData.id
  159. }
  160. const res = await addBankCard(submitData)
  161. uni.hideLoading()
  162. if (res.code === 200) {
  163. uni.showToast({
  164. icon: 'success',
  165. title: '保存成功'
  166. })
  167. setTimeout(() => {
  168. uni.navigateBack()
  169. }, 1500)
  170. } else {
  171. uni.showToast({
  172. icon: 'none',
  173. title: res.msg || '保存失败'
  174. })
  175. }
  176. } catch (e) {
  177. uni.hideLoading()
  178. console.error('保存银行卡失败', e)
  179. uni.showToast({
  180. icon: 'none',
  181. title: e.message || '保存失败'
  182. })
  183. }
  184. },
  185. handleUnbind() {
  186. uni.showModal({
  187. title: '提示',
  188. content: '确定要解除绑定该银行卡吗?',
  189. success: async (res) => {
  190. if (res.confirm) {
  191. try {
  192. uni.showLoading({ title: '解除绑定中...' })
  193. const result = await deleteBankCard({ id: this.cardId })
  194. uni.hideLoading()
  195. if (result.code === 200) {
  196. uni.showToast({
  197. icon: 'success',
  198. title: '解除绑定成功'
  199. })
  200. setTimeout(() => {
  201. uni.navigateBack()
  202. }, 1500)
  203. } else {
  204. uni.showToast({
  205. icon: 'none',
  206. title: result.msg || '解除绑定失败'
  207. })
  208. }
  209. } catch (e) {
  210. uni.hideLoading()
  211. uni.showToast({
  212. icon: 'none',
  213. title: '解除绑定失败'
  214. })
  215. }
  216. }
  217. }
  218. })
  219. }
  220. }
  221. }
  222. </script>
  223. <style lang="stylus">
  224. .text-placeholder{
  225. color: #C8C9CC !important;
  226. }
  227. </style>
  228. <style lang="scss" scoped>
  229. .container {
  230. // min-height: 100vh;
  231. background: #f5f5f5;
  232. display: flex;
  233. flex-direction: column;
  234. }
  235. .navbar {
  236. display: flex;
  237. align-items: center;
  238. justify-content: space-between;
  239. padding: 20rpx 24rpx;
  240. background: #fff;
  241. border-bottom: 1rpx solid #f0f0f0;
  242. .nav-left {
  243. width: 60rpx;
  244. .back-icon {
  245. font-size: 36rpx;
  246. color: #333;
  247. font-weight: bold;
  248. }
  249. }
  250. .nav-title {
  251. flex: 1;
  252. text-align: center;
  253. font-size: 36rpx;
  254. font-weight: bold;
  255. color: #333;
  256. }
  257. .nav-right {
  258. display: flex;
  259. align-items: center;
  260. gap: 24rpx;
  261. width: 60rpx;
  262. justify-content: flex-end;
  263. .more-icon {
  264. font-size: 32rpx;
  265. color: #333;
  266. }
  267. .eye-icon {
  268. font-size: 32rpx;
  269. color: #333;
  270. }
  271. }
  272. }
  273. .content {
  274. // flex: 1;
  275. // padding-bottom: 140rpx;
  276. }
  277. .form-section {
  278. background: #fff;
  279. // margin: 24rpx;
  280. // border-radius: 16rpx;
  281. overflow: hidden;
  282. .form-item {
  283. display: flex;
  284. align-items: center;
  285. justify-content: space-between;
  286. padding: 28rpx 32rpx;
  287. // min-height: 100rpx
  288. .form-label {
  289. font-family: PingFang SC, PingFang SC;
  290. font-weight: 400;
  291. font-size: 28rpx;
  292. color: #666666;
  293. width: 150rpx;
  294. }
  295. .form-input {
  296. flex: 1;
  297. font-size: 28rpx;
  298. color: #333;
  299. text-align: right;
  300. margin-left: 24rpx;
  301. &.picker-input {
  302. display: flex;
  303. align-items: center;
  304. justify-content: flex-end;
  305. .arrow-right {
  306. font-size: 32rpx;
  307. color: #999;
  308. margin-left: 8rpx;
  309. }
  310. }
  311. }
  312. }
  313. // placeholder 样式(需要独立定义,不能嵌套)
  314. .placeholder {
  315. //color: #C8C9CC !important;
  316. }
  317. .divider {
  318. height: 1rpx;
  319. background: #EBEDF0;
  320. margin-left: 24rpx;
  321. }
  322. }
  323. .notes-section {
  324. // background: #fff;
  325. // border-radius: 16rpx;
  326. padding: 22rpx 32rpx;
  327. // margin: 0 24rpx 24rpx;
  328. .notes-title {
  329. font-family: PingFang SC, PingFang SC;
  330. font-weight: 400;
  331. font-size: 28rpx;
  332. color: #999999;
  333. line-height: 40rpx;
  334. }
  335. .notes-item {
  336. font-family: PingFang SC, PingFang SC;
  337. font-weight: 400;
  338. font-size: 28rpx;
  339. color: #999999;
  340. line-height: 40rpx;
  341. }
  342. }
  343. .bottom-bar {
  344. display: flex;
  345. align-items: center;
  346. flex-direction: column;
  347. padding: 32rpx;
  348. .save-btn {
  349. width: 100%;
  350. height: 88rpx;
  351. background: #388BFF;
  352. border-radius: 44rpx;
  353. display: flex;
  354. align-items: center;
  355. justify-content: center;
  356. font-size: 32rpx;
  357. color: #fff;
  358. }
  359. .unbind-btn{
  360. margin-top: 20rpx;
  361. width: 100%;
  362. height: 88rpx;
  363. color: #388BFF;
  364. display: flex;
  365. align-items: center;
  366. justify-content: center;
  367. font-size: 32rpx;
  368. }
  369. }
  370. </style>