WastoreForm.php 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\modules\admin\models\WechatThirdForm;
  9. use app\utils\Wechat\WechatMini;
  10. use EasyWeChat\Kernel\BaseClient;
  11. use app\models\Order;
  12. use app\models\User;
  13. use app\models\Goods;
  14. use app\models\MdGoods;
  15. use app\models\Express;
  16. use app\models\DeliveryInfo;
  17. use app\models\DeliveryMaiyatian;
  18. use app\models\DeliveryKeloop;
  19. use app\modules\admin\models\maiyatian\MaiyatianForm;
  20. use app\modules\admin\models\keloop\KeloopForm;
  21. use app\models\Option;
  22. use app\constants\OptionSetting;
  23. use EasyWeChat\Factory;
  24. class WastoreForm extends WechatThirdForm
  25. {
  26. public static $test = 0;
  27. public static $confs = [];
  28. public static function conf($store_id = 0, $refresh = 0) {
  29. $cacheK = $store_id;
  30. if(isset(self::$confs[$cacheK]) && !$refresh){
  31. return self::$confs[$cacheK];
  32. }
  33. $conf = json_decode(Option::get(OptionSetting::WASTORE, $store_id, 'store', '{}')['value'], true);
  34. if(!$conf){
  35. $conf = [
  36. 'is_open' => 0,
  37. ];
  38. }
  39. self::$confs[$cacheK] = $conf;
  40. return $conf;
  41. }
  42. public static function saveConf($store_id = 0, $config = []) {
  43. $oldConf = json_decode(Option::get(OptionSetting::WASTORE, $store_id, 'store', '{}')['value'], true);
  44. $conf = array_merge($oldConf, $config);
  45. $set = Option::set(OptionSetting::WASTORE, json_encode($conf), $store_id, 'store');
  46. self::conf($store_id, 1);
  47. return $set;
  48. }
  49. public static function getConf($store_id = 0, $key = '') {
  50. $config = self::conf($store_id);
  51. return $config[$key];
  52. }
  53. public static function pluginIsOpen($store_id = 0) {
  54. $is_open = self::getConf($store_id, 'is_open');
  55. return $is_open;
  56. }
  57. public function testApp(&$mini_id = 0) {
  58. $config =[];
  59. $config['app_id'] = 'wx1ce41243db1e4373';
  60. $config['secret'] = '6563aceb2ef97e0d2f5b884f11e43544';
  61. $config['response_type'] = 'array';
  62. $app = Factory::miniProgram($config);
  63. $mini_id = 88;
  64. return $app;
  65. }
  66. public function wastoreApi($url = '', $store_id = 0, $mini_id = 0, $postData = [], $call = null) {
  67. try {
  68. try {
  69. $app = WechatMini::getWechatConfig($store_id, $mini_id);
  70. $mini_id = WechatMini::$mini_id;
  71. } catch (\Throwable $e) {
  72. \Yii::error([__METHOD__, $e]);
  73. }
  74. WastoreForm::$test && $app = $this->testApp($mini_id);
  75. if (!$app) {
  76. throw new \Exception('小程序配置获取失败');
  77. }
  78. $call && $postData = $call($mini_id);
  79. $client = new BaseClient($app);
  80. $result = $client->httpPostJson($url, $postData);
  81. debug_log([$url, $store_id, $mini_id, $result, $postData], __CLASS__ . '.log');
  82. if (!empty($result)) {
  83. if ($result['errcode'] == '0') {
  84. return [
  85. 'code' => 0,
  86. 'msg' => "操作成功",
  87. 'data' => $result,
  88. 'postData' => $postData,
  89. 'url' => $url,
  90. 'mini_id' => $mini_id,
  91. ];
  92. }
  93. }
  94. \Yii::error([__METHOD__, $url, $mini_id, $result, $postData]);
  95. return [
  96. 'code' => 1,
  97. 'msg' => "操作失败。" . $result['errmsg'],
  98. 'data' => $result,
  99. 'postData' => $postData,
  100. 'url' => $url,
  101. 'mini_id' => $mini_id,
  102. ];
  103. } catch (\Exception $e) {
  104. \Yii::error([__METHOD__, $e, $postData, $url, $mini_id]);
  105. return [
  106. 'code' => 1,
  107. 'msg' => "操作失败," . $e->getMessage(),
  108. 'postData' => $postData,
  109. 'url' => $url,
  110. 'mini_id' => $mini_id,
  111. ];
  112. }
  113. }
  114. //
  115. public function rid($store_id = 0, $mini_id = 0, $rid = null) {
  116. $postData = ['rid' => $rid];
  117. return $this->wastoreApi('cgi-bin/openapi/rid/get', $store_id, $mini_id, $postData);
  118. }
  119. //门店快送 上传图片
  120. public function wastoreImgUpload($store_id = 0, $mini_id = 0, $img_url = null, $media = null) {
  121. try {
  122. $app = WechatMini::getWechatConfig($store_id, $mini_id);
  123. $mini_id = WechatMini::$mini_id;
  124. } catch (\Throwable $e) {
  125. \Yii::error([__METHOD__, $e]);
  126. }
  127. WastoreForm::$test && $app = $this->testApp($mini_id);
  128. if (!$app) {
  129. throw new \Exception('小程序配置获取失败');
  130. }
  131. $upload_type = $img_url ? 1 : 0;
  132. $postData = [
  133. 'resp_type' => 1,
  134. 'upload_type' => $upload_type,
  135. ];
  136. $img_url && $postData['img_url'] = $img_url;
  137. $media && $postData['media'] = $media;
  138. $client = new BaseClient($app);
  139. // $result = $client->imgUpload($img_url ?: $media, 1, $upload_type);
  140. $result = $client->httpPostJson('shop/img/upload', $postData, $postData);
  141. if (!empty($result)) {
  142. if ($result['errcode'] == '0') {
  143. return $result['img_info']['temp_img_url'];
  144. }
  145. }
  146. throw new \Exception('上传图片失败。' . $result['errcode'] . $result['errmsg'] . json_encode($postData, JSON_UNESCAPED_UNICODE));
  147. }
  148. //门店快送查询开通状态
  149. public function wastoreSceneIsOpen($store_id = 0, $mini_id = 0) {
  150. $postData = [];
  151. return $this->wastoreApi('shop/scene/list', $store_id, $mini_id, $postData, function() use ($postData){
  152. $postData = [
  153. 'group_id_list' => [9],
  154. ];
  155. return $postData;
  156. });
  157. }
  158. //门店快送开通
  159. public function wastoreSceneOpen($store_id = 0, $mini_id = 0) {
  160. $postData = [];
  161. return $this->wastoreApi('shop/scene/enable', $store_id, $mini_id, $postData, function() use ($postData){
  162. $postData = [
  163. 'group_id_list' => [9],
  164. ];
  165. return $postData;
  166. });
  167. }
  168. //门店的服务类目
  169. public function wastoreStoreCategoryGet($store_id = 0, $mini_id = 0) {
  170. $postData = [];
  171. return $this->wastoreApi('shop/store/category/get', $store_id, $mini_id, $postData);
  172. }
  173. //门店快送新建更新门店
  174. public function wastoreSave($store_id = 0, $mini_id = 0, $md_id = 0, $params = [], $out_store_id = '') {
  175. $postData = $params['postData'];
  176. $postData['store_info']['path'] = $postData['store_info']['path'] ?: '/pages/cat/cat';
  177. $postData['store_info']['cat_id'] = $postData['store_info']['cat_id'] ?: 32;
  178. $call = function($mini_id) use ($postData, $store_id, $md_id, $out_store_id){
  179. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  180. $out_store_id && $postData['out_store_id'] = $out_store_id;
  181. if (!empty($postData['store_info']['image'])) {
  182. $postData['store_info']['image'] = $this->wastoreImgUpload($store_id, $mini_id, $postData['store_info']['image']);
  183. }else{
  184. $postData['store_info']['image'] = null;
  185. }
  186. if (!empty($postData['store_info']['pics'])) {
  187. foreach ($postData['store_info']['pics'] as &$item) {
  188. $item = $this->wastoreImgUpload($store_id, $mini_id, $item);
  189. }
  190. }
  191. if (!empty($postData['store_info']['certificate'])) {
  192. foreach ($postData['store_info']['certificate'] as &$item) {
  193. $item = $this->wastoreImgUpload($store_id, $mini_id, $item);
  194. }
  195. }
  196. return $postData;
  197. };
  198. if ($out_store_id) {
  199. $url = 'shop/store/update';
  200. } else {
  201. $url = 'shop/store/add';
  202. }
  203. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  204. }
  205. //门店快送删除门店
  206. public function wastoreDel($store_id = 0, $mini_id = 0, $md_id = 0, $out_store_id = '') {
  207. $postData = [];
  208. $call = function($mini_id) use ($postData, $store_id, $md_id, $out_store_id){
  209. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  210. $out_store_id && $postData['out_store_id'] = $out_store_id;
  211. return $postData;
  212. };
  213. $url = 'shop/store/delete';
  214. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  215. }
  216. //门店快送获取门店最新提交信息
  217. public function wastoreAuditGet($store_id = 0, $mini_id = 0, $md_id = 0, $out_store_id = '') {
  218. $postData = [];
  219. $call = function($mini_id) use ($postData, $store_id, $md_id, $out_store_id){
  220. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  221. $out_store_id && $postData['out_store_id'] = $out_store_id;
  222. return $postData;
  223. };
  224. $url = 'shop/store/audit/get';
  225. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  226. }
  227. //门店快送获取门店最新过审信息
  228. public function wastoreGet($store_id = 0, $mini_id = 0, $md_id = 0, $out_store_id = '') {
  229. $postData = [];
  230. $call = function($mini_id) use ($postData, $store_id, $md_id, $out_store_id){
  231. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  232. $out_store_id && $postData['out_store_id'] = $out_store_id;
  233. return $postData;
  234. };
  235. $url = 'shop/store/get';
  236. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  237. }
  238. //门店快送列出门店最新提交信息
  239. public function wastoreAuditList($store_id = 0, $mini_id = 0, $page = 1, $pageSize = 100) {
  240. $postData = [];
  241. $offset = ($page - 1) * $pageSize;
  242. $postData['offset'] = (int)$offset;
  243. $postData['limit'] = (int)$pageSize;
  244. $url = 'shop/store/audit/list';
  245. return $this->wastoreApi($url, $store_id, $mini_id, $postData);
  246. }
  247. //门店快送列出门店最新过审信息
  248. public function wastoreList($store_id = 0, $mini_id = 0, $page = 1, $pageSize = 100) {
  249. $postData = [];
  250. $offset = ($page - 1) * $pageSize;
  251. $postData['offset'] = (int)$offset;
  252. $postData['limit'] = (int)$pageSize;
  253. $url = 'shop/store/list';
  254. return $this->wastoreApi($url, $store_id, $mini_id, $postData);
  255. }
  256. //门店快送门店设置为休息状态
  257. public function wastoreClose($store_id = 0, $mini_id = 0, $md_id = 0, $close_time = null, $out_store_id = '') {
  258. $postData = [];
  259. $close_time && $postData['close_time'] = $close_time;
  260. $call = function($mini_id) use ($postData, $store_id, $md_id, $out_store_id){
  261. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  262. $out_store_id && $postData['out_store_id'] = $out_store_id;
  263. return $postData;
  264. };
  265. $url = 'shop/store/close';
  266. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  267. }
  268. //门店快送门店设置为营业状态
  269. public function wastoreOpen($store_id = 0, $mini_id = 0, $md_id = 0, $open_time = null, $out_store_id = '') {
  270. $postData = [];
  271. $open_time && $postData['open_time'] = $open_time;
  272. $call = function($mini_id) use ($postData, $store_id, $md_id, $out_store_id){
  273. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  274. $out_store_id && $postData['out_store_id'] = $out_store_id;
  275. return $postData;
  276. };
  277. $url = 'shop/store/open';
  278. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  279. }
  280. //门店快送门店设置为“不展示”
  281. public function wastoreHide($store_id = 0, $mini_id = 0, $md_id = 0, $out_store_id = '') {
  282. $postData = [];
  283. $call = function($mini_id) use ($postData, $store_id, $md_id, $out_store_id){
  284. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  285. $out_store_id && $postData['out_store_id'] = $out_store_id;
  286. return $postData;
  287. };
  288. $url = 'shop/store/hide';
  289. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  290. }
  291. //门店快送门店设置为“展示”
  292. public function wastoreShow($store_id = 0, $mini_id = 0, $md_id = 0, $out_store_id = '') {
  293. $postData = [];
  294. $call = function($mini_id) use ($postData, $store_id, $md_id, $out_store_id){
  295. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  296. $out_store_id && $postData['out_store_id'] = $out_store_id;
  297. return $postData;
  298. };
  299. $url = 'shop/store/show';
  300. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  301. }
  302. //门店快送添加修改门店配送设置
  303. public function wastoreDeliverySave($store_id = 0, $mini_id = 0, $md_id = 0, $params = [], $out_store_id = '') {
  304. $postData = $params['postData'];
  305. $postData['delivery']['intracity_type'] = 1;
  306. $call = function($mini_id) use ($postData, $store_id, $md_id, $out_store_id){
  307. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  308. $out_store_id && $postData['out_store_id'] = $out_store_id;
  309. return $postData;
  310. };
  311. $url = 'shop/store/delivery/update';
  312. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  313. }
  314. //门店快送获取门店配送设置
  315. public function wastoreDeliveryGet($store_id = 0, $mini_id = 0, $md_id = 0, $out_store_id = '') {
  316. $postData = [];
  317. $call = function($mini_id) use ($postData, $store_id, $md_id, $out_store_id){
  318. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  319. $out_store_id && $postData['out_store_id'] = $out_store_id;
  320. return $postData;
  321. };
  322. $url = 'shop/store/delivery/get';
  323. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  324. }
  325. //门店快送获取商品类目详情
  326. public function wastoreSpuCategory($store_id = 0, $mini_id = 0, $parent_id = null) {
  327. $postData = [];
  328. $parent_id && $postData['parent_id'] = $parent_id;
  329. $url = 'shop/store/spu/category';
  330. return $this->wastoreApi($url, $store_id, $mini_id, $postData);
  331. }
  332. //门店快送添加更新SPU
  333. public function wastoreSpuAdd($store_id = 0, $mini_id = 0, $goods_id = 0, $params = [], $out_product_id = null, $scene_group = 9) {
  334. $postData = [];
  335. if(!empty($postData['scene_group_list'])){
  336. if(is_array($postData['scene_group_list'])){
  337. $scene_group = (int)$postData['scene_group_list'][0];
  338. }else{
  339. $scene_group = (int)$postData['scene_group_list'];
  340. }
  341. }
  342. $call = function($mini_id) use ($postData, $store_id, $goods_id, $params, $out_product_id, $scene_group){
  343. $goods = \app\models\Goods::findOne($goods_id);
  344. $postData['title'] = mb_substr($goods['name'], 0, 60);
  345. $postData['path'] = '/goods/goods/goods?id=' . $goods_id;
  346. $postData['head_img'][] = $this->wastoreImgUpload($store_id, $mini_id, $goods['cover_pic']);
  347. // $pic_list = \app\models\GoodsPic::find()->select('pic_url')->where(['goods_id' => $goods_id, 'is_delete' => 0])->asArray()->all();
  348. // foreach($pic_list as $item){
  349. // $item['pic_url'] && $postData['head_img'][] = $this->wastoreImgUpload($store_id, $mini_id, $item['pic_url']);
  350. // }
  351. $postData['third_cat_id'] = $params['postData']['third_cat_id'] ?: 102;
  352. $postData['skus'] = [];
  353. $goods::skuAttr($goods);
  354. $attrs = json_decode($goods['attr'], true);
  355. foreach($attrs as $attr){
  356. $sku = [];
  357. $sku['out_sku_id'] = $attr['cyy_skuId'];
  358. // $attr['pic'] && $sku['thumb_img'] = $this->wastoreImgUpload($store_id, $mini_id, $attr['pic']);
  359. $sku['sale_price'] = $attr['price'] * 100;
  360. // $sku['sku_code'] = $attr['no'];
  361. foreach($attr['attr_list'] as $item){
  362. $attr_group = $goods->getAttrGroupByAttId($item['attr_id']);
  363. $attr_group_name = $attr_group ? $attr_group->attr_group_name : null;
  364. $sku['sku_attrs'][] = [
  365. 'attr_key' => $attr_group_name,
  366. 'attr_value' => $item['attr_name'],
  367. ];
  368. }
  369. $postData['skus'][] = $sku;
  370. }
  371. // $postData = $params['postData'];
  372. $postData['scene_group_list'] = [$scene_group];
  373. $postData['out_product_id'] = implode('_', [$store_id, $mini_id, $scene_group, $goods_id]);
  374. $out_product_id && $postData['out_product_id'] = $out_product_id;
  375. return $postData;
  376. };
  377. $url = 'shop/spu/add';
  378. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  379. }
  380. //门店快送删除SPU
  381. public function wastoreSpuDel($store_id = 0, $mini_id = 0, $goods_id = 0, $out_product_id = null, $scene_group = 9) {
  382. $postData = [];
  383. $call = function($mini_id) use ($postData, $store_id, $goods_id, $out_product_id, $scene_group){
  384. $postData['scene_group_list'] = [$scene_group];
  385. $postData['out_product_id'] = implode('_', [$store_id, $mini_id, $scene_group, $goods_id]);
  386. $out_product_id && $postData['out_product_id'] = $out_product_id;
  387. return $postData;
  388. };
  389. $url = 'shop/spu/del';
  390. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  391. }
  392. //门店快送上架SPU
  393. public function wastoreSpuListing($store_id = 0, $mini_id = 0, $goods_id = 0, $out_product_id = null, $scene_group = 9) {
  394. $postData = [];
  395. $call = function($mini_id) use ($postData, $store_id, $goods_id, $out_product_id, $scene_group){
  396. $postData['scene_group_list'] = [$scene_group];
  397. $postData['out_product_id'] = implode('_', [$store_id, $mini_id, $scene_group, $goods_id]);
  398. $out_product_id && $postData['out_product_id'] = $out_product_id;
  399. return $postData;
  400. };
  401. $url = 'shop/spu/listing';
  402. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  403. }
  404. //门店快送下架SPU
  405. public function wastoreSpuDelisting($store_id = 0, $mini_id = 0, $goods_id = 0, $out_product_id = null, $scene_group = 9) {
  406. $postData = [];
  407. $call = function($mini_id) use ($postData, $store_id, $goods_id, $out_product_id, $scene_group){
  408. $postData['scene_group_list'] = [$scene_group];
  409. $postData['out_product_id'] = implode('_', [$store_id, $mini_id, $scene_group, $goods_id]);
  410. $out_product_id && $postData['out_product_id'] = $out_product_id;
  411. return $postData;
  412. };
  413. $url = 'shop/spu/delisting';
  414. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  415. }
  416. //门店快送获取SPU列表
  417. public function wastoreSpuGetList($store_id = 0, $mini_id = 0, $need_edit_spu = 1, $status = -1, $page = 1, $page_size = 100, $scene_group = 9) {
  418. $postData = ['scene_group' => $scene_group];
  419. $postData['need_edit_spu'] = $need_edit_spu;
  420. (isset($status) && $status > -1) && $postData['status'] = $status;
  421. $postData['page'] = $page;
  422. $postData['page_size'] = $page_size;
  423. $url = 'shop/spu/get_list';
  424. return $this->wastoreApi($url, $store_id, $mini_id, $postData);
  425. }
  426. //门店快送获取SPU详情
  427. public function wastoreSpuGet($store_id = 0, $mini_id = 0, $need_edit_spu = 1, $goods_id = 0, $out_product_id = null, $scene_group = 9) {
  428. $postData = ['scene_group' => $scene_group];
  429. $postData['need_edit_spu'] = $need_edit_spu;
  430. $call = function($mini_id) use ($postData, $store_id, $goods_id, $out_product_id, $scene_group){
  431. $postData['out_product_id'] = implode('_', [$store_id, $mini_id, $scene_group, $goods_id]);
  432. $out_product_id && $postData['out_product_id'] = $out_product_id;
  433. return $postData;
  434. };
  435. $url = 'shop/spu/get';
  436. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  437. }
  438. // //门店快送添加门店商品
  439. // public function wastoreStoreSpuAdd($store_id = 0, $mini_id = 0, $md_id = 0, $goods_id = 0, $out_product_id = null, $scene_group = 9) {
  440. // $postData = [];
  441. // $call = function($mini_id) use ($postData, $store_id, $md_id, $goods_id, $out_product_id, $scene_group){
  442. // $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  443. // $postData['out_spu_id'] = implode('_', [$store_id, $mini_id, $scene_group, $goods_id]);
  444. // $out_product_id && $postData['out_spu_id_list'][] = $out_product_id;
  445. // return $postData;
  446. // };
  447. // $url = 'shop/store/spu/add';
  448. // return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  449. // }
  450. //门店快送批量添加门店商品
  451. public function wastoreStoreSpuBatchAdd($store_id = 0, $mini_id = 0, $md_id = 0, $goods_ids = [], $out_product_id = null, $scene_group = 9) {
  452. $postData = [];
  453. (!is_array($goods_ids) && $goods_ids) && $goods_ids = explode(',', $goods_ids);
  454. $call = function($mini_id) use ($postData, $store_id, $md_id, $goods_ids, $out_product_id, $scene_group){
  455. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  456. foreach($goods_ids as $goods_id){
  457. $postData['out_spu_id_list'][] = implode('_', [$store_id, $mini_id, $scene_group, $goods_id]);
  458. }
  459. $out_product_id && $postData['out_spu_id_list'][] = $out_product_id;
  460. return $postData;
  461. };
  462. $url = 'shop/store/spu/batch/add';
  463. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  464. }
  465. // //门店快送移除门店商品
  466. // public function wastoreStoreSpuDel($store_id = 0, $mini_id = 0, $md_id = 0, $goods_ids = [], $out_product_id = null, $scene_group = 9) {
  467. // $postData = [];
  468. // $call = function($mini_id) use ($postData, $store_id, $md_id, $goods_ids, $out_product_id, $scene_group){
  469. // $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  470. // $postData['out_spu_id'] = implode('_', [$store_id, $mini_id, $scene_group, $goods_id]);
  471. // $out_product_id && $postData['out_spu_id_list'][] = $out_product_id;
  472. // return $postData;
  473. // };
  474. // $url = 'shop/store/spu/delete';
  475. // return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  476. // }
  477. //门店快送批量移除门店商品
  478. public function wastoreStoreSpuBatchDel($store_id = 0, $mini_id = 0, $md_id = 0, $goods_ids = [], $out_product_id = null, $scene_group = 9) {
  479. $postData = [];
  480. (!is_array($goods_ids) && $goods_ids) && $goods_ids = explode(',', $goods_ids);
  481. $call = function($mini_id) use ($postData, $store_id, $md_id, $goods_ids, $out_product_id, $scene_group){
  482. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  483. foreach($goods_ids as $goods_id){
  484. $postData['out_spu_id_list'][] = implode('_', [$store_id, $mini_id, $scene_group, $goods_id]);
  485. }
  486. $out_product_id && $postData['out_spu_id_list'][] = $out_product_id;
  487. return $postData;
  488. };
  489. $url = 'shop/store/spu/batch/delete';
  490. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  491. }
  492. //门店快送查询门店内商品
  493. public function wastoreStoreSpuList($store_id = 0, $mini_id = 0, $md_id = 0) {
  494. $postData = [];
  495. $call = function($mini_id) use ($postData, $store_id, $md_id){
  496. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  497. return $postData;
  498. };
  499. $url = 'shop/store/spu/list';
  500. $res = $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  501. if(!$res['code'] && !$res['data']['errcode'] && $res['data']['total_num']){
  502. $goodsList = [];
  503. $goods_ids = [];
  504. foreach($res['data']['out_spu_id_list'] as $item){
  505. $idInfo = explode('_', $item);
  506. $gid = array_pop($idInfo);
  507. $goods_ids[$gid] = $item;
  508. }
  509. $statusList = $this->wastoreStoreSpuBatchGetStatus($store_id, $mini_id, $md_id, array_keys($goods_ids));
  510. if(!$statusList['code']){
  511. $status = array_combine(array_column($statusList['data']['spu_list'], 'out_spu_id'), $statusList['data']['spu_list']);
  512. }
  513. if($goods_ids){
  514. $goodsList = Goods::find()->where(['id' => array_keys($goods_ids)])->asArray()->all();
  515. foreach($goodsList as &$g){
  516. $g['out_spu_id'] = $goods_ids[$g['id']];
  517. $g['spu_status'] = 0;
  518. if($status){
  519. $g['spu_status'] = $status[$g['out_spu_id']]['status']['sale_status'];
  520. }
  521. }
  522. }
  523. $res['data']['goodsList'] = $goodsList;
  524. }
  525. return $res;
  526. }
  527. //门店快送批量上架门店商品
  528. public function wastoreStoreSpuBatchListing($store_id = 0, $mini_id = 0, $md_id = 0, $goods_ids = [], $out_product_id = null, $scene_group = 9) {
  529. $postData = [];
  530. $call = function($mini_id) use ($postData, $store_id, $md_id, $goods_ids, $out_product_id, $scene_group){
  531. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  532. (!is_array($goods_ids) && $goods_ids) && $goods_ids = explode(',', $goods_ids);
  533. foreach($goods_ids as $goods_id){
  534. $postData['spu_list'][]['out_spu_id'] = implode('_', [$store_id, $mini_id, $scene_group, $goods_id]);
  535. }
  536. $out_product_id && $postData['spu_list'][]['out_spu_id'] = $out_product_id;
  537. return $postData;
  538. };
  539. $url = 'shop/store/spu/batch/listing';
  540. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  541. }
  542. //门店快送批量下架门店商品
  543. public function wastoreStoreSpuBatchDelisting($store_id = 0, $mini_id = 0, $md_id = 0, $goods_ids = [], $out_product_id = null, $scene_group = 9) {
  544. $postData = [];
  545. $call = function($mini_id) use ($postData, $store_id, $md_id, $goods_ids, $out_product_id, $scene_group){
  546. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  547. (!is_array($goods_ids) && $goods_ids) && $goods_ids = explode(',', $goods_ids);
  548. foreach($goods_ids as $goods_id){
  549. $postData['out_spu_id_list'][] = implode('_', [$store_id, $mini_id, $scene_group, $goods_id]);
  550. }
  551. $out_product_id && $postData['out_spu_id_list'][] = $out_product_id;
  552. return $postData;
  553. };
  554. $url = 'shop/store/spu/batch/delisting';
  555. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  556. }
  557. //门店快送门店商品批量查询上下架状态
  558. public function wastoreStoreSpuBatchGetStatus($store_id = 0, $mini_id = 0, $md_id = 0, $goods_ids = [], $out_product_id = null, $scene_group = 9) {
  559. $postData = [];
  560. $call = function($mini_id) use ($postData, $store_id, $md_id, $goods_ids, $out_product_id, $scene_group){
  561. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  562. (!is_array($goods_ids) && $goods_ids) && $goods_ids = explode(',', $goods_ids);
  563. foreach($goods_ids as $goods_id){
  564. $postData['out_spu_id_list'][] = implode('_', [$store_id, $mini_id, $scene_group, $goods_id]);
  565. }
  566. $out_product_id && $postData['out_spu_id_list'][] = $out_product_id;
  567. return $postData;
  568. };
  569. $url = 'shop/store/spu/batch/get/status';
  570. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  571. }
  572. //门店快送批量更新门店价格
  573. public function storePriceBatchUpdate($store_id = 0, $mini_id = 0, $md_id = 0, $goods_ids = []) {
  574. $skuList = [];
  575. (!is_array($goods_ids) && $goods_ids) && $goods_ids = explode(',', $goods_ids);
  576. foreach($goods_ids as $goods_id){
  577. $goods = Goods::findOne($goods_id);
  578. if($md_id){
  579. $md_goods = MdGoods::findOne(['goods_id' => $goods_id, 'md_id' => $md_id]);
  580. if($md_goods){
  581. $goods = [
  582. 'id' => $goods_id,
  583. 'attr' => $md_goods->attr,
  584. ];
  585. }
  586. }
  587. Goods::skuAttr($goods);
  588. $attrs = json_decode($goods['attr'], true);
  589. foreach($attrs as $attr){
  590. $sku = [];
  591. $sku['out_sku_id'] = $attr['cyy_skuId'];
  592. $sku['price_info']['sale_price'] = $attr['price'] * 100;
  593. $skuList[] = $sku;
  594. }
  595. if($skuList){
  596. foreach(array_chunk($skuList, 100) as $list){
  597. $res = $this->wastoreStorePriceBatchUpdate($store_id, $mini_id, $md_id, $list);
  598. if($res['code']){
  599. return $res;
  600. }
  601. }
  602. }
  603. }
  604. return [
  605. 'code' => 0,
  606. 'msg' => 'ok',
  607. ];
  608. }
  609. //门店快送门店商品批量设置SKU价格
  610. public function wastoreStorePriceBatchUpdate($store_id = 0, $mini_id = 0, $md_id = 0, $sku_list = []) {
  611. $postData = [];
  612. $postData['sku_list'] = $sku_list;
  613. $call = function($mini_id) use ($postData, $store_id, $md_id){
  614. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  615. return $postData;
  616. };
  617. $url = 'shop/store/price/batch/update';
  618. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  619. }
  620. //门店快送门店商品批量获取SKU价格
  621. public function wastoreStorePriceBatchGet($store_id = 0, $mini_id = 0, $md_id = 0, $out_sku_id_list = []) {
  622. // $out_sku_id_list = ['cyy_22890_413_427'];
  623. $postData = [];
  624. (is_string($out_sku_id_list) && $out_sku_id_list) && $out_sku_id_list = explode(',', $out_sku_id_list);
  625. $postData['out_sku_id_list'] = $out_sku_id_list;
  626. $call = function($mini_id) use ($postData, $store_id, $md_id){
  627. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  628. return $postData;
  629. };
  630. $url = 'shop/store/price/batch/get';
  631. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  632. }
  633. //门店快送门店商品批量设置SKU库存
  634. public function wastoreStoreStockBatchUpdate($store_id = 0, $mini_id = 0, $md_id = 0, $sku_list = []) {
  635. $sku_list = [
  636. [
  637. "out_sku_id" => 'cyy_22890_413_427',
  638. "stock" => [
  639. "stock_quantity" => 1000
  640. ]
  641. ]
  642. ];
  643. $postData = [];
  644. $postData['sku_list'] = $sku_list;
  645. $call = function($mini_id) use ($postData, $store_id, $md_id){
  646. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  647. return $postData;
  648. };
  649. $url = 'shop/store/stock/batch/update';
  650. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  651. }
  652. //门店快送门店商品批量获取SKU库存
  653. public function wastoreStoreStockBatchGet($store_id = 0, $mini_id = 0, $md_id = 0, $out_sku_id_list = []) {
  654. // $out_sku_id_list = ['cyy_22890_413_427'];
  655. $postData = [];
  656. (is_string($out_sku_id_list) && $out_sku_id_list) && $out_sku_id_list = explode(',', $out_sku_id_list);
  657. $postData['out_sku_id_list'] = $out_sku_id_list;
  658. $call = function($mini_id) use ($postData, $store_id, $md_id){
  659. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  660. return $postData;
  661. };
  662. $url = 'shop/store/stock/batch/get';
  663. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  664. }
  665. //门店快送提交体验规范审核
  666. public function wastoreSubmitWxaAudit($store_id = 0, $mini_id = 0) {
  667. $postData = [];
  668. $url = 'shop/store/submit_wxa_audit';
  669. return $this->wastoreApi($url, $store_id, $mini_id, $postData);
  670. }
  671. //门店快送列出频道页卡片活动模板
  672. public function wastoreChannelCardActivityList($store_id = 0, $mini_id = 0, $template_id = 0) {
  673. $postData = [];
  674. $template_id && $postData['template_id'] = $template_id;
  675. $url = 'shop/channel/card/activity/list';
  676. return $this->wastoreApi($url, $store_id, $mini_id, $postData);
  677. }
  678. //门店快送删除频道页卡片活动模板
  679. public function wastoreChannelCardActivityDel($store_id = 0, $mini_id = 0, $template_id = 0) {
  680. $postData = [];
  681. $template_id && $postData['template_id'] = $template_id;
  682. $url = 'shop/channel/card/activity/delete';
  683. return $this->wastoreApi($url, $store_id, $mini_id, $postData);
  684. }
  685. //门店快送新增更新频道页卡片活动模板
  686. public function wastoreChannelCardActivityUpsert($store_id = 0, $mini_id = 0, $card_activity = [], $template_id = 0) {
  687. $postData = [];
  688. $postData['card_activity'] = $card_activity;
  689. $template_id && $postData['card_activity']['template_id'] = $template_id;
  690. $call = function($mini_id) use ($postData, $store_id){
  691. $postData['card_activity']['activity_image'] = $this->wastoreImgUpload($store_id, $mini_id, $postData['card_activity']['activity_image']);
  692. return $postData;
  693. };
  694. $url = 'shop/channel/card/activity/upsert';
  695. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  696. }
  697. //门店快送频道页门店商品卡片活动配置
  698. public function wastoreChannelSpuUpdate($store_id = 0, $mini_id = 0, $md_id = 0, $goods_ids = [], $card_activity_id = 0, $scene_group = 9) {
  699. $postData = [];
  700. $card_activity_id && $postData['card_activity_id'] = $card_activity_id;
  701. $call = function($mini_id) use ($postData, $store_id, $md_id, $goods_ids, $scene_group){
  702. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  703. (!is_array($goods_ids) && $goods_ids) && $goods_ids = explode(',', $goods_ids);
  704. foreach($goods_ids as $goods_id){
  705. $postData['out_spu_id_list'][] = implode('_', [$store_id, $mini_id, $scene_group, $goods_id]);
  706. }
  707. return $postData;
  708. };
  709. $url = 'shop/channel/spu/update';
  710. return $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  711. }
  712. //门店快送查询频道页门店商品卡片活动
  713. public function wastoreChannelSpuGet($store_id = 0, $mini_id = 0, $md_id = 0) {
  714. $postData = [];
  715. $call = function($mini_id) use ($postData, $store_id, $md_id){
  716. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  717. return $postData;
  718. };
  719. $url = 'shop/channel/spu/get';
  720. $res = $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  721. if(!$res['code']){
  722. $res['data']['temp'] = $this->wastoreChannelCardActivityList($store_id, $mini_id, $res['data']['card_activity']['template_id']);
  723. $goodsList = [];
  724. $goods_ids = [];
  725. foreach($res['data']['out_spu_id_list'] as $item){
  726. $idInfo = explode('_', $item);
  727. $gid = array_pop($idInfo);
  728. $goods_ids[$gid] = $item;
  729. }
  730. $statusList = $this->wastoreStoreSpuBatchGetStatus($store_id, $mini_id, $md_id, array_keys($goods_ids));
  731. if(!$statusList['code']){
  732. $status = array_combine(array_column($statusList['data']['spu_list'], 'out_spu_id'), $statusList['data']['spu_list']);
  733. }
  734. if($goods_ids){
  735. $goodsList = Goods::find()->where(['id' => array_keys($goods_ids)])->asArray()->all();
  736. foreach($goodsList as &$g){
  737. $g['out_spu_id'] = $goods_ids[$g['id']];
  738. $g['spu_status'] = 0;
  739. if($status){
  740. $g['spu_status'] = $status[$g['out_spu_id']]['status']['sale_status'];
  741. }
  742. }
  743. }
  744. $res['data']['goodsList'] = $goodsList;
  745. }
  746. return $res;
  747. }
  748. //门店快送获取用户实时情况
  749. public function wastoreSessionGet($store_id = 0, $mini_id = 0, $session_key = '') {
  750. $postData = ['session_key' => $session_key];
  751. $url = 'shop/store/session/get';
  752. return $this->wastoreApi($url, $store_id, $mini_id, $postData);
  753. }
  754. public static function afterOrderOtherDataSave($order) {
  755. try{
  756. if(!self::pluginIsOpen($order->store_id)){
  757. return;
  758. }
  759. $self = new WastoreForm();
  760. $self->wastoreShopOrderAdd($order->store_id, 0, $order->md_id, $order);
  761. } catch (\Exception $e) {
  762. \Yii::error([__METHOD__, $e]);
  763. }
  764. }
  765. public static function afterOrderPrepay($order, $payInfo = ['prepay_id' => '']) {
  766. try{
  767. if(!self::pluginIsOpen($order->store_id)){
  768. return;
  769. }
  770. $prepay_id = $payInfo['prepay_id'];
  771. $self = new WastoreForm();
  772. $self->wastoreShopOrderUpdatePayInfo($order->store_id, 0, $order, $prepay_id);
  773. } catch (\Exception $e) {
  774. \Yii::error([__METHOD__, $e]);
  775. }
  776. }
  777. public static function afterOrderPay($order) {
  778. try{
  779. $self = new WastoreForm();
  780. $self->wastoreShopOrderPay($order->store_id, 0, $order);
  781. } catch (\Exception $e) {
  782. \Yii::error([__METHOD__, $e]);
  783. }
  784. }
  785. public static function afterOrderCancel($order) {
  786. try{
  787. $self = new WastoreForm();
  788. $self->wastoreShopOrderClose($order->store_id, 0, $order);
  789. } catch (\Exception $e) {
  790. \Yii::error([__METHOD__, $e]);
  791. }
  792. }
  793. public static function afterOrderSend($order) {
  794. try{
  795. $self = new WastoreForm();
  796. $self->wastoreShopDeliverySend($order->store_id, 0, $order);
  797. self::afterDeliveryChange($order->store_id, $order);
  798. } catch (\Exception $e) {
  799. \Yii::error([__METHOD__, $e]);
  800. }
  801. }
  802. public static function afterOrderConfirm($order) {
  803. try{
  804. $self = new WastoreForm();
  805. $self->wastoreShopWaybillFinish($order->store_id, 0, $order);
  806. $self->wastoreShopDeliveryReceive($order->store_id, 0, $order);
  807. } catch (\Exception $e) {
  808. \Yii::error([__METHOD__, $e]);
  809. }
  810. }
  811. public static function afterOrderSave($insert, $changedAttributes, $order) {
  812. try{
  813. if(!self::pluginIsOpen($order->store_id)){
  814. return;
  815. }
  816. //支付订单
  817. if (($order->is_pay == 1) && ($changedAttributes['is_pay'] != 1)) {
  818. self::afterOrderPay($order);
  819. }
  820. //取消订单
  821. if (($order->trade_status == Order::ORDER_FLOW_CANCEL) && ($changedAttributes['trade_status'] != Order::ORDER_FLOW_CANCEL)) {
  822. self::afterOrderCancel($order);
  823. }
  824. //发货
  825. if (($order->trade_status == Order::ORDER_FLOW_SEND) && ($changedAttributes['trade_status'] != Order::ORDER_FLOW_SEND)) {
  826. self::afterOrderSend($order);
  827. }
  828. //发货
  829. if (($order->trade_status == Order::ORDER_FLOW_CONFIRM) && ($changedAttributes['trade_status'] != Order::ORDER_FLOW_CONFIRM)) {
  830. self::afterOrderConfirm($order);
  831. }
  832. } catch (\Exception $e) {
  833. \Yii::error([__METHOD__, $e]);
  834. }
  835. }
  836. public static function afterDeliveryChange($store_id, $order) {
  837. try{
  838. if(!self::pluginIsOpen($store_id)){
  839. return;
  840. }
  841. $self = new self();
  842. $self->wastoreShopWaybillUpdate($store_id, 0, $order);
  843. } catch (\Exception $e) {
  844. \Yii::error([__METHOD__, $e]);
  845. }
  846. }
  847. //门店快送创建订单
  848. public function wastoreShopOrderAdd($store_id = 0, $mini_id = 0, $md_id = 0, $order = null) {
  849. if(!is_object($order)){
  850. $order = Order::findOne($order);
  851. }
  852. if(!$cache = cache()->get('wastoreShopOrderAdd' . $order->id)){
  853. return;
  854. }
  855. $postData = [];
  856. $call = function($mini_id) use ($postData, $store_id, $md_id, $order, $cache){
  857. $postData['out_store_id'] = implode('_', [$store_id, $mini_id, $md_id]);
  858. $postData['out_order_id'] = $order->order_no;
  859. $postData['create_time'] = date('Y-m-d H:i:s', $order->created_at);
  860. $user = User::findOne($order->user_id);
  861. $postData['openid'] = $user->wechat_open_id;
  862. $postData['path'] = 'order/order-detail/order-detail?id=' . $order->id;
  863. $postData['fund_type'] = 3;
  864. $postData['trace_id'] = $cache['traceId'];
  865. $postData['delivery_free'] = $order->is_offline ? true : false;
  866. if(!$postData['delivery_free']){
  867. $address = json_decode($order->address_data, true);
  868. $postData['address_info'] = [
  869. 'receiver_name' => $order->name,
  870. 'detailed_address' => $order->address,
  871. 'tel_number' => $order->mobile,
  872. 'province' => $address['province'],
  873. 'city' => $address['city'],
  874. 'town' => $address['district'],
  875. ];
  876. }
  877. $freight = $order->express_price;
  878. if($order->is_delivery){
  879. $deliveryInfo = DeliveryInfo::findOne(['order_no' => $order->order_no, 'is_delete' => 0]);
  880. $freight = $deliveryInfo['fee'];
  881. }
  882. $product_infos = [];
  883. foreach($order->detail as $ad){
  884. $scene_group = 9;
  885. $goods_id = $ad['goods_id'];
  886. $out_product_id = implode('_', [$store_id, $mini_id, $scene_group, $goods_id]);
  887. $goods = json_decode($ad['goods_info'], true);
  888. $skuAttr = Goods::skuAttr($goods, json_decode($ad['attr'], true));
  889. $product_info = [
  890. 'out_product_id' => $out_product_id,
  891. 'out_sku_id' => $skuAttr['cyy_skuId'],
  892. 'product_cnt' => $ad['num'],
  893. 'sale_price' => $skuAttr['price'] * 100,
  894. 'sku_real_price' => $skuAttr['price'] * 100,
  895. 'title' => $goods['name'],
  896. 'head_img' => $skuAttr['pic'] ?: $goods['cover_pic'],
  897. 'path' => 'goods/goods/goods?id=' . $goods_id,
  898. ];
  899. $product_infos[] = $product_info;
  900. }
  901. $postData['order_detail'] = [
  902. 'price_info' => [
  903. 'freight' => $freight * 100,
  904. 'order_price' => $order->pay_price * 100,
  905. ],
  906. 'product_infos' => $product_infos,
  907. ];
  908. return $postData;
  909. };
  910. $url = 'shop/order/add';
  911. $res = $this->wastoreApi($url, $store_id, $mini_id, $postData, $call);
  912. if(!$res['code'] && !$res['data']['errcode']){
  913. cache()->delete('wastoreShopOrderAdd' . $order->id);
  914. }
  915. return $res;
  916. }
  917. //门店快送获取订单
  918. public function wastoreShopOrderGet($store_id = 0, $mini_id = 0, $order = null) {
  919. if(!is_object($order)){
  920. $order = Order::findOne($order);
  921. }
  922. $postData = [];
  923. $postData['out_order_id'] = $order->order_no;
  924. $user = User::findOne($order->user_id);
  925. $postData['openid'] = $user->wechat_open_id;
  926. $url = 'shop/order/get';
  927. return $this->wastoreApi($url, $store_id, $mini_id, $postData);
  928. }
  929. //门店快送更新支付信息
  930. public function wastoreShopOrderUpdatePayInfo($store_id = 0, $mini_id = 0, $order = null, $prepay_id = '', $prepay_time = 0) {
  931. if(!is_object($order)){
  932. $order = Order::findOne($order);
  933. }
  934. $wastoreOrder = $this->wastoreShopOrderGet($store_id, $mini_id, $order);
  935. if($wastoreOrder['code'] || $wastoreOrder['data']['errcode'] || empty($wastoreOrder['data']['order'])){
  936. return;
  937. }
  938. $postData = [];
  939. $postData['out_order_id'] = $order->order_no;
  940. $user = User::findOne($order->user_id);
  941. $postData['openid'] = $user->wechat_open_id;
  942. $postData['pay_info'] = [
  943. 'prepay_id' => $prepay_id,
  944. 'prepay_time' => date('Y-m-d H:i:s', $prepay_time ?: time()),
  945. ];
  946. $url = 'shop/order/update_pay_info';
  947. $res = $this->wastoreApi($url, $store_id, $mini_id, $postData);
  948. return $res;
  949. }
  950. //门店快送支付订单
  951. public function wastoreShopOrderPay($store_id = 0, $mini_id = 0, $order = null) {
  952. if(!is_object($order)){
  953. $order = Order::findOne($order);
  954. }
  955. if(!$order->is_pay){
  956. return;
  957. }
  958. $wastoreOrder = $this->wastoreShopOrderGet($store_id, $mini_id, $order);
  959. if($wastoreOrder['code'] || $wastoreOrder['data']['errcode'] || empty($wastoreOrder['data']['order'])){
  960. return;
  961. }
  962. $postData = [];
  963. $postData['out_order_id'] = $order->order_no;
  964. $user = User::findOne($order->user_id);
  965. $postData['openid'] = $user->wechat_open_id;
  966. $postData['pay_time'] = date('Y-m-d H:i:s', $order->pay_time);
  967. $pay_method_type = $order->pay_type == Order::PAY_TYPE_WECHAT ? 0 : 99;
  968. $postData['pay_method_type'] = $pay_method_type;
  969. $url = 'shop/order/pay';
  970. $res = $this->wastoreApi($url, $store_id, $mini_id, $postData);
  971. return $res;
  972. }
  973. //门店快送支付订单
  974. public function wastoreShopOrderClose($store_id = 0, $mini_id = 0, $order = null) {
  975. if(!is_object($order)){
  976. $order = Order::findOne($order);
  977. }
  978. if(!$order->is_pay){
  979. return;
  980. }
  981. $wastoreOrder = $this->wastoreShopOrderGet($store_id, $mini_id, $order);
  982. if($wastoreOrder['code'] || $wastoreOrder['data']['errcode'] || empty($wastoreOrder['data']['order'])){
  983. return;
  984. }
  985. $postData = [];
  986. $postData['out_order_id'] = $order->order_no;
  987. $user = User::findOne($order->user_id);
  988. $postData['openid'] = $user->wechat_open_id;
  989. $postData['close_reason'] = '订单取消.';
  990. $close_type = $order->is_pay ? 2 : 0;
  991. $postData['close_type'] = $close_type;
  992. $url = 'shop/order/close';
  993. $res = $this->wastoreApi($url, $store_id, $mini_id, $postData);
  994. return $res;
  995. }
  996. //门店快送订单发货
  997. public function wastoreShopDeliverySend($store_id = 0, $mini_id = 0, $order = null) {
  998. if(!is_object($order)){
  999. $order = Order::findOne($order);
  1000. }
  1001. if(!$order->is_pay){
  1002. return;
  1003. }
  1004. $wastoreOrder = $this->wastoreShopOrderGet($store_id, $mini_id, $order);
  1005. if($wastoreOrder['code'] || $wastoreOrder['data']['errcode'] || empty($wastoreOrder['data']['order'])){
  1006. return;
  1007. }
  1008. $postData = [];
  1009. $postData['out_order_id'] = $order->order_no;
  1010. $user = User::findOne($order->user_id);
  1011. $postData['openid'] = $user->wechat_open_id;
  1012. $delivery_id = '';
  1013. $waybill_id = '';
  1014. $delivery_type = 6;
  1015. if($order->is_offline){
  1016. $delivery_type = 4;
  1017. }
  1018. if($order->express_no){
  1019. $delivery_type = 1;
  1020. $waybill_id = $order->express_no;
  1021. $delivery_id = Express::getExpressCode($order->express);
  1022. }
  1023. if($order->is_delivery){
  1024. $delivery_type = 6;
  1025. $delivery_id = 'OTHER';
  1026. $waybill_id = $order->order_no;
  1027. }
  1028. $postData['delivery_list'] = [[
  1029. 'delivery_id' => $delivery_id,
  1030. 'waybill_id' => $waybill_id,
  1031. 'delivery_type' => $delivery_type,
  1032. 'product_info_list' => $wastoreOrder['data']['order']['order_detail']['product_infos'],
  1033. ]];
  1034. $url = 'shop/delivery/send';
  1035. $res = $this->wastoreApi($url, $store_id, $mini_id, $postData);
  1036. return $res;
  1037. }
  1038. //门店快送更新配送状态
  1039. public function wastoreShopWaybillUpdate($store_id = 0, $mini_id = 0, $order = null) {
  1040. if(!is_object($order)){
  1041. $order = Order::findOne($order);
  1042. }
  1043. if(!$order->is_pay){
  1044. return;
  1045. }
  1046. $wastoreOrder = $this->wastoreShopOrderGet($store_id, $mini_id, $order);
  1047. if($wastoreOrder['code'] || $wastoreOrder['data']['errcode'] || empty($wastoreOrder['data']['order'])){
  1048. return;
  1049. }
  1050. $deliveryInfo = DeliveryInfo::findOne(['order_no' => $order->order_no, 'is_delete' => 0]);
  1051. if($deliveryInfo['wechat_delivery_type']){
  1052. return;
  1053. }
  1054. $delivery_state = 40000;
  1055. if($deliveryInfo['is_local']){
  1056. if($deliveryInfo['local_status'] < 2){
  1057. $delivery_state = 20000;
  1058. }
  1059. }
  1060. $deliveryMaiyatian = DeliveryMaiyatian::findOne(['order_no' => $order->order_no, 'store_id' => $store_id]);
  1061. if($deliveryMaiyatian){
  1062. if($deliveryMaiyatian['delivery_status'] != MaiyatianForm::DELIVERY_STATUS_DELIVERING){
  1063. $delivery_state = 20000;
  1064. }
  1065. }
  1066. $deliveryKeloop = DeliveryKeloop::findOne(['order_no' => $order->order_no, 'store_id' => $store_id]);
  1067. if($deliveryKeloop){
  1068. if($deliveryKeloop['delivery_status'] != KeloopForm::DELIVERY_STATUS_DELIVERING){
  1069. $delivery_state = 20000;
  1070. }
  1071. }
  1072. $postData = [];
  1073. $postData['out_order_id'] = $order->order_no;
  1074. $user = User::findOne($order->user_id);
  1075. $postData['openid'] = $user->wechat_open_id;
  1076. $postData['waybill_id'] = $order->order_no;
  1077. $postData['delivery_man_name'] = $deliveryInfo->rider_name;
  1078. $postData['delivery_man_phone'] = $deliveryInfo->rider_mobile;
  1079. $postData['delivery_state'] = $delivery_state;
  1080. $postData['expected_time'] = $order->delivery_time ?: ($order->send_time + 86400 * 5);
  1081. $url = 'shop/waybill/update';
  1082. $res = $this->wastoreApi($url, $store_id, $mini_id, $postData);
  1083. return $res;
  1084. }
  1085. //门店快送配送单已完成
  1086. public function wastoreShopWaybillFinish($store_id = 0, $mini_id = 0, $order = null) {
  1087. if(!is_object($order)){
  1088. $order = Order::findOne($order);
  1089. }
  1090. $postData = [];
  1091. $postData['out_order_id'] = $order->order_no;
  1092. $user = User::findOne($order->user_id);
  1093. $postData['openid'] = $user->wechat_open_id;
  1094. $postData['waybill_id'] = $order->order_no;
  1095. $postData['finish_time'] = time();
  1096. $url = 'shop/waybill/finish';
  1097. $res = $this->wastoreApi($url, $store_id, $mini_id, $postData);
  1098. return $res;
  1099. }
  1100. //门店快送订单已完成
  1101. public function wastoreShopDeliveryReceive($store_id = 0, $mini_id = 0, $order = null) {
  1102. if(!is_object($order)){
  1103. $order = Order::findOne($order);
  1104. }
  1105. $postData = [];
  1106. $postData['out_order_id'] = $order->order_no;
  1107. $user = User::findOne($order->user_id);
  1108. $postData['openid'] = $user->wechat_open_id;
  1109. $url = 'shop/delivery/receive';
  1110. $res = $this->wastoreApi($url, $store_id, $mini_id, $postData);
  1111. return $res;
  1112. }
  1113. }