Nessuna descrizione

Order.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. * 更新erp商品
  94. */
  95. public static function mjErpGoodsUpdate($goods){
  96. $goods["method"] = "maijiayun.goods.update";
  97. $result = self::mjApi($goods);
  98. return $result;
  99. }
  100. /**
  101. * 批量添加sku
  102. */
  103. public static function mjBatchSkuAdd($sku){
  104. $sku["method"] = "maijiayun.eshop.sku.batchAdd"; //批量添加
  105. //$sku["method"] = "maijiayun.eshop.sku.add";
  106. $result = self::mjApi($sku);
  107. return $result;
  108. }
  109. /**
  110. * 添加单个sku
  111. */
  112. public static function mjSkuAdd($sku){
  113. $sku["method"] = "maijiayun.eshop.sku.add";
  114. $result = self::mjApi($sku);
  115. return $result;
  116. }
  117. /**
  118. * 修改Erp sku
  119. */
  120. public static function mjErpSkuUpdate($sku){
  121. $sku["method"] = "maijiayun.goods.sku.update";
  122. $result = self::mjApi($sku);
  123. return $result;
  124. }
  125. /**
  126. * 修改shop sku
  127. */
  128. public static function mjSkuUpdate($sku){
  129. $sku["method"] = "maijiayun.eshop.sku.update";
  130. $result = self::mjApi($sku);
  131. return $result;
  132. }
  133. /**
  134. * 获取 sku 库存
  135. */
  136. public static function mjWarehouseSkuGet($sku){
  137. $sku["method"] = "maijiayun.warehouse.sku.list";
  138. $result = self::mjApi($sku);
  139. return $result;
  140. }
  141. public static function mjApi($params = array()){
  142. $accessKey = "B1E69297B5DA44DAB35099A5F28F41D9"; //erp 生成的 accessKey
  143. $accessSecret = "4sN2LbylhOglelMP";
  144. //$accessKey = "F82A757089D84F89B1782EDC3A4DA82A"; //erp 生成的 accessKey
  145. //$accessSecret = "wB2ueW8eQOHXNWcN";
  146. $params["timestamp"] = time()."000";
  147. $params["version"] = "v1";
  148. $reqparams = $params;
  149. ksort($reqparams);
  150. $sign = "";
  151. foreach($reqparams as $k=>$val){
  152. if(is_array($val)){
  153. $val = json_encode($val,320);
  154. }
  155. $sign.=$k.$val;
  156. }
  157. $params["accessKey"] = $accessKey;
  158. $params["token"] = strtoupper(sha1($accessKey.$sign.$accessSecret));
  159. $param = json_encode($params);
  160. $ch = curl_init();
  161. $headers = array("Content-type:application/json;charset='utf-8'","Accept:application/json", "Cache-Control: no-cache", "Pragma: no-cache");
  162. curl_setopt($ch, CURLOPT_URL, "https://api.erp.maijiayun.cn"); //api 地址
  163. curl_setopt($ch, CURLOPT_POST, 1);
  164. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  165. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  166. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  167. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  168. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  169. $response = curl_exec($ch);
  170. curl_close($ch);
  171. $response = json_decode($response, true);
  172. if (!$response || !is_array($response)) {
  173. return false;
  174. }
  175. if (!array_key_exists("isOk", $response)) {
  176. return false;
  177. } elseif( $response['isOk'] == false){
  178. Log::errorLog($params,['mjApi_response'=>$response],"order/mjApi",0,'');
  179. return false;
  180. } else {
  181. return $response;
  182. }
  183. }
  184. # 生成外部订单号
  185. public static function createOuterCode(){
  186. $order_sn = rand(100, 9999). substr(time(), 5, 5). rand(10, 9999);
  187. return $order_sn;
  188. }
  189. /**
  190. * 导出excel
  191. * @param $data
  192. * @param string
  193. */
  194. public static function export_excel($data, $filename = '未命名.xlsx', $indexKey, $title) {
  195. if( !is_array($indexKey)) return false;
  196. $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');
  197. //初始化PHPExcel()
  198. $objPHPExcel = new PHPExcel();
  199. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  200. //接下来就是写数据到表格里面去
  201. $objActSheet = $objPHPExcel->getActiveSheet();
  202. foreach($title as $k=>$item){
  203. $objActSheet->setCellValue($header_arr[$k].'1',$item);
  204. }
  205. $startRow = 2;
  206. foreach ($data as $row) {
  207. foreach ($indexKey as $key => $value){
  208. //这里是设置单元格的内容
  209. $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);
  210. }
  211. $startRow++;
  212. }
  213. header("Pragma: public");
  214. header("Expires: 0");
  215. header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
  216. header("Content-Type:application/force-download");
  217. header("Content-Type:application/vnd.ms-execl");
  218. header("Content-Type:application/octet-stream");
  219. header("Content-Type:application/download");;
  220. header('Content-Disposition:attachment;filename='.$filename.'');
  221. header("Content-Transfer-Encoding:binary");
  222. $objWriter->save('php://output');
  223. exit();
  224. }
  225. }