tp3.2 try catch结合事务写法(ajax请求)

try{
            if($shopid && !in_array($shopid, $shopIds)){
                throw new \Exception('非法操作');
            }
           $info = M('logistics_returns')->where(['did' => $did,'shop_id' => $shopid,'storage_status' => 'undeal'])->find();
           if (empty($info)){
                throw new \Exception('未查询到此运单');
            }
            M()->startTrans();
            $res = M('logistics_returns')->delete($did);
            $res1 = M('deliver')->where(['Did' => $did,'apply_return' => 1])->save(['apply_return' => 0]);
            if ($res1 && $res){
                M()->commit();
                ajax_return($did,'取消申请成功',1);
            }else{
                throw new \Exception("取消申请失败");
            }
        }catch (\Exception $e) {
            M()->rollback();
            ajax_return($did,$e->getMessage(),0);
        }
<script>
    function cancelIntercept(obj){
        var msg = '确定要取消申请吗';
        var shopid=$(obj).attr('shopid');
        var orderid=$(obj).attr('orderid');
        var did=$(obj).attr('did');
        if (confirm(msg) == true){
            $.ajax({
                type: 'GET',
                url: '/Admin/SelfDeliveryOrder/cancelIntercept',
                cache:false,
                data: {orderid:orderid,shopid:shopid,did:did},
                dataType: 'json',
                success: function (e){
                    console.log(e);
                    if (e.error_code == 1) {
                        alert(e.error_message);
                        location.replace(location);
                    } else {
                        alert(e.error_message);
                    }
                }
            });
        }else{
            return false;
        }

    }
</script>
<a href="javascript:;" did="<?=$v['did']?>" orderid="<?=$v['orderid']?>" shopid="<?=$v['shop_id']?>" onClick="cancelIntercept(this)">取消申请</a>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容