审查视图

application/api/controller/GuanYiCloud.php 9.1 KB
王智 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php
/**
 * Created by PhpStorm.
 * User: 86132
 * Date: 2020/12/1
 * Time: 14:03
 */

namespace app\api\controller;

use app\common\controller\Api;


/**
 * 管易云API
 */
class GuanYiCloud extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
王智 authored
21
王智 authored
22 23

//获取店铺信息
王智 authored
24
    function getShops()
王智 authored
25 26 27 28 29 30
    {
        $data = array();
        $data['appkey'] = APPKEY;
        $data['sessionkey'] = SESSIONKEY;
        $data['method'] = 'gy.erp.shop.get';
        $data['page_no'] = '1';
王智 authored
31
        $data['page_size'] = '100';
王智 authored
32
        $data['sign'] = $this->sign($data, SECRET);
王智 authored
33
        $this->success('成功', json_decode($this->mycurl(URL, $data)));
王智 authored
34 35
    }
王智 authored
36 37
// 会员查询
    function getVip()
王智 authored
38 39 40 41
    {
        $data = array();
        $data['appkey'] = APPKEY;
        $data['sessionkey'] = SESSIONKEY;
王智 authored
42
        $data['method'] = 'gy.erp.vip.get';
王智 authored
43 44 45
        $data['page_no'] = '1';
        $data['page_size'] = '10';
        $data['sign'] = $this->sign($data, SECRET);
王智 authored
46
        $this->success('成功', json_decode($this->mycurl(URL, $data)));
王智 authored
47 48
    }
王智 authored
49
    // 会员新增
王智 authored
50
    function addVip($UnionID, $Nickname)
王智 authored
51 52 53 54
    {
        $data = array();
        $data['appkey'] = APPKEY;
        $data['sessionkey'] = SESSIONKEY;
王智 authored
55 56
        $data['method'] = 'gy.erp.vip.add';
        //会员代码
王智 authored
57
        $data['code'] = $UnionID;
王智 authored
58
        //会员名称
王智 authored
59
        $data['name'] = $Nickname;
王智 authored
60 61 62 63 64 65 66 67
        //所属店铺
        $data['shop_code'] = 'ZCZXCX';
        //地址列表
        $receive_infos = array();
        $receive_infos[] = array(
            //名称
            'name' => '地址',
            //收货人
王智 authored
68
            'receiver' => $Nickname,
王智 authored
69
            //收获手机
王智 authored
70
            'mobile' => 18888888888,
王智 authored
71
            //收货详细地址
王智 authored
72
            'address' => '中国'
王智 authored
73 74
        );
        $data['receive_infos'] = $receive_infos;
王智 authored
75
        $data['sign'] = $this->sign($data, SECRET);
王智 authored
76 77 78 79
        $Rult = json_decode(json_encode(json_decode($this->mycurl(URL, $data))), true);
        if ($Rult['success'] == false) {
            $this->error('失败');
        }
王智 authored
80 81 82 83 84 85 86 87 88 89 90 91 92
    }

//获取仓库信息
    function getWarehouses()
    {
        $data = array();
        $data['appkey'] = APPKEY;
        $data['sessionkey'] = SESSIONKEY;
        $data['method'] = 'gy.erp.warehouse.get';
        $data['page_no'] = '1';
        $data['page_size'] = '10';
        // $data['code'] = '001';
        $data['sign'] = $this->sign($data, SECRET);
王智 authored
93
        $this->success('成功', json_decode($this->mycurl(URL, $data)));
王智 authored
94 95 96 97 98 99 100 101 102 103 104 105 106
    }

// 商品查询
    function getGoods()
    {
        $data = array();
        $data['appkey'] = APPKEY;
        $data['sessionkey'] = SESSIONKEY;
        $data['method'] = 'gy.erp.items.get';
        $data['page_no'] = '1';
        $data['page_size'] = '10';
        // $data['code'] = 'ZZLN000005';
        $data['sign'] = $this->sign($data, SECRET);
王智 authored
107
        $this->success('成功', json_decode($this->mycurl(URL, $data)));
王智 authored
108 109
    }
王智 authored
110
王智 authored
111
//获取订单 $OrderSn 平台单号
王智 authored
112
    public function getOrders($OrderSn)
王智 authored
113 114 115 116 117
    {
        $data = array();
        $data['appkey'] = APPKEY;
        $data['sessionkey'] = SESSIONKEY;
        $data['method'] = 'gy.erp.trade.get';
王智 authored
118
        $data['platform_code'] = $OrderSn;
王智 authored
119 120
        // $date['receiver_mobile']='15928552965';
        $data['sign'] = $this->sign($data, SECRET);
王智 authored
121 122 123
        $Array = json_decode(json_encode(json_decode($this->mycurl(URL, $data))), true);
        //验证
        if (empty($Array['orders'])) {
王智 authored
124
            $this->error('获取订单失败:平台单号' . $OrderSn);
王智 authored
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        }
        $delivery_state = $Array['orders'][0]['delivery_state'];
        $refund = $Array['orders'][0]['details'][0]['refund'];
        $platform_code = $Array['orders'][0]['platform_code'];
        if (!empty($Array['orders'][0]['deliverys'])) {
            $express_name = $Array['orders'][0]['deliverys'][0]['express_name'];
            $express_code = $Array['orders'][0]['deliverys'][0]['express_code'];
            $mail_no = $Array['orders'][0]['deliverys'][0]['mail_no'];
        } else {
            $express_name = '';
            $express_code = '';
            $mail_no = '';
        }
        $Return = [
            //发货状态 0-未发货//1-部分发货//2-全部发货
            'delivery_state' => $delivery_state,
            //平台单号
            'platform_code' => $platform_code,
            //快递公司名称
            'express_name' => $express_name,
            //快递公司代码
            'express_code' => $express_code,
            //快递单号
            'mail_no' => $mail_no
        ];
        $Json = json_encode($Return);
        return $Json;
王智 authored
152 153 154 155
    }


//新增订单
王智 authored
156
    function addOrder($OrderSn, $OrderTime, $PayTime, $UnionID, $Name, $Mobile, $Address, $Province, $City, $District, $details, $Money, $PayOrder)
王智 authored
157 158 159 160 161
    {
        $data = array();
        $data['appkey'] = APPKEY;
        $data['sessionkey'] = SESSIONKEY;
        $data['method'] = 'gy.erp.trade.add';
王智 authored
162
        //订单类型 //Sales-销售订单//Return-换货订单//Charge-费用订单//Delivery-补发货订单//Invoice-补发票订单
王智 authored
163
        $data['order_type_code'] = 'Sales';
王智 authored
164
        //退款状态 //0:未退款//1:退款完成//2:退款中
王智 authored
165
        $data['refund'] = 0;
王智 authored
166
        $data['oid'] = $OrderSn;
王智 authored
167
        //店铺代码
王智 authored
168
        $data['shop_code'] = 'ZCZXCX';
王智 authored
169
        //平台单号
王智 authored
170
        $data['platform_code'] = $OrderSn;
王智 authored
171
        //下单时间
王智 authored
172
        $data['deal_datetime'] = $OrderTime;
王智 authored
173
        //支付时间
王智 authored
174
        $data['pay_datetime'] = $PayTime;
王智 authored
175
        //会员代码
王智 authored
176
        $data['vip_code'] = $UnionID;
王智 authored
177
        //买家留言
王智 authored
178
        $data['buyer_memo'] = '';
王智 authored
179
        //收货人
王智 authored
180 181 182 183 184 185
        $data['receiver_name'] = $Name;
        $data['receiver_mobile'] = $Mobile;
        $data['receiver_address'] = $Address;
        $data['receiver_province'] = $Province;
        $data['receiver_city'] = $City;
        $data['receiver_district'] = $District;
王智 authored
186 187 188
        $data['details'] = $details;
        $payments = array();
        $payments[] = array(
王智 authored
189 190 191
            //支付放视
            'pay_type_code' => 'WechatPay',
            //支付金额
王智 authored
192
            'payment' => $Money,
王智 authored
193
            //交易号
王智 authored
194
            'pay_code' => $PayOrder,
王智 authored
195
            //支付时间
王智 authored
196
            'paytime' => ''
王智 authored
197 198 199 200
        );
        $data['payments'] = $payments;
        $invoices = array();
        $invoices[] = array(
王智 authored
201
            //发票部分
王智 authored
202
            'invoice_type' => 1,
王智 authored
203
            'invoice_title' => '个人',
王智 authored
204 205 206
        );
        $data['invoices'] = $invoices;
        $data['sign'] = $this->sign($data, SECRET);
王智 authored
207 208 209 210
        $Rult = json_decode(json_encode(json_decode($this->mycurl(URL, $data))), true);
        if ($Rult['success'] == false) {
            $this->error('失败');
        }
王智 authored
211 212
    }
王智 authored
213
    /*Config*/
王智 authored
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    function mycurl($url, $data)
    {
        $data_string = $this->json_encode_ch($data);
        $data_string = urlencode($data_string);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type:text/json;charset=utf-8',
            'Content-Length:' . strlen($data_string)
        ));
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }

    function sign($data, $secret)
    {
        if (empty($data)) {
            return "";
        }
        unset($data['sign']); //可选,具体看传参
        $data = $this->json_encode_ch($data);
        $sign = strtoupper(md5($secret . $data . $secret));
        return $sign;
    }

    function json_encode_ch($arr)
    {
        return urldecode(json_encode($this->url_encode_arr($arr)));
    }

    function url_encode_arr($arr)
    {
        if (is_array($arr)) {
            foreach ($arr as $k => $v) {
                $arr[$k] = $this->url_encode_arr($v);
            }
        } elseif (!is_numeric($arr) && !is_bool($arr)) {
            $arr = urlencode($arr);
        }
        return $arr;
    }
王智 authored
259 260

    //获取订单oid
王智 authored
261
    public function GetOrderOid($OrderSn)
王智 authored
262 263 264 265 266 267 268 269 270
    {
        $data = array();
        $data['appkey'] = APPKEY;
        $data['sessionkey'] = SESSIONKEY;
        $data['method'] = 'gy.erp.trade.get';
        $data['platform_code'] = $OrderSn;
        // $date['receiver_mobile']='15928552965';
        $data['sign'] = $this->sign($data, SECRET);
        $Array = json_decode(json_encode(json_decode($this->mycurl(URL, $data))), true);
王智 authored
271 272 273 274
        //验证
        if (empty($Array['orders'])) {
            $this->error('获取订单失败:平台单号' . $OrderSn);
        }
王智 authored
275 276 277 278 279 280 281 282 283 284 285 286
    }


    //获取订单oid
    public function OrderUpdate()
    {
        $OrderSn = input('order');
        $data = array();
        $data['appkey'] = APPKEY;
        $data['sessionkey'] = SESSIONKEY;
        $data['method'] = 'gy.erp.trade.refund.update';
        $data['tid'] = $OrderSn;
王智 authored
287
        $data['oid'] = $OrderSn;
王智 authored
288 289 290 291 292
        $data['refund_state'] = 2;
        // $date['receiver_mobile']='15928552965';
        $data['sign'] = $this->sign($data, SECRET);
        $Array = json_decode(json_encode(json_decode($this->mycurl(URL, $data))), true);
        dump($Array);
王智 authored
293
    }
王智 authored
294
}