add_retail.vue 7.6 KB

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