説明なし

Order.php 7.1KB

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