editBankCard.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. <picker mode="selector" :range="bankList" range-key="name" @change="onBankChange">
  9. <view class="form-input picker-input" :class="{ placeholder: !formData.bankName }">
  10. {{ formData.bankName || '请选择开户行' }}
  11. <image class="w36 h36" src="@/static/image/icon_my_more.png" mode=""></image>
  12. </view>
  13. </picker>
  14. </view>
  15. <view class="divider"></view>
  16. <view class="form-item">
  17. <view class="form-label">支行</view>
  18. <picker mode="selector" :range="branchList" range-key="name" @change="onBranchChange">
  19. <view class="form-input picker-input" :class="{ placeholder: !formData.bankBranch }">
  20. {{ formData.bankBranch || '请选择支行' }}
  21. <image class="w36 h36" src="@/static/image/icon_my_more.png" mode=""></image>
  22. </view>
  23. </picker>
  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. v-model="formData.bankCardNo"
  31. placeholder="请输入银行卡号"
  32. type="number"
  33. maxlength="19"
  34. placeholder-class="text-placeholder"
  35. />
  36. </view>
  37. </view>
  38. <!-- 注意事项 -->
  39. <view class="notes-section">
  40. <view class="notes-title">注意:</view>
  41. <view class="notes-item">1、只能绑定认证用户本人的银行卡;</view>
  42. <view class="notes-item">2、每次更换次数不得超过三次。</view>
  43. </view>
  44. </scroll-view>
  45. <!-- 底部按钮 -->
  46. <view class="bottom-bar">
  47. <view class="save-btn" @click="handleSave">保存</view>
  48. <!-- <view class="unbind-btn" @click="handleUnbind">解除绑定</view> -->
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. import { addBankCard, deleteBankCard, getBankCardInfo, getBankList, getBranchList } from '@/api/bankCard'
  54. export default {
  55. data() {
  56. return {
  57. cardId: '',
  58. formData: {
  59. "bankBranch": "",
  60. "bankCardNo": "",
  61. "bankName": ""
  62. },
  63. bankList: [
  64. { name: '中国工商银行', id: 1 },
  65. { name: '中国建设银行', id: 2 },
  66. { name: '中国银行', id: 3 },
  67. { name: '中国农业银行', id: 4 }
  68. ],
  69. branchList: [{ name: '北京支行', id: 1 },
  70. { name: '上海支行', id: 2 },
  71. { name: '广州支行', id: 3 },
  72. { name: '重庆支行', id: 4 }
  73. ]
  74. }
  75. },
  76. onLoad(options) {
  77. // if (options.id) {
  78. // this.cardId = options.id
  79. // }
  80. this.loadBankCardDetail()
  81. //this.loadBankList()
  82. },
  83. methods: {
  84. async loadBankCardDetail() {
  85. try {
  86. uni.showLoading({ title: '加载中...' })
  87. const res = await getBankCardInfo({ id: this.cardId })
  88. uni.hideLoading()
  89. if (res.code === 200 && res.data) {
  90. // 根据接口返回的数据结构映射字段
  91. const data = res.data
  92. this.formData = {
  93. ...this.formData,
  94. bankBranch: data.bankBranch,
  95. bankCardNo: data.bankCardNo,
  96. bankName: data.bankName
  97. }
  98. // 加载支行列表(如果存在银行ID)
  99. // if (this.formData.bankId) {
  100. // this.loadBranchList(this.formData.bankId)
  101. // }
  102. }
  103. } catch (e) {
  104. uni.hideLoading()
  105. console.error('加载银行卡详情失败', e)
  106. }
  107. },
  108. async loadBankList() {
  109. try {
  110. const res = await getBankList()
  111. if (res.code === 200 && res.data) {
  112. this.bankList = res.data
  113. } else {
  114. this.bankList = [
  115. { name: '中国工商银行', id: 1 },
  116. { name: '中国建设银行', id: 2 },
  117. { name: '中国银行', id: 3 },
  118. { name: '中国农业银行', id: 4 }
  119. ]
  120. }
  121. } catch (e) {
  122. this.bankList = [
  123. { name: '中国工商银行', id: 1 },
  124. { name: '中国建设银行', id: 2 },
  125. { name: '中国银行', id: 3 },
  126. { name: '中国农业银行', id: 4 }
  127. ]
  128. }
  129. },
  130. async loadBranchList(bankId) {
  131. try {
  132. const res = await getBranchList({ bankId })
  133. if (res.code === 200 && res.data) {
  134. this.branchList = res.data
  135. } else {
  136. this.branchList = [
  137. { name: '北京支行', id: 1 },
  138. { name: '上海支行', id: 2 },
  139. { name: '广州支行', id: 3 }
  140. ]
  141. }
  142. } catch (e) {
  143. this.branchList = [
  144. { name: '北京支行', id: 1 },
  145. { name: '上海支行', id: 2 },
  146. { name: '广州支行', id: 3 }
  147. ]
  148. }
  149. },
  150. onBankChange(e) {
  151. const index = e.detail.value
  152. this.formData.bankName = this.bankList[index].name
  153. //this.formData.bankCardNo = this.bankList[index].id
  154. this.formData.bankBranch = '' // 清空支行选择
  155. // if (this.formData.bankCardNo) {
  156. // this.loadBranchList(this.formData.bankCardNo)
  157. // }
  158. },
  159. onBranchChange(e) {
  160. const index = e.detail.value
  161. this.formData.bankBranch = this.branchList[index].name
  162. //this.formData.bankCardNo = this.branchList[index].id
  163. },
  164. goBack() {
  165. uni.navigateBack()
  166. },
  167. async handleSave() {
  168. // 表单验证
  169. if (!this.formData.bankName) {
  170. uni.showToast({
  171. icon: 'none',
  172. title: '请选择开户行'
  173. })
  174. return
  175. }
  176. if (!this.formData.bankBranch) {
  177. uni.showToast({
  178. icon: 'none',
  179. title: '请选择支行'
  180. })
  181. return
  182. }
  183. if (!this.formData.bankCardNo) {
  184. uni.showToast({
  185. icon: 'none',
  186. title: '请输入银行卡号'
  187. })
  188. return
  189. }
  190. // if (this.formData.bankCardNo.length < 16) {
  191. // uni.showToast({
  192. // icon: 'none',
  193. // title: '请输入正确的银行卡号'
  194. // })
  195. // return
  196. // }
  197. try {
  198. uni.showLoading({ title: '保存中...' })
  199. // 构建提交数据,按照接口字段要求
  200. const submitData = {
  201. bankName: this.formData.bank || this.formData.bankName,
  202. bankBranch: this.formData.branch || this.formData.bankBranch,
  203. bankCardNo: this.formData.cardNumber || this.formData.bankCardNo
  204. }
  205. // 如果有ID(编辑时),添加id字段
  206. if (this.cardId || this.formData.id) {
  207. submitData.id = this.cardId || this.formData.id
  208. }
  209. // 如果有bankId和branchId,也一并提交
  210. if (this.formData.bankId) {
  211. submitData.bankId = this.formData.bankId
  212. }
  213. if (this.formData.branchId) {
  214. submitData.branchId = this.formData.branchId
  215. }
  216. const res = await addBankCard(submitData)
  217. uni.hideLoading()
  218. if (res.code === 200) {
  219. uni.showToast({
  220. icon: 'success',
  221. title: '保存成功'
  222. })
  223. setTimeout(() => {
  224. uni.navigateBack()
  225. }, 1500)
  226. } else {
  227. uni.showToast({
  228. icon: 'none',
  229. title: res.msg || '保存失败'
  230. })
  231. }
  232. } catch (e) {
  233. uni.hideLoading()
  234. console.error('保存银行卡失败', e)
  235. uni.showToast({
  236. icon: 'none',
  237. title: e.message || '保存失败'
  238. })
  239. }
  240. },
  241. handleUnbind() {
  242. uni.showModal({
  243. title: '提示',
  244. content: '确定要解除绑定该银行卡吗?',
  245. success: async (res) => {
  246. if (res.confirm) {
  247. try {
  248. uni.showLoading({ title: '解除绑定中...' })
  249. const result = await deleteBankCard({ id: this.cardId })
  250. uni.hideLoading()
  251. if (result.code === 200) {
  252. uni.showToast({
  253. icon: 'success',
  254. title: '解除绑定成功'
  255. })
  256. setTimeout(() => {
  257. uni.navigateBack()
  258. }, 1500)
  259. } else {
  260. uni.showToast({
  261. icon: 'none',
  262. title: result.msg || '解除绑定失败'
  263. })
  264. }
  265. } catch (e) {
  266. uni.hideLoading()
  267. uni.showToast({
  268. icon: 'none',
  269. title: '解除绑定失败'
  270. })
  271. }
  272. }
  273. }
  274. })
  275. }
  276. }
  277. }
  278. </script>
  279. <style lang="stylus">
  280. .text-placeholder{
  281. color: #C8C9CC !important;
  282. }
  283. </style>
  284. <style lang="scss" scoped>
  285. .container {
  286. // min-height: 100vh;
  287. background: #f5f5f5;
  288. display: flex;
  289. flex-direction: column;
  290. }
  291. .navbar {
  292. display: flex;
  293. align-items: center;
  294. justify-content: space-between;
  295. padding: 20rpx 24rpx;
  296. background: #fff;
  297. border-bottom: 1rpx solid #f0f0f0;
  298. .nav-left {
  299. width: 60rpx;
  300. .back-icon {
  301. font-size: 36rpx;
  302. color: #333;
  303. font-weight: bold;
  304. }
  305. }
  306. .nav-title {
  307. flex: 1;
  308. text-align: center;
  309. font-size: 36rpx;
  310. font-weight: bold;
  311. color: #333;
  312. }
  313. .nav-right {
  314. display: flex;
  315. align-items: center;
  316. gap: 24rpx;
  317. width: 60rpx;
  318. justify-content: flex-end;
  319. .more-icon {
  320. font-size: 32rpx;
  321. color: #333;
  322. }
  323. .eye-icon {
  324. font-size: 32rpx;
  325. color: #333;
  326. }
  327. }
  328. }
  329. .content {
  330. // flex: 1;
  331. // padding-bottom: 140rpx;
  332. }
  333. .form-section {
  334. background: #fff;
  335. // margin: 24rpx;
  336. // border-radius: 16rpx;
  337. overflow: hidden;
  338. .form-item {
  339. display: flex;
  340. align-items: center;
  341. justify-content: space-between;
  342. padding: 28rpx 32rpx;
  343. // min-height: 100rpx
  344. .form-label {
  345. font-family: PingFang SC, PingFang SC;
  346. font-weight: 400;
  347. font-size: 28rpx;
  348. color: #666666;
  349. width: 150rpx;
  350. }
  351. .form-input {
  352. flex: 1;
  353. font-size: 28rpx;
  354. color: #333;
  355. text-align: right;
  356. margin-left: 24rpx;
  357. &.picker-input {
  358. display: flex;
  359. align-items: center;
  360. justify-content: flex-end;
  361. .arrow-right {
  362. font-size: 32rpx;
  363. color: #999;
  364. margin-left: 8rpx;
  365. }
  366. }
  367. }
  368. }
  369. // placeholder 样式(需要独立定义,不能嵌套)
  370. .placeholder {
  371. //color: #C8C9CC !important;
  372. }
  373. .divider {
  374. height: 1rpx;
  375. background: #EBEDF0;
  376. margin-left: 24rpx;
  377. }
  378. }
  379. .notes-section {
  380. // background: #fff;
  381. // border-radius: 16rpx;
  382. padding: 22rpx 32rpx;
  383. // margin: 0 24rpx 24rpx;
  384. .notes-title {
  385. font-family: PingFang SC, PingFang SC;
  386. font-weight: 400;
  387. font-size: 28rpx;
  388. color: #999999;
  389. line-height: 40rpx;
  390. }
  391. .notes-item {
  392. font-family: PingFang SC, PingFang SC;
  393. font-weight: 400;
  394. font-size: 28rpx;
  395. color: #999999;
  396. line-height: 40rpx;
  397. }
  398. }
  399. .bottom-bar {
  400. display: flex;
  401. align-items: center;
  402. flex-direction: column;
  403. padding: 32rpx;
  404. .save-btn {
  405. width: 100%;
  406. height: 88rpx;
  407. background: #388BFF;
  408. border-radius: 44rpx;
  409. display: flex;
  410. align-items: center;
  411. justify-content: center;
  412. font-size: 32rpx;
  413. color: #fff;
  414. }
  415. .unbind-btn{
  416. margin-top: 20rpx;
  417. width: 100%;
  418. height: 88rpx;
  419. color: #388BFF;
  420. display: flex;
  421. align-items: center;
  422. justify-content: center;
  423. font-size: 32rpx;
  424. }
  425. }
  426. </style>