説明なし

OrderScript.php 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 OrderScript extends Model
  13. {
  14. public $timestamps = false;
  15. /**
  16. * 添加订单
  17. */
  18. public static function mjOrderAdd($orderList){
  19. $orderList["method"] = "maijiayun.order.add";
  20. //status格式
  21. $staus_arr = array(
  22. 0 => '待付款',
  23. 1 => '已付款待审核',
  24. 2 => '已审核待发货',
  25. 3 => '已发货',
  26. );
  27. $orderList['status'] = $staus_arr[$orderList['status']];
  28. $result = self::mjApi($orderList);
  29. return $result;
  30. }
  31. /**
  32. * 更新订单
  33. */
  34. public static function mjOrderUpdate($orderList){
  35. $orderList["method"] = "maijiayun.order.update";
  36. $staus_arr = array(
  37. 0 => '待付款',
  38. 1 => '已付款待审核',
  39. 2 => '已审核待发货',
  40. 3 => '已发货',
  41. 4 => '已取消',
  42. );
  43. $orderList['status'] = $staus_arr[$orderList['status']];
  44. $result = self::mjApi($orderList);
  45. return $result;
  46. }
  47. /**
  48. * 取消订单
  49. */
  50. public static function mjOrderDel($orderList){
  51. $orderList["method"] = "maijiayun.order.update";
  52. $orderList['status'] = '已取消';
  53. $result = self::mjApi($orderList);
  54. return $result;
  55. }
  56. /**
  57. * 获取订单
  58. */
  59. public static function mjOrderGet($order){
  60. $order["method"] = "maijiayun.order.get";
  61. $result = self::mjApi($order);
  62. return $result;
  63. }
  64. /**
  65. * 添加Erp商品
  66. */
  67. public static function mjErpGoodsAdd($goods){
  68. $goods["method"] = "maijiayun.goods.sku.add";
  69. $result = self::mjApi($goods);
  70. return $result;
  71. }
  72. /**
  73. * 添加商品
  74. */
  75. public static function mjGoodsAdd($goods){
  76. $goods["method"] = "maijiayun.eshop.goods.add";
  77. $result = self::mjApi($goods);
  78. return $result;
  79. }
  80. /**
  81. * 更新商品
  82. */
  83. public static function mjGoodsUpdate($goods){
  84. $goods["method"] = "maijiayun.eshop.goods.update";
  85. $result = self::mjApi($goods);
  86. return $result;
  87. }
  88. /**
  89. * 更新erp商品
  90. */
  91. public static function mjErpGoodsUpdate($goods){
  92. $goods["method"] = "maijiayun.goods.update";
  93. $result = self::mjApi($goods);
  94. return $result;
  95. }
  96. /**
  97. * 批量添加sku
  98. */
  99. public static function mjBatchSkuAdd($sku){
  100. $sku["method"] = "maijiayun.eshop.sku.batchAdd"; //批量添加
  101. //$sku["method"] = "maijiayun.eshop.sku.add";
  102. $result = self::mjApi($sku);
  103. return $result;
  104. }
  105. /**
  106. * 添加单个sku
  107. */
  108. public static function mjSkuAdd($sku){
  109. $sku["method"] = "maijiayun.eshop.sku.add";
  110. $result = self::mjApi($sku);
  111. return $result;
  112. }
  113. /**
  114. * 修改Erp sku
  115. */
  116. public static function mjErpSkuUpdate($sku){
  117. $sku["method"] = "maijiayun.goods.sku.update";
  118. $result = self::mjApi($sku);
  119. return $result;
  120. }
  121. /**
  122. * 修改shop sku
  123. */
  124. public static function mjSkuUpdate($sku){
  125. $sku["method"] = "maijiayun.eshop.sku.update";
  126. $result = self::mjApi($sku);
  127. return $result;
  128. }
  129. /**
  130. * 获取 sku 库存
  131. */
  132. public static function mjWarehouseSkuGet($sku){
  133. $sku["method"] = "maijiayun.warehouse.sku.list";
  134. $result = self::mjApi($sku);
  135. return $result;
  136. }
  137. public static function mjApi($params = array()){
  138. $accessKey = env('ERP_ACCESS_KEY');
  139. $accessSecret = env('ERP_ACCESS_SECRET');
  140. $params["timestamp"] = time()."000";
  141. $params["version"] = "v1";
  142. $reqparams = $params;
  143. ksort($reqparams);
  144. $sign = "";
  145. foreach($reqparams as $k=>$val){
  146. if(is_array($val)){
  147. $val = json_encode($val,320);
  148. }
  149. $sign.=$k.$val;
  150. }
  151. $params["accessKey"] = $accessKey;
  152. $params["token"] = strtoupper(sha1($accessKey.$sign.$accessSecret));
  153. $param = json_encode($params);
  154. $ch = curl_init();
  155. $headers = array("Content-type:application/json;charset='utf-8'","Accept:application/json", "Cache-Control: no-cache", "Pragma: no-cache");
  156. curl_setopt($ch, CURLOPT_URL, "http://w8t1clwezn5y.cn-north-1.jdcloud-api.net/open/openapi"); //api 地址
  157. curl_setopt($ch, CURLOPT_POST, 1);
  158. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  159. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  160. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  161. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  162. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  163. $response = curl_exec($ch);
  164. curl_close($ch);
  165. $response = json_decode($response, true);
  166. Log::scriptLog($params,['mjApi_response'=>$response],"order/mjApi",1,'');
  167. if (!$response || !is_array($response)) {
  168. return false;
  169. }
  170. if (!array_key_exists("isOk", $response)) {
  171. return false;
  172. } elseif( $response['isOk'] == false){
  173. Log::scriptLog($params,['mjApi_response'=>$response],"order/mjApi",0,'');
  174. return false;
  175. } else {
  176. return $response;
  177. }
  178. }
  179. /*更新城市天气信息*/
  180. public static function updateWeather($order_id) {
  181. $redisKey = 'seafood_order_city_weather';
  182. //查询订单收件人所在的城市(过滤关键字 市)
  183. $city = Order::where('id', $order_id)->pluck('receiverCity');
  184. $city = str_replace('市','',$city);
  185. $specialCity = array('朝阳区','嘉定区','海淀区','昌平区','密云区','大兴区','房山区','通州区','普陀区',);
  186. if(in_array($city, $specialCity)) {
  187. $city = str_replace('区','',$city);
  188. }
  189. //查询redis中是否有该键,若没有则查询
  190. $weather = RedisModel::get($redisKey.'_'.$city);
  191. if($weather) {
  192. $sellerMemo = Order::where('id', $order_id)->pluck('sellerMemo');
  193. if(strstr($sellerMemo, $city.'天气状况:'.$weather)) {
  194. return true;
  195. }
  196. $sellerMemo = $sellerMemo.' '.$city.'天气状况:'.$weather;
  197. Log::scriptLog(['order_id'=>$order_id, 'city'=> $city , 'app_key'=>''],['weather'=>$weather],"order/weather",1,'');
  198. Order::where('id', $order_id)->update(['sellerMemo'=>$sellerMemo]);
  199. } else {
  200. $appKey = self::getAppKey();
  201. $url = 'http://apis.juhe.cn/simpleWeather/query?city='.urlencode($city).'&key='.$appKey;
  202. $result = file_get_contents($url);
  203. $result = json_decode($result, true);
  204. if($result['error_code'] == '0') {
  205. //将数据缓存到redis里
  206. $today = date('Y-m-d',time());
  207. $tomorrow = date('Y-m-d',strtotime('+1 days'));
  208. $weather = '';
  209. foreach ($result['result']['future'] as $value) {
  210. if($value['date'] == $today) {
  211. $weather .= $today.'日气温:'.$value['temperature'].'; ';
  212. }
  213. if($value['date'] == $tomorrow) {
  214. $weather .= $tomorrow.'日气温:'.$value['temperature'];
  215. }
  216. }
  217. RedisModel::set($redisKey.'_'.$city, $weather);
  218. $endTime = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days'))) -1;
  219. $expire = $endTime - time();
  220. RedisModel::expire($redisKey.'_'.$city, $expire);
  221. $sellerMemo = Order::where('id', $order_id)->pluck('sellerMemo');
  222. if(strstr($sellerMemo, $city.'天气状况:'.$weather)) {
  223. return true;
  224. }
  225. $sellerMemo = $sellerMemo.' '.$city.'天气状况:'.$weather;
  226. Log::scriptLog(['order_id'=>$order_id, 'city'=> $city , 'app_key'=>$appKey],['weather'=>$weather],"order/weather",1,'');
  227. Order::where('id', $order_id)->update(['sellerMemo'=>$sellerMemo]);
  228. } else if($result['error_code'] == '10012'){
  229. //判断是否为第二个APPkey也用完了
  230. $key = (string) RedisModel::get('seafood_order_city_weather_key');
  231. if($key == '0') {
  232. //超过100次/天上限 更换APPKey
  233. RedisModel::set('seafood_order_city_weather_key', '1');
  234. $endTime = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days'))) -1;
  235. $expire = $endTime - time();
  236. RedisModel::expire('seafood_order_city_weather_key', $expire);
  237. self::updateWeather($order_id);
  238. }
  239. }
  240. }
  241. return true;
  242. }
  243. public static function getAppKey() {
  244. $redisKey = 'seafood_order_city_weather_key';
  245. $appKeyArr = ['32c8f844f8055f70f702fc28ec930e52', '85bfc19adb25998b2559d7ceba950e13'];
  246. $appKey = (string)RedisModel::get($redisKey);
  247. if($appKey != '') {
  248. return $appKeyArr[$appKey];
  249. } else {
  250. RedisModel::set($redisKey, '0');
  251. $endTime = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days'))) -1;
  252. $expire = $endTime - time();
  253. RedisModel::expire($redisKey, $expire);
  254. return $appKeyArr['0'];
  255. }
  256. }
  257. }