add_order.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <template>
  2. <view class="page-container">
  3. <view class="card-item">
  4. <view class="card-header">
  5. <view class="card-title">收货信息</view>
  6. </view>
  7. <view class="card-body form-body">
  8. <view class="form-item">
  9. <text class="form-label">收货人</text>
  10. <input
  11. class="form-input"
  12. v-model="shippingAddress.name"
  13. placeholder="请输入收货人姓名"
  14. placeholder-class="placeholder"
  15. />
  16. </view>
  17. <view class="form-item">
  18. <text class="form-label">联系方式</text>
  19. <input
  20. class="form-input"
  21. type="number"
  22. maxlength="11"
  23. v-model="shippingAddress.phone"
  24. placeholder="请输入手机号码"
  25. placeholder-class="placeholder"
  26. />
  27. </view>
  28. <view class="form-item no-border">
  29. <text class="form-label">收货地址</text>
  30. <input
  31. class="form-input"
  32. v-model="shippingAddress.fullAddress"
  33. placeholder="请输入详细收货地址"
  34. placeholder-class="placeholder"
  35. />
  36. </view>
  37. </view>
  38. </view>
  39. <view class="card-item">
  40. <view class="card-header">
  41. <view class="card-title">商品信息</view>
  42. </view>
  43. <view class="card-body product-list">
  44. <view class="product-item" v-for="(item, index) in productList" :key="item.id">
  45. <view class="product-info">
  46. <text class="product-index">{{ index + 1 }}.</text>
  47. <text class="product-name">{{ item.name_ }}</text>
  48. </view>
  49. <uni-number-box max="500" v-model="item.count"></uni-number-box>
  50. </view>
  51. </view>
  52. </view>
  53. <view class="card-item">
  54. <view class="card-header">
  55. <view class="card-title">订单信息</view>
  56. </view>
  57. <view class="card-body">
  58. <view class="remark-label">订单备注</view>
  59. <textarea
  60. class="remark-textarea"
  61. v-model="remarks"
  62. placeholder="请输入备注信息"
  63. placeholder-class="placeholder"
  64. />
  65. </view>
  66. </view>
  67. <view class="footer-submit">
  68. <button class="submit-btn" @click="submitOrder">提交订单</button>
  69. </view>
  70. </view>
  71. </template>
  72. <script>
  73. import {getProductList,addDealerOrder,getOrderDetail,editDealerOrder} from "@/api/hexiao";
  74. export default {
  75. data() {
  76. return {
  77. id:0,
  78. // 收货地址信息,初始为空
  79. shippingAddress: {
  80. name: '',
  81. phone: '',
  82. fullAddress: ''
  83. },
  84. // 商品列表
  85. productList: [
  86. ],
  87. // 订单备注
  88. remarks: '',
  89. isLoading:false
  90. };
  91. },
  92. onLoad(opt){
  93. getProductList().then(res=>{
  94. this.productList = res.data;
  95. })
  96. this.id = opt.id;
  97. if(this.id>0){
  98. getOrderDetail(this.id).then(res=>{
  99. let data = res.data;
  100. this.shippingAddress.name = data.delivery.receiverName;
  101. this.shippingAddress.phone = data.delivery.receiverPhone;
  102. this.shippingAddress.fullAddress = data.delivery.address;
  103. let items = data.items;
  104. for (let i = 0; i < items.length; i++) {
  105. let item = items[i];
  106. let product = this.productList.find(p => p.type_ === item.productId);
  107. if (product) {
  108. product.count = item.quantity;
  109. }
  110. }
  111. this.remarks = data.remark;
  112. })
  113. }
  114. },
  115. methods: {
  116. // 提交订单
  117. submitOrder() {
  118. // 1. 数据校验 (此部分保持不变)
  119. if (!this.shippingAddress.name) {
  120. return uni.showToast({ title: '请输入收货人', icon: 'none' });
  121. }
  122. if (!/^1[3-9]\d{9}$/.test(this.shippingAddress.phone)) {
  123. return uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
  124. }
  125. if (!this.shippingAddress.fullAddress) {
  126. return uni.showToast({ title: '请输入收货地址', icon: 'none' });
  127. }
  128. const items = [];
  129. for (let i = 0; i < this.productList.length; i++) {
  130. let item = this.productList[i];
  131. if(!item.count){
  132. continue;
  133. }
  134. items.push({
  135. productId: item.type_, // 键名从 id 改为 productId
  136. quantity: item.count
  137. })
  138. }
  139. if(items.length === 0){
  140. return uni.showToast({ title: '请至少选择一件商品', icon: 'none' });
  141. }
  142. // 2. 【核心改动】按照您指定的格式组合提交数据
  143. const finalData = {
  144. // 备注信息,键名为 remark
  145. remark: this.remarks,
  146. // 收货信息,键名为 delivery,是一个对象
  147. delivery: {
  148. receiverName: this.shippingAddress.name,
  149. receiverPhone: this.shippingAddress.phone,
  150. address: this.shippingAddress.fullAddress
  151. },
  152. // 商品列表,键名为 items,是一个数组
  153. items: items
  154. };
  155. uni.showLoading({ title: '正在提交...' });
  156. let self = this;
  157. if(self.isLoading){
  158. uni.showToast({ title: '请勿重复提交', icon: 'none' });
  159. return
  160. }
  161. self.isLoading = true;
  162. if(this.id>0){
  163. finalData.orderId = this.id;
  164. editDealerOrder(finalData).then(res=>{
  165. if(res.data){
  166. uni.showToast({ title: '订单提交成功', icon: 'success' });
  167. }
  168. uni.hideLoading();
  169. setTimeout(() =>{
  170. self.isLoading = false;
  171. uni.reLaunch({url:"/pages/hexiao/jxs/order"});
  172. }, 1500);
  173. })
  174. }else{
  175. addDealerOrder(finalData).then(res=>{
  176. if(res.data){
  177. uni.showToast({ title: '订单提交成功', icon: 'success' });
  178. }
  179. uni.hideLoading();
  180. setTimeout(() =>{
  181. self.isLoading = false;
  182. uni.reLaunch({url:"/pages/hexiao/jxs/order"});
  183. }, 1500);
  184. })
  185. }
  186. }
  187. }
  188. }
  189. </script>
  190. <style lang="scss" scoped>
  191. .page-container {
  192. min-height: 100vh;
  193. background: linear-gradient(to bottom, #e4efff, #f5f6fa 60%);
  194. padding: 30rpx;
  195. box-sizing: border-box;
  196. padding-bottom: 180rpx;
  197. }
  198. .card-item {
  199. background-color: #ffffff;
  200. border-radius: 20rpx;
  201. padding: 30rpx;
  202. margin-bottom: 30rpx;
  203. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
  204. position: relative;
  205. &::before {
  206. content: '';
  207. position: absolute;
  208. left: 0;
  209. top: 35rpx;
  210. width: 8rpx;
  211. height: 30rpx;
  212. background-color: #3c82f8;
  213. border-radius: 0 4rpx 4rpx 0;
  214. }
  215. }
  216. .card-header {
  217. display: flex;
  218. justify-content: space-between;
  219. align-items: center;
  220. padding-bottom: 10rpx; // 缩小header和body间距
  221. .card-title {
  222. font-size: 32rpx;
  223. font-weight: bold;
  224. }
  225. }
  226. .card-body {
  227. padding-top: 10rpx;
  228. &.form-body {
  229. padding-top: 0;
  230. }
  231. }
  232. // 新增:表单项样式
  233. .form-item {
  234. display: flex;
  235. flex-direction: column;
  236. padding: 20rpx 0;
  237. border-bottom: 1rpx solid #f5f5f5;
  238. &.no-border {
  239. border-bottom: none;
  240. }
  241. .form-label {
  242. font-size: 28rpx;
  243. color: #666;
  244. }
  245. .form-input {
  246. font-size: 30rpx;
  247. color: #333;
  248. padding-top: 15rpx;
  249. }
  250. .placeholder {
  251. color: #c0c4cc;
  252. }
  253. }
  254. .product-list {
  255. border-top: 1rpx solid #f5f5f5;
  256. .product-item {
  257. display: flex;
  258. justify-content: space-between;
  259. align-items: center;
  260. padding: 20rpx 0;
  261. .product-info {
  262. display: flex;
  263. align-items: center;
  264. .product-index {
  265. font-size: 28rpx;
  266. color: #999;
  267. margin-right: 15rpx;
  268. }
  269. .product-name {
  270. font-size: 30rpx;
  271. font-weight: 500;
  272. }
  273. }
  274. }
  275. }
  276. .remark-label {
  277. font-size: 28rpx;
  278. color: #666;
  279. }
  280. .remark-textarea {
  281. margin-top: 20rpx;
  282. width: 100%;
  283. font-size: 30rpx;
  284. background-color: #f5f6fa;
  285. padding: 20rpx;
  286. box-sizing: border-box;
  287. border-radius: 10rpx;
  288. height: 150rpx;
  289. }
  290. .footer-submit {
  291. position: fixed;
  292. left: 0;
  293. bottom: 0;
  294. width: 100%;
  295. padding: 20rpx 30rpx;
  296. background-color: #f5f6fa;
  297. box-sizing: border-box;
  298. padding-bottom: calc(20rpx + constant(safe-area-inset-bottom));
  299. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  300. z-index: 100;
  301. }
  302. .submit-btn {
  303. background-color: #3c82f8;
  304. color: #ffffff;
  305. border-radius: 50rpx;
  306. font-size: 32rpx;
  307. height: 90rpx;
  308. line-height: 90rpx;
  309. &::after { border: none; }
  310. }
  311. </style>