Нет описания

Order.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. //status格式
  26. $staus_arr = array(
  27. 0 => '待付款',
  28. 1 => '已付款待审核',
  29. 2 => '已审核待发货',
  30. 3 => '已发货',
  31. );
  32. $orderList['status'] = $staus_arr[$orderList['status']];
  33. $result = self::mjApi($orderList);
  34. return $result;
  35. }
  36. /**
  37. * 更新订单
  38. */
  39. public static function mjOrderUpdate($orderList){
  40. $orderList["method"] = "maijiayun.order.update";
  41. $staus_arr = array(
  42. 0 => '待付款',
  43. 1 => '已付款待审核',
  44. 2 => '已审核待发货',
  45. 3 => '已发货',
  46. );
  47. $orderList['status'] = $staus_arr[$orderList['status']];
  48. $result = self::mjApi($orderList);
  49. return $result;
  50. }
  51. /**
  52. * 取消订单
  53. */
  54. public static function mjOrderDel($orderList){
  55. $orderList["method"] = "maijiayun.order.update";
  56. $orderList['status'] = '已取消';
  57. $result = self::mjApi($orderList);
  58. return $result;
  59. }
  60. /**
  61. * 获取订单
  62. */
  63. public static function mjOrderGet($order){
  64. $order["method"] = "maijiayun.order.get";
  65. $result = self::mjApi($order);
  66. return $result;
  67. }
  68. /**
  69. * 添加Erp商品
  70. */
  71. public static function mjErpGoodsAdd($goods){
  72. $goods["method"] = "maijiayun.goods.sku.add";
  73. $result = self::mjApi($goods);
  74. return $result;
  75. }
  76. /**
  77. * 添加商品
  78. */
  79. public static function mjGoodsAdd($goods){
  80. $goods["method"] = "maijiayun.eshop.goods.add";
  81. $result = self::mjApi($goods);
  82. return $result;
  83. }
  84. /**
  85. * 更新商品
  86. */
  87. public static function mjGoodsUpdate($goods){
  88. $goods["method"] = "maijiayun.eshop.goods.update";
  89. $result = self::mjApi($goods);
  90. return $result;
  91. }
  92. /**
  93. * 批量添加sku
  94. */
  95. public static function mjBatchSkuAdd($sku){
  96. $sku["method"] = "maijiayun.eshop.sku.batchAdd"; //批量添加
  97. //$sku["method"] = "maijiayun.eshop.sku.add";
  98. $result = self::mjApi($sku);
  99. return $result;
  100. }
  101. /**
  102. * 添加单个sku
  103. */
  104. public static function mjSkuAdd($sku){
  105. $sku["method"] = "maijiayun.eshop.sku.add";
  106. $result = self::mjApi($sku);
  107. return $result;
  108. }
  109. /**
  110. * 修改Erp sku
  111. */
  112. public static function mjErpSkuUpdate($sku){
  113. $sku["method"] = "maijiayun.goods.sku.update";
  114. $result = self::mjApi($sku);
  115. return $result;
  116. }
  117. /**
  118. * 修改shop sku
  119. */
  120. public static function mjSkuUpdate($sku){
  121. $sku["method"] = "maijiayun.eshop.sku.update";
  122. $result = self::mjApi($sku);
  123. return $result;
  124. }
  125. /**
  126. * 获取 sku 库存
  127. */
  128. public static function mjWarehouseSkuGet($sku){
  129. $sku["method"] = "maijiayun.warehouse.sku.list";
  130. $result = self::mjApi($sku);
  131. return $result;
  132. }
  133. public static function mjApi($params = array()){
  134. $accessKey = "B1E69297B5DA44DAB35099A5F28F41D9"; //erp 生成的 accessKey
  135. $accessSecret = "4sN2LbylhOglelMP";
  136. $params["timestamp"] = time()."000";
  137. $params["version"] = "v1";
  138. $reqparams = $params;
  139. ksort($reqparams);
  140. $sign = "";
  141. foreach($reqparams as $k=>$val){
  142. if(is_array($val)){
  143. $val = json_encode($val,320);
  144. }
  145. $sign.=$k.$val;
  146. }
  147. $params["accessKey"] = $accessKey;
  148. $params["token"] = strtoupper(sha1($accessKey.$sign.$accessSecret));
  149. $param = json_encode($params);
  150. $ch = curl_init();
  151. $headers = array("Content-type:application/json;charset='utf-8'","Accept:application/json", "Cache-Control: no-cache", "Pragma: no-cache");
  152. curl_setopt($ch, CURLOPT_URL, "https://api.erp.maijiayun.cn"); //api 地址
  153. curl_setopt($ch, CURLOPT_POST, 1);
  154. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  155. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  156. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  157. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  158. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  159. $response = curl_exec($ch);
  160. curl_close($ch);
  161. $response = json_decode($response, true);
  162. if (!$response || !is_array($response)) {
  163. return false;
  164. }
  165. if (!array_key_exists("isOk", $response)) {
  166. return false;
  167. } elseif( $response['isOk'] == false){
  168. Log::errorLog($params,['mjApi_response'=>$response],"order/mjApi",0,'');
  169. return false;
  170. } else {
  171. return $response;
  172. }
  173. }
  174. # 生成外部订单号
  175. public static function createOuterCode(){
  176. $order_sn = rand(100, 9999). substr(time(), 5, 5). rand(10, 9999);
  177. return $order_sn;
  178. }
  179. /**
  180. * 导出excel
  181. * @param $data
  182. * @param string
  183. */
  184. public static function export_excel($data, $filename = '未命名.xlsx', $indexKey, $title) {
  185. if( !is_array($indexKey)) return false;
  186. $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');
  187. //初始化PHPExcel()
  188. $objPHPExcel = new PHPExcel();
  189. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  190. //接下来就是写数据到表格里面去
  191. $objActSheet = $objPHPExcel->getActiveSheet();
  192. foreach($title as $k=>$item){
  193. $objActSheet->setCellValue($header_arr[$k].'1',$item);
  194. }
  195. $startRow = 2;
  196. foreach ($data as $row) {
  197. foreach ($indexKey as $key => $value){
  198. //这里是设置单元格的内容
  199. $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);
  200. }
  201. $startRow++;
  202. }
  203. header("Pragma: public");
  204. header("Expires: 0");
  205. header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
  206. header("Content-Type:application/force-download");
  207. header("Content-Type:application/vnd.ms-execl");
  208. header("Content-Type:application/octet-stream");
  209. header("Content-Type:application/download");;
  210. header('Content-Disposition:attachment;filename='.$filename.'');
  211. header("Content-Transfer-Encoding:binary");
  212. $objWriter->save('php://output');
  213. exit();
  214. }
  215. }