Selaa lähdekoodia

套餐卡券和商家端验证码

zhangxiaobin 1 vuosi sitten
vanhempi
commit
51296dc772

+ 46 - 0
application/admin/controller/Packagegift.php

@@ -3,6 +3,7 @@
 namespace app\admin\controller;
 
 use app\common\controller\Backend;
+use think\Db;
 
 /**
  * 套餐卡券管理
@@ -43,6 +44,8 @@ class Packagegift extends Backend
         $this->relationSearch = true;
         //设置过滤方法
         $this->request->filter(['strip_tags', 'trim']);
+        $package_id = input('package_id',0);
+        $this->assignconfig('package_id',$package_id);
         if ($this->request->isAjax()) {
             //如果发送的来源是Selectpage,则转发到Selectpage
             if ($this->request->request('keyField')) {
@@ -69,4 +72,47 @@ class Packagegift extends Backend
         return $this->view->fetch();
     }
 
+    /**
+     * 添加
+     *
+     * @return string
+     * @throws \think\Exception
+     */
+    public function add()
+    {
+        $package_id = input('package_id',0);
+
+        if (false === $this->request->isPost()) {
+            $this->assign('package_id',$package_id);
+            return $this->view->fetch();
+        }
+        $params = $this->request->post('row/a');
+        if (empty($params)) {
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $params = $this->preExcludeFields($params);
+
+        if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
+            $params[$this->dataLimitField] = $this->auth->id;
+        }
+        $result = false;
+        Db::startTrans();
+        try {
+            //是否采用模型验证
+            if ($this->modelValidate) {
+                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
+                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
+                $this->model->validateFailException()->validate($validate);
+            }
+            $result = $this->model->allowField(true)->save($params);
+            Db::commit();
+        } catch (ValidateException|PDOException|Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        if ($result === false) {
+            $this->error(__('No rows were inserted'));
+        }
+        $this->success();
+    }
 }

+ 2 - 13
application/admin/view/packagegift/add.html

@@ -1,20 +1,9 @@
 <form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
-    <div class="form-group">
-        <label class="control-label col-xs-12 col-sm-2">{:__('Company_id')}:</label>
-        <div class="col-xs-12 col-sm-8">
-            <input id="c-company_id" data-rule="required" data-source="company/index" data-field="title" class="form-control selectpage" name="row[company_id]" type="text" value="">
-        </div>
-    </div>
-    <div class="form-group">
-        <label class="control-label col-xs-12 col-sm-2">{:__('Package_id')}:</label>
-        <div class="col-xs-12 col-sm-8">
-            <input id="c-package_id" data-rule="required" data-source="package/index" data-field="title" class="form-control selectpage" name="row[package_id]" type="text" value="">
-        </div>
-    </div>
+    <input name="row[package_id]" type="hidden" value="{$package_id}">
     <div class="form-group">
         <label class="control-label col-xs-12 col-sm-2">{:__('Coupon_id')}:</label>
         <div class="col-xs-12 col-sm-8">
-            <input id="c-coupon_id" data-rule="required" data-source="coupon/index" class="form-control selectpage" name="row[coupon_id]" type="text" value="">
+            <input id="c-coupon_id" data-rule="required" data-source="coupons/index" class="form-control selectpage" name="row[coupon_id]" type="text" value="">
         </div>
     </div>
     <div class="form-group">

+ 1 - 8
application/admin/view/packagegift/edit.html

@@ -1,15 +1,8 @@
 <form id="edit-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
-
-    <div class="form-group">
-        <label class="control-label col-xs-12 col-sm-2">{:__('Package_id')}:</label>
-        <div class="col-xs-12 col-sm-8">
-            <input id="c-package_id" data-rule="required" data-source="package/index" class="form-control selectpage" name="row[package_id]" type="text" value="{$row.package_id|htmlentities}">
-        </div>
-    </div>
     <div class="form-group">
         <label class="control-label col-xs-12 col-sm-2">{:__('Coupon_id')}:</label>
         <div class="col-xs-12 col-sm-8">
-            <input id="c-coupon_id" data-rule="required" data-source="coupon/index" class="form-control selectpage" name="row[coupon_id]" type="text" value="{$row.coupon_id|htmlentities}">
+            <input id="c-coupon_id" data-rule="required" data-source="coupons/index" class="form-control selectpage" name="row[coupon_id]" type="text" value="{$row.coupon_id|htmlentities}">
         </div>
     </div>
     <div class="form-group">

+ 18 - 2
application/api/controller/Sms.php

@@ -4,6 +4,7 @@ namespace app\api\controller;
 
 use app\common\controller\Api;
 use app\common\library\Sms as Smslib;
+use app\common\model\CompanyStaff;
 use app\common\model\User;
 use app\common\service\SmsService;
 use think\Db;
@@ -26,12 +27,12 @@ class Sms extends Api
      * @param string $mobile 手机号
      * @param string $event 事件名称
      */
-    public function send()
+    public function send($params=[])
     {
         $mobile = $this->request->post("mobile");
         $event = $this->request->post("event");
         $event = $event ? $event : 'register';
-
+        $this->success(__('发送成功'));
         if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
             $this->error(__('手机号不正确'));
         }
@@ -45,6 +46,9 @@ class Sms extends Api
         }
         if ($event) {
             $userinfo = User::getByMobile($mobile);
+            if (isset($params['is_company']) && $params['is_company'] ==1) {//兼容商家端获取验证码
+                $userinfo = CompanyStaff::getByMobile($mobile);
+            }
             if ($event == 'register' && $userinfo) {
                 //已被注册
                 $this->error(__('已被注册'));
@@ -68,6 +72,18 @@ class Sms extends Api
     }
 
     /**
+     * 商家端短信
+     * @return void
+     */
+    public function companySend()
+    {
+        $params = [
+            'is_company' => 1,
+        ];
+        $this->send($params);
+    }
+
+    /**
      * 检测验证码
      *
      * @ApiMethod (POST)

+ 4 - 4
application/api/controller/UserCoupons.php

@@ -34,10 +34,10 @@ class UserCoupons extends Api
             $whereOr = [];
             if ($status == 1) {
                 $where['remain'] = ['gt',0];
-                $whereOr['endtime'] = ['gt',time()];
+                $whereOr['endtime'] = [['gt',time()],['eq',0],'or'];
             } else {
                 $whereOr['remain'] = ['elt',0];
-                $whereOr['endtime'] = ['lt',time()];
+                $whereOr['endtime'] = [['lt',time()],['neq',0],'and'];
             }
             $result = $this->model->field($field)->where($where)->where(function($query) use ($whereOr){
                 $query->whereOr($whereOr);
@@ -45,7 +45,7 @@ class UserCoupons extends Api
 
             if (!empty($result)) {
                 foreach ($result as $key => &$value) {
-                    !empty($value['endtime']) && $value['endtime'] = date('Y.m.d H:i:s',$value['endtime']);
+                    $value['endtime'] = empty($value['endtime']) ? '' :  date('Y.m.d H:i:s',$value['endtime']);
                 }
             }
             $this->success('获取成功',$result);
@@ -73,7 +73,7 @@ class UserCoupons extends Api
                 throw new Exception('未找到相关信息');
             }
             //验证
-            if ($modelData['endtime'] < time() || $modelData['remain'] < 1) {
+            if (($modelData['remain'] > 0 && $modelData['endtime'] < time() && $modelData['endtime'] > 0) || $modelData['remain'] < 1) {
                 throw new Exception('该券已无法使用');
             }
             $text = 'coupon_'.$modelData['check_code'];

+ 18 - 3
application/common/service/OrderService.php

@@ -53,9 +53,9 @@ class OrderService
             if (empty($package)) {
                 throw new Exception('未找到套餐信息');
             }
-            if ($package['company_id'] != $companyId) {
+            /*if ($package['company_id'] != $companyId) {
                 throw new Exception('您绑定的门店和购买套餐门店不一致');
-            }
+            }*/
             if ($payType == 'wallet') {//验证余额
                 $userWalletWhere['user_id'] = $userId;
                 $userWalletWhere['company_id'] = $companyId;
@@ -208,6 +208,21 @@ class OrderService
             } else {
                 $checkCodeStr = Random::alnum(8);
             }
+            if ($package['type'] == 2) {//卡券类型直接已完成
+                $orderStatus = 3;
+                $userCouponsParams = [
+                    'package_id' => $packageId,
+                    'company_id' => $companyId,
+                    'user_id'    => $userId,
+                    'payorder_id'=> $payOrderId,
+                ];
+                $userCouponsRes = $userService->packageCoupons($userCouponsParams);
+                if (!$userCouponsRes['status']) {
+                    throw new Exception($userCouponsRes['msg']);
+                }
+            } else {
+                $orderStatus = 1;
+            }
             $orderData = [
                 'check_code'      => $checkCodeStr,
                 'orderno'         => createUniqueNo('O', $userId),//订单号
@@ -224,7 +239,7 @@ class OrderService
                 'server_info'     => isset($package['info']) ? $package['info'] : '',//套餐内容
                 'server_images'   => isset($package['images']) ? $package['images'] : '',//套餐图片
                 'pay_fee'         => isset($package['price']) ? $package['price'] : 0.00,//套餐价格
-                'status'          => 1,//状态:1=待核销,2=待处理,3=已完成,4=已取消
+                'status'          => $orderStatus,//状态:1=待核销,2=待处理,3=已完成,4=已取消
                 'pay_time'        => $time,
                 'paytype'         => $orderPayType,//支付方式:1=线下,2=余额,3=微信
                 'total_fee'       => isset($package['price']) ? $package['price'] : 0.00,//支付总额

+ 64 - 0
application/common/service/UserService.php

@@ -3,6 +3,7 @@
 namespace app\common\service;
 
 use app\common\model\PreOrder;
+use fast\Random;
 use GuzzleHttp\Client;
 use think\Db;
 use think\Exception;
@@ -279,4 +280,67 @@ class UserService
         }
         return $result;
     }
+
+    /**
+     * 套餐卡券购买
+     * @return array
+     */
+    public function packageCoupons($params=[])
+    {
+        $result = [
+            'status' => 1,
+            'msg'    => '',
+            'data'   => [],
+        ];
+        try {
+            $packageId = isset($params['package_id']) ? $params['package_id'] : 0;
+            $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
+            $userId    = isset($params['user_id']) ? $params['user_id'] : 0;
+            $payOrderId= isset($params['payorder_id']) ? $params['payorder_id'] : 0;
+            //赠送优惠券
+            $pg = 'package_gift';
+            $c  = 'coupons';
+            $field = $pg.'.*,'.$c.'.name,'.$c.'.info,'.$c.'.days';
+            $packageGiftWhere['package_id'] = $packageId;
+            $packageGiftWhere[$c.'.status'] = 1;
+            $rechargeGift = Db::name($pg)->alias($pg)->field($field)
+                ->join($c,$c.'.id = '.$pg.'.coupon_id','LEFT')->where($packageGiftWhere)->select();
+            $time = time();
+            foreach ($rechargeGift as $k => $v) {
+                $checkCode[] = Random::alnum(8);
+            }
+            $orderService = new OrderService();//防重复
+            $checkCodeRes = $orderService->getCheckCoupons($checkCode);
+            foreach ($rechargeGift as $key => $value) {
+                $endtime = $value['days'] == 0 ? 0 : $time + 86400 * $value['days'];
+                if ($checkCodeRes['status']) {
+                    $checkCodeStr = isset($checkCodeRes['data'][$key]) ? $checkCodeRes['data'][$key] : '';
+                } else {
+                    $checkCodeStr = Random::alnum(8);
+                }
+                $userCouponsData[] = [
+                    'check_code' => $checkCodeStr,
+                    'user_id' => $userId,
+                    'company_id' => $companyId,
+                    'coupons_id' => $value['coupon_id'],
+                    'coupon_name' => $value['name'],
+                    'coupon_info' => $value['info'],
+                    'createtime' => $time,
+                    'endtime' => $endtime,
+                    'number' => $value['number'],
+                    'remain' => $value['number'],
+                    'payorder_id' => $payOrderId,
+                    'getfrom' => '购买套餐',
+                ];
+            }
+            $userCouponsRes = Db::name('user_coupons')->insertAll($userCouponsData);
+            if (!$userCouponsRes) {
+                throw new Exception('套餐券获取失败');
+            }
+        } catch (Exception $e) {
+            $result['status'] = 0;
+            $result['msg'] = $e->getMessage();
+        }
+        return $result;
+    }
 }

+ 11 - 2
public/assets/js/backend/packagegift.js

@@ -5,8 +5,8 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
             // 初始化表格参数配置
             Table.api.init({
                 extend: {
-                    index_url: 'packagegift/index' + location.search,
-                    add_url: 'packagegift/add',
+                    index_url: 'packagegift/index/package_id/' + Config.package_id + location.search,
+                    add_url: 'packagegift/add/package_id/' + Config.package_id,
                     edit_url: 'packagegift/edit',
                     del_url: 'packagegift/del',
                     multi_url: 'packagegift/multi',
@@ -47,6 +47,15 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
         },
         api: {
             bindevent: function () {
+                $("#c-company_id").data("params",function(){
+                    return {custom:{status:1}};
+                });
+                $("#c-package_id").data("params",function(){
+                    return {custom:{company_id:$('#c-company_id').val(),status:1}};
+                });
+                $("#c-coupon_id").data("params",function(){
+                    return {custom:{company_id:$('#c-company_id').val(),status:1}};
+                });
                 Form.api.bindevent($("form[role=form]"));
             }
         }