LiveGoodsLogic.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\live;
  20. use app\common\exception\WechatException;
  21. use app\common\service\WeChatConfigService;
  22. use EasyWeChat\Factory;
  23. use think\Exception;
  24. /**
  25. * 直播商品逻辑层
  26. * Class LiveGoodsLogic
  27. * @package app\adminapi\logic\live
  28. */
  29. class LiveGoodsLogic
  30. {
  31. /**
  32. * @notes 直播商品列表
  33. * @param int $limitOffset
  34. * @param int $limitLength
  35. * @return array|string
  36. * @throws \GuzzleHttp\Exception\GuzzleException
  37. * @throws \Psr\SimpleCache\InvalidArgumentException
  38. * @author cjhao
  39. * @date 2021/11/27 10:52
  40. */
  41. public function lists(int $limitOffset,int $limitLength,int $type){
  42. try{
  43. $config = WeChatConfigService::getMnpConfig();
  44. $app = Factory::miniProgram($config);
  45. $accessToken = $app->access_token->getToken()['access_token'];
  46. $params = ['access_token'=>$accessToken,'status'=>$type,'offset'=>$limitOffset-1,'limit'=>$limitLength];
  47. $result = $app->broadcast->getApproved($params);
  48. if (0 != $result['errcode']) {
  49. throw new WechatException( $result['errmsg'],$result['errcode'] );
  50. }
  51. $goodsList = [];
  52. foreach ($result['goods'] as $goods){
  53. $goodsList[] = [
  54. 'goods_id' => $goods['goodsId'],
  55. 'name' => $goods['name'],
  56. 'image' => $goods['coverImgUrl'],
  57. 'price_type' => $goods['priceType'],
  58. 'url' => $goods['url'],
  59. 'price' => $goods['price'],
  60. 'price2' => $goods['price2'],
  61. ];
  62. }
  63. $list = [
  64. 'lists' => $goodsList,
  65. 'count' => $result['total'],
  66. 'page_no' => $limitOffset,
  67. 'page_size' => $limitLength,
  68. ];
  69. return $list;
  70. }catch (Exception $e){
  71. return $e->getMessage();
  72. }
  73. }
  74. /**
  75. * @notes 添加直播商品
  76. * @param array $post
  77. * @return bool|string
  78. * @author cjhao
  79. * @date 2021/11/23 16:25
  80. */
  81. public function add(array $post)
  82. {
  83. try {
  84. $data = [
  85. 'coverImgUrl' => $post['image'],
  86. 'name' => $post['name'],
  87. 'priceType' => $post['price_type'],
  88. 'price' => $post['price'],
  89. 'price2' => $post['price2'],
  90. 'url' => $post['url'],
  91. ];
  92. $config = WeChatConfigService::getMnpConfig();
  93. $app = Factory::miniProgram($config);
  94. $result = $app->broadcast->create($data);
  95. if (0 != $result['errcode']) {
  96. throw new WechatException($result['errmsg'],$result['errcode']);
  97. }
  98. return true;
  99. } catch (\Exception $e) {
  100. return $e->getMessage();
  101. }
  102. }
  103. /**
  104. * @notes 删除直播商品
  105. * @param int $goods_id
  106. * @author cjhao
  107. * @date 2021/11/23 18:02
  108. */
  109. public function del(int $goodsId)
  110. {
  111. try {
  112. $config = WeChatConfigService::getMnpConfig();
  113. $app = Factory::miniProgram($config);
  114. $result = $app->broadcast->delete($goodsId);
  115. if (0 != $result['errcode']) {
  116. throw new WechatException($result['errmsg'],$result['errcode']);
  117. }
  118. return true;
  119. } catch (Exception $e) {
  120. return $e->getMessage();
  121. }
  122. }
  123. }