add_retail.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <view class="page-container">
  3. <view class="form-card">
  4. <view class="form-item">
  5. <text class="form-label">店铺名称</text>
  6. <input
  7. class="form-input"
  8. v-model="formData.storeName"
  9. placeholder="请输入店铺名称"
  10. placeholder-class="placeholder"
  11. />
  12. </view>
  13. <view class="form-item">
  14. <text class="form-label">店主名称</text>
  15. <input
  16. class="form-input"
  17. v-model="formData.ownerName"
  18. placeholder="请输入店主名称"
  19. placeholder-class="placeholder"
  20. />
  21. </view>
  22. <view class="form-item">
  23. <text class="form-label">联系方式</text>
  24. <input
  25. class="form-input"
  26. type="number"
  27. maxlength="11"
  28. v-model="formData.contact"
  29. placeholder="请输入联系方式"
  30. placeholder-class="placeholder"
  31. />
  32. </view>
  33. <view class="form-item">
  34. <text class="form-label">店铺地址</text>
  35. <input
  36. class="form-input"
  37. v-model="formData.address"
  38. placeholder="请输入店铺地址"
  39. placeholder-class="placeholder"
  40. />
  41. </view>
  42. </view>
  43. <view class="footer-save-button" v-if="edit">
  44. <button class="save-btn" @click="submitForm" >保 存</button>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import {addStore, getRetailDetail, updateStore} from "../../../../api/hexiao";
  50. export default {
  51. data() {
  52. return {
  53. edit:false,
  54. // 表单数据
  55. formData: {
  56. storeId: 0,
  57. storeName: '',
  58. ownerName: '',
  59. contact: '',
  60. address: ''
  61. }
  62. };
  63. },
  64. onLoad(opt){
  65. this.edit = opt.edit == 1;
  66. if(opt.id){
  67. this.formData.storeId = opt.id;
  68. getRetailDetail(opt.id).then(res=>{
  69. let data = res.data;
  70. this.formData = {
  71. storeId: data.id,
  72. storeName: data.store_name,
  73. ownerName: data.contact_name,
  74. contact: data.contact_phone,
  75. address: data.address
  76. }
  77. })
  78. }
  79. },
  80. methods: {
  81. // 提交表单
  82. submitForm() {
  83. // --- 手动进行数据校验 ---
  84. if (!this.formData.storeName) {
  85. uni.showToast({ title: '请输入店铺名称', icon: 'none' });
  86. return;
  87. }
  88. if (!this.formData.ownerName) {
  89. uni.showToast({ title: '请输入店主名称', icon: 'none' });
  90. return;
  91. }
  92. if (!this.formData.contact) {
  93. uni.showToast({ title: '请输入联系方式', icon: 'none' });
  94. return;
  95. }
  96. // 简单的手机号格式校验
  97. if (!/^1[3-9]\d{9}$/.test(this.formData.contact)) {
  98. uni.showToast({ title: '请输入正确的手机号码', icon: 'none' });
  99. return;
  100. }
  101. if (!this.formData.address) {
  102. uni.showToast({ title: '请输入店铺地址', icon: 'none' });
  103. return;
  104. }
  105. console.log('校验通过,准备提交的数据:', this.formData);
  106. // 执行提交逻辑
  107. uni.showLoading({ title: '正在保存...' });
  108. if(this.formData.storeId>0){
  109. updateStore(this.formData.storeId,this.formData.storeName,this.formData.ownerName,this.formData.contact,this.formData.address)
  110. .then(res=>{
  111. uni.hideLoading();
  112. if(res.code == 0){
  113. uni.showToast({
  114. title: '保存成功',
  115. icon: 'success'
  116. });
  117. uni.navigateBack();
  118. }else{
  119. uni.showToast({
  120. title: res.msg,
  121. icon: 'none'
  122. });
  123. }
  124. })
  125. }else{
  126. addStore(this.formData.storeName,this.formData.ownerName,this.formData.contact,this.formData.address)
  127. .then(res=>{
  128. uni.hideLoading();
  129. if(res.code == 0){
  130. uni.showToast({
  131. title: '保存成功',
  132. icon: 'success'
  133. });
  134. uni.navigateBack();
  135. }else{
  136. uni.showToast({
  137. title: res.msg,
  138. icon: 'none'
  139. });
  140. }
  141. })
  142. }
  143. }
  144. }
  145. }
  146. </script>
  147. <style lang="scss" scoped>
  148. .page-container {
  149. min-height: 100vh;
  150. background: linear-gradient(to bottom, #e4efff, #f5f6fa 40%);
  151. padding: 30rpx;
  152. box-sizing: border-box;
  153. }
  154. .form-card {
  155. background-color: #ffffff;
  156. border-radius: 20rpx;
  157. padding: 0 40rpx; // 左右内边距
  158. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
  159. }
  160. .form-item {
  161. padding: 30rpx 0;
  162. border-bottom: 1rpx solid #f0f0f0;
  163. // 最后一个item不需要下边框
  164. &:last-child {
  165. border-bottom: none;
  166. }
  167. }
  168. .form-label {
  169. display: block; // 确保独占一行
  170. font-size: 30rpx;
  171. color: #333;
  172. font-weight: 500;
  173. }
  174. .form-input {
  175. margin-top: 20rpx; // 与label的间距
  176. font-size: 28rpx;
  177. color: #333;
  178. }
  179. .placeholder {
  180. color: #c0c4cc;
  181. }
  182. .footer-save-button {
  183. position: fixed;
  184. left: 0;
  185. bottom: 0;
  186. width: 100%;
  187. padding: 20rpx 30rpx;
  188. background-color: #f5f6fa;
  189. box-sizing: border-box;
  190. padding-bottom: calc(20rpx + constant(safe-area-inset-bottom));
  191. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  192. }
  193. .save-btn {
  194. background-color: #409eff;
  195. color: #ffffff;
  196. border-radius: 50rpx;
  197. font-size: 32rpx;
  198. height: 90rpx;
  199. line-height: 90rpx;
  200. &::after {
  201. border: none;
  202. }
  203. }
  204. </style>