order.vue 23 KB

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