order.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <template>
  2. <view>
  3. <view class="address-box" @click="selectAddr">
  4. <view class="address-box-left">
  5. <view v-if="!address" class="address-box-unselect">选择收货地址</view>
  6. <view v-if="address" class="address-box-select">
  7. <view class="address-box-select-text1">
  8. {{address.address1}}{{address.address2}}{{address.room}}
  9. </view>
  10. <view class="address-box-select-text2">
  11. {{address.name}} {{address.phone}}
  12. </view>
  13. </view>
  14. </view>
  15. <view class="address-box-right">
  16. <image src="../../images/jiantouyou.png"></image>
  17. </view>
  18. </view>
  19. <view class="main">
  20. <view class="order-box-list">
  21. <view class="order-box-header">
  22. <text class="order-box-title">{{orgName}}</text>
  23. <text class="order-box-desc"></text>
  24. </view>
  25. <view v-for="(item,index) in goods" :key="index" class="order-box-body">
  26. <view class="order-box-body-left">
  27. <image v-if="item.imageLoad" :src="item.img" @error="onErrorImg(item)" @load="onSuccessImg(item)" mode="aspectFill"></image>
  28. <image v-if="!item.imageLoad" src="../../images/errorbg.png" mode="aspectFill"></image>
  29. </view>
  30. <view class="order-box-body-middle">
  31. <text class="order-box-body-desc">{{item.title}}</text>
  32. <text class="order-box-body-cate">x{{item.nums}}</text>
  33. </view>
  34. <view class="order-box-body-right">
  35. ¥{{(item.nums*parseFloat(item.price)).toFixed(2)}}
  36. </view>
  37. </view>
  38. <view class="order-box-footer">
  39. 合计 ¥{{totalPrice}}元
  40. </view>
  41. </view>
  42. </view>
  43. <view class="order-btn" @click="addOrder">提交订单</view>
  44. </view>
  45. </template>
  46. <script>
  47. var app = getApp();
  48. export default {
  49. data() {
  50. return {
  51. goods: [],
  52. totalPrice: 0,
  53. orgName: '',
  54. deliveryTime: null,
  55. address: null,
  56. type: 0
  57. }
  58. },
  59. onLoad() {
  60. this.orgName = uni.getStorageSync(app.globalData.storagePre+'orgName');
  61. this.getAddr();
  62. },
  63. onShow() {
  64. var that = this;
  65. try {
  66. const orderData =JSON.parse(uni.getStorageSync(app.globalData.storagePre+'orderData'));
  67. const selectGoods = orderData.goods;
  68. this.deliveryTime = orderData.day;
  69. this.type = orderData.type;
  70. if (selectGoods.length > 0) {
  71. this.goods = selectGoods;
  72. console.log(3333,orderData.goods,orderData);
  73. this.formatGoods();
  74. }else{
  75. console.log(2222);
  76. uni.navigateBack({
  77. delta: 1
  78. });
  79. }
  80. } catch (e) {
  81. console.log(1111,e);
  82. uni.navigateBack({
  83. delta: 1
  84. });
  85. }
  86. app.ajaxReadyCallback = res => { //各个接口统一回调方法
  87. var apiname = res.data.apiname;
  88. console.log(res);
  89. if(apiname == 'addr'){
  90. that.address = res.data.data;
  91. } else if (apiname == 'order') {
  92. uni.showToast({
  93. title: '下单成功',
  94. icon: 'none',
  95. duration: 2000
  96. })
  97. setTimeout(function(){
  98. uni.navigateTo({
  99. url: '/pages/order/index'
  100. });
  101. },1000);
  102. }
  103. }
  104. },
  105. methods: {
  106. formatGoods(){
  107. this.totalPrice = 0;
  108. this.goods.forEach((item) => {
  109. this.totalPrice += item.nums*parseFloat(item.price);
  110. });
  111. },
  112. getAddr(){
  113. app.ajax({
  114. url: app.globalData.serverUrl + 'shopAddress/addressOne',
  115. type: 'POST',
  116. apiname: 'addr'
  117. });
  118. },
  119. addOrder(){
  120. const garr = [];
  121. this.goods.forEach((item) => {
  122. garr.push({
  123. id: item.id,
  124. nums: item.nums
  125. });
  126. })
  127. app.ajax({
  128. url: app.globalData.serverUrl + 'shopOrder/addOrder',
  129. type: 'POST',
  130. apiname: 'order',
  131. data: {
  132. address_id: this.address.id,
  133. delivery_time: this.deliveryTime,
  134. type: this.type,
  135. json: JSON.stringify(garr)
  136. }
  137. });
  138. },
  139. selectAddr(){
  140. var that = this;
  141. uni.navigateTo({
  142. url: '/pages/address/index?type=1',
  143. events: {
  144. // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
  145. acceptDataFromOpenedPage: function(data) {
  146. console.log('jieshou:',data)
  147. that.address = data;
  148. }
  149. },
  150. success: function(res) {
  151. // 通过eventChannel向被打开页面传送数据
  152. res.eventChannel.emit('acceptDataFromOpenerPage', { addressId: that.address?that.address.id:0 })
  153. }
  154. })
  155. },
  156. onErrorImg(item){
  157. this.$set(item, 'imageLoad', false);
  158. this.$forceUpdate();
  159. },
  160. onSuccessImg(item){
  161. this.$set(item, 'imageLoad', true);
  162. this.$forceUpdate();
  163. },
  164. }
  165. }
  166. </script>
  167. <style lang="scss" scoped>
  168. .order-btn{
  169. width: 673rpx;
  170. height: 72rpx;
  171. border-radius: 10rpx;
  172. color: #FFFFFF;
  173. background-color: $theme-color;
  174. line-height: 72rpx;
  175. font-size: 38rpx;
  176. text-align: center;
  177. margin: 0 auto;
  178. margin-top: 40rpx;
  179. }
  180. .address-box{
  181. width: 680rpx;
  182. height: 90rpx;
  183. margin: 20rpx auto;
  184. background-color: #FFFFFF;
  185. border-radius: 10rpx;
  186. padding: 10rpx 20rpx;
  187. }
  188. .address-box-left{
  189. display: inline-block;
  190. width: 630rpx;
  191. height: 90rpx;
  192. float: left;
  193. }
  194. .address-box-unselect{
  195. width: 100%;
  196. height: 90rpx;
  197. line-height: 90rpx;
  198. }
  199. .address-box-select{
  200. width: 100%;
  201. height: 90rpx;
  202. line-height: 45rpx;
  203. }
  204. .address-box-select-text1{
  205. color: #333333;
  206. font-size: 34rpx;
  207. height: 45rpx;
  208. overflow: hidden;
  209. text-overflow: ellipsis; /* 超出部分省略号 */
  210. display: -webkit-box; /** 对象作为伸缩盒子模型显示 **/
  211. -webkit-box-orient: vertical; /** 设置或检索伸缩盒对象的子元素的排列方式 **/
  212. -webkit-line-clamp: 1; /** 显示的行数 **/
  213. }
  214. .address-box-select-text2{
  215. color: #949494;
  216. font-size: 28rpx;
  217. height: 45rpx;
  218. overflow: hidden;
  219. text-overflow: ellipsis; /* 超出部分省略号 */
  220. display: -webkit-box; /** 对象作为伸缩盒子模型显示 **/
  221. -webkit-box-orient: vertical; /** 设置或检索伸缩盒对象的子元素的排列方式 **/
  222. -webkit-line-clamp: 1; /** 显示的行数 **/
  223. }
  224. .address-box-right{
  225. display: inline-block;
  226. width: 20rpx;
  227. height: 90rpx;
  228. line-height: 90rpx;
  229. text-align: right;
  230. float: right;
  231. }
  232. .address-box-right image{
  233. width: 14rpx;
  234. height: 24%;
  235. vertical-align: middle;
  236. }
  237. .order-box-list{
  238. width: 680rpx;
  239. height: auto;
  240. border-radius: 8rpx;
  241. margin: 15rpx auto;
  242. background-color: #FFFFFF;
  243. padding: 20rpx;
  244. overflow: hidden;
  245. }
  246. .order-box-header{
  247. width: 680rpx;
  248. height: 78rpx;
  249. line-height: 78rpx;
  250. border-bottom: 1rpx solid #F6F6F6;
  251. }
  252. .order-box-title{
  253. display: inline-block;
  254. width: 500rpx;
  255. height: 100%;
  256. font-size: 34rpx;
  257. font-weight: bold;
  258. }
  259. .order-box-desc{
  260. display: inline-block;
  261. width: 180rpx;
  262. text-align: right;
  263. height: 100%;
  264. color: #949494;
  265. font-size: 30rpx;
  266. }
  267. .order-box-body{
  268. padding: 20rpx 0 30rpx 0;
  269. overflow: hidden;
  270. }
  271. .order-box-body-left{
  272. display: inline-block;
  273. width: 88rpx;
  274. height: 88rpx;
  275. float: left;
  276. }
  277. .order-box-body-left image{
  278. width: 100%;
  279. height: 100%;
  280. border-radius: 10rpx;
  281. }
  282. .order-box-body-middle{
  283. display: inline-block;
  284. width: 420rpx;
  285. float: left;
  286. padding-left: 20rpx;
  287. }
  288. .order-box-body-right{
  289. display: inline-block;
  290. width: 140rpx;
  291. height: 88rpx;
  292. line-height: 88rpx;
  293. float: right;
  294. text-align: right;
  295. }
  296. .order-box-body-desc{
  297. display: inline-block;
  298. width: 100%;
  299. font-size: 30rpx;
  300. font-weight: bold;
  301. color: #333333;
  302. }
  303. .order-box-body-cate{
  304. display: inline-block;
  305. width: 100%;
  306. font-size: 24rpx;
  307. color: #949494;
  308. }
  309. .order-box-footer{
  310. border-top: 1rpx solid #F6F6F6;
  311. padding: 20rpx;
  312. text-align: right;
  313. }
  314. </style>