审查视图

app/portal/controller/SalesmangoodsController.php 10.9 KB
anyv authored
1 2 3 4 5 6 7 8 9 10 11
<?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;
anyv authored
12
use EasyWeChat\Foundation\Application;
anyv authored
13 14 15 16 17 18 19 20
use think\Db;
class SalesmangoodsController extends WeChatBaseController{

    /**
     * 业务员商品管理
     */
    public function salesman_goods(){
anyv authored
21 22
        $uid = cmf_get_current_user_id();
        $data_goods = Db::name('goods') -> where("uid =".$uid." and type =2") -> order("sort desc") -> select() -> toArray();
5  
anyv authored
23 24 25 26 27 28 29 30
        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);
anyv authored
31 32 33 34
        return $this -> fetch();

    }
6  
anyv authored
35 36 37 38 39
    /**
     * 业务员商品上移
     */
    public function goods_move_up(){
5  
anyv authored
40
        $uid = cmf_get_current_user_id();
6  
anyv authored
41
        $goods_id = $_POST['goods_id'];
5  
anyv authored
42
        $data_goods = Db::name('goods') -> where('id',$goods_id) -> find();
5  
anyv authored
43
        $data = Db::name('goods') -> where("uid =".$uid." and type =2") -> order("sort desc") -> select() -> toArray();
5  
anyv authored
44 45
        foreach ($data as $key => $val){
            if($val['sort'] == $data_goods['sort']){
5  
anyv authored
46
                $sort = $data[$key-1]['sort'];
5  
anyv authored
47
                $data[$key-1]['sort'] = $data[$key]['sort'];
5  
anyv authored
48
                Db::name('goods') -> where('id',$data[$key-1]['id']) -> update(['sort'=>$data[$key-1]['sort']]);
5  
anyv authored
49
                $data[$key]['sort'] = $sort;
5  
anyv authored
50
                Db::name('goods') -> where('id',$data[$key]['id']) -> update(['sort'=>$data[$key]['sort']]);
5  
anyv authored
51 52 53
            }
        }
        return true;
6  
anyv authored
54 55

    }
anyv authored
56
5  
anyv authored
57 58 59 60 61 62 63
    /**
     * 业务员商品删除
     */
    public function goods_del(){

        $id = $_POST['goods_id'];
        $data = Db::name('goods') -> delete($id);
4  
anyv authored
64
        Db::name('shopping_cart') -> where("goods_id",$id) -> delete();
5  
anyv authored
65 66 67 68 69 70 71
        if($data){
            return true;
        }else{
            return false;
        }

    }
anyv authored
72
anyv authored
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    /**
     * 业务员商品费用查询
     */
    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;
        }
anyv authored
95
anyv authored
96
    }
anyv authored
97
anyv authored
98 99 100 101 102
    /**
     * 添加业务员商品页
     */
    public function Salesman_goods_add(){
anyv authored
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        if($this -> request -> isPost()){
            $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'];
4  
anyv authored
120
            $data['printing'] = $_POST['printing'];
anyv authored
121 122 123 124 125 126 127 128 129 130 131 132
            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);
           }
anyv authored
133 134 135 136 137 138 139 140 141 142 143
            $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{
5  
anyv authored
144 145 146 147 148 149 150 151

            $options=config('wechat_config');
            $app = new Application($options);

            $js = $app->js;
            $jssdk=$js->config(array('onMenuShareAppMessage', 'onMenuShareTimeline','onMenuShareQQ'), false,false,true);
            $this->assign('jssdk',$jssdk);
anyv authored
152 153
            return $this -> fetch();
        }
anyv authored
154 155

    }
anyv authored
156
anyv authored
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    /**
     * 点击编辑商品
     */
    public function Salesman_goods_edit(){

        if($this -> request -> isPost()){
            $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'];
5  
anyv authored
179
            $data['printing'] = $_POST['printing'];
anyv authored
180 181 182 183 184 185 186 187 188 189 190 191
            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);
            }
anyv authored
192 193 194 195 196 197 198 199 200 201
            $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();
anyv authored
202 203 204 205 206
            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]);
                }
9  
anyv authored
207
            }
anyv authored
208 209 210 211 212
            $this -> assign('goods_data',$goods_data);
            return $this -> fetch();
        }

    }
anyv authored
213
4  
anyv authored
214 215 216 217 218 219 220 221
    // 获取签名
    public function getSignPackage() {
        // 获取token
        $token = $this->getAccessToken();
        // 获取ticket
        $ticketList = $this->getJsApiTicket($token['accessToken']);
        $ticket = $ticketList['ticket'];
        // 该URL为使用JSSDK接口的URL
anyv authored
222
        $url = 'http://xkeasy.w.bronet.cn/Salesmangoods/salesman_goods_add';
4  
anyv authored
223 224 225 226 227 228
        // 时间戳
        $timestamp = time();
        // 随机字符串
        $nonceStr = $this->createNoncestr();
        // 这里参数的顺序要按照 key 值 ASCII 码升序排序 j -> n -> t -> u
        $string = "jsapi_ticket=$ticket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
4  
anyv authored
229
        $signature = sha1($string);
4  
anyv authored
230
        $signPackage = array (
4  
anyv authored
231
            "appId" => "wxdf34ec2179e19b9a",
4  
anyv authored
232 233 234 235 236 237 238 239 240
            "nonceStr" => $nonceStr,
            "timestamp" => $timestamp,
            "url" => $url,
            "signature" => $signature,
            "rawString" => $string,
            "ticket" => $ticket,
            "token" => $token['accessToken']
        );
        // 提供数据给前端
5  
anyv authored
241
        return json_encode($signPackage);
4  
anyv authored
242 243 244 245 246
    }

    public function getAccessToken() {
        // access_token 应该全局存储与更新
        // 获取数据库中的access_token
4  
anyv authored
247
        $token = Db::name('token') -> where('id',1) -> find();
4  
anyv authored
248
        // accessToken过期或不存在时
4  
anyv authored
249 250
        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";
4  
anyv authored
251 252 253 254 255
            // 微信返回的信息
            $returnData = json_decode($this->httpGet($url));
            // 组装数据
            $resData['accessToken'] = $returnData->access_token;
            $resData['expiresIn'] = $returnData->expires_in;
4  
anyv authored
256
            $resData['time'] = time();
4  
anyv authored
257
            // 把数据存进数据库
4  
anyv authored
258
            Db::name('token') -> where('id',1) -> update($resData);
4  
anyv authored
259 260 261 262 263 264 265
            $res = $resData;
        }else{
            $res = $token;
        }
        return $res;
    }
4  
anyv authored
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    public function getJsApiTicket($accessToken) {
        // jsapi_ticket 应该全局存储与更新
        // 获取数据库中的jsapi_ticket
        $ticket = Db::name('token') -> where('id',2) -> find();
        // 如果ticket失效
        if($ticket['time'] + $ticket['expiresIn'] < time() || $ticket['ticket'] == NULL){
            $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$accessToken&&type=jsapi";
            // 微信返回的信息
            $returnData = json_decode($this->httpGet($url));
            // 组装数据
            $resData['ticket'] = $returnData->ticket;
            $resData['expiresIn'] = $returnData ->expires_in;
            $resData['time'] = time();
            // 把数据存进数据库
            Db::name('token') -> where('id',2) -> update($resData);
            $res = $resData;
        }else{
            $res = $ticket;
        }
        return $res;
    }

    // 创建随机字符串
    private function createNoncestr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for($i = 0; $i < $length; $i ++) {
            $str .= substr ( $chars, mt_rand ( 0, strlen ( $chars ) - 1 ), 1 );
        }
        return $str;
    }


    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;
    }


4  
anyv authored
311 312 313 314 315 316 317 318 319 320 321 322











anyv authored
323 324 325 326 327 328 329 330 331 332 333 334











}