技術棧
Appgallery connect
開發準備
上一節我們已經實現了優惠券的選擇,并且成功的把券后的價格也展示給用戶,不能使用的優惠券我們也用友好的方式告知用戶,這一節我們來實現優惠券內容的下一步,優惠券內容結合訂單進行結算提交
功能分析
因為我們之前的訂單列表是訂單相關商品相關是分開的,所以在這里我們同樣要把優惠券的內容分開,只存儲id進去后續再查詢出對應的券金額,我們首先就是要修改訂單表,然后在券選擇的同時拿到優惠券的相關內容,提交訂單時把優惠券內容一起提交,方便我們后續的訂單詳情內查詢券后價
代碼實現
首先修改orderlist的表內容
{"CloudDBZoneName": "default","objectTypeName": "order_list","fields": [{"fieldName": "id", "fieldType": "Integer", "notNull": true, "belongPrimaryKey": true},{"fieldName": "user_id", "fieldType": "Integer", "notNull": true, "defaultValue": 0},{"fieldName": "order_code", "fieldType": "String"},{"fieldName": "order_status", "fieldType": "Integer"},{"fieldName": "order_product_id", "fieldType": "String"},{"fieldName": "coupon_id", "fieldType": "Integer"},{"fieldName": "address", "fieldType": "String"},{"fieldName": "nickname", "fieldType": "String"},{"fieldName": "phone", "fieldType": "String"},{"fieldName": "order_remark", "fieldType": "String"},{"fieldName": "pay_type", "fieldType": "String"},{"fieldName": "order_create_time", "fieldType": "String"},{"fieldName": "order_pay_time", "fieldType": "String"},{"fieldName": "order_delivery_time", "fieldType": "String"},{"fieldName": "order_over_time", "fieldType": "String"}],"indexes": [{"indexName": "field1Index", "indexList": [{"fieldName":"id","sortType":"ASC"}]}],"permissions": [{"role": "World", "rights": ["Read", "Upsert", "Delete"]},{"role": "Authenticated", "rights": ["Read", "Upsert", "Delete"]},{"role": "Creator", "rights": ["Read", "Upsert", "Delete"]},{"role": "Administrator", "rights": ["Read", "Upsert", "Delete"]}]
}
然后我們在選擇券的時候拿到券的id,這里我們用回調的方式實現
//自定義彈窗頁面onItemSelected: (coupon_id:number) => void= () => {};//結算頁@State coupon_id:number=0couponController: CustomDialogController| null = new CustomDialogController({builder: CouponCheckDialog({couponPrice:this.couponPrice,price:this.price(),onItemSelected:(coupon_id:number)=>{this.coupon_id=coupon_id}}),alignment: DialogAlignment.Bottom,customStyle:true});
結算訂單時合并信息提交
Text("提交訂單").fontColor(Color.White).padding(10).borderRadius(10).backgroundColor("#d81e06").fontSize(14).onClick(async ()=>{if (this.addressInfo!=null) {let databaseZone = cloudDatabase.zone('default');try {for (let i = 0; i < this.productList.length; i++) {let productPush = new order_product_list();productPush.id=this.codeId+iproductPush.order_product_id=this.codeIdproductPush.img=this.productList[i].productImgAddressproductPush.price=this.productList[i].productPriceproductPush.name=this.productList[i].productNameproductPush.originalPrice=this.productList[i].productOriginalPriceproductPush.spec=this.productList[i].productSpecNameproductPush.buyAmount=this.productList[i].buyAmountlet num = await databaseZone.upsert(productPush);hilog.info(0x0000, 'testTag', `Succeeded in upserting data, result: ${num}`);}}catch (e) {hilog.info(0x0000, 'testTag', `Succeeded in upserting data, result: ${e}`);}let orderPush = new order_list();orderPush.id=Math.floor(Math.random() * 1000000)orderPush.user_id=this.user!.user_idorderPush.order_product_id=String(this.codeId)orderPush.order_code=this.generateOrderNo(10)orderPush.order_status=0if (this.remark!='') {orderPush.order_remark=this.remark}orderPush.coupon_id=this.coupon_idorderPush.address=this.addressInfo.addressorderPush.nickname=this.addressInfo.nikeNameorderPush.phone=this.addressInfo.phoneorderPush.order_create_time=this.formatCurrentDate()orderPush.order_pay_time=this.formatCurrentDate()let num = await databaseZone.upsert(orderPush);hilog.info(0x0000, 'testTag', `Succeeded in upserting data, result: ${num}`);if (num>0) {for (let i = 0; i < this.productList.length; i++) {if (this.productList[i].isNeedPay) {let item = new cart_product_list();item.id=this.productList[i].idlet listData = await databaseZone.delete(item);hilog.info(0x0000, 'testTag', `Succeeded in upserting data, result: ${listData}`);}}let eventData: emitter.EventData = {data: {}};let innerEvent: emitter.InnerEvent = {eventId: 1012,priority: emitter.EventPriority.HIGH};emitter.emit(innerEvent, eventData);router.replaceUrl({url:'pages/view/OrderSuccessPage',params:orderPush})}} else {showToast("請先選擇地址")}})
到這里我們就實現了結算訂單跟優惠券的關聯