confirmOrder.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <view class="pages confirmOrder">
  3. <view class="order-list">
  4. <custom-nodata v-if="cartList.length === 0"></custom-nodata>
  5. <view class="order-list-item" v-for="(item,index) in cartList" :key="index">
  6. <view class="image-content">
  7. <image :src="$onlineImg + item.goodsImages | firstImg" mode="aspectFill"></image>
  8. </view>
  9. <view class="text-content">
  10. <view class="title">{{item.goodsName}} </view>
  11. <!-- <view class="oId">产品编号: {{ item.oId }}</view> -->
  12. <view class="price">{{item.price}}元 / KG</view>
  13. <view class="totalcarbon">累计碳汇量{{item.totalcarbon}}kg</view>
  14. <view class="total"><text>¥</text>{{item.totalPrice | keepTwoNum}}</view>
  15. <view class="nums">
  16. <view class="countNum">
  17. <view class="countNum-action reduce" @click="calcValue('reduce',item)">-</view>
  18. <view class="countNum-input">
  19. <input class="uni-input" v-model="item.total" type="number" @blur="onInput(item)"/>
  20. </view>
  21. <view class="countNum-action add" @click="calcValue('add',item)">+</view>
  22. </view>
  23. </view>
  24. <!-- {{cartListTmp}} -->
  25. </view>
  26. </view>
  27. </view>
  28. <!-- <uni-list class="pay-way">
  29. <uni-list-item title="支付方式" rightText="微信支付"></uni-list-item>
  30. </uni-list> -->
  31. <view class="pay-wrap" @click="submitOrder">
  32. <view class="pay-con">
  33. <text class="pay-con-til">立即支付</text>
  34. <view class="num">
  35. <text>¥</text>
  36. {{totalPrice | keepTwoNum}}
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. import { mapMutations } from 'vuex';
  44. // import uniList from "@/components/uni-list/uni-list.vue"
  45. // import uniListItem from "@/components/uni-list-item/uni-list-item.vue"
  46. import customNodata from '@/components/custom-nodata/custom-nodata.vue'
  47. import { debounce } from '@/utils/util.js'
  48. export default{
  49. components:{
  50. // uniList,
  51. // uniListItem,
  52. customNodata
  53. },
  54. onLoad(options) {
  55. let self = this;
  56. let arr = this.$store.state.cartListTmp
  57. console.log('options',JSON.parse(JSON.stringify(options)));
  58. console.log('this.$store.state.cartListTmp',JSON.parse(JSON.stringify(this.$store.state.cartListTmp)));
  59. for(let i = 0; i < arr.length; i++){
  60. this.cartList.push({
  61. goodsName:arr[i].goodsName,
  62. goodsImages:arr[i].goodsImages,
  63. goodsId:arr[i].guid,
  64. price:arr[i].price,
  65. carbonVal:arr[i].carbonVal,
  66. total:arr[i].carbonSkin,
  67. totalPrice:arr[i].carbonSkin * arr[i].price,
  68. totalcarbon:arr[i].carbonSkin
  69. })
  70. };
  71. uni.getStorage({
  72. key:'accessToken',
  73. success: function (res) {
  74. self.token = res.data.accessToken;
  75. // console.log('token',res.data.accessToken);
  76. }
  77. });
  78. },
  79. onUnload() {
  80. //页面卸载购物清单
  81. this.clearCart()
  82. },
  83. data(){
  84. return{
  85. token:'',
  86. requested:true,//是否请求中
  87. min:1,
  88. cartList:[]
  89. }
  90. },
  91. methods:{
  92. ...mapMutations(['clearCart']),
  93. onInput(item){
  94. console.log(item.total)
  95. if(!(/(^[1-9]\d*$)/.test(item.total))){
  96. item.total = 1
  97. if(item.total < this.min){
  98. item.total = 1
  99. }
  100. }
  101. item.totalPrice = item.price * item.total
  102. },
  103. //数量计算
  104. calcValue(type,item){
  105. if (type === "reduce") {
  106. item.total--
  107. if (item.total < this.min) {
  108. item.total = 1
  109. }
  110. item.totalPrice = item.price * item.total;
  111. item.totalcarbon = item.total*10;
  112. } else if (type === "add") {
  113. // console.log('this.$store.state.cartListTmp',JSON.parse(JSON.stringify(this.$store.state.cartListTmp)));
  114. // console.log('this.$store.state.cartListTmp.canSold',this.$store.state.cartListTmp[0].canSold);
  115. item.total++
  116. item.totalPrice = item.price * item.total
  117. item.totalcarbon = item.total*10;
  118. }
  119. },
  120. //提交校验
  121. submitOrder(){
  122. if(this.requested){
  123. this.requested = false;
  124. uni.showLoading({
  125. title: '订单生成中。'
  126. });
  127. if(this.cartList.length == 0){
  128. this.$api.msg("请先选择产品!")
  129. return;
  130. }else{
  131. let userInfo = uni.getStorageSync("userInfo");
  132. console.log( uni.getStorageSync("linkType") );
  133. let params = {
  134. // customerId:userInfo.guid,
  135. goods:this.cartList,
  136. // linkType: uni.getStorageSync("linkType") || '',
  137. // activityId:uni.getStorageSync("togetherId") || ''
  138. }
  139. this.syncData(params);
  140. }
  141. }else{
  142. this.$api.msg('请求中,请稍等!', 2500);
  143. return
  144. };
  145. },
  146. //订单提交,拿到订单id
  147. syncData(data){
  148. console.log('data',data);
  149. // return
  150. this.$api.http.post(this.config.apiBaseurl + "/carbon-h5/wap/order/createOrder",data,{
  151. header: {
  152. Accept:'application/json',
  153. Authorization: 'Bearer '+ this.token, //注意Bearer后面有一空格
  154. },
  155. }).then(res=>{
  156. console.log('res',res);
  157. this.requested = true;
  158. if(res.data.retHead.errCode == 0){
  159. this.pullWeichatPay(res.data.retBody)
  160. }else{
  161. this.$api.msg(res.data.retHead.errMsg)
  162. }
  163. uni.hideLoading()
  164. }).catch( err =>{
  165. console.log('createOrder err',err)
  166. })
  167. },
  168. //拉起微信支付
  169. async pullWeichatPay(id){
  170. console.log('订单ID',id);
  171. let userInfo = uni.getStorageSync("userInfo");
  172. return;
  173. let params = {
  174. orderId:id,
  175. openid:userInfo.wxId
  176. }
  177. this.$pay.wxPay(params).then(res =>{
  178. if(res.code == 0){
  179. uni.reLaunch({
  180. url: '/pages/buySuccess/buySuccess?orderId=' + params.orderId
  181. })
  182. }else if(res.code == 1){
  183. uni.navigateTo({
  184. url: '/pages/userCenter/myOrder/myOrder'
  185. })
  186. }else if(res.code == 2){
  187. this.$api.msg("支付失败,请检查!")
  188. setTimeout(()=>{
  189. uni.navigateTo({
  190. url: '/pages/userCenter/myOrder/myOrder'
  191. });
  192. },1000)
  193. }
  194. })
  195. }
  196. },
  197. computed:{
  198. //订单总价计算
  199. totalPrice:function(){
  200. let totalPrice = 0
  201. let len = this.cartList.length
  202. for(let i = 0; i < len;i++){
  203. totalPrice += this.cartList[i].totalPrice
  204. }
  205. return totalPrice
  206. }
  207. }
  208. }
  209. </script>
  210. <style>
  211. @import url("confirmOrder.css");
  212. </style>