add_retail.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 class="form-item">
  43. <text class="form-label">店铺照片</text>
  44. <uni-file-picker
  45. class="file-picker"
  46. ref="files"
  47. :del-icon="edit"
  48. file-mediatype="image"
  49. mode="grid"
  50. :limit="1"
  51. title="最多选择1张图片"
  52. v-model="formData.photos"
  53. />
  54. </view>
  55. </view>
  56. <view class="footer-save-button" v-if="edit">
  57. <button class="save-btn" @click="submitForm" >保 存</button>
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. import {addStore, getRetailDetail, updateStore, uploadImage} from "@/api/hexiao";
  63. export default {
  64. data() {
  65. return {
  66. edit:false,
  67. // 表单数据
  68. formData: {
  69. photos: [],
  70. storeId: 0,
  71. storeName: '',
  72. ownerName: '',
  73. contact: '',
  74. address: ''
  75. }
  76. };
  77. },
  78. onLoad(opt){
  79. this.edit = opt.edit == 1;
  80. if(opt.id){
  81. this.formData.storeId = opt.id;
  82. getRetailDetail(opt.id).then(res=>{
  83. let data = res.data;
  84. this.formData = {
  85. storeId: data.id,
  86. storeName: data.store_name,
  87. ownerName: data.contact_name,
  88. contact: data.contact_phone,
  89. address: data.address,
  90. photos: [{
  91. "url":data.store_photo,
  92. }]
  93. }
  94. })
  95. }
  96. },
  97. methods: {
  98. // 提交表单
  99. async submitForm() {
  100. if(this.$refs.files.files.length === 0){
  101. uni.showToast({ title: '请上传门店照片', icon: 'none' });
  102. return;
  103. }
  104. const imageUrlarr = await uploadImage(this.$refs.files.files);
  105. if(imageUrlarr.length === 0){
  106. uni.showToast({ title: '门店照片上传失败,请重试', icon: 'none' });
  107. return;
  108. }
  109. // --- 手动进行数据校验 ---
  110. if (!this.formData.storeName) {
  111. uni.showToast({ title: '请输入店铺名称', icon: 'none' });
  112. return;
  113. }
  114. if (!this.formData.ownerName) {
  115. uni.showToast({ title: '请输入店主名称', icon: 'none' });
  116. return;
  117. }
  118. if (!this.formData.contact) {
  119. uni.showToast({ title: '请输入联系方式', icon: 'none' });
  120. return;
  121. }
  122. // 简单的手机号格式校验
  123. if (!/^1[3-9]\d{9}$/.test(this.formData.contact)) {
  124. uni.showToast({ title: '请输入正确的手机号码', icon: 'none' });
  125. return;
  126. }
  127. if (!this.formData.address) {
  128. uni.showToast({ title: '请输入店铺地址', icon: 'none' });
  129. return;
  130. }
  131. console.log('校验通过,准备提交的数据:', this.formData);
  132. // 执行提交逻辑
  133. uni.showLoading({ title: '正在保存...' });
  134. if(this.formData.storeId>0){
  135. updateStore(this.formData.storeId,this.formData.storeName,this.formData.ownerName,this.formData.contact,this.formData.address,imageUrlarr[0])
  136. .then(res=>{
  137. uni.hideLoading();
  138. if(res.code == 0){
  139. uni.showToast({
  140. title: '保存成功',
  141. icon: 'success'
  142. });
  143. uni.navigateBack();
  144. }else{
  145. uni.showToast({
  146. title: res.msg,
  147. icon: 'none'
  148. });
  149. }
  150. })
  151. }else{
  152. addStore(this.formData.storeName,this.formData.ownerName,this.formData.contact,this.formData.address,imageUrlarr[0])
  153. .then(res=>{
  154. uni.hideLoading();
  155. if(res.code == 0){
  156. uni.showToast({
  157. title: '保存成功',
  158. icon: 'success'
  159. });
  160. uni.navigateBack();
  161. }else{
  162. uni.showToast({
  163. title: res.msg,
  164. icon: 'none'
  165. });
  166. }
  167. })
  168. }
  169. }
  170. }
  171. }
  172. </script>
  173. <style lang="scss" scoped>
  174. .page-container {
  175. min-height: 100vh;
  176. background: linear-gradient(to bottom, #e4efff, #f5f6fa 40%);
  177. padding: 30rpx;
  178. box-sizing: border-box;
  179. }
  180. .form-card {
  181. background-color: #ffffff;
  182. border-radius: 20rpx;
  183. padding: 0 40rpx; // 左右内边距
  184. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
  185. }
  186. .form-item {
  187. padding: 30rpx 0;
  188. border-bottom: 1rpx solid #f0f0f0;
  189. // 最后一个item不需要下边框
  190. &:last-child {
  191. border-bottom: none;
  192. }
  193. }
  194. .form-label {
  195. display: block; // 确保独占一行
  196. font-size: 30rpx;
  197. color: #333;
  198. font-weight: 500;
  199. }
  200. .form-input {
  201. margin-top: 20rpx; // 与label的间距
  202. font-size: 28rpx;
  203. color: #333;
  204. }
  205. .placeholder {
  206. color: #c0c4cc;
  207. }
  208. .footer-save-button {
  209. position: fixed;
  210. left: 0;
  211. bottom: 0;
  212. width: 100%;
  213. padding: 20rpx 30rpx;
  214. background-color: #f5f6fa;
  215. box-sizing: border-box;
  216. padding-bottom: calc(20rpx + constant(safe-area-inset-bottom));
  217. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  218. }
  219. .save-btn {
  220. background-color: #409eff;
  221. color: #ffffff;
  222. border-radius: 50rpx;
  223. font-size: 32rpx;
  224. height: 90rpx;
  225. line-height: 90rpx;
  226. &::after {
  227. border: none;
  228. }
  229. }
  230. </style>