add_order.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. };
  90. },
  91. onLoad(opt){
  92. getProductList().then(res=>{
  93. this.productList = res.data;
  94. })
  95. this.id = opt.id;
  96. if(this.id>0){
  97. getOrderDetail(this.id).then(res=>{
  98. let data = res.data;
  99. this.shippingAddress.name = data.delivery.receiverName;
  100. this.shippingAddress.phone = data.delivery.receiverPhone;
  101. this.shippingAddress.fullAddress = data.delivery.address;
  102. let items = data.items;
  103. for (let i = 0; i < items.length; i++) {
  104. let item = items[i];
  105. let product = this.productList.find(p => p.type_ === item.productId);
  106. if (product) {
  107. product.count = item.quantity;
  108. }
  109. }
  110. this.remarks = data.remark;
  111. })
  112. }
  113. },
  114. methods: {
  115. // 提交订单
  116. submitOrder() {
  117. // 1. 数据校验 (此部分保持不变)
  118. if (!this.shippingAddress.name) {
  119. return uni.showToast({ title: '请输入收货人', icon: 'none' });
  120. }
  121. if (!/^1[3-9]\d{9}$/.test(this.shippingAddress.phone)) {
  122. return uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
  123. }
  124. if (!this.shippingAddress.fullAddress) {
  125. return uni.showToast({ title: '请输入收货地址', icon: 'none' });
  126. }
  127. const items = [];
  128. for (let i = 0; i < this.productList.length; i++) {
  129. let item = this.productList[i];
  130. if(!item.count){
  131. continue;
  132. }
  133. items.push({
  134. productId: item.type_, // 键名从 id 改为 productId
  135. quantity: item.count
  136. })
  137. }
  138. if(items.length === 0){
  139. return uni.showToast({ title: '请至少选择一件商品', icon: 'none' });
  140. }
  141. // 2. 【核心改动】按照您指定的格式组合提交数据
  142. const finalData = {
  143. // 备注信息,键名为 remark
  144. remark: this.remarks,
  145. // 收货信息,键名为 delivery,是一个对象
  146. delivery: {
  147. receiverName: this.shippingAddress.name,
  148. receiverPhone: this.shippingAddress.phone,
  149. address: this.shippingAddress.fullAddress
  150. },
  151. // 商品列表,键名为 items,是一个数组
  152. items: items
  153. };
  154. uni.showLoading({ title: '正在提交...' });
  155. if(this.id>0){
  156. finalData.orderId = this.id;
  157. editDealerOrder(finalData).then(res=>{
  158. if(res.data){
  159. uni.showToast({ title: '订单提交成功', icon: 'success' });
  160. }
  161. uni.hideLoading();
  162. setTimeout(() => uni.reLaunch({url:"/pages/cjx/hexiao/jxs/order"}), 1500);
  163. })
  164. }else{
  165. addDealerOrder(finalData).then(res=>{
  166. if(res.data){
  167. uni.showToast({ title: '订单提交成功', icon: 'success' });
  168. }
  169. uni.hideLoading();
  170. setTimeout(() => uni.reLaunch({url:"/pages/cjx/hexiao/jxs/order"}), 1500);
  171. })
  172. }
  173. }
  174. }
  175. }
  176. </script>
  177. <style lang="scss" scoped>
  178. .page-container {
  179. min-height: 100vh;
  180. background: linear-gradient(to bottom, #e4efff, #f5f6fa 60%);
  181. padding: 30rpx;
  182. box-sizing: border-box;
  183. padding-bottom: 180rpx;
  184. }
  185. .card-item {
  186. background-color: #ffffff;
  187. border-radius: 20rpx;
  188. padding: 30rpx;
  189. margin-bottom: 30rpx;
  190. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
  191. position: relative;
  192. &::before {
  193. content: '';
  194. position: absolute;
  195. left: 0;
  196. top: 35rpx;
  197. width: 8rpx;
  198. height: 30rpx;
  199. background-color: #3c82f8;
  200. border-radius: 0 4rpx 4rpx 0;
  201. }
  202. }
  203. .card-header {
  204. display: flex;
  205. justify-content: space-between;
  206. align-items: center;
  207. padding-bottom: 10rpx; // 缩小header和body间距
  208. .card-title {
  209. font-size: 32rpx;
  210. font-weight: bold;
  211. }
  212. }
  213. .card-body {
  214. padding-top: 10rpx;
  215. &.form-body {
  216. padding-top: 0;
  217. }
  218. }
  219. // 新增:表单项样式
  220. .form-item {
  221. display: flex;
  222. flex-direction: column;
  223. padding: 20rpx 0;
  224. border-bottom: 1rpx solid #f5f5f5;
  225. &.no-border {
  226. border-bottom: none;
  227. }
  228. .form-label {
  229. font-size: 28rpx;
  230. color: #666;
  231. }
  232. .form-input {
  233. font-size: 30rpx;
  234. color: #333;
  235. padding-top: 15rpx;
  236. }
  237. .placeholder {
  238. color: #c0c4cc;
  239. }
  240. }
  241. .product-list {
  242. border-top: 1rpx solid #f5f5f5;
  243. .product-item {
  244. display: flex;
  245. justify-content: space-between;
  246. align-items: center;
  247. padding: 20rpx 0;
  248. .product-info {
  249. display: flex;
  250. align-items: center;
  251. .product-index {
  252. font-size: 28rpx;
  253. color: #999;
  254. margin-right: 15rpx;
  255. }
  256. .product-name {
  257. font-size: 30rpx;
  258. font-weight: 500;
  259. }
  260. }
  261. }
  262. }
  263. .remark-label {
  264. font-size: 28rpx;
  265. color: #666;
  266. }
  267. .remark-textarea {
  268. margin-top: 20rpx;
  269. width: 100%;
  270. font-size: 30rpx;
  271. background-color: #f5f6fa;
  272. padding: 20rpx;
  273. box-sizing: border-box;
  274. border-radius: 10rpx;
  275. height: 150rpx;
  276. }
  277. .footer-submit {
  278. position: fixed;
  279. left: 0;
  280. bottom: 0;
  281. width: 100%;
  282. padding: 20rpx 30rpx;
  283. background-color: #f5f6fa;
  284. box-sizing: border-box;
  285. padding-bottom: calc(20rpx + constant(safe-area-inset-bottom));
  286. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  287. z-index: 100;
  288. }
  289. .submit-btn {
  290. background-color: #3c82f8;
  291. color: #ffffff;
  292. border-radius: 50rpx;
  293. font-size: 32rpx;
  294. height: 90rpx;
  295. line-height: 90rpx;
  296. &::after { border: none; }
  297. }
  298. </style>