bianjunhui 1 год назад
Родитель
Сommit
60ae29c56a

+ 143 - 0
application/admin/controller/Appindex.php

@@ -3,6 +3,11 @@
 namespace app\admin\controller;
 
 use app\common\controller\Backend;
+use think\Db;
+use think\exception\DbException;
+use think\exception\PDOException;
+use think\exception\ValidateException;
+use app\common\model\Coupon;
 
 /**
  * 
@@ -32,6 +37,144 @@ class Appindex extends Backend
      * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
      * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
      */
+    /**
+     * 添加
+     *
+     * @return string
+     * @throws \think\Exception
+     */
+    public function add()
+    {
+        if (false === $this->request->isPost()) {
+            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);
+            $result = Db::name('appindex')->insertGetId($params);
+            if($params['showType']==1){
+                $send =new Coupon();
+                $re = $send->getProduct($params['link']);
+                $product=array();
+                if(isset($re['status'])&&$re['status']==1){
+                    $product =$re['data']['product'];
+                }
+                if($product){
+                    $new=array();
+                    foreach ($product as $k=>$v){
+                        $new[$k]['pid']=$result;
+                        $new[$k]['title']=$v['name'];
+                        $new[$k]['link']=$v['entity_id'];
+                        $new[$k]['oldPrice']=$v['price'];
+                        $new[$k]['nowPrice']=number_format($v['final_price'], 2);
+                        $new[$k]['sale_num']=$v['sold'];
+                        $new[$k]['sort_order']=$k+1;
+                        $new[$k]['images']='https://www.bilisar.com/media/catalog/product'.$v['image'];
+                    }
+                    Db::name('appindexdes')->insertAll($new);
+                }else{
+                    Db::rollback();
+                    $this->error('该分类没产品!');
+                }
+            }
+            Db::commit();
+        } catch (ValidateException|PDOException|Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        if ($result === false) {
+            $this->error(__('No rows were inserted'));
+        }
+        $this->success();
+    }
 
+    /**
+     * 编辑
+     *
+     * @param $ids
+     * @return string
+     * @throws DbException
+     * @throws \think\Exception
+     */
+    public function edit($ids = null)
+    {
+        $row = $this->model->get($ids);
+        if (!$row) {
+            $this->error(__('No Results were found'));
+        }
+        $adminIds = $this->getDataLimitAdminIds();
+        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
+            $this->error(__('You have no permission'));
+        }
+        if (false === $this->request->isPost()) {
+            $this->view->assign('row', $row);
+            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);
+        $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 . '.edit' : $name) : $this->modelValidate;
+                $row->validateFailException()->validate($validate);
+            }
+            $result = $row->allowField(true)->save($params);
+            if($row['showType']==1){
+                Db::name('appindexdes')->where('pid',$ids)->delete();
+                $send =new Coupon();
+                $re = $send->getProduct($params['link']);
+                $product=array();
+                if(isset($re['status'])&&$re['status']==1){
+                    $product =$re['data']['product'];
+                }
+                if($product){
+                    $new=array();
+                    foreach ($product as $k=>$v){
+                        $new[$k]['pid']=$ids;
+                        $new[$k]['title']=$v['name'];
+                        $new[$k]['link']=$v['entity_id'];
+                        $new[$k]['oldPrice']=$v['price'];
+                        $new[$k]['nowPrice']=number_format($v['final_price'], 2);
+                        $new[$k]['sale_num']=$v['sold'];
+                        $new[$k]['sort_order']=$k+1;
+                        $new[$k]['images']='https://www.bilisar.com/media/catalog/product'.$v['image'];
+                    }
+                    Db::name('appindexdes')->insertAll($new);
+                }else{
+                    Db::rollback();
+                    $this->error('该分类没产品!');
+                }
+            }
+            Db::commit();
+        } catch (ValidateException|PDOException|Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        if (false === $result) {
+            $this->error(__('No rows were updated'));
+        }
+        $this->success();
+    }
 }

+ 0 - 18
application/admin/view/appindexdes/add.html

@@ -32,24 +32,6 @@
             <ul class="row list-inline faupload-preview" id="p-image"></ul>
         </div>
     </div>
-    <div class="form-group">
-        <label class="control-label col-xs-12 col-sm-2">{:__('Oldprice')}:</label>
-        <div class="col-xs-12 col-sm-8">
-            <input id="c-oldPrice" class="form-control" name="row[oldPrice]" type="text">
-        </div>
-    </div>
-    <div class="form-group">
-        <label class="control-label col-xs-12 col-sm-2">{:__('Nowprice')}:</label>
-        <div class="col-xs-12 col-sm-8">
-            <input id="c-nowPrice" class="form-control" name="row[nowPrice]" type="text">
-        </div>
-    </div>
-    <div class="form-group">
-        <label class="control-label col-xs-12 col-sm-2">{:__('Sale_num')}:</label>
-        <div class="col-xs-12 col-sm-8">
-            <input id="c-sale_num" class="form-control" name="row[sale_num]" type="number">
-        </div>
-    </div>
     <div class="form-group">
         <label class="control-label col-xs-12 col-sm-2">{:__('Type')}:</label>
         <div class="col-xs-12 col-sm-8">

+ 0 - 18
application/admin/view/appindexdes/edit.html

@@ -32,24 +32,6 @@
             <ul class="row list-inline faupload-preview" id="p-image"></ul>
         </div>
     </div>
-    <div class="form-group">
-        <label class="control-label col-xs-12 col-sm-2">{:__('Oldprice')}:</label>
-        <div class="col-xs-12 col-sm-8">
-            <input id="c-oldPrice" class="form-control" name="row[oldPrice]" type="text" value="{$row.oldPrice|htmlentities}">
-        </div>
-    </div>
-    <div class="form-group">
-        <label class="control-label col-xs-12 col-sm-2">{:__('Nowprice')}:</label>
-        <div class="col-xs-12 col-sm-8">
-            <input id="c-nowPrice" class="form-control" name="row[nowPrice]" type="text" value="{$row.nowPrice|htmlentities}">
-        </div>
-    </div>
-    <div class="form-group">
-        <label class="control-label col-xs-12 col-sm-2">{:__('Sale_num')}:</label>
-        <div class="col-xs-12 col-sm-8">
-            <input id="c-sale_num" class="form-control" name="row[sale_num]" type="number" value="{$row.sale_num|htmlentities}">
-        </div>
-    </div>
     <div class="form-group">
         <label class="control-label col-xs-12 col-sm-2">{:__('Type')}:</label>
         <div class="col-xs-12 col-sm-8">

+ 36 - 0
application/common/model/Coupon.php

@@ -33,4 +33,40 @@ class Coupon Extends Model
         return $result; // 返回数据
     }
 
+    public function getProduct($pid){
+        $url ='http://www.bilisar.com/rest/V1/rewrite/categories/'.$pid.'/products';
+        $getFields = array(
+            'currencyCode' => 'USB',
+            'page' => 1,
+            'size' => 4,
+            'order' => 'position',
+            'dir' => 'DESC',
+        );
+        $queryString = http_build_query($getFields); // 将参数转换为查询字符串
+        $url = $url . '?' . $queryString; // 构建完整的URL
+        $re = $this->httpGet($url);
+        return json_decode($re,true);
+    }
+
+
+    public static function httpGet($url)
+    {
+        // 初始化cURL会话
+        $ch = curl_init();
+// 设置cURL选项
+        curl_setopt($ch, CURLOPT_URL, $url); // 你要访问的URL
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将curl_exec()获取的信息以字符串返回,而不是直接输出
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//curl 取消 ssl 检查
+// 执行cURL会话
+        $response = curl_exec($ch);
+// 检查是否有错误发生
+        if(curl_errno($ch)){
+            echo 'cURL error: ' . curl_error($ch);
+        }
+// 关闭cURL会话
+        curl_close($ch);
+// 打印响应内容
+        return $response;
+    }
+
 }