StbzController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers\cloud;
  8. use app\constants\OptionSetting;
  9. use app\models\District;
  10. use app\modules\admin\controllers\BaseController;
  11. use app\models\Option;
  12. use app\models\Supplier;
  13. use app\modules\admin\models\PlatformForm;
  14. use app\modules\admin\models\SupplierForm;
  15. use Yii;
  16. class StbzController extends BaseController
  17. {
  18. // 设置胜天半子账号
  19. public function actionAccount()
  20. {
  21. if (\Yii::$app->request->isPost) {
  22. $data = [
  23. 'key' => \post_params('key', ''),
  24. 'secret' => \post_params('secret', ''),
  25. 'url' => \post_params('url', ''),
  26. ];
  27. Option::set('stbz_account', json_encode($data), 0, 'saas');
  28. try {
  29. $res = $this->regSupplier();
  30. if ($res['code'] != 0) {
  31. return $this->asJson([
  32. 'code' => 1,
  33. 'msg' => $res['msg'],
  34. ]);
  35. }
  36. return $this->asJson([
  37. 'code' => 0,
  38. 'msg' => '保存成功',
  39. ]);
  40. } catch (\Exception $e) {
  41. return $this->asJson([
  42. 'code' => 0,
  43. 'msg' => $e->getMessage(),
  44. ]);
  45. }
  46. }
  47. $data = Option::get('stbz_account', 0, 'saas', \json_encode([
  48. 'key' => '',
  49. 'secret' => '',
  50. 'url' => ''
  51. ]));
  52. return $this->asJson([
  53. 'code' => 0,
  54. 'msg' => 'success',
  55. 'data' => json_decode($data['value']),
  56. ]);
  57. }
  58. // 注册供货商账号
  59. public function regSupplier()
  60. {
  61. $supplier = Supplier::find()->where(['is_stbz' => 1])->one();
  62. if ($supplier) {
  63. return [
  64. 'code' => 0,
  65. 'msg' => '账号已创建',
  66. ];
  67. }
  68. $name = Yii::$app->security->generateRandomString(24);
  69. $password = '12345678';
  70. $logo = \Yii::$app->request->hostInfo . '/web/v1/statics/images/stbz.jpg';
  71. $form = new PlatformForm();
  72. $form->supplier_name = '胜天半子';
  73. $form->name = $name;
  74. $form->password = $password;
  75. $form->tel = '11111111111';
  76. $form->logo = $logo;
  77. $form->type = '1';
  78. $form->rate = '0.00';
  79. $form->email = 'stbz@qq.com';
  80. $form->certificate = $logo;
  81. $form->id_card_front = $logo;
  82. $form->id_card_reverse = $logo;
  83. $form->sale_day = 7;
  84. $res = $form->createSupplier();
  85. if ($res['code'] == 0) {
  86. $supplier_id = $res['data']['cloud_supplier_id'];
  87. $form = new PlatformForm();
  88. $form->status = 1;
  89. $form->supplier_id = $supplier_id;
  90. $ss = $form->setSupplierStatus();
  91. }
  92. return $res;
  93. }
  94. /**
  95. * 获取来源列表
  96. * @return \yii\web\Response
  97. * @throws \yii\base\InvalidConfigException
  98. * @throws \yii\db\Exception
  99. * @author Syan mzsongyan@gmail.com
  100. * @date 2023-09-14
  101. */
  102. public function actionGetSourceList()
  103. {
  104. $key = md5(json_encode(['type' => 'stbz_get_source_list']));
  105. if (\Yii::$app->cache->exists($key)) {
  106. $data = \Yii::$app->cache->get($key);
  107. $returnData = \json_decode($data, true);
  108. } else {
  109. $data = \stbz_client()->getApiResponse('post', '/v2/GetSource');
  110. $returnData = \json_decode($data, true);
  111. if ($returnData['code'] != 1) {
  112. return $this->asJson([
  113. 'code' => 1,
  114. 'msg' => $returnData['msg'] ?: $returnData['message'],
  115. ]);
  116. }
  117. \Yii::$app->cache->set($key, $data, 600);
  118. }
  119. return $this->asJson([
  120. 'code' => 0,
  121. 'msg' => 'success',
  122. 'data' => $returnData['data'],
  123. ]);
  124. }
  125. /**
  126. * 获取一级分类列表
  127. * @return \yii\web\Response
  128. * @author Syan mzsongyan@gmail.com
  129. * @date 2022-09-13
  130. */
  131. public function actionGetCatList()
  132. {
  133. $source = \get_params('source', 0);
  134. $key = md5(json_encode(['type' => 'stbz_get_cat_list', 'source' => $source]));
  135. if (\Yii::$app->cache->exists($key)) {
  136. $data = \Yii::$app->cache->get($key);
  137. $returnData = \json_decode($data, true);
  138. } else {
  139. $data = \stbz_client()->getApiResponse('get', '/v2/Category/GetCategory', [
  140. 'parent_id' => 0,
  141. 'source' => $source,
  142. ]);
  143. $returnData = \json_decode($data, true);
  144. if ($returnData['code'] != 1) {
  145. return $this->asJson([
  146. 'code' => 1,
  147. 'msg' => $returnData['msg'] ?: $returnData['message'],
  148. 'data' => $returnData,
  149. ]);
  150. }
  151. \Yii::$app->cache->set($key, $data, 600);
  152. }
  153. return $this->asJson([
  154. 'code' => 0,
  155. 'msg' => 'success',
  156. 'data' => $returnData['data'],
  157. ]);
  158. }
  159. /**
  160. * 获取选品库商品列表
  161. * @return \yii\web\Response
  162. * @author Syan mzsongyan@gmail.com
  163. * @date 2022-09-13
  164. */
  165. public function actionGetGoodsList()
  166. {
  167. $search_words = \get_params('search_words');
  168. $page = \get_params('page', 1);
  169. $limit = \get_params('limit', 10);
  170. $category_id = \get_params('category_id', 0);
  171. $source = \get_params('source', 0);
  172. // 协议价
  173. $price_left = \get_params('price_left', 0);
  174. $price_right = \get_params('price_right', 0);
  175. // 指导价
  176. $zhidao_price_left = \get_params('zhidao_price_left', 0);
  177. $zhidao_price_right = \get_params('zhidao_price_right', 0);
  178. // 营销价
  179. $yingxiao_price_left = \get_params('yingxiao_price_left', 0);
  180. $yingxiao_price_right = \get_params('yingxiao_price_right', 0);
  181. // 常规利润率
  182. $changgui_lirun_left = \get_params('changgui_lirun_left', 0);
  183. $changgui_lirun_right = \get_params('changgui_lirun_right', 0);
  184. // 营销利润率
  185. $yingxiao_lirun_left = \get_params('yingxiao_lirun_left', 0);
  186. $yingxiao_lirun_right = \get_params('yingxiao_lirun_right', 0);
  187. // 是否包邮
  188. $is_baoyou = \get_params('is_baoyou', -1);
  189. // 排序字段
  190. $sort_field = \get_params('sort_field', '');
  191. $sort_type = \get_params('sort_type', '');
  192. $params = [
  193. 'page' => $page > 50 ? 50 : $page,
  194. 'limit' => $limit,
  195. 'recommend' => 2,
  196. ];
  197. if ($source > 0) {
  198. $params['source'] = $source;
  199. }
  200. if ($search_words) {
  201. $params['search_words'] = $search_words;
  202. }
  203. if ($category_id > 0) {
  204. $params['category_id'] = $category_id;
  205. }
  206. if ($price_left > 0) { // 协议价
  207. $params['agreement_price'] = [
  208. 'from' => $price_left,
  209. 'to' => $price_right > 0 ? $price_right : 99999,
  210. ];
  211. }
  212. if ($zhidao_price_left > 0) { // 指导价
  213. $params['guide_price'] = [
  214. 'from' => $zhidao_price_left,
  215. 'to' => $zhidao_price_right > 0 ? $zhidao_price_right : 99999,
  216. ];
  217. }
  218. if ($yingxiao_price_left > 0) { // 营销价
  219. $params['activity_price'] = [
  220. 'from' => $yingxiao_price_left,
  221. 'to' => $yingxiao_price_right > 0 ? $yingxiao_price_right : 99999,
  222. ];
  223. }
  224. if ($changgui_lirun_left > 0) { // 常规利润率
  225. $params['promotion_rate'] = [
  226. 'from' => $changgui_lirun_left,
  227. 'to' => $changgui_lirun_right > 0 ? $changgui_lirun_right : 99999,
  228. ];
  229. }
  230. if ($yingxiao_lirun_left > 0) { // 营销利润率
  231. $params['activity_rate'] = [
  232. 'from' => $yingxiao_lirun_left,
  233. 'to' => $yingxiao_lirun_right > 0 ? $yingxiao_lirun_right : 99999,
  234. ];
  235. }
  236. if ($is_baoyou > -1) {
  237. $params['is_free_shipping'] = $is_baoyou;
  238. }
  239. if ($sort_field) {
  240. $params['type'] = $sort_field;
  241. }
  242. if ($sort_type) {
  243. $params['sort'] = $sort_type;
  244. }
  245. $key = md5(json_encode(\array_merge($params, ['cache_type' => 'stbz_get_goods_list'])));
  246. if (\Yii::$app->cache->exists($key)) {
  247. $data = \Yii::$app->cache->get($key);
  248. $returnData = \json_decode($data, true);
  249. } else {
  250. $data = \stbz_client()->getApiResponse('post', '/v2/GoodsStorage/NewList', $params);
  251. $returnData = \json_decode($data, true);
  252. if ($returnData['code'] != 1) {
  253. return $this->asJson([
  254. 'code' => 1,
  255. 'msg' => $returnData['msg'] ?: $returnData['message'],
  256. ]);
  257. }
  258. \Yii::$app->cache->set($key, $data, 600);
  259. }
  260. foreach ($returnData['data']['list'] as &$datum) {
  261. $datum['rate'] = 0;
  262. $diff_price = $datum['guide_price'] - $datum['agreement_price'];
  263. if ($diff_price > 0) {
  264. $datum['rate'] = sprintf("%.2f", $diff_price / $datum['agreement_price']);
  265. }
  266. }
  267. return $this->asJson([
  268. 'code' => 0,
  269. 'msg' => 'success',
  270. 'data' => $returnData['data'],
  271. ]);
  272. }
  273. /**
  274. * 获取商品详情
  275. * @return \yii\web\Response
  276. * @author Syan mzsongyan@gmail.com
  277. * @date 2022-09-16
  278. */
  279. public function actionGetGoodsItem($goodsId = null)
  280. {
  281. $id = \get_params('id', $goodsId);
  282. $params = [
  283. 'id' => $id,
  284. ];
  285. $key = md5(json_encode(\array_merge($params, ['type' => 'stbz_get_goods_item'])));
  286. if (\Yii::$app->cache->exists($key)) {
  287. $data = \Yii::$app->cache->get($key);
  288. $returnData = \json_decode($data, true);
  289. } else {
  290. $data = \stbz_client()->getApiResponse('get', '/v2/Goods/Detail', $params);
  291. $returnData = \json_decode($data, true);
  292. if ($returnData['code'] != 1) {
  293. return $this->asJson([
  294. 'code' => 1,
  295. 'msg' => $returnData['msg'] ?: $returnData['message'],
  296. ]);
  297. }
  298. \Yii::$app->cache->set($key, $data, 600);
  299. }
  300. return $this->asJson([
  301. 'code' => 0,
  302. 'msg' => 'success',
  303. 'data' => $returnData['data'],
  304. ]);
  305. }
  306. public function actionCheckGoodsImport() {
  307. try {
  308. $post = \post_params();
  309. $stbz_id = $post['id'];
  310. $cloud_store_token = get_platform_token();
  311. $goods_url = "/cloud/user/getStbzGoods";
  312. $goods_data = [];
  313. $goods_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  314. $goods_data['id'] = $stbz_id; //默认为 1
  315. $domain = (new OptionSetting)->getCloudDomainName();
  316. $goods_data_info = cloud_post($domain . $goods_url,$goods_data);
  317. $goods_data_info = json_decode($goods_data_info,true);
  318. if($goods_data_info['code'] != 0){
  319. throw new \Exception($goods_data_info['msg']);
  320. }else{
  321. return $this->asJson($goods_data_info);
  322. }
  323. } catch (\Exception $e) {
  324. return $this->asJson([
  325. 'code' => 1,
  326. 'msg' => $e->getMessage()
  327. ]);
  328. }
  329. }
  330. //批量添加云仓产品
  331. public function actionAddCloudGoodsGroup() {
  332. $post = \post_params();
  333. try {
  334. $ids = $post['ids'];
  335. if (is_string($ids)) {
  336. $ids = explode(',', $ids);
  337. }
  338. foreach ($ids as $id) {
  339. $goods = $this->actionGetGoodsItem($id);
  340. $goods = $goods->data;
  341. if ($goods['code'] == 1) {
  342. throw new \Exception($goods['msg']);
  343. }
  344. $attr = $this->handleAttr($goods['data']['specs'], $goods['data']['cover']);
  345. $data = [
  346. 'cat_id' => \implode(',', $post['cat']),
  347. 'name' => $goods['data']['title'],
  348. 'goods_no' => (string)$id,
  349. 'g_s_pic_url' => $goods['data']['cover'],
  350. 'desc' => $goods['data']['description'],
  351. 'g_price' => sprintf('%.2f', $goods['data']['cost_price'] * 0.01),
  352. 'g_wholesale_price' => sprintf('%.2f', $goods['data']['sale_price'] * 0.01),
  353. 'g_wholesale_ladder_rebate' => '[]',
  354. 'g_wholesale_rebate_switch' => '0',
  355. 'weight' => '',
  356. 'g_original_price' => sprintf('%.2f', $goods['data']['sale_price'] * 0.01),
  357. 'g_unit' => $goods['data']['unit'],
  358. 'g_shop_count' => 1,
  359. 'num' => $goods['data']['total_stock'],
  360. 'use_attr' => '1',
  361. 'g_pic_list' => $goods['data']['covers'],
  362. 'g_send_type' => '0',
  363. 'attrs' => $attr,
  364. 'is_change' => 1,
  365. 'stbz_goods_id' => $id,
  366. 'market_price' => sprintf('%.2f', $goods['data']['market_price'] * 0.01),
  367. ];
  368. $result = $this->commonHandle($data, $id);
  369. if (intval($result['code']) !== 0) {
  370. throw new \Exception($result['msg']);
  371. }
  372. }
  373. return $this->asJson([
  374. 'code' => 0,
  375. 'msg' => '操作成功'
  376. ]);
  377. } catch (\Exception $e) {
  378. return $this->asJson([
  379. 'code' => 1,
  380. 'msg' => $e->getMessage() . $e->getLine()
  381. ]);
  382. }
  383. }
  384. // 添加云仓商品
  385. public function actionAddCloudGoods()
  386. {
  387. $post = \post_params();
  388. $goods = $this->actionGetGoodsItem($post['id']);
  389. $goods = $goods->data;
  390. if ($goods['code'] == 1) {
  391. return $this->asJson([
  392. 'code' => 1,
  393. 'msg' => $goods['msg'],
  394. ]);
  395. }
  396. if ($post['attr']) {
  397. foreach ($post['attr'] as &$attr_item) {
  398. $attr_item['original_price'] = $attr_item['wholesale_price'];
  399. }
  400. }
  401. $data = [
  402. 'cat_id' => \implode(',', $post['cat']),
  403. 'name' => $post['name'],
  404. 'goods_no' => (string)$post['id'],
  405. 'g_s_pic_url' => $goods['data']['cover'],
  406. 'desc' => $goods['data']['description'],
  407. 'g_price' => $post['price'],
  408. 'g_wholesale_price' => $post['wholesale_price'],
  409. 'g_wholesale_ladder_rebate' => '[]',
  410. 'g_wholesale_rebate_switch' => '0',
  411. 'weight' => '',
  412. 'g_original_price' => $post['original_price'],
  413. 'g_unit' => $goods['data']['unit'],
  414. 'g_shop_count' => 1,
  415. 'num' => $goods['data']['total_stock'],
  416. 'use_attr' => '1',
  417. 'g_pic_list' => $goods['data']['covers'],
  418. 'g_send_type' => '0',
  419. 'attrs' => $post['attr'],
  420. 'is_change' => 1,
  421. 'stbz_goods_id' => $post['id'],
  422. 'market_price' => sprintf('%.2f', ($goods['data']['market_price'] * 0.01))
  423. ];
  424. return $this->asJson($this->commonHandle($data, $post['id']));
  425. }
  426. public function commonHandle($data, $id = 0) {
  427. $supplier = Supplier::findOne(['is_stbz' => 1, 'is_delete' => 0]);
  428. if (!$supplier) {
  429. return [
  430. 'code' => 1,
  431. 'msg' => '供货商未找到',
  432. ];
  433. }
  434. $params = [
  435. 'goods_ids' => $id
  436. ];
  437. $stbz_result = \stbz_client()->getApiResponse('get', '/v2/GoodsStorage/Add', $params);
  438. $returnData = \json_decode($stbz_result, true);
  439. if ($returnData['code'] != 1 && $returnData['code'] != 0) {
  440. return [
  441. 'code' => 1,
  442. 'msg' => $returnData['msg'],
  443. ];
  444. }
  445. $supplierForm = new SupplierForm();
  446. $supplierForm->attributes = $data;
  447. $supplierForm->token_supplier_id = $supplier->cloud_supplier_id;
  448. $setGoods = $supplierForm->setGoods();
  449. if ($setGoods['code'] == 1) {
  450. return $setGoods;
  451. }
  452. // 提交审核
  453. $supplierForm = new SupplierForm();
  454. $supplierForm->attributes = ['goods_id' => $setGoods['data']['goods_id']];
  455. $supplierForm->token_supplier_id = $supplier->cloud_supplier_id;
  456. $submitAudit = $supplierForm->auditSubmit();
  457. if ($submitAudit['code'] == 1) {
  458. return $submitAudit;
  459. }
  460. // 审核商品
  461. $form = new PlatformForm();
  462. $form->attributes = [
  463. 'id' => $submitAudit['data']['goods_id'],
  464. 'status' => 1,
  465. ];
  466. return $form->auditHandle();
  467. }
  468. public function handleAttr($specs, $image) {
  469. $attr = [];
  470. foreach ($specs['options'] as $i) {
  471. $attr_list = [];
  472. $value_ids = explode('_', $i['spec_value_ids']);
  473. foreach ($value_ids as $ii) {
  474. foreach ($specs['values'] as $iii) {
  475. if ($ii == $iii['id']) {
  476. foreach ($specs['names'] as $iiii) {
  477. if ($iiii['id'] == $iii['spec_name_id']) {
  478. $attr_list = array_merge($attr_list, [
  479. [
  480. 'attr_group_name' => ($iiii['name'] === '默认' ? '规格' : $iiii['name']),
  481. 'attr_name' => $iii['name']
  482. ]
  483. ]);
  484. }
  485. }
  486. }
  487. }
  488. }
  489. $attr = array_merge($attr, [
  490. [
  491. 'attr_list' => $attr_list,
  492. 'num' => $i['stock'],
  493. 'price' => sprintf('%.2f', $i['cost_price'] * 0.01),
  494. 'wholesale_price' => sprintf('%.2f', $i['sale_price'] * 0.01),
  495. 'original_price' => sprintf('%.2f', $i['sale_price'] * 0.01),
  496. 'no' => $i['id'],
  497. 'pic' => $i['image'] == '' ? $image : $i['image'],
  498. 'share_commission_first' => '',
  499. 'share_commission_second' => '',
  500. 'share_commission_third' => '',
  501. ]
  502. ]);
  503. }
  504. return $attr;
  505. }
  506. // 获取资金流水
  507. public function actionGetCashFlow()
  508. {
  509. $page = \get_params('page', 1);
  510. $limit = \get_params('limit', 10);
  511. $params = [
  512. 'page' => $page,
  513. 'limit' => $limit,
  514. ];
  515. $key = md5(json_encode(\array_merge($params, ['type' => 'stbz_get_cash'])));
  516. if (\Yii::$app->cache->exists($key)) {
  517. $data = \Yii::$app->cache->get($key);
  518. $returnData = \json_decode($data, true);
  519. } else {
  520. $data = \stbz_client()->getApiResponse('get', '/v2/cash/flow', [
  521. 'page' => $page,
  522. 'limit' => $limit,
  523. ]);
  524. $returnData = \json_decode($data, true);
  525. if ($returnData['code'] != 1) {
  526. return $this->asJson([
  527. 'code' => 1,
  528. 'msg' => $returnData['msg'],
  529. ]);
  530. }
  531. \Yii::$app->cache->set($key, $data, 600);
  532. }
  533. return $this->asJson([
  534. 'code' => 0,
  535. 'msg' => 'success',
  536. 'data' => $returnData['data'],
  537. ]);
  538. }
  539. public function actionSubmitOrder()
  540. {
  541. $post = \post_params();
  542. $address_data = [];
  543. $orderNoHead = substr($post['order_no'], 0, 2);
  544. if($orderNoHead == 'PO'){
  545. $address_data = json_decode($post['address_data'] ?? "", true);
  546. if (empty($address_data)) {
  547. $orderTransit = \app\models\OrderTransit::findOne(['cloud_order_id' => $post['id']]);
  548. if ($orderTransit) {
  549. $order = \app\models\Order::findOne($orderTransit->order_id);
  550. if ($order) {
  551. if ($order->is_offline && $order->md_id) {
  552. $province_name = '';
  553. $city_name = '';
  554. $district_name = '';
  555. $address = '';
  556. $md = \app\models\Md::findOne($order->md_id);
  557. if ($md) {
  558. $province_name = District::findOne($md->province)->name;
  559. $city_name = District::findOne($md->city)->name;
  560. $district_name = District::findOne($md->district)->name;
  561. $address = $md->address;
  562. }
  563. } else {
  564. $province_name = District::findOne($order->province_id)->name;
  565. $city_name = District::findOne($order->city_id)->name;
  566. $district_name = District::findOne($order->district_id)->name;
  567. $address = $order->address;
  568. }
  569. $address_data = [
  570. 'province' => $province_name,
  571. 'city' => $city_name ?? '',
  572. 'district' => $district_name ?? '',
  573. 'address' => $address ?? ''
  574. ];
  575. }
  576. }
  577. }
  578. }
  579. if (isset($post['pifa']) && $post['pifa']) {
  580. $address = $address_data;
  581. if (empty($address)) {
  582. $address = json_decode($post['address_data'], true);
  583. }
  584. $spu = [];
  585. foreach ($post['goods_list'] as $goods) {
  586. $spu[] = [
  587. 'number' => $goods['num'],
  588. 'sku' => $goods['attr']['no'],
  589. ];
  590. }
  591. $params = [
  592. 'spu' => $spu,
  593. 'address' => [
  594. 'consignee' => $post['name'],
  595. 'phone' => $post['mobile'],
  596. 'province' => $address['province'] ?: $post['province'],
  597. 'city' => $address['city'] ?: $post['city'],
  598. 'area' => $address['district'] ?: $post['district'],
  599. 'street' => '',
  600. 'description' => $address['address'] ?: $post['address'],
  601. ]
  602. ];
  603. } else {
  604. $spu = [];
  605. foreach ($post['goods_list'] as $goods) {
  606. $spu[] = [
  607. 'number' => $goods['num'],
  608. 'sku' => $goods['attr']['no'],
  609. ];
  610. }
  611. $params = [
  612. 'spu' => $spu,
  613. 'address' => [
  614. 'consignee' => $post['name'],
  615. 'phone' => $post['mobile'],
  616. 'province' => $post['province'],
  617. 'city' => $post['city'],
  618. 'area' => $post['district'],
  619. 'street' => '',
  620. 'description' => $post['address'],
  621. ]
  622. ];
  623. }
  624. $checkData = \stbz_client()->getApiResponse('post', '/v2/order/beforeCheck', $params);
  625. $checkData = \json_decode($checkData, true);
  626. if ($checkData['code'] != 1) {
  627. return $this->asJson([
  628. 'code' => 1,
  629. 'msg' => $checkData['msg'],
  630. ]);
  631. }
  632. $params['orderSn'] = $post['order_no'];
  633. $data = \stbz_client()->getApiResponse('post', '/v2/order', $params);
  634. $data = \json_decode($data, true);
  635. if ($data['code'] != 1) {
  636. return $this->asJson([
  637. 'code' => 1,
  638. 'msg' => $data['msg'],
  639. ]);
  640. }
  641. //获取订单信息
  642. $order_url = "/cloud/supplier/supplierSetStbzOrderStatus";
  643. $order_data['order_id'] = $post['id'];
  644. $order_data['status'] = 1;
  645. $order_data['access_token'] = get_supplier_token($post['supplier_id']);
  646. $orderNoHead = substr($post['order_no'], 0, 2);
  647. if ($orderNoHead === 'PO') {
  648. $order_data['order_type'] = 1;
  649. } elseif ($orderNoHead === 'OR') {
  650. $order_data['order_type'] = 2;
  651. }
  652. $order_data['status'] = 1;
  653. $domain = (new OptionSetting)->getCloudDomainName();
  654. $orderInfo = cloud_post($domain. $order_url,
  655. $order_data);
  656. $orderInfo = json_decode($orderInfo, true);
  657. if ((int)$orderInfo['code'] !== 0) {
  658. return $this->asJson($orderInfo);
  659. }
  660. return $this->asJson([
  661. 'code' => 0,
  662. 'msg' => '下单成功',
  663. ]);
  664. }
  665. }