preOrder.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <view style="padding: 20rpx;">
  3. <view class="box" style="margin-bottom: 10rpx;" @click="selectAddress('取货')">
  4. <uni-section title="取货地址" type="line"></uni-section>
  5. <view v-if="pickAddress.id">
  6. <view style="font-weight: bold; font-size: 32rpx; margin-bottom: 10rpx;">{{ pickAddress.address + pickAddress.doorNo }}</view>
  7. <view style="color: #888; margin-bottom: 10rpx;">
  8. <text style="margin-right: 20rpx;">{{ pickAddress.userName }}</text>
  9. <text>{{ pickAddress.phone }}</text>
  10. </view>
  11. </view>
  12. <view style="color: #888;" v-else>请选择取货地址</view>
  13. </view>
  14. <view class="box" style="margin-bottom: 10rpx;" @click="selectAddress('收货')">
  15. <uni-section title="收货地址" type="line"></uni-section>
  16. <view v-if="recieveAddress.id">
  17. <view style="font-weight: bold; font-size: 32rpx; margin-bottom: 10rpx;">{{ recieveAddress.address + recieveAddress.doorNo }}</view>
  18. <view style="color: #888; margin-bottom: 10rpx;">
  19. <text style="margin-right: 20rpx;">{{ recieveAddress.userName }}</text>
  20. <text>{{ recieveAddress.phone }}</text>
  21. </view>
  22. </view>
  23. <view style="color: #888;" v-else>请选择收货地址</view>
  24. </view>
  25. <view class="box">
  26. <uni-forms :modelValue="form" :rules="rules" ref="formRef" label-width="160rpx" label-align="right" validateTrigger="blur">
  27. <uni-forms-item label="物品名称" name="name" required>
  28. <uni-easyinput type="text" v-model="form.name" placeholder="请输入" />
  29. </uni-forms-item>
  30. <uni-forms-item label="描述信息" name="descr">
  31. <uni-easyinput type="text" v-model="form.descr" placeholder="请输入描述信息" />
  32. </uni-forms-item>
  33. <uni-forms-item label="重量(kg)" name="weight">
  34. <uni-easyinput type="text" v-model="form.weight" placeholder="请输入物品重量" />
  35. </uni-forms-item>
  36. <uni-forms-item label="小费(元)" name="price" required>
  37. <uni-number-box v-model="form.price" :min="1" style="position: relative; top: 10rpx;"></uni-number-box>
  38. </uni-forms-item>
  39. <uni-forms-item label="订单类型" name="type" required>
  40. <uni-data-select v-model="form.type" :localdata="orderTypes"></uni-data-select>
  41. </uni-forms-item>
  42. <uni-forms-item label="物品图片" name="img">
  43. <uni-file-picker limit="1" :image-styles="imageStyles" :del-icon="false" :disable-preview="true"
  44. fileMediatype="image" v-model="imgs" @select="handleImgUploadSuccess"></uni-file-picker>
  45. </uni-forms-item>
  46. <uni-forms-item label="备注" name="comment">
  47. <uni-easyinput type="text" v-model="form.comment" placeholder="请输入备注信息" />
  48. </uni-forms-item>
  49. </uni-forms>
  50. <view>
  51. <button type="primary" @click="addOrder" class="my-button-primary">提交订单</button>
  52. </view>
  53. </view>
  54. </view>
  55. </template>
  56. <script>
  57. export default {
  58. data() {
  59. return {
  60. form: { price: 1, type: '' }, // 表单绑定的对象
  61. rules: {
  62. name: {
  63. rules: [{
  64. required: true,
  65. errorMessage: '请填写物品名称',
  66. }]
  67. },
  68. price: {
  69. rules: [{
  70. required: true,
  71. errorMessage: '请设置小费',
  72. }]
  73. },
  74. type: {
  75. rules: [{
  76. required: true,
  77. errorMessage: '请设置订单类型',
  78. }]
  79. }
  80. },
  81. imgs: [],
  82. imageStyles: {
  83. "height": 80, // 边框高度
  84. "width": 80, // 边框宽度
  85. "border":{ // 如果为 Boolean 值,可以控制边框显示与否
  86. "color":"#eee", // 边框颜色
  87. "width":"1px", // 边框宽度
  88. "style":"solid", // 边框样式
  89. "radius":"50%" // 边框圆角,支持百分比
  90. }
  91. },
  92. orderTypes: [
  93. { value: '代拿快递', text: "代拿快递" },
  94. { value: '代取餐品', text: "代取餐品" },
  95. { value: '代买零食', text: "代买零食" },
  96. { value: '代送鲜花', text: "代送鲜花" },
  97. ],
  98. pickAddress: {},
  99. recieveAddress: {}
  100. }
  101. },
  102. onShow() {
  103. let orderStore = uni.getStorageSync('orderStore')
  104. this.form.type = orderStore?.type
  105. this.pickAddress = orderStore?.pickAddress || {}
  106. this.recieveAddress = orderStore?.recieveAddress || {}
  107. },
  108. methods: {
  109. selectAddress(addressType) {
  110. uni.navigateTo({
  111. url: '/pages/address/address?addressType=' + addressType
  112. })
  113. },
  114. addOrder() {
  115. if (!this.pickAddress.id) {
  116. uni.showToast({
  117. icon: 'none',
  118. title: '请设置取货地址'
  119. })
  120. return
  121. }
  122. if (!this.recieveAddress.id) {
  123. uni.showToast({
  124. icon: 'none',
  125. title: '请设置收货地址'
  126. })
  127. return
  128. }
  129. if (this.pickAddress.id === this.recieveAddress.id) {
  130. uni.showToast({
  131. icon: 'none',
  132. title: '取货地址不能跟收货地址一致'
  133. })
  134. return
  135. }
  136. // 设置表单里的地址信息
  137. this.form.addressId = this.pickAddress.id
  138. this.form.targetId = this.recieveAddress.id
  139. this.$refs.formRef.validate().then(res => {
  140. this.$request.post('/orders/addOrder', this.form).then(res => {
  141. if (res.code === '200') {
  142. uni.showToast({
  143. icon: 'success',
  144. title: '下单成功'
  145. })
  146. // 清除缓存
  147. uni.removeStorageSync('orderStore')
  148. setTimeout(() => {
  149. uni.switchTab({
  150. url: '/pages/index/index'
  151. })
  152. }, 500)
  153. } else {
  154. uni.showToast({
  155. icon: 'none',
  156. title: res.msg
  157. })
  158. }
  159. })
  160. })
  161. .catch(error => {
  162. console.error(error)
  163. })
  164. },
  165. handleImgUploadSuccess(e) {
  166. let _this = this
  167. const filePath = e.tempFilePaths[0]
  168. uni.uploadFile({
  169. url: _this.$baseUrl +'/files/upload', //自己的后端接口(默认发送post请求) 注意 _this.$baseUrl需要在全局变量定义
  170. filePath: filePath,
  171. name:"file", //这里应为自己后端文件形参的名字
  172. success(res) {
  173. let url = JSON.parse(res.data || '{}').data // 获取返回的图像链接
  174. _this.form.img = url // 给表单图像属性赋值
  175. }
  176. })
  177. }
  178. }
  179. }
  180. </script>
  181. <style>
  182. </style>