confirmOrder.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.carbonVal}}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.carbonVal" 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].carbonSkin,
  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.carbonVal)
  95. if(!(/(^[1-9]\d*$)/.test(item.carbonVal))){
  96. item.carbonVal = 1
  97. if(item.carbonVal < this.min){
  98. item.carbonVal = 1
  99. }
  100. }
  101. item.totalPrice = item.price * item.carbonVal
  102. },
  103. //数量计算
  104. calcValue(type,item){
  105. if (type === "reduce") {
  106. item.total--
  107. if (item.carbonVal < this.min) {
  108. item.carbonVal = 1
  109. }
  110. item.totalPrice = item.price * item.carbonVal;
  111. // item.totalcarbon = item.carbonVal*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.carbonVal++
  116. item.totalPrice = item.price * item.carbonVal
  117. console.log('this.cartList',this.cartList);
  118. // item.totalcarbon = item.carbonVal*10;
  119. }
  120. },
  121. //提交校验
  122. submitOrder(){
  123. if(this.requested){
  124. this.requested = false;
  125. uni.showLoading({
  126. title: '订单生成中。'
  127. });
  128. if(this.cartList.length == 0){
  129. this.$api.msg("请先选择产品!")
  130. return;
  131. }else{
  132. let userInfo = uni.getStorageSync("userInfo");
  133. // console.log( uni.getStorageSync("linkType") );
  134. let params = {
  135. // customerId:userInfo.guid,
  136. goods:this.cartList,
  137. // linkType: uni.getStorageSync("linkType") || '',
  138. // activityId:uni.getStorageSync("togetherId") || ''
  139. }
  140. this.syncData(params);
  141. }
  142. }else{
  143. this.$api.msg('请求中,请稍等!', 2500);
  144. return
  145. };
  146. },
  147. //订单提交,拿到订单id
  148. syncData(data){
  149. console.log('data',data);
  150. // return
  151. this.$api.http.post(this.config.apiBaseurl + "/carbon-h5/wap/order/createOrder",data,{
  152. header: {
  153. Accept:'application/json',
  154. Authorization: 'Bearer '+ this.token, //注意Bearer后面有一空格
  155. },
  156. }).then(res=>{
  157. console.log('res',res);
  158. this.requested = true;
  159. if(res.data.retHead.errCode == 0){
  160. this.pullWeichatPay(res.data.retBody)
  161. }else{
  162. this.$api.msg(res.data.retHead.errMsg)
  163. }
  164. uni.hideLoading()
  165. }).catch( err =>{
  166. console.log('createOrder err',err)
  167. })
  168. },
  169. //拉起微信支付
  170. pullWeichatPay(item){
  171. let self = this;
  172. console.log('pay item',item);
  173. let userInfo = uni.getStorageSync("userInfo");
  174. console.log('userInfo',userInfo);
  175. // return
  176. let params = {
  177. orderId:item.guid,
  178. // payType:item.payType,
  179. openid:userInfo.wxId,
  180. "tradeType":"test"
  181. };
  182. this.$api.http.post(this.config.apiBaseurl + "/carbon-h5/wechat/pay",params,{
  183. header: {
  184. Accept:'application/json',
  185. Authorization: 'Bearer '+ this.token, //注意Bearer后面有一空格
  186. },
  187. }).then(res=>{
  188. uni.requestPayment({
  189. provider: 'wxpay',
  190. timeStamp:res.data.retBody.timeStamp,
  191. nonceStr: res.data.retBody.nonceStr,
  192. package: res.data.retBody.packageValue,
  193. signType: 'MD5',
  194. paySign: res.data.retBody.paySign,
  195. success: function (res) {
  196. self.$api.href('/pages/usercenter/orderList/orderList')
  197. console.log('success:' + JSON.stringify(res));
  198. },
  199. fail: function (err) {
  200. console.log('fail:' + JSON.stringify(err));
  201. }
  202. });
  203. }).catch( err =>{
  204. console.log('pay err',err)
  205. })
  206. }
  207. },
  208. computed:{
  209. //订单总价计算
  210. totalPrice:function(){
  211. let totalPrice = 0
  212. let len = this.cartList.length
  213. for(let i = 0; i < len;i++){
  214. totalPrice += this.cartList[i].totalPrice
  215. }
  216. return totalPrice
  217. }
  218. }
  219. }
  220. </script>
  221. <style>
  222. @import url("confirmOrder.css");
  223. </style>