Live.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\live\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. class Live extends BaseModel
  16. {
  17. private $app;
  18. /**
  19. * 微信直播间接口错误码
  20. * @var array
  21. */
  22. private $room_error = [
  23. 1003 => '商品id不存在',
  24. 47001 => '入参格式不符合规范',
  25. 200002 => '入参错误',
  26. 300001 => '禁止创建/更新商品 或 禁止编辑&更新房间',
  27. 300002 => '名称长度不符合规则',
  28. 300006 => '图片上传失败',
  29. 300022 => '此房间号不存在',
  30. 300023 => '房间状态 拦截(当前房间状态不允许此操作)',
  31. 300024 => '商品不存在',
  32. 300025 => '商品审核未通过',
  33. 300026 => '房间商品数量已经满额',
  34. 300027 => '导入商品失败',
  35. 300028 => '房间名称违规',
  36. 300029 => '主播昵称违规',
  37. 300030 => '主播微信号不合法',
  38. 300031 => '直播间封面图不合规',
  39. 300032 => '直播间分享图违规',
  40. 300033 => '添加商品超过直播间上限',
  41. 300034 => '主播微信昵称长度不符合要求',
  42. 300035 => '主播微信号不存在',
  43. 300036 => '主播微信号未实名认证',
  44. ];
  45. /**
  46. * 微信直播商品接口错误码
  47. * @var array
  48. */
  49. private $goods_error = [
  50. 300001 => '商品创建功能被封禁',
  51. 300002 => '名称长度不符合规则',
  52. 300003 => '价格输入不合规',
  53. 300004 => '商品名称存在违规违法内容',
  54. 300005 => '商品图片存在违规违法内容',
  55. 300006 => '图片上传失败',
  56. 300007 => '线上小程序版本不存在该链接',
  57. 300008 => '添加商品失败',
  58. 300009 => '商品审核撤回失败',
  59. 300010 => '商品审核状态不对',
  60. 300011 => 'API不允许操作非API创建的商品',
  61. 300012 => '没有提审额度(每天500次提审额度)',
  62. 300013 => '提审失败',
  63. 300014 => '审核中,无法删除',
  64. 300017 => '商品未提审',
  65. 300021 => '商品添加成功,审核失败',
  66. ];
  67. public function __construct($site_id = 0)
  68. {
  69. //微信小程序配置
  70. $weapp_config_model = new WeappConfigModel();
  71. $weapp_config = $weapp_config_model->getWeappConfig($site_id);
  72. $weapp_config = $weapp_config[ "data" ][ "value" ];
  73. if (isset($weapp_config[ 'is_authopen' ]) && addon_is_exit('wxoplatform')) {
  74. $plateform_config_model = new WxOplatformConfigModel();
  75. $plateform_config = $plateform_config_model->getOplatformConfig();
  76. $plateform_config = $plateform_config[ "data" ][ "value" ];
  77. $config = [
  78. 'app_id' => $plateform_config[ "appid" ] ?? '',
  79. 'secret' => $plateform_config[ "secret" ] ?? '',
  80. 'token' => $plateform_config[ "token" ] ?? '',
  81. 'aes_key' => $plateform_config[ "aes_key" ] ?? '',
  82. 'log' => [
  83. 'level' => 'debug',
  84. 'permission' => 0777,
  85. 'file' => 'runtime/log/wechat/oplatform.logs',
  86. ],
  87. ];
  88. $open_platform = Factory::openPlatform($config);
  89. $this->app = $open_platform->miniProgram($weapp_config[ 'authorizer_appid' ], $weapp_config[ 'authorizer_refresh_token' ]);
  90. } else {
  91. $config = [
  92. 'app_id' => $weapp_config[ "appid" ] ?? '',
  93. 'secret' => $weapp_config[ "appsecret" ] ?? '',
  94. 'response_type' => 'array',
  95. 'log' => [
  96. 'level' => 'debug',
  97. 'permission' => 0777,
  98. 'file' => 'runtime/log/wechat/easywechat.logs',
  99. ],
  100. ];
  101. $this->app = Factory::miniProgram($config);
  102. }
  103. }
  104. /**
  105. * 添加永久图片素材
  106. * @param $path
  107. * @return array
  108. * @throws \GuzzleHttp\Exception\GuzzleException
  109. */
  110. public function addImageMedia($path)
  111. {
  112. try {
  113. $result = $this->app->media->uploadImage($path);
  114. if (isset($result[ 'errcode' ])) {
  115. return $this->error('', '图片上传失败');
  116. } else {
  117. return $this->success($result);
  118. }
  119. } catch (\Exception $e) {
  120. return $this->error('', $e->getMessage());
  121. }
  122. }
  123. /**
  124. * 获取直播间列表
  125. * @param int $start
  126. * @param int $limit
  127. * @return array
  128. * @throws \GuzzleHttp\Exception\GuzzleException
  129. */
  130. public function getRoomList($start = 0, $limit = PAGE_LIST_ROWS)
  131. {
  132. try {
  133. $result = $this->app->live->room->getLiveInfo([ 'start' => $start, 'limit' => $limit ]);
  134. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  135. return $this->success([ 'list' => $result[ 'room_info' ], 'total' => $result[ 'total' ] ]);
  136. } else {
  137. return $this->error('', $this->room_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
  138. }
  139. } catch (\Exception $e) {
  140. return $this->error('', $e->getMessage());
  141. }
  142. }
  143. /**
  144. * 获取回放视频
  145. * @param $room_id
  146. * @param int $start
  147. * @param int $limit
  148. * @return array
  149. * @throws \GuzzleHttp\Exception\GuzzleException
  150. */
  151. public function getPlaybackInfo($room_id, $start = 0, $limit = PAGE_LIST_ROWS)
  152. {
  153. try {
  154. $data = [
  155. 'action' => 'get_replay',
  156. 'room_id' => $room_id,
  157. 'start' => $start,
  158. 'limit' => $limit
  159. ];
  160. $result = $this->app->live->room->getLiveInfo($data);
  161. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  162. return $this->success([ 'list' => $result[ 'live_replay' ], 'total' => $result[ 'total' ] ]);
  163. } else {
  164. return $this->error('', $this->room_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
  165. }
  166. } catch (\Exception $e) {
  167. return $this->error('', $e->getMessage());
  168. }
  169. }
  170. /**
  171. * 创建直播间
  172. * @param $param
  173. */
  174. public function createRoom($param)
  175. {
  176. try {
  177. $result = $this->app->live->room->create($param);
  178. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  179. return $this->success($result);
  180. } else {
  181. return $this->error('', $this->room_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
  182. }
  183. } catch (\Exception $e) {
  184. return $this->error('', $e->getMessage());
  185. }
  186. }
  187. /**
  188. * 给直播间添加商品
  189. * @param int $room_id
  190. * @param array $goods_ids
  191. * @return array
  192. * @throws \GuzzleHttp\Exception\GuzzleException
  193. */
  194. public function roomAddGoods(int $room_id, array $goods_ids)
  195. {
  196. try {
  197. $result = $this->app->live->room->addGoods([ 'roomId' => $room_id, 'ids' => $goods_ids ]);
  198. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  199. return $this->success($result);
  200. } else {
  201. return $this->error('', $this->room_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
  202. }
  203. } catch (\Exception $e) {
  204. return $this->error('', $e->getMessage());
  205. }
  206. }
  207. /**
  208. * 获取商品列表
  209. * @param int $start
  210. * @param int $limit
  211. * @param int $status
  212. * @return array
  213. * @throws \GuzzleHttp\Exception\GuzzleException
  214. */
  215. public function getGoodsList($start = 0, $limit = PAGE_LIST_ROWS, $status = 2)
  216. {
  217. try {
  218. $result = $this->app->live->goods->getGoodsList([ 'offset' => $start, 'limit' => $limit, 'status' => $status ]);
  219. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  220. return $this->success($result);
  221. } else {
  222. return $this->error('', $this->goods_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
  223. }
  224. } catch (\Exception $e) {
  225. return $this->error('', $e->getMessage());
  226. }
  227. }
  228. /**
  229. * 添加商品审核
  230. * @param $param
  231. * @return array
  232. */
  233. public function addGoodsAudit($param)
  234. {
  235. try {
  236. $result = $this->app->live->goods->add($param);
  237. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  238. return $this->success($result);
  239. } else {
  240. return $this->error('', $this->goods_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
  241. }
  242. } catch (\Exception $e) {
  243. return $this->error('', $e->getMessage());
  244. }
  245. }
  246. /**
  247. * 删除商品
  248. */
  249. public function deleteGoods($goods_id)
  250. {
  251. try {
  252. $result = $this->app->live->goods->delete([ 'goodsId' => $goods_id ]);
  253. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  254. return $this->success($result);
  255. } else {
  256. return $this->error('', $this->goods_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
  257. }
  258. } catch (\Exception $e) {
  259. return $this->error('', $e->getMessage());
  260. }
  261. }
  262. /**
  263. * 获取商品状态
  264. * @param array $goods_ids
  265. * @return array
  266. * @throws \GuzzleHttp\Exception\GuzzleException
  267. */
  268. public function getGoodsStatus(array $goods_ids)
  269. {
  270. try {
  271. $result = $this->app->live->goods->getStatus([ 'goods_ids' => $goods_ids ]);
  272. if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
  273. return $this->success($result[ 'goods' ]);
  274. } else {
  275. return $this->error('', $this->goods_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
  276. }
  277. } catch (\Exception $e) {
  278. return $this->error('', $e->getMessage());
  279. }
  280. }
  281. }