diff --git a/backend/main.py b/backend/main.py index 7b42836..d282ca4 100644 --- a/backend/main.py +++ b/backend/main.py @@ -107,6 +107,23 @@ def shop_check(): else: return jsonify({'status': False}) +@app.route('/shop/
/products', methods=['GET']) +def shop_products(address): + db = sqlite3.connect(DATABASE) + cursor = db.cursor() + cursor.execute("SELECT `products`.`id`, `products`.`name`, `products`.`price`, `products`.`code` FROM `shops`, `products` \ + WHERE `shops`.id = `products`.`shop_id`and `shops`.`address`=?", (address, )) + result = cursor.fetchall() + + ans = {'products': {}} + for product in result: + ans['products'][str(product[3])] = { + 'id': product[0], + 'name': product[1], + 'price': str(product[2]), + } + return jsonify(ans) + if __name__ == '__main__': initDB() app.run(host="0.0.0.0") diff --git a/src/views/ShopPayView.vue b/src/views/ShopPayView.vue index ae8f038..8a22d6c 100644 --- a/src/views/ShopPayView.vue +++ b/src/views/ShopPayView.vue @@ -22,29 +22,57 @@ export default { web3: null, token: null, bank: null, - picName: '', - pic: '', - credit: '', - arrear: '', + warningModalStatus: false, + successModalStatus: false, + msg: '', + isWaiting: false, + products: {}, + productCar: [], + orderId: '0' } }, async mounted() { - // if (!this.$cookies.isKey('linked')) { - // this.$router.push('/') - // } this.web3 = new Web3(window.ethereum) - this.clientAddr = this.$cookies.get('address') + if (this.$cookies.isKey('address')) { + this.clientAddr = this.$cookies.get('address') + } else { + console.log("Use default address") + this.clientAddr = import.meta.env.VITE_DEFAULT_SHOP + } this.web3.eth.defaultAccount = this.clientAddr this.token = new this.web3.eth.Contract(SBT, this.SBTAddress) this.bank = new this.web3.eth.Contract(Bank, this.BankAddress) + + var result = await fetch(import.meta.env.VITE_BACKEND_PREFIX + "/shop/" + this.clientAddr + "/products") + var data = await result.json() + for (let product in data['products']) { + this.products[product] = data['products'][product] + } + }, + computed: { + amount() { + if (!this.web3) { + return 0 + } + var ans = 0 + for (let product of this.productCar) { + ans += Number(product['price']) * product['count'] + } + return this.web3.utils.fromWei(ans.toString()) + } }, methods: { onScanSuccess(decodedText, decodedResult) { // handle the scanned code as you like, for example: console.log(`Code matched = ${decodedText}`, decodedResult); - var last_id = decodedText.split('/').pop() + var product = this.products[decodedText] + this.productCar.push({ + 'name': product['name'], + 'id': product['id'], + 'price': product['price'], + 'count': 1 + }) this.scanner.clear() - this.$router.push('/client/pay/'+last_id) }, scan() { this.scanner = new Html5QrcodeScanner( @@ -52,6 +80,36 @@ export default { { fps: 10, qrbox: { width: 250, height: 250 } }, /* verbose= */ false); this.scanner.render(this.onScanSuccess); + }, + async send() { + this.isWaiting = true + + var data = {} + data['from'] = this.clientAddr + data['products'] = [] + for (let product of this.productCar) { + data['products'].push({ + 'product_id': product['id'], + 'count': product['count'] + }) + } + try { + var result = await fetch(import.meta.env.VITE_BACKEND_PREFIX+"/order", { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data) + }) + this.orderId = await result.json() + this.msg = '完成' + this.successModalStatus = true + } catch (error) { + this.msg = '錯誤' + this.warningModalStatus = true + } + this.productCar = [] + this.isWaiting = false } } } @@ -71,18 +129,43 @@ export default {| 商品清單 | +|||
|---|---|---|---|
| {{ product['name'] }} | +{{ this.web3.utils.fromWei(product['price']) }} ETH | +個 | +|
| 共計 {{ amount }} ETH | +|||