add_retail.vue 6.5 KB

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