柳暗花明笔记

    自己搞了个小接口,现丑了

    所属分类:[ php&web ] 猫哲学家 发表于:2014-10-29 11:27 浏览:547


    /**
    * Api controller class
    * @author Lanmiz
    */
    class Api
    {
    private $results;

    private $access;

    private $key;

    private $error_status;

    private $success_status;

    public function __construct()
    {
    $this->success_status = 1;

    $this->error_status = 0;

    $this->results['status'] = $this->error_status;

    $this->results['message'] = 'Unknown error.';

    $this->results['data'] = array();

    $this->key = '123456';

    date_default_timezone_set('UTC');

    $token = md5($this->key.date('Y-m-d'));

    if ($token != $this->request->post('token')) {
    $this->_exit_report('Permission denied. Please contact 260177392@qq.com.');
    }
    }

    /**
    * Get product info
    * @param $post sku
    * @author Lanmiz 2014-10-28
    */
    public function action_get_product_info() 
    {
    $product_model = new Model_Product;

    $sku_str = $this->request->post('sku');

    $result_data = array();

    try 
    {
    $sku_arr = explode(',', $sku_str);
    $limit = NULL;
    $offset = NULL;

    // Product fields
    $fields = array('sku', 'name', 'name_url', 'price');
    if (count($sku_arr) > 1000) {
    $this->_exit_report('Too much sku.');
    }
    $where = array(
    array('sku', 'IN', $sku_arr), 
    array('on_sale', '=', 1),
    array('hidden', '=', 0),
    );

    $products = $product_model->find($limit, $offset, $where, $order_by = array(), $fields);

    if (empty($products)) {
    $this->_exit_report('Not found any sku.');
    }

    foreach ($products as $product) {
    // Get promotion
    $promotion = array();
    $promotion_db = $product_model->get_product_promotion_price($limit, $offset, $where = array(array('sku', '=', $product['sku'])), $orderby = array(array('amount_start', 'asc')), $single = false);

    if ( ! empty($promotion_db) ) {
    $promotion1 = array();
    foreach ($promotion_db as $val) {
    $promotion1['start'] = $val['amount_start'];
    $promotion1['end'] = $val['amount_end'];
    $promotion1['price'] = round($val['promotion_price'], 2);
    $promotion[] = $promotion1;
    }
    }
    $product['promotion'] = $promotion;
    $result_data[] = $product;
    }

    $this->_exit_report('Success', $this->success_status, $result_data);

    } catch (Exception $e) {
    $this->_exit_report($ex->getMessage(), $this->success_status, $result_data);
    }
    }

    private function _exit_report($message = 'Unknown error.', $status = 0, $data = array()) {
    $this->results['status'] = $status;
    $this->results['message'] = $message;
    $this->results['data'] = $data;
    die(json_encode($this->results));
    }

    }

    标签: php 
    上一编文章 ubuntu 14.04禁用系统错误报告System Program Problem Detected 下一编文章 数组的排序函数
    最近文章
    发表评论