SellerMiniproLive.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /**
  3. * 机构直播管理
  4. */
  5. namespace app\api\controller;
  6. use think\facade\Lang;
  7. /**
  8. * DSKMS多用户商城
  9. *
  10. * ----------------------------------------------------------------------------
  11. *
  12. * 控制器
  13. */
  14. class SellerMiniproLive extends MobileSeller
  15. {
  16. private $errorMsg = array(
  17. '-1' => '系统错误',
  18. '1' => '未创建直播间',
  19. '1003' => '商品id不存在',
  20. '47001' => '入参格式不符合规范',
  21. '200002' => '入参错误',
  22. '300001' => '禁止创建/更新商品 或 禁止编辑&更新房间',
  23. '300002' => '名称长度不符合规则',
  24. '300006' => '图片上传失败(如:mediaID过期)',
  25. '300022' => '此房间号不存在',
  26. '300023' => '房间状态 拦截(当前房间状态不允许此操作)',
  27. '300024' => '商品不存在',
  28. '300025' => '商品审核未通过',
  29. '300026' => '房间商品数量已经满额',
  30. '300027' => '导入商品失败',
  31. '300028' => '房间名称违规',
  32. '300029' => '主播昵称违规',
  33. '300030' => '主播微信号不合法',
  34. '300031' => '直播间封面图不合规',
  35. '300032' => '直播间分享图违规',
  36. '300033' => '添加商品超过直播间上限',
  37. '300034' => '主播微信昵称长度不符合要求',
  38. '300035' => '主播微信号不存在',
  39. '300036' => '主播微信号未实名认证',
  40. '300037' => '购物直播频道封面图不合规',
  41. '300038' => '未在小程序管理后台配置客服',
  42. '300039' => '主播副号微信号不合法',
  43. '300040' => '名称含有非限定字符(含有特殊字符)',
  44. '300041' => '创建者微信号不合法',
  45. '300042' => '推流中禁止编辑房间',
  46. '300043' => '每天只允许一场直播开启关注',
  47. '500001' => '副号不合规',
  48. '500002' => '副号未实名',
  49. '500003' => '已经设置过副号了,不能重复设置',
  50. '500004' => '不能设置重复的副号',
  51. '500005' => '副号不能和主号重复',
  52. '600001' => '用户已被添加为小助手',
  53. '600002' => '找不到用户',
  54. '9410000' => '直播间列表为空',
  55. '9410001' => '获取房间失败',
  56. '9410002' => '获取商品失败',
  57. '9410003' => '获取回放失败'
  58. );
  59. public function initialize()
  60. {
  61. parent::initialize();
  62. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/live.lang.php');
  63. }
  64. /**
  65. * @api {POST} api/SellerMiniproLive/get_room_list 直播间列表
  66. * @apiVersion 1.0.0
  67. * @apiGroup SellerMiniproLive
  68. *
  69. * @apiHeader {String} X-DS-KEY 卖家授权token
  70. *
  71. * @apiParam {Int} page 页码
  72. * @apiParam {Int} pagesize 每页显示数量
  73. *
  74. * @apiSuccess {String} code 返回码,10000为成功
  75. * @apiSuccess {String} message 返回消息
  76. * @apiSuccess {String} result 返回数据
  77. */
  78. public function get_room_list()
  79. {
  80. $minipro_live_model = model('minipro_live');
  81. $minipro_live_room_goods_model = model('minipro_live_room_goods');
  82. $room_list = $minipro_live_model->getMiniproLiveList(array(array('member_id', '=', $this->store_info['member_id'])));
  83. foreach ($room_list as $key => $val) {
  84. $room_list[$key]['goods_list'] = $minipro_live_room_goods_model->getMiniproLiveRoomGoodsList(array(array('minipro_live_id', '=', $val['minipro_live_id'])));
  85. foreach ($room_list[$key]['goods_list'] as $k => $v) {
  86. $room_list[$key]['goods_list'][$k]['goods_image_url'] = goods_cthumb($v['goods_image'], 480, $v['store_id']);
  87. }
  88. }
  89. $result = array_merge(array('minipro_live_list' => $room_list), mobile_page($minipro_live_model->page_info));
  90. ds_json_encode(10000, '', $result);
  91. }
  92. /**
  93. * @api {POST} api/SellerMiniproLive/get_room_info 直播间信息
  94. * @apiVersion 1.0.0
  95. * @apiGroup SellerMiniproLive
  96. *
  97. * @apiHeader {String} X-DS-KEY 卖家授权token
  98. *
  99. * @apiParam {Int} minipro_live_id 直播id
  100. *
  101. * @apiSuccess {String} code 返回码,10000为成功
  102. * @apiSuccess {String} message 返回消息
  103. * @apiSuccess {String} result 返回数据
  104. */
  105. public function get_room_info()
  106. {
  107. $minipro_live_id = intval(input('param.minipro_live_id'));
  108. $minipro_live_model = model('minipro_live');
  109. $room_info = $minipro_live_model->getMiniproLiveInfo(array(array('member_id', '=', $this->store_info['member_id']), array('minipro_live_id', '=', $minipro_live_id)));
  110. if (!$room_info) {
  111. ds_json_encode(10001, lang('minipro_live_empty'));
  112. }
  113. ds_json_encode(10000, '', array('minipro_live_info' => $room_info));
  114. }
  115. /**
  116. * @api {POST} api/SellerMiniproLive/add_room 新增直播间
  117. * @apiVersion 1.0.0
  118. * @apiGroup SellerMiniproLive
  119. *
  120. * @apiHeader {String} X-DS-KEY 卖家授权token
  121. *
  122. * @apiParam {Int} goods_id 商品id
  123. *
  124. * @apiSuccess {String} code 返回码,10000为成功
  125. * @apiSuccess {String} message 返回消息
  126. * @apiSuccess {String} result 返回数据
  127. */
  128. public function add_room()
  129. {
  130. $wechat_model = model('wechat');
  131. $wechat_model->getOneWxconfig();
  132. $accessToken = $wechat_model->getAccessToken('miniprogram', 0);
  133. if ($wechat_model->error_code) {
  134. ds_json_encode(10001, $wechat_model->error_message);
  135. }
  136. $data = array(
  137. 'name' => input('param.minipro_live_name'),
  138. 'coverImg' => input('param.coverImg'),
  139. 'startTime' => input('param.minipro_live_start_time'),
  140. 'endTime' => input('param.minipro_live_end_time'),
  141. 'anchorName' => input('param.minipro_live_nickname'),
  142. 'anchorWechat' => input('param.minipro_live_wxid'),
  143. 'shareImg' => input('param.feedsImg'),
  144. 'feedsImg' => input('param.feedsImg'),
  145. 'isFeedsPublic' => 0,
  146. 'type' => 0,
  147. 'screenType' => 0,
  148. 'closeLike' => 0,
  149. 'closeGoods' => 0,
  150. 'closeComment' => 0,
  151. 'closeReplay' => 0,
  152. 'closeShare' => 0,
  153. 'closeKf' => 1,
  154. );
  155. if (!$data['name'] || !$data['coverImg'] || !$data['startTime'] || !$data['endTime'] || !$data['anchorName'] || !$data['anchorWechat'] || !$data['shareImg']) {
  156. ds_json_encode(10001, lang('minipro_live_data_incomplete'));
  157. }
  158. if (mb_strlen($data['name']) < 3 || mb_strlen($data['name']) > 17) {
  159. ds_json_encode(10001, lang('minipro_live_name_error'));
  160. }
  161. if ($data['startTime'] < (TIMESTAMP - 10 * 60)) {
  162. ds_json_encode(10001, lang('minipro_live_start_time_error'));
  163. }
  164. $time = $data['endTime'] - $data['startTime'];
  165. if ($time < 0 || $time > (72 * 3600)) {
  166. ds_json_encode(10001, lang('minipro_live_time_error'));
  167. }
  168. if (mb_strlen($data['anchorName']) < 2 || mb_strlen($data['anchorName']) > 15) {
  169. ds_json_encode(10001, lang('minipro_live_author_error'));
  170. }
  171. $res = http_request('https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token=' . $accessToken, 'POST', $data);
  172. $res = json_decode($res, true);
  173. if (!$res || $res['errcode']) {
  174. $msg = lang('minipro_live_add_fail') . $res['errcode'];
  175. if (isset($this->errorMsg[$res['errcode']])) {
  176. $msg = $this->errorMsg[$res['errcode']];
  177. } else if (isset($res['errmsg'])) {
  178. $msg = $res['errmsg'];
  179. }
  180. ds_json_encode(10001, $msg);
  181. }
  182. $minipro_live_model = model('minipro_live');
  183. $minipro_live_model->addMiniproLive(array(
  184. 'minipro_live_name' => $data['name'],
  185. 'member_id' => $this->store_info['member_id'],
  186. 'store_id' => $this->store_info['store_id'],
  187. 'minipro_live_wxid' => $data['anchorWechat'],
  188. 'minipro_live_nickname' => $data['anchorName'],
  189. 'minipro_live_start_time' => $data['startTime'],
  190. 'minipro_live_end_time' => $data['endTime'],
  191. 'minipro_live_image' => input('param.minipro_live_image'),
  192. 'minipro_live_add_time' => TIMESTAMP,
  193. 'minipro_live_room_id' => $res['roomId'],
  194. ));
  195. ds_json_encode(10000, '', array('roomId' => $res['roomId']));
  196. }
  197. /**
  198. * @api {POST} api/SellerMiniproLive/import_room_goods 导入直播间商品
  199. * @apiVersion 1.0.0
  200. * @apiGroup SellerMiniproLive
  201. *
  202. * @apiHeader {String} X-DS-KEY 卖家授权token
  203. *
  204. * @apiParam {Int} goods_id 商品id
  205. *
  206. * @apiSuccess {String} code 返回码,10000为成功
  207. * @apiSuccess {String} message 返回消息
  208. * @apiSuccess {String} result 返回数据
  209. */
  210. public function import_room_goods()
  211. {
  212. $wechat_model = model('wechat');
  213. $wechat_model->getOneWxconfig();
  214. $accessToken = $wechat_model->getAccessToken('miniprogram', 0);
  215. if ($wechat_model->error_code) {
  216. ds_json_encode(10001, $wechat_model->error_message);
  217. }
  218. $roomId = input('param.roomId');
  219. $ids = input('param.ids/a');
  220. $minipro_live_model = model('minipro_live');
  221. $minipro_live_info = $minipro_live_model->getMiniproLiveInfo(array(array('minipro_live_room_id', '=', $roomId), array('member_id', '=', $this->store_info['member_id'])));
  222. if (!$minipro_live_info) {
  223. ds_json_encode(10001, lang('minipro_live_empty'));
  224. }
  225. $data = array(
  226. 'roomId' => $roomId,
  227. 'ids' => $ids,
  228. );
  229. $res = http_request('https://api.weixin.qq.com/wxaapi/broadcast/room/addgoods?access_token=' . $accessToken, 'POST', $data);
  230. $res = json_decode($res, true);
  231. if (!$res || $res['errcode']) {
  232. ds_json_encode(10001, isset($res['errmsg']) ? $res['errmsg'] : lang('minipro_live_import_goods_fail') . $res['errcode']);
  233. }
  234. $minipro_live_room_goods_model = model('minipro_live_room_goods');
  235. $minipro_live_goods_model = model('minipro_live_goods');
  236. $goods_model = model('goods');
  237. $minipro_live_goods_list = $minipro_live_goods_model->getMiniproLiveGoodsList(array(array('minipro_live_goods_result_id', 'in', $ids)));
  238. foreach ($minipro_live_goods_list as $val) {
  239. $data = array(
  240. 'minipro_live_id' => $minipro_live_info['minipro_live_id'],
  241. 'store_id' => $val['store_id'],
  242. 'store_name' => $val['store_name'],
  243. 'goods_id' => $val['goods_id'],
  244. 'goods_commonid' => $val['goods_commonid'],
  245. 'goods_name' => $val['goods_name'],
  246. 'goods_image' => $val['goods_image'],
  247. 'goods_price' => $val['goods_price'],
  248. 'minipro_live_goods_result_id' => $val['minipro_live_goods_result_id'],
  249. );
  250. $goods_info = $goods_model->getGoodsCommonInfoByID($val['goods_commonid']);
  251. if ($goods_info) {
  252. $data['gc_id'] = $goods_info['gc_id'];
  253. $data['gc_id_1'] = $goods_info['gc_id_1'];
  254. $data['gc_id_2'] = $goods_info['gc_id_2'];
  255. $data['gc_id_3'] = $goods_info['gc_id_3'];
  256. $data['gc_name'] = $goods_info['gc_name'];
  257. }
  258. $minipro_live_room_goods_model->addMiniproLiveRoomGoods($data);
  259. }
  260. ds_json_encode(10000);
  261. }
  262. /**
  263. * @api {POST} api/SellerMiniproLive/get_goods_list 商品列表
  264. * @apiVersion 1.0.0
  265. * @apiGroup SellerMiniproLive
  266. *
  267. * @apiHeader {String} X-DS-KEY 卖家授权token
  268. *
  269. * @apiParam {Int} goods_id 商品id
  270. *
  271. * @apiSuccess {String} code 返回码,10000为成功
  272. * @apiSuccess {String} message 返回消息
  273. * @apiSuccess {String} result 返回数据
  274. */
  275. public function get_goods_list()
  276. {
  277. $condition = array();
  278. $condition[] = array('goodscommon.store_id', '=', $this->store_info['store_id']);
  279. $condition[] = array('goodscommon.goods_verify', '=', 1);
  280. $condition[] = array('goodscommon.goods_state', '=', 1);
  281. $fields = 'goods_id,goodscommon.gc_id,goodscommon.store_id,goodscommon.goods_commonid,goodscommon.goods_name,goodscommon.goods_price,goodscommon.goods_addtime,goodscommon.goods_image,goodscommon.goods_state,goodscommon.goods_lock';
  282. $goods_model = model('goods');
  283. $goods_list = $goods_model->getGoodsUnionList($condition, $fields, 'goodscommon.goods_commonid desc', 'goodscommon.goods_commonid', $this->pagesize);
  284. $minipro_live_goods_model = model('minipro_live_goods');
  285. foreach ($goods_list as $key => $val) {
  286. $goods_list[$key]['goods_image_url'] = goods_cthumb($val['goods_image'], 480, $val['store_id']);
  287. $minipro_live_goods_info = $minipro_live_goods_model->getMiniproLiveGoodsInfo(array(array('goods_commonid', '=', $val['goods_commonid'])));
  288. $goods_list[$key]['minipro_live_goods_info'] = $minipro_live_goods_info;
  289. }
  290. $result = array_merge(array('goods_list' => $goods_list), mobile_page($goods_model->page_info));
  291. ds_json_encode(10000, '', $result);
  292. }
  293. /**
  294. * @api {POST} api/SellerMiniproLive/upload_media 上传临时素材
  295. * @apiVersion 1.0.0
  296. * @apiGroup SellerMiniproLive
  297. *
  298. * @apiHeader {String} X-DS-KEY 卖家授权token
  299. *
  300. * @apiParam {File} file 文件
  301. *
  302. * @apiSuccess {String} code 返回码,10000为成功
  303. * @apiSuccess {String} message 返回消息
  304. * @apiSuccess {String} result 返回数据
  305. */
  306. public function upload_media()
  307. {
  308. $wechat_model = model('wechat');
  309. $wechat_model->getOneWxconfig();
  310. $res = $wechat_model->uploadMedia('image', $_FILES['file']['tmp_name'], $_FILES['file']['type'], $_FILES['file']['name']);
  311. if (!$res['code']) {
  312. ds_json_encode(10001, $res['msg']);
  313. }
  314. $media_id = $res['data'];
  315. $file_name = '';
  316. if (input('param.save')) {
  317. $res = ds_upload_pic(ATTACH_MINIPRO_LIVE, 'file');
  318. if (!$res['code']) {
  319. ds_json_encode(10001, $res['msg']);
  320. }
  321. $file_name = $res['data']['file_name'];
  322. }
  323. ds_json_encode(10000, '', array('media_id' => $media_id, 'file_name' => $file_name));
  324. }
  325. }