Geen omschrijving

Order.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/12/5
  6. * Time: 15:07
  7. */
  8. namespace App;
  9. use Illuminate\Database\Eloquent\Model;
  10. use PHPExcel;
  11. use PHPExcel_Writer_Excel2007;
  12. class Order extends Model
  13. {
  14. public $timestamps = false;
  15. protected $table = "order";
  16. public static function olist(){
  17. $result = Order::orderBy('id', 'desc')->get();
  18. return json_decode(json_encode($result),true);
  19. }
  20. /**
  21. * 添加订单
  22. */
  23. public static function mjOrderAdd($orderList){
  24. $orderList["method"] = "maijiayun.order.add";
  25. $result = self::mjApi($orderList);
  26. return $result;
  27. }
  28. /**
  29. * 添加商品
  30. */
  31. public static function mjGoodsAdd($goods){
  32. $orderList["method"] = "maijiayun.eshop.goods.add";
  33. $result = self::mjApi($goods);
  34. return $result;
  35. }
  36. public static function mjApi($params = array()){
  37. $accessKey = "B1E69297B5DA44DAB35099A5F28F41D9"; //erp 生成的 accessKey
  38. $accessSecret = "4sN2LbylhOglelMP";
  39. $params["timestamp"] = time()."000";
  40. $params["version"] = "v1";
  41. $reqparams = $params;
  42. ksort($reqparams);
  43. $sign = "";
  44. foreach($reqparams as $k=>$val){
  45. if(is_array($val)){
  46. $val = json_encode($val,320);
  47. }
  48. $sign.=$k.$val;
  49. }
  50. $params["accessKey"] = $accessKey;
  51. $params["token"] = strtoupper(sha1($accessKey.$sign.$accessSecret));
  52. $param = json_encode($params);
  53. $ch = curl_init();
  54. $headers = array("Content-type:application/json;charset='utf-8'","Accept:application/json", "Cache-Control: no-cache", "Pragma: no-cache");
  55. curl_setopt($ch, CURLOPT_URL, "https://api.erp.maijiayun.cn"); //api 地址
  56. curl_setopt($ch, CURLOPT_POST, 1);
  57. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  58. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  59. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  60. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  61. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  62. $response = curl_exec($ch);
  63. curl_close($ch);
  64. $response = json_decode($response, true);
  65. if (!$response || !is_array($response)) {
  66. return false;
  67. }
  68. if (!array_key_exists("isOk", $response)) {
  69. return false;
  70. } else {
  71. return $response;
  72. }
  73. }
  74. # 生成外部订单号
  75. public static function createOuterCode(){
  76. $order_sn = rand(100, 9999). substr(time(), 5, 5). rand(10, 9999);
  77. return $order_sn;
  78. }
  79. /**
  80. * 导出excel
  81. * @param $data
  82. * @param string
  83. */
  84. public static function export_excel($data, $filename = '未命名.xlsx', $indexKey, $title) {
  85. if( !is_array($indexKey)) return false;
  86. $header_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
  87. //初始化PHPExcel()
  88. $objPHPExcel = new PHPExcel();
  89. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  90. //接下来就是写数据到表格里面去
  91. $objActSheet = $objPHPExcel->getActiveSheet();
  92. foreach($title as $k=>$item){
  93. $objActSheet->setCellValue($header_arr[$k].'1',$item);
  94. }
  95. $startRow = 2;
  96. foreach ($data as $row) {
  97. foreach ($indexKey as $key => $value){
  98. //这里是设置单元格的内容
  99. $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);
  100. }
  101. $startRow++;
  102. }
  103. header("Pragma: public");
  104. header("Expires: 0");
  105. header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
  106. header("Content-Type:application/force-download");
  107. header("Content-Type:application/vnd.ms-execl");
  108. header("Content-Type:application/octet-stream");
  109. header("Content-Type:application/download");;
  110. header('Content-Disposition:attachment;filename='.$filename.'');
  111. header("Content-Transfer-Encoding:binary");
  112. $objWriter->save('php://output');
  113. exit();
  114. }
  115. }