order.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. <template>
  2. <div>
  3. <div v-loading="loading">
  4. <div class="white-box">
  5. <div class="table">
  6. <el-table
  7. :data="goods"
  8. style="width: 100%"
  9. show-summary
  10. :summary-method="getSummaries">
  11. <el-table-column label="Product Name" prop="GOODS_NAME"><!--商品名称-->
  12. </el-table-column>
  13. <el-table-column label="Product picture" ><!--图片-->
  14. <template slot-scope="scope">
  15. <img :src="scope.row.COVER" alt="" style="width:100%">
  16. </template>
  17. </el-table-column>
  18. <el-table-column label="Product price" prop="member_price"><!--商品价格-->
  19. <template slot-scope="scope">
  20. <span>{{ Math.round(scope.row.member_price * 100) / 100 }}</span>
  21. </template>
  22. </el-table-column>
  23. <el-table-column label="Member BV" prop="member_pv" v-if="category_type === 1"><!--会员PV-->
  24. <template slot-scope="scope">
  25. <span>{{ Math.round(scope.row.member_pv * 100) / 100 }}</span>
  26. </template>
  27. </el-table-column>
  28. <el-table-column label="税率" prop="TAX_RATE"><!--税率-->
  29. <template slot-scope="scope">
  30. <span>{{ Math.round(scope.row.TAX_RATE * 100) / 100 }}</span>
  31. </template>
  32. </el-table-column>
  33. <el-table-column label="税额" prop="tax_amount_plus"><!--税额-->
  34. <template slot-scope="scope">
  35. <span>{{ Math.round(scope.row.tax_amount_plus * 100) / 100 }}</span>
  36. </template>
  37. </el-table-column>
  38. <!-- <el-table-column label="库存" prop="STORE_NUMS">-->
  39. <!-- </el-table-column>-->
  40. <el-table-column label="Quantity" prop="chose_num"><!--数量-->
  41. </el-table-column>
  42. <el-table-column label="Total Amount" prop="member_price_plus"><!--合计金额-->
  43. <template slot-scope="scope">
  44. <span>{{ Math.round(scope.row.member_price_plus * 100) / 100 }}</span>
  45. </template>
  46. </el-table-column>
  47. <el-table-column label="Total BV" prop="member_pv_plus" v-if="category_type === 1"><!--合计PV-->
  48. <template slot-scope="scope">
  49. <span>{{ Math.round(scope.row.member_pv_plus * 100) / 100 }}</span>
  50. </template>
  51. </el-table-column>
  52. </el-table>
  53. </div>
  54. <div class="address_box">
  55. Please select the shipping address: <!-- 请选择收货地址 -->
  56. <el-radio-group v-model="addressId" @change='choseAddress'>
  57. <div v-for="(item , index) in all_address" :key='index' class="address">
  58. <el-radio :label="item.ID" >
  59. Full address:{{item.ADDRESS}}, {{item.CITY_NAME}}, {{item.LGA_NAME}}, {{item.PROVINCE_NAME}} &nbsp;&nbsp;&nbsp;&nbsp;
  60. <!-- 详细地址 --> &nbsp;&nbsp;
  61. <!-- 收件人姓名 --> Recipient name:{{item.CONSIGNEE}}&nbsp;&nbsp;&nbsp;&nbsp;
  62. <!-- 手机号码 --> Phone number:{{item.MOBILE}}
  63. </el-radio>
  64. </div>
  65. <div class="address">
  66. <el-radio label="100000000000000000">Self Pick-up</el-radio><!--自提-->
  67. </div>
  68. </el-radio-group>
  69. </div>
  70. <div class="address_box">
  71. Please select payment method:<!-- 请选择支付方式 -->
  72. <el-radio-group v-model="payType" @change='chosePayType'>
  73. <div v-for="(item, index) in payList" :key='index' class="address">
  74. <el-radio :label="item.label">{{ item.name }}</el-radio>
  75. </div>
  76. </el-radio-group>
  77. </div>
  78. <div class="box address_box">
  79. Total orders:<!-- 订单合计 -->
  80. <div class="sum">
  81. <div class="sum_box" v-if="category_type === 1">
  82. <div>Freight</div><!-- 运费 -->
  83. <div><span v-if="category_type === 1">{{ prefixSign }}</span> {{ payType === "cash" ? freight : pointFreight }} <span v-if="category_type === 1">{{ unit }}</span></div>
  84. </div>
  85. <div class="sum_box">
  86. <div>Amount paid</div><!-- 实付金额 -->
  87. <div><span v-if="category_type === 1">{{ prefixSign }}</span> {{ payType === "cash" ? cashSum : pointsSum }} <span v-if="category_type === 1">{{ unit }}</span></div>
  88. </div>
  89. </div>
  90. </div>
  91. <div class="box address_box">
  92. Account Balance: <!-- 账户余额 -->
  93. <div class="sum">
  94. <div v-if="category_type === 1">
  95. <!-- <div class="sum_box">-->
  96. <!-- <div>账户积分</div>-->
  97. <!-- <div>{{ balance.points }}</div>-->
  98. <!-- </div>-->
  99. <div class="sum_box">
  100. <div>Account Balance</div><!-- 账户余额 -->
  101. <div>₦ {{ balance.cash }} NGN</div>
  102. </div>
  103. <!-- <div class="sum_box">-->
  104. <!-- <div>Exchange points</div>&lt;!&ndash; 兑换点数 &ndash;&gt;-->
  105. <!-- <div>{{ balance.exchange }}</div>-->
  106. <!-- </div>-->
  107. </div>
  108. <div v-if="category_type === 4">
  109. <div class="sum_box">
  110. <div>Travel bonus</div><!-- 旅游积分 -->
  111. <div>{{ balance.tourism_points }}</div>
  112. </div>
  113. </div>
  114. <div v-if="category_type === 5">
  115. <div class="sum_box">
  116. <div>Car House bonus</div><!-- 名车积分 -->
  117. <div>{{ balance.garage_points }}</div>
  118. </div>
  119. </div>
  120. </div>
  121. </div>
  122. <div>
  123. <el-button type="primary" @click="goToAccounts()" :loading="submitButtonStat">Pay</el-button><!--去结算-->
  124. </div>
  125. </div>
  126. </div>
  127. <el-dialog title="Pay" v-if="visible" :visible.sync="visible" width="30%" v-loading="payStackLoading" :before-close="handleClose">
  128. <section>
  129. <!-- <h1>Lorem Ipsum Dolor Sit Amet</h1>-->
  130. <div class="formcontainer">
  131. <el-divider></el-divider>
  132. <div class="container">
  133. <el-form :model="form">
  134. <el-form-item label="Email" label-width="100px" required>
  135. <el-input v-model="form.email" autocomplete="off"></el-input>
  136. </el-form-item>
  137. <el-form-item label="Amount" label-width="100px" required>
  138. <el-input v-model="form.amount" autocomplete="off" readonly></el-input>
  139. </el-form-item>
  140. </el-form>
  141. </div>
  142. </div>
  143. </section>
  144. <paystack
  145. :firstname="form.firstname"
  146. :lastname="form.lastname"
  147. :amount="form.amount * 100"
  148. :email="form.email"
  149. :metadata="form.metadata"
  150. :currency="form.currency"
  151. :paystackkey="form.publicKey"
  152. :reference="reference"
  153. :callback="processPayment"
  154. :close="processClose"
  155. >
  156. <el-button type="primary" size="small">Pay</el-button>
  157. </paystack>
  158. <el-button type="danger" size="small" class="cancelButton" @click="handleClose">Cancel</el-button>
  159. </el-dialog>
  160. <el-dialog title="Tips" :visible.sync="payDialog" :show-close="false" width="350px" :close="handleOrderList">
  161. <el-result icon="success" title="Successful">
  162. <template slot="extra">
  163. <span style="color: #008efa; font-size: 30px;">{{ countdown }}</span>
  164. </template>
  165. </el-result>
  166. </el-dialog>
  167. </div>
  168. </template>
  169. <script>
  170. import network from '@/utils/network'
  171. import tool from '@/utils/tool'
  172. import userInfo from '@/utils/userInfo'
  173. import { PAY_STACK_PUBLIC_KEY, PAY_STACK_CURRENCY } from '@/utils/config'
  174. import paystack from 'vue-paystack'
  175. export default{
  176. name:'order',
  177. data(){
  178. return{
  179. loading: true,
  180. goods:[],
  181. payList:[],
  182. all_address:[],
  183. balance:{},
  184. addressId:'',
  185. payType:'',
  186. payTypeId: '',
  187. pointsSum:'',
  188. cashSum:'',
  189. freight:'',
  190. pointFreight:'',
  191. goodsId:'',
  192. goodsNum:'',
  193. payPassword:'',
  194. submitButtonStat:false,
  195. category_type: '',
  196. prefixSign: '₦',
  197. unit: 'NGN',
  198. sn: '',
  199. payDialog: false,
  200. countdown: 5,
  201. visible: false,
  202. payStackLoading: false,
  203. form: {
  204. publicKey: PAY_STACK_PUBLIC_KEY,
  205. currency: PAY_STACK_CURRENCY,
  206. firstname: userInfo.userName(),
  207. lastname: '',
  208. email: userInfo.userEmail(),
  209. amount: 0, // kobo
  210. orderSn: '',
  211. metadata: {
  212. cart_id: this.sn,
  213. custom_fields: [
  214. {
  215. display_name: 'orderSn',
  216. variable_name: 'orderSn',
  217. value: this.sn
  218. },
  219. ]
  220. },
  221. },
  222. }
  223. },
  224. components: {
  225. paystack
  226. },
  227. created(){
  228. let option= sessionStorage.getItem('order_goods');
  229. this.category_type = parseInt(sessionStorage.getItem('category_type'))
  230. this.getShowCart();
  231. if(option){
  232. let pageGoodsList=JSON.parse(option);
  233. // console.log(pageGoodsList);
  234. let pageList;
  235. for( let i in pageGoodsList ) {
  236. pageList = pageGoodsList[i];
  237. if (!pageList) continue;
  238. pageList.map((pageData, index)=>{
  239. if( Number(pageData.chose_num) > 0 ) {
  240. let discount = pageData.DISCOUNT / 100;
  241. pageData.member_price = Math.round(pageData.SELL_PRICE * discount * 100) / 100;
  242. pageData.member_price_plus = Math.round(pageData.SELL_PRICE * Number(pageData.chose_num) * discount * 100) / 100;
  243. pageData.member_pv = Math.round(pageData.PRICE_PV * discount * 100) / 100;
  244. pageData.member_pv_plus = Math.round(pageData.PRICE_PV * Number(pageData.chose_num) * discount * 100) / 100;
  245. pageData.tax_amount_plus = Math.round((pageData.member_price - pageData.member_price / (1 + pageData.TAX_RATE / 100)) * pageData.chose_num * 100) / 100;
  246. this.goods.push(pageData)
  247. }
  248. })
  249. }
  250. this.getSumMoney();
  251. }
  252. },
  253. computed: {
  254. reference() {
  255. let text = "";
  256. let possible =
  257. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  258. for (let i = 0; i < 10; i++)
  259. text += possible.charAt(Math.floor(Math.random() * possible.length));
  260. return text;
  261. }
  262. },
  263. methods:{
  264. cancelOrder(){
  265. history.go(-1)
  266. },
  267. setFreight(){
  268. if(this.addressId=='100000000000000000'){//如果地址为自提,则运费为0
  269. this.pointFreight = this.freight = 0;
  270. }
  271. },
  272. getSummaries(param) {
  273. const { columns, data } = param;
  274. const sums = [];
  275. columns.forEach((column, index) => {
  276. if (index === 0) {
  277. sums[index] = 'Total';// 合计
  278. return;
  279. }
  280. const values = data.map(item => Number(item[column.property]));
  281. if ((!values.every(value => isNaN(value))) && [5, 6, 7, 8].includes(index)) {
  282. sums[index] = values.reduce((prev, curr) => {
  283. const value = Number(curr);
  284. if (!isNaN(value)) {
  285. return Math.round((prev + curr) * 100) / 100;
  286. } else {
  287. return Math.round(prev * 100) / 100;
  288. }
  289. }, 0);
  290. }
  291. });
  292. return sums;
  293. },
  294. goToAccounts(){
  295. let amountBalance = 0
  296. // 前置判断余额是否够支付
  297. if (this.category_type === 1) {
  298. if (this.payType === 'cash') {
  299. amountBalance = this.balance.cash
  300. } else if (this.payType === 'exchange') {
  301. amountBalance = this.balance.exchange
  302. }
  303. } else if (this.category_type === 4) {
  304. amountBalance = this.balance.tourism_points
  305. } else if (this.category_type === 5) {
  306. amountBalance = this.balance.garage_points
  307. }
  308. // 提示信息
  309. let tips = 'Balance' //余额
  310. if (this.payType !== 'cash' && this.payType !== 'pay_stack') {
  311. console.log(this.payList, this.payType)
  312. let payObj = this.payList.find((item) => {
  313. return item.label === this.payType
  314. })
  315. tips = (payObj.length <= 0) ? '' : payObj.name
  316. }
  317. // 余额是否充足
  318. if ((amountBalance > 0) && (this.payType !== 'pay_stack') && ((amountBalance - this.cashSum) < 0)) {
  319. let tips = this.payList[this.payType] ? this.payList[this.payType].label : 'Balance'
  320. this.$message({
  321. message: tips + ' insufficient, unable to buy products', // 不足,无法购买商品
  322. type: 'error'
  323. })
  324. this.submitButtonStat = false
  325. return false
  326. }
  327. this.submitButtonStat = true
  328. // let path = 'sure-order'
  329. this.$prompt('Please enter your payment password', 'Hint', {//'请输入支付密码', '提示'
  330. confirmButtonText: 'Confirm',//确定
  331. cancelButtonText: 'Cancel',//取消
  332. inputType: 'password',
  333. inputPattern: /\S+/,
  334. inputErrorMessage: 'Please enter your payment password'//请输入支付密码
  335. }).then(({value}) => {
  336. this.payPassword = value
  337. let params = {
  338. addressId: this.addressId,
  339. payType: this.payType,
  340. goodsId: this.goodsId,
  341. goodsNum: this.goodsNum,
  342. payPassword: this.payPassword,
  343. email: this.form.email,
  344. }
  345. // PayStack支付
  346. if (this.payType === 'pay_stack') {
  347. return network.postData('shop/sure-approach-order', params).then((response) => {
  348. this.submitButtonStat = false
  349. this.form.orderSn = response.SN
  350. this.form.amount = this.cashSum
  351. this.form.metadata.custom_fields[0].value = response.SN
  352. this.visible = true
  353. }).catch(() => {
  354. this.submitButtonStat = true
  355. })
  356. } else {
  357. // 非PayStack支付
  358. return network.postData('shop/sure-order', params).then(() => {
  359. this.submitButtonStat = false
  360. this.$router.push({path: `/shop/order-list`})
  361. }).catch(() => {
  362. this.submitButtonStat = true
  363. })
  364. }
  365. }).catch(() => {
  366. this.submitButtonStat = false
  367. })
  368. },
  369. getSumMoney(){
  370. // let points_plus_sum=[];
  371. let cash_plus_sum=[];
  372. let goodsId=[];
  373. let goodsNum=[];
  374. let choseNum = 0;
  375. this.goods.map((item,index)=>{
  376. choseNum = Number(item.chose_num);
  377. if( choseNum > 0 ) {
  378. // points_plus_sum.push(item.SELL_PRICE * choseNum);
  379. cash_plus_sum.push(item.SELL_PRICE * choseNum*(item.DISCOUNT/100));
  380. goodsId.push(item.ID);
  381. goodsNum.push(choseNum);
  382. }
  383. })
  384. this.goodsNum=goodsNum;
  385. this.goodsId=goodsId;
  386. //增加运费03-12yuan
  387. let payAmount = tool.sum(cash_plus_sum);
  388. // let payPointAmount = tool.sum(points_plus_sum);
  389. // 只有普通商品有运费
  390. if (this.category_type === 1) {
  391. if(payAmount>=300){
  392. this.pointFreight = this.freight = 0;
  393. }else{
  394. this.pointFreight = this.freight = 15;
  395. }
  396. this.setFreight();
  397. }
  398. // if(payPointAmount>=300){
  399. // this.pointFreight = 0;
  400. // }else{
  401. // this.pointFreight = 15;
  402. // }
  403. //console.log('points_plus_sum',points_plus_sum);
  404. //console.log('cash_plus_sum',cash_plus_sum);
  405. // this.pointsSum=tool.formatPrice(tool.sum(points_plus_sum) + this.pointFreight);
  406. this.pointsSum = this.cashSum = this.form.amount = tool.formatPrice(tool.sum(cash_plus_sum) + this.freight) ;
  407. },
  408. getShowCart(){
  409. network.getData('shop/show-cart', { categoryType: this.categoryType })
  410. .then(response => {
  411. this.loading = false
  412. // 收货地址
  413. this.all_address = response.allAddress;
  414. this.all_address.map((item,index)=>{
  415. if(item.IS_DEFAULT==1){
  416. this.addressId = item.ID
  417. }
  418. })
  419. // 账户
  420. this.balance = response.userBalance
  421. // 支付方式
  422. for (let item of response.sellType) {
  423. if (item.id === this.category_type) {
  424. this.payList = item.sell_type
  425. break
  426. }
  427. }
  428. // 支付方式的第一项默认选中
  429. this.payType = Object.values(this.payList)[0]['label']
  430. })
  431. },
  432. // 切换支付方式
  433. chosePayType(type) {
  434. this.payType = type
  435. },
  436. choseAddress(addressId){
  437. this.getSumMoney()
  438. },
  439. // 关闭支付回调
  440. handleClose() {
  441. let _this = this
  442. _this.$confirm('Confirm to close?').then(() => {
  443. return network.postData('shop/delete-approach-order', {orderSn: this.form.orderSn}).then(() => {
  444. // 关闭支付模态框
  445. _this.visible = false
  446. // 关闭购物车页面,返回到订单列表页
  447. sessionStorage.setItem('order_goods', null)
  448. sessionStorage.setItem('category_type', null)
  449. this.$router.push({path: `/shop/index`})
  450. })
  451. }).catch(() => {
  452. // 关闭支付模态框
  453. _this.visible = false
  454. // 关闭购物车页面,返回到订单列表页
  455. sessionStorage.setItem('order_goods', null)
  456. sessionStorage.setItem('category_type', null)
  457. this.$router.push({path: `/shop/index`})
  458. })
  459. },
  460. // 支付成功回调
  461. processPayment(response) {
  462. // 关闭支付页面
  463. this.visible = false
  464. this.payStackLoading = false
  465. // 显示支付成功模态框
  466. this.payDialog = true
  467. this.handleCountdown()
  468. },
  469. // 关闭支付回调
  470. processClose() {
  471. // 关闭支付模态框
  472. this.visible = false
  473. // 关闭购物车页面,返回到订单列表页
  474. sessionStorage.removeItem('order_goods')
  475. sessionStorage.removeItem('category_type')
  476. this.$router.push({path: `/shop/index`})
  477. },
  478. handleOrderList() {
  479. this.$router.push({path: `/shop/order-list`})
  480. },
  481. // 启动倒计时
  482. handleCountdown() {
  483. //创建定时器
  484. setInterval(() => {
  485. // 每隔1秒把time的值减一,赋值给span标签
  486. this.countdown--
  487. if (this.countdown === 0) {
  488. // 倒计时结束,跳转到订单列表
  489. this.$router.push({path: `/shop/order-list`})
  490. }
  491. }, 1000)
  492. },
  493. }
  494. }
  495. </script>
  496. <style scoped>
  497. .address{
  498. /* height: 3rem; */
  499. line-height: 3.5rem;
  500. }
  501. .address_box{
  502. border-bottom: 1px solid #e3e3e3;
  503. }
  504. .sum{
  505. display: inline-block;
  506. }
  507. .box{
  508. margin: 1rem 0;
  509. display: flex;
  510. justify-content: flex-start;
  511. align-items: center;
  512. padding-bottom: 1rem;
  513. }
  514. .sum_box{
  515. display: flex;
  516. margin-left: 1rem;
  517. }
  518. .sum_box > div{
  519. line-height: 2rem;
  520. }
  521. .sum_box > div:nth-child(1){
  522. margin-right: 1rem;
  523. }
  524. .payButton {
  525. border: none;
  526. padding: 0;
  527. }
  528. </style>