confirmOrder.vue 6.8 KB

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