SalesmangoodsController.php 10.0 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\WeChatBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;
class SalesmangoodsController extends WeChatBaseController{

    /**
     * 业务员商品管理
     */
    public function salesman_goods(){

        $uid = cmf_get_current_user_id();
        $data_goods = Db::name('goods') -> where("uid =".$uid." and type =2") -> order("sort desc") -> select() -> toArray();
        if(!empty($data_goods)){
            foreach ($data_goods as $key => $val){
                $price = explode('.',$val['price']);
                $data_goods[$key]['price0'] = $price[0];
                $data_goods[$key]['price1'] = $price[1];
            }
        }
        $this -> assign('data_goods',$data_goods);
        return $this -> fetch();

    }

    /**
     * 业务员商品上移
     */
    public function goods_move_up(){

        $uid = cmf_get_current_user_id();
        $goods_id = $_POST['goods_id'];
        $data_goods = Db::name('goods') -> where('id',$goods_id) -> find();
        $data = Db::name('goods') -> where("uid =".$uid." and type =2") -> order("sort desc") -> select() -> toArray();
        foreach ($data as $key => $val){
            if($val['sort'] == $data_goods['sort']){
                $sort = $data[$key-1]['sort'];
                $data[$key-1]['sort'] = $data[$key]['sort'];
                Db::name('goods') -> where('id',$data[$key-1]['id']) -> update(['sort'=>$data[$key-1]['sort']]);
                $data[$key]['sort'] = $sort;
                Db::name('goods') -> where('id',$data[$key]['id']) -> update(['sort'=>$data[$key]['sort']]);
            }
        }
        return true;

    }

    /**
     * 业务员商品删除
     */
    public function goods_del(){

        $id = $_POST['goods_id'];
        $data = Db::name('goods') -> delete($id);
        Db::name('shopping_cart') -> where("goods_id",$id) -> delete();
        if($data){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 业务员商品费用查询
     */
    public function goods_cost(){

        $id = $_POST['goods_id'];
        $data = Db::name('goods') -> where('id',$id) -> find();
        return json_encode($data);

    }

    /**
     * 点击费用编辑
     */
    public function moneyedit(){

        $data = Db::name('goods') -> where('id',$_POST['goods_id']) -> update(['price'=>$_POST['price'],'money'=>$_POST['money']]);
        if($data){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 添加业务员商品页
     */
    public function Salesman_goods_add(){

        if($this -> request -> isPost()){
            $uid = cmf_get_current_user_id();
            $salesman_goods = Db::name('goods') -> where("uid=".$uid." and type = 2") -> select();
            if(count($salesman_goods)>=8){
                return 3;
            }
            if($_POST['money']>$_POST['price']){
                return 2;
            }
            $data['show_img'] = $_POST['show_img'];
            $data['thumbnail'] = $_POST['thumbnail'];
            $data['book_name'] = $_POST['book_name'];
            $data['money'] = $_POST['money'];
            $data['price'] = $_POST['price'];
            $data['pricing'] = $_POST['pricing'];
            $data['instr'] = $_POST['instr'];
            $data['book_number'] = $_POST['book_number'];
            /*$data['paper'] = $_POST['paper'];*/
            $data['book_size'] = $_POST['book_size'];
            $data['binding'] = $_POST['binding'];
            $data['author'] = $_POST['author'];
            $data['press'] = $_POST['press'];
            $data['press_time'] = $_POST['press_time'];
            $data['suit'] = $_POST['suit'];
            $data['grade'] = $_POST['grade'];
            $data['printing'] = $_POST['printing'];
            $data['zixun_phone'] = $_POST['zixun_phone'];
            if($_POST['img0'] != ''){
                $det_img[0] = $_POST['img0'];
            }
            if($_POST['img1'] != ''){
                $det_img[1] = $_POST['img1'];
            }
            if($_POST['img2'] != ''){
                $det_img[2] = $_POST['img2'];
            }
           if(!empty($det_img)){
               $data['det_img'] = json_encode($det_img);
           }
            $data['create_time'] = time();
            $data['type'] = 2;
            $data['uid'] = cmf_get_current_user_id();
            $goods_id = Db::name('goods') -> insertGetId($data);
            Db::name('goods') -> where("id=".$goods_id) -> update(['sort'=>$goods_id]);
            if($goods_id){
                return true;
            }else{
                return false;
            }
        }else{

            $options=config('wechat_config');
            $app = new Application($options);
            $js = $app->js;
            $jssdk=$js->config(array('chooseImage','uploadImage'), false,false,true);
            $this->assign('jssdk',$jssdk);

            return $this -> fetch();
        }

    }

    /**
     * 点击编辑商品
     */
    public function Salesman_goods_edit(){

        if($this -> request -> isPost()){
            if($_POST['money']>$_POST['price']){
                return 2;
            }
            $data['show_img'] = $_POST['show_img'];
            $data['thumbnail'] = $_POST['thumbnail'];
            $data['book_name'] = $_POST['book_name'];
            $data['money'] = $_POST['money'];
            $data['price'] = $_POST['price'];
            $data['pricing'] = $_POST['pricing'];
            $data['instr'] = $_POST['instr'];
            $data['book_number'] = $_POST['book_number'];
            /*$data['paper'] = $_POST['paper'];*/
            $data['book_size'] = $_POST['book_size'];
            $data['binding'] = $_POST['binding'];
            $data['author'] = $_POST['author'];
            $data['press'] = $_POST['press'];
            $data['press_time'] = $_POST['press_time'];
            $data['suit'] = $_POST['suit'];
            $data['grade'] = $_POST['grade'];
            $data['printing'] = $_POST['printing'];
            $data['zixun_phone'] = $_POST['zixun_phone'];
            if($_POST['img0'] != ''){
                $det_img[0] = $_POST['img0'];
            }
            if($_POST['img1'] != ''){
                $det_img[1] = $_POST['img1'];
            }
            if($_POST['img2'] != ''){
                $det_img[2] = $_POST['img2'];
            }
            if(!empty($det_img)){
                $data['det_img'] = json_encode($det_img);
            }
            $data['id'] = $_POST['id'];
            $up_data = Db::name('goods') -> update($data);
            if($up_data){
               return true;
            }else{
                return false;
            }
        }else{
            $id = $this -> request -> param();
            $goods_data = Db::name('goods') -> where('id',$id['goods_id']) -> find();
            if(!empty($goods_data['det_img'])){
                $goods_data['det_img'] = json_decode($goods_data['det_img'],true);
                foreach ($goods_data['det_img'] as $key => $val){
                    $goods_data['det_img'][$key] = cmf_get_image_url($goods_data['det_img'][$key]);
                }
            }
            $this -> assign('goods_data',$goods_data);

            $options=config('wechat_config');
            $app = new Application($options);
            $js = $app->js;
            $jssdk=$js->config(array('chooseImage','uploadImage'), false,false,true);
            $this->assign('jssdk',$jssdk);

            return $this -> fetch();
        }

    }

    /**
     * 上传图片
     */
    public function savePicture(){

        $access_token=$this->getAccessToken();
        $filepath = $_POST['mediaId'];
        $str = 'uploads/'.date('YmdHis').time().rand('1000000','99999999').'.png';
        $targetName = $str;
        $access_token = $access_token['accessToken'];
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$access_token&media_id=$filepath";
//        $file = file_get_contents("http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$access_token&media_id=$filepath");
        $file = $this->httpGet($url);
        file_put_contents($targetName,$file);
        $msg["filename"] = '/'.$str;
        return json_encode($msg);

    }



    public function getAccessToken() {
        // access_token 应该全局存储与更新
        // 获取数据库中的access_token
        $token = Db::name('token') -> where('id',1) -> find();
        // accessToken过期或不存在时
        if($token['time'] + $token['expiresIn'] < time() || $token['accessToken'] == NULL){
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="."wxdf34ec2179e19b9a"."&secret="."133ced06db2a45843ab52abc691ee82d";
            // 微信返回的信息
            $returnData = json_decode($this->httpGet($url));
            // 组装数据
            $resData['accessToken'] = $returnData->access_token;
            $resData['expiresIn'] = $returnData->expires_in;
            $resData['time'] = time();
            // 把数据存进数据库
            Db::name('token') -> where('id',1) -> update($resData);
            $res = $resData;
        }else{
            $res = $token;
        }
        return $res;
    }

    private function httpGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
        curl_setopt($curl, CURLOPT_TIMEOUT, 500 );
        curl_setopt($curl, CURLOPT_URL, $url );
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }




























}