Weapp.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\shopcomponent\model;
  11. use addon\weapp\model\Config as WeappConfigModel;
  12. use addon\wxoplatform\model\Config as WxOplatformConfigModel;
  13. use app\model\BaseModel;
  14. use EasyWeChat\Factory;
  15. use think\facade\Cache;
  16. class Weapp extends BaseModel
  17. {
  18. public function __construct($site_id = 0)
  19. {
  20. //微信小程序配置
  21. $weapp_config_model = new WeappConfigModel();
  22. $weapp_config = $weapp_config_model->getWeappConfig($site_id);
  23. $weapp_config = $weapp_config[ "data" ][ "value" ];
  24. if (isset($weapp_config[ 'is_authopen' ]) && addon_is_exit('wxoplatform')) {
  25. $plateform_config_model = new WxOplatformConfigModel();
  26. $plateform_config = $plateform_config_model->getOplatformConfig();
  27. $plateform_config = $plateform_config[ "data" ][ "value" ];
  28. $config = [
  29. 'app_id' => $plateform_config[ "appid" ] ?? '',
  30. 'secret' => $plateform_config[ "secret" ] ?? '',
  31. 'token' => $plateform_config[ "token" ] ?? '',
  32. 'aes_key' => $plateform_config[ "aes_key" ] ?? '',
  33. 'log' => [
  34. 'level' => 'debug',
  35. 'permission' => 0777,
  36. 'file' => 'runtime/log/wechat/oplatform.logs',
  37. ],
  38. ];
  39. $open_platform = Factory::openPlatform($config);
  40. $this->app = $open_platform->miniProgram($weapp_config[ 'authorizer_appid' ], $weapp_config[ 'authorizer_refresh_token' ]);
  41. } else {
  42. $config = [
  43. 'app_id' => $weapp_config[ "appid" ] ?? '',
  44. 'secret' => $weapp_config[ "appsecret" ] ?? '',
  45. 'response_type' => 'array',
  46. 'log' => [
  47. 'level' => 'debug',
  48. 'permission' => 0777,
  49. 'file' => 'runtime/log/wechat/easywechat.logs',
  50. ],
  51. ];
  52. $this->app = Factory::miniProgram($config);
  53. }
  54. }
  55. /**
  56. * 检测自定义交易组件接入状态
  57. * @return array
  58. * @throws \GuzzleHttp\Exception\GuzzleException
  59. */
  60. public function checkRegister()
  61. {
  62. try {
  63. $result = $this->app->mini_store->check();
  64. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  65. return $this->success($result[ 'data' ]);
  66. } else {
  67. return $this->error('', $result[ 'errmsg' ]);
  68. }
  69. } catch (\Exception $e) {
  70. return $this->error('', $e->getMessage());
  71. }
  72. }
  73. /**
  74. * 接入申请
  75. * @return array
  76. * @throws \GuzzleHttp\Exception\GuzzleException
  77. */
  78. public function apply()
  79. {
  80. try {
  81. $result = $this->app->mini_store->apply();
  82. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  83. return $this->success();
  84. } else {
  85. return $this->error('', $result[ 'errmsg' ]);
  86. }
  87. } catch (\Exception $e) {
  88. return $this->error('', $e->getMessage());
  89. }
  90. }
  91. /**
  92. * 获取微信类目
  93. * @return array
  94. */
  95. public function getCatList()
  96. {
  97. try {
  98. $result = $this->app->mini_store->get();
  99. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  100. return $this->success($result[ 'third_cat_list' ]);
  101. } else {
  102. return $this->error('', $result[ 'errmsg' ]);
  103. }
  104. } catch (\Exception $e) {
  105. return $this->error('', $e->getMessage());
  106. }
  107. }
  108. /**
  109. * 提交类目资质
  110. * @param $param
  111. * @return array
  112. */
  113. public function auditCategory($param)
  114. {
  115. try {
  116. $result = $this->app->mini_store->auditCategory($param);
  117. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  118. return $this->success($result[ 'audit_id' ]);
  119. } else {
  120. return $this->error($result[ 'errcode' ] ?? '', $result[ 'errmsg' ]);
  121. }
  122. } catch (\Exception $e) {
  123. return $this->error('', $e->getMessage());
  124. }
  125. }
  126. /**
  127. * 添加商品
  128. * @param $param
  129. * @return array
  130. */
  131. public function addSpu($param)
  132. {
  133. try {
  134. $result = $this->app->mini_store->addSpu($param);
  135. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  136. return $this->success($result[ 'data' ]);
  137. } else {
  138. return $this->error('', $result[ 'errmsg' ]);
  139. }
  140. } catch (\Exception $e) {
  141. return $this->error('', $e->getMessage());
  142. }
  143. }
  144. /**
  145. * 更新商品
  146. * @param $param
  147. * @return array
  148. * @throws \GuzzleHttp\Exception\GuzzleException
  149. */
  150. public function updateSpu($param)
  151. {
  152. try {
  153. $result = $this->app->mini_store->updateSpu($param);
  154. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  155. return $this->success($result[ 'data' ]);
  156. } else {
  157. return $this->error('', $result[ 'errmsg' ]);
  158. }
  159. } catch (\Exception $e) {
  160. return $this->error('', $e->getMessage());
  161. }
  162. }
  163. /**
  164. * 获取商品
  165. * @param $param
  166. * @return array
  167. * @throws \GuzzleHttp\Exception\GuzzleException
  168. */
  169. public function getSpuPage($param)
  170. {
  171. try {
  172. $result = $this->app->mini_store->getSpuList($param);
  173. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  174. return $this->success([ 'total' => $result[ 'total_num' ], 'list' => $result[ 'spus' ] ]);
  175. } else {
  176. return $this->error('', $result[ 'errmsg' ]);
  177. }
  178. } catch (\Exception $e) {
  179. return $this->error('', $e->getMessage());
  180. }
  181. }
  182. /**
  183. * 商品上架
  184. * @param $param
  185. * @return array
  186. * @throws \GuzzleHttp\Exception\GuzzleException
  187. */
  188. public function listing($param)
  189. {
  190. try {
  191. $result = $this->app->mini_store->listingSpu($param[ 'product_id' ] ?? '', $param[ 'out_product_id' ] ?? '');
  192. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  193. return $this->success();
  194. } else {
  195. return $this->error('', $result[ 'errmsg' ]);
  196. }
  197. } catch (\Exception $e) {
  198. return $this->error('', $e->getMessage());
  199. }
  200. }
  201. /**
  202. * 商品下架
  203. * @param $param
  204. * @return array
  205. * @throws \GuzzleHttp\Exception\GuzzleException
  206. */
  207. public function delisting($param)
  208. {
  209. try {
  210. $result = $this->app->mini_store->delistingSpu($param[ 'product_id' ] ?? '', $param[ 'out_product_id' ] ?? '');
  211. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  212. return $this->success();
  213. } else {
  214. return $this->error('', $result[ 'errmsg' ]);
  215. }
  216. } catch (\Exception $e) {
  217. return $this->error('', $e->getMessage());
  218. }
  219. }
  220. /**
  221. * 删除商品
  222. * @param $param
  223. * @return array
  224. * @throws \GuzzleHttp\Exception\GuzzleException
  225. */
  226. public function delSpu($param)
  227. {
  228. try {
  229. $result = $this->app->mini_store->delSpu($param[ 'product_id' ] ?? '', $param[ 'out_product_id' ] ?? '');
  230. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  231. return $this->success();
  232. } else {
  233. return $this->error('', $result[ 'errmsg' ]);
  234. }
  235. } catch (\Exception $e) {
  236. return $this->error('', $e->getMessage());
  237. }
  238. }
  239. /**
  240. * 创建订单
  241. * @param $param
  242. * @return array
  243. */
  244. public function addOrder($param)
  245. {
  246. try {
  247. $result = $this->app->mini_store->addOrder($param);
  248. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  249. return $this->success($result[ 'data' ]);
  250. } else {
  251. return $this->error('', $result[ 'errmsg' ]);
  252. }
  253. } catch (\Exception $e) {
  254. return $this->error('', $e->getMessage());
  255. }
  256. }
  257. /**
  258. * 获取订单
  259. * @param $param
  260. * @return array
  261. */
  262. public function getOrder($param)
  263. {
  264. try {
  265. $result = $this->app->mini_store->getOrder($param[ 'order_id' ] ?? '', $param[ 'out_order_id' ] ?? '', $param[ 'openid' ]);
  266. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  267. return $this->success($result[ 'order' ]);
  268. } else {
  269. return $this->error('', $result[ 'errmsg' ]);
  270. }
  271. } catch (\Exception $e) {
  272. return $this->error('', $e->getMessage());
  273. }
  274. }
  275. /**
  276. * 订单支付状态同步
  277. * @param $param
  278. * @return array
  279. * @throws \GuzzleHttp\Exception\GuzzleException
  280. */
  281. public function pay($param)
  282. {
  283. try {
  284. $result = $this->app->mini_store->payOrder($param);
  285. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  286. return $this->success();
  287. } else {
  288. return $this->error('', $result[ 'errmsg' ]);
  289. }
  290. } catch (\Exception $e) {
  291. return $this->error('', $e->getMessage());
  292. }
  293. }
  294. /**
  295. * 获取快递公司列表
  296. * @return array
  297. * @throws \GuzzleHttp\Exception\GuzzleException
  298. */
  299. public function getCompanyList()
  300. {
  301. $cache = Cache::get('weixinCompanyList');
  302. if ($cache) return $cache;
  303. try {
  304. $result = $this->app->mini_store->getDeliveryCompanyList();
  305. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  306. $data = $this->success($result[ 'company_list' ]);
  307. Cache::set('weixinCompanyList', $data);
  308. return $data;
  309. } else {
  310. return $this->error('', $result[ 'errmsg' ]);
  311. }
  312. } catch (\Exception $e) {
  313. return $this->error('', $e->getMessage());
  314. }
  315. }
  316. /**
  317. * 订单发货
  318. * @param $param
  319. * @return array
  320. * @throws \GuzzleHttp\Exception\GuzzleException
  321. */
  322. public function sendDelivery($param)
  323. {
  324. try {
  325. $result = $this->app->mini_store->sendDelivery($param[ 'order_id' ] ?? '', $param[ 'out_order_id' ] ?? '', $param[ 'openid' ], $param[ 'finish_all_delivery' ], $param[ 'delivery_list' ]);
  326. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  327. return $this->success();
  328. } else {
  329. return $this->error('', $result[ 'errmsg' ]);
  330. }
  331. } catch (\Exception $e) {
  332. return $this->error('', $e->getMessage());
  333. }
  334. }
  335. /**
  336. * 订单收货
  337. * @param $param
  338. * @return array
  339. * @throws \GuzzleHttp\Exception\GuzzleException
  340. */
  341. public function recieveDelivery($param)
  342. {
  343. try {
  344. $result = $this->app->mini_store->recieveDelivery($param[ 'order_id' ] ?? '', $param[ 'out_order_id' ] ?? '', $param[ 'openid' ]);
  345. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  346. return $this->success();
  347. } else {
  348. return $this->error('', $result[ 'errmsg' ]);
  349. }
  350. } catch (\Exception $e) {
  351. return $this->error('', $e->getMessage());
  352. }
  353. }
  354. /**
  355. * 创建售后
  356. * @param $param
  357. * @return array
  358. * @throws \GuzzleHttp\Exception\GuzzleException
  359. */
  360. public function addAftersale($param)
  361. {
  362. try {
  363. $result = $this->app->mini_store->addAftersale($param);
  364. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  365. return $this->success();
  366. } else {
  367. return $this->error('', $result[ 'errmsg' ]);
  368. }
  369. } catch (\Exception $e) {
  370. return $this->error('', $e->getMessage());
  371. }
  372. }
  373. /**
  374. * 获取审核状态
  375. * @param $audit_id
  376. * @return array
  377. */
  378. public function getAuditResult($audit_id)
  379. {
  380. try {
  381. $result = $this->app->mini_store->auditResult($audit_id);
  382. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  383. return $this->success($result[ 'data' ]);
  384. } else {
  385. return $this->error('', $result[ 'errmsg' ]);
  386. }
  387. } catch (\Exception $e) {
  388. return $this->error('', $e->getMessage());
  389. }
  390. }
  391. /**
  392. * 完成接入任务
  393. * @param $item
  394. * @return array
  395. */
  396. public function finishAccessInfo($item)
  397. {
  398. try {
  399. $result = $this->app->mini_store->finish_access_info($item);
  400. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  401. return $this->success();
  402. } else {
  403. return $this->error('', $result[ 'errmsg' ]);
  404. }
  405. } catch (\Exception $e) {
  406. return $this->error('', $e->getMessage());
  407. }
  408. }
  409. /**
  410. * 获取图片信息
  411. * @param $item
  412. * @return array
  413. */
  414. public function getImg($url)
  415. {
  416. try {
  417. $result = $this->app->mini_store->uploadImg($url);
  418. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  419. return $this->success($result);
  420. } else {
  421. return $this->error('', $result[ 'errmsg' ]);
  422. }
  423. } catch (\Exception $e) {
  424. return $this->error('', $e->getMessage());
  425. }
  426. }
  427. public function updateShop($params)
  428. {
  429. try {
  430. $result = $this->app->mini_store->updateShop($params);
  431. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  432. return $this->success($result);
  433. } else {
  434. return $this->error('', $result[ 'errmsg' ]);
  435. }
  436. } catch (\Exception $e) {
  437. return $this->error('', $e->getMessage());
  438. }
  439. }
  440. public function updateOrderType($params)
  441. {
  442. try {
  443. $result = $this->app->mini_store->updateOrderType($params);
  444. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  445. return $this->success($result);
  446. } else {
  447. return $this->error('', $result[ 'errmsg' ]);
  448. }
  449. } catch (\Exception $e) {
  450. return $this->error('', $e->getMessage());
  451. }
  452. }
  453. public function createOrder($params)
  454. {
  455. try {
  456. $result = $this->app->mini_store->addOrder($params);
  457. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  458. return $this->success($result);
  459. } else {
  460. return $this->error('', $result[ 'errmsg' ]);
  461. }
  462. } catch (\Exception $e) {
  463. return $this->error('', $e->getMessage());
  464. }
  465. }
  466. public function getPaymentParams($params)
  467. {
  468. try {
  469. $result = $this->app->mini_store->getPaymentParams($params);
  470. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  471. return $this->success($result);
  472. } else {
  473. return $this->error('', $result[ 'errmsg' ]);
  474. }
  475. } catch (\Exception $e) {
  476. return $this->error('', $e->getMessage());
  477. }
  478. }
  479. /**
  480. * @param $params
  481. * @return array
  482. * 同意退款
  483. */
  484. public function orderRefund($params)
  485. {
  486. try {
  487. $result = $this->app->mini_store->orderRefund($params);
  488. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  489. return $this->success($result);
  490. } else {
  491. return $this->error('', $result[ 'errmsg' ]);
  492. }
  493. } catch (\Exception $e) {
  494. return $this->error('', $e->getMessage());
  495. }
  496. }
  497. /**
  498. * @param $params
  499. * @return array
  500. * 拒绝退款
  501. */
  502. public function orderNoRefund($params)
  503. {
  504. try {
  505. $result = $this->app->mini_store->orderNoRefund($params);
  506. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  507. return $this->success($result);
  508. } else {
  509. return $this->error('', $result[ 'errmsg' ]);
  510. }
  511. } catch (\Exception $e) {
  512. return $this->error('', $e->getMessage());
  513. }
  514. }
  515. /**
  516. * @param $params
  517. * @return array
  518. * 同意退货
  519. */
  520. public function aceptreturn($params)
  521. {
  522. try {
  523. $result = $this->app->mini_store->aceptreturn($params);
  524. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  525. return $this->success($result);
  526. } else {
  527. return $this->error('', $result[ 'errmsg' ]);
  528. }
  529. } catch (\Exception $e) {
  530. return $this->error('', $e->getMessage());
  531. }
  532. }
  533. /**
  534. * @param $params
  535. * @return array
  536. * 同意退货
  537. */
  538. public function cancel($params)
  539. {
  540. try {
  541. $result = $this->app->mini_store->cancel($params);
  542. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  543. return $this->success($result);
  544. } else {
  545. return $this->error('', $result[ 'errmsg' ]);
  546. }
  547. } catch (\Exception $e) {
  548. return $this->error('', $e->getMessage());
  549. }
  550. }
  551. /**
  552. * 卖家发货
  553. */
  554. public function uploadreturninfo($params)
  555. {
  556. try {
  557. $result = $this->app->mini_store->uploadreturninfo($params);
  558. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  559. return $this->success($result);
  560. } else {
  561. return $this->error('', $result[ 'errmsg' ]);
  562. }
  563. } catch (\Exception $e) {
  564. return $this->error('', $e->getMessage());
  565. }
  566. }
  567. //获取售后详情
  568. public function getAftersale($params)
  569. {
  570. try {
  571. $result = $this->app->mini_store->getAftersale($params);
  572. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  573. return $this->success($result);
  574. } else {
  575. return $this->error('', $result[ 'errmsg' ]);
  576. }
  577. } catch (\Exception $e) {
  578. return $this->error('', $e->getMessage());
  579. }
  580. }
  581. /**
  582. * 更新售后
  583. * @param $param
  584. * @return array
  585. * @throws \GuzzleHttp\Exception\GuzzleException
  586. */
  587. public function updateAftersale($params)
  588. {
  589. try {
  590. $result = $this->app->mini_store->updateAftersale($params);
  591. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  592. return $this->success($result);
  593. } else {
  594. return $this->error('', $result[ 'errmsg' ]);
  595. }
  596. } catch (\Exception $e) {
  597. return $this->error('', $e->getMessage());
  598. }
  599. }
  600. /**
  601. * @param $params
  602. * @return array
  603. * 获取售后订单列表
  604. */
  605. public function getOrderList($params)
  606. {
  607. try {
  608. $result = $this->app->mini_store->getOrderList($params);
  609. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  610. return $this->success($result);
  611. } else {
  612. return $this->error('', $result[ 'errmsg' ]);
  613. }
  614. } catch (\Exception $e) {
  615. return $this->error('', $e->getMessage());
  616. }
  617. }
  618. }