MiniprogrameLianMengUtil.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. namespace App\Http\Utils\Wechat\Miniprograme;
  3. use App\Exceptions\CommonException;
  4. use App\Http\Bean\Util\Wechat\Miniprograme\GetOrderSearchParamBean;
  5. use App\Http\Bean\Util\Wechat\Miniprograme\GetProductGenerateParamBean;
  6. use App\Http\Bean\Util\Wechat\Miniprograme\GetProductListParamBean;
  7. use App\Http\Enum\ErrorEnum;
  8. use App\Http\Utils\BaseUtil;
  9. use App\Http\Utils\LoggerFactoryUtil;
  10. use Tool\ShanTaoTool\HttpCurl;
  11. /**
  12. * 小程序联盟推客
  13. * Class MiniprogrameLianMengUtil
  14. * @package App\Http\Utils\Wechat\Miniprograme
  15. */
  16. class MiniprogrameLianMengUtil extends BaseUtil
  17. {
  18. /**
  19. * 获取接口密钥
  20. * @return bool|string|void
  21. */
  22. public static function getMiniLianMengAccessToken()
  23. {
  24. return parent::getAccessToken(env("WECHAT_MINIPROGRAME_LIANMENG_APP_ID"),env("WECHAT_MINIPROGRAME_LIANMENG_APP_SECRET"));
  25. }
  26. /**
  27. * 获取联盟商品类目列表及类目ID
  28. */
  29. public static function getProductCategory()
  30. {
  31. $url = "https://api.weixin.qq.com/union/promoter/product/category";
  32. $params = [
  33. "access_token"=>self::getMiniLianMengAccessToken()
  34. ];
  35. $res = HttpCurl::getCurl($url,$params);
  36. if($res["errcode"]!=0){
  37. $instance = new LoggerFactoryUtil(MiniprogrameLianMengUtil::class);
  38. $instance->info("微信信息:".json_encode($res));
  39. throw new CommonException(ErrorEnum::ERROR_SYSTEM);
  40. }
  41. return $res["productCats"];
  42. }
  43. /**
  44. * 查询全量商品
  45. * @param GetProductListParamBean $getProductListParamBean
  46. */
  47. public static function getProductList(GetProductListParamBean $getProductListParamBean)
  48. {
  49. $url = "https://api.weixin.qq.com/union/promoter/product/list";
  50. $params = [
  51. "access_token"=>self::getMiniLianMengAccessToken(),
  52. "from"=>$getProductListParamBean->getFrom(),
  53. "limit"=>$getProductListParamBean->getLimit()
  54. ];
  55. //搜索关键词
  56. if($getProductListParamBean->getQuery()){
  57. $params["query"] = $getProductListParamBean->getQuery();
  58. }
  59. //搜索类型
  60. if($getProductListParamBean->getQueryType()){
  61. $params["queryType"] = $getProductListParamBean->getQueryType();
  62. }
  63. //商品最高价格
  64. if($getProductListParamBean->getMaxPrice()){
  65. $params["maxPrice"] = $getProductListParamBean->getMaxPrice();
  66. }
  67. //商品最低价格
  68. if($getProductListParamBean->getMinPrice()){
  69. $params["minPrice"] = $getProductListParamBean->getMinPrice();
  70. }
  71. //佣金金额下限
  72. if($getProductListParamBean->getMinCommissionValue()){
  73. $params["minCommissionValue"] = $getProductListParamBean->getMinCommissionValue();
  74. }
  75. //佣金比例下限
  76. if($getProductListParamBean->getMinCommissionRatio()){
  77. $params["minCommissionRatio"] = $getProductListParamBean->getMinCommissionRatio();
  78. }
  79. //排序
  80. if($getProductListParamBean->getSortType()){
  81. $params["sortType"] = $getProductListParamBean->getSortType();
  82. }
  83. //单个类目
  84. if($getProductListParamBean->getCategoryId()){
  85. $params["categoryId"] = $getProductListParamBean->getCategoryId();
  86. }
  87. //小商店appId列表
  88. if($getProductListParamBean->getShopAppIds()){
  89. $params["shopAppIds"] = $getProductListParamBean->getShopAppIds();
  90. }
  91. //是否有联盟全
  92. if($getProductListParamBean->getHasCoupon()){
  93. $params["hasCoupon"] = $getProductListParamBean->getHasCoupon();
  94. }
  95. //多个类目ID
  96. if($getProductListParamBean->getCategory()){
  97. $params["category"] = $getProductListParamBean->getCategory();
  98. }
  99. //黑名单类目ID
  100. if($getProductListParamBean->getNoCategory()){
  101. $params["noCategory"] = $getProductListParamBean->getNoCategory();
  102. }
  103. //商品ID
  104. if($getProductListParamBean->getProductId()){
  105. $params["productId"] = $getProductListParamBean->getProductId();
  106. }
  107. //配送方式
  108. if($getProductListParamBean->getShippingMethods()){
  109. $params["shippingMethods"] = $getProductListParamBean->getShippingMethods();
  110. }
  111. //发货地址
  112. if($getProductListParamBean->getAddressList()){
  113. $params["addressList"] = $getProductListParamBean->getAddressList();
  114. }
  115. $res = HttpCurl::getCurl($url,$params);
  116. if($res["errcode"]!=0){
  117. $instance = new LoggerFactoryUtil(MiniprogrameLianMengUtil::class);
  118. $instance->info("微信信息:".json_encode($res));
  119. throw new CommonException(ErrorEnum::ERROR_SYSTEM);
  120. }
  121. return [
  122. "total"=>$res["total"],
  123. "product_list"=>$res["productList"]
  124. ];
  125. }
  126. /**
  127. * 查询联盟精选商品
  128. * @param GetProductListParamBean $getProductListParamBean
  129. */
  130. public static function getProductSelect(GetProductListParamBean $getProductListParamBean)
  131. {
  132. $url = "https://api.weixin.qq.com/union/promoter/product/select";
  133. $params = [
  134. "access_token"=>self::getMiniLianMengAccessToken(),
  135. "from"=>$getProductListParamBean->getFrom(),
  136. "limit"=>$getProductListParamBean->getLimit()
  137. ];
  138. //商品最高价格
  139. if($getProductListParamBean->getMaxPrice()){
  140. $params["maxPrice"] = $getProductListParamBean->getMaxPrice();
  141. }
  142. //商品最低价格
  143. if($getProductListParamBean->getMinPrice()){
  144. $params["minPrice"] = $getProductListParamBean->getMinPrice();
  145. }
  146. //佣金金额下限
  147. if($getProductListParamBean->getMinCommissionValue()){
  148. $params["minCommissionValue"] = $getProductListParamBean->getMinCommissionValue();
  149. }
  150. //佣金比例下限
  151. if($getProductListParamBean->getMinCommissionRatio()){
  152. $params["minCommissionRatio"] = $getProductListParamBean->getMinCommissionRatio();
  153. }
  154. //排序
  155. if($getProductListParamBean->getSortType()){
  156. $params["sortType"] = $getProductListParamBean->getSortType();
  157. }
  158. //单个类目
  159. if($getProductListParamBean->getCategoryId()){
  160. $params["categoryId"] = $getProductListParamBean->getCategoryId();
  161. }
  162. //小商店appId列表
  163. if($getProductListParamBean->getShopAppIds()){
  164. $params["shopAppIds"] = $getProductListParamBean->getShopAppIds();
  165. }
  166. //是否有联盟全
  167. if($getProductListParamBean->getHasCoupon()){
  168. $params["hasCoupon"] = $getProductListParamBean->getHasCoupon();
  169. }
  170. //多个类目ID
  171. if($getProductListParamBean->getCategory()){
  172. $params["category"] = $getProductListParamBean->getCategory();
  173. }
  174. //黑名单类目ID
  175. if($getProductListParamBean->getNoCategory()){
  176. $params["noCategory"] = $getProductListParamBean->getNoCategory();
  177. }
  178. //商品ID
  179. if($getProductListParamBean->getProductId()){
  180. $params["productId"] = $getProductListParamBean->getProductId();
  181. }
  182. //配送方式
  183. if($getProductListParamBean->getShippingMethods()){
  184. $params["shippingMethods"] = $getProductListParamBean->getShippingMethods();
  185. }
  186. //发货地址
  187. if($getProductListParamBean->getAddressList()){
  188. $params["addressList"] = $getProductListParamBean->getAddressList();
  189. }
  190. $res = HttpCurl::getCurl($url,$params);
  191. if($res["errcode"]!=0){
  192. $instance = new LoggerFactoryUtil(MiniprogrameLianMengUtil::class);
  193. $instance->info("微信信息:".json_encode($res));
  194. throw new CommonException(ErrorEnum::ERROR_SYSTEM);
  195. }
  196. return [
  197. "total"=>$res["total"],
  198. "product_list"=>$res["productList"]
  199. ];
  200. }
  201. /**
  202. * 获取商品推广素材
  203. * @param GetProductGenerateParamBean $getProductGenerateParamBean
  204. */
  205. public static function getProductGenerate(GetProductGenerateParamBean $getProductGenerateParamBean)
  206. {
  207. $url = "https://api.weixin.qq.com/union/promoter/product/generate?access_token=".self::getMiniLianMengAccessToken();
  208. $params = [
  209. "pid"=>$getProductGenerateParamBean->getPid(),
  210. "productList"=>$getProductGenerateParamBean->getProductList()
  211. ];
  212. // dd(json_encode($params));
  213. $res = HttpCurl::postCurl($url,$params);
  214. if($res["errcode"]!=0){
  215. $instance = new LoggerFactoryUtil(MiniprogrameLianMengUtil::class);
  216. $instance->info("微信信息:".json_encode($res));
  217. throw new CommonException(ErrorEnum::ERROR_SYSTEM);
  218. }
  219. return $res["list"];
  220. }
  221. /**
  222. * 添加推广位
  223. * @param $promotionSourceName string 推广位名称
  224. */
  225. public static function addPromotion($promotionSourceName)
  226. {
  227. $url = "https://api.weixin.qq.com/union/promoter/promotion/add?access_token=".self::getMiniLianMengAccessToken();
  228. $params = [
  229. "promotionSourceName"=>$promotionSourceName
  230. ];
  231. $res = HttpCurl::postCurl($url,$params);
  232. if($res["errcode"]!=0){
  233. $instance = new LoggerFactoryUtil(MiniprogrameLianMengUtil::class);
  234. $instance->info("微信信息:".json_encode($res));
  235. throw new CommonException(ErrorEnum::ERROR_SYSTEM);
  236. }
  237. return $res["pid"];
  238. }
  239. /**
  240. * 删除推广位
  241. * @param $promotionSourcePid string 推广位pid
  242. * @param $promotionSourceName string 推广位名称
  243. * @throws CommonException
  244. */
  245. public static function deletePromotion($promotionSourcePid, $promotionSourceName)
  246. {
  247. $url = "https://api.weixin.qq.com/union/promoter/promotion/del?access_token=".self::getMiniLianMengAccessToken();
  248. $params = [
  249. "promotionSourceName"=>$promotionSourceName,
  250. "promotionSourcePid"=>$promotionSourcePid
  251. ];
  252. $res = HttpCurl::postCurl($url,$params);
  253. if($res["errcode"]!=0){
  254. $instance = new LoggerFactoryUtil(MiniprogrameLianMengUtil::class);
  255. $instance->info("微信信息:".json_encode($res));
  256. throw new CommonException(ErrorEnum::ERROR_SYSTEM);
  257. }
  258. }
  259. /**
  260. * 修改推广位
  261. * @param $previousPromotionInfo array 修改钱推广位信息
  262. * @param $promotionInfo array 修改后推广位信息
  263. */
  264. public static function modifyPromotion($previousPromotionInfo, $promotionInfo)
  265. {
  266. $url = "https://api.weixin.qq.com/union/promoter/promotion/upd?access_token=".self::getMiniLianMengAccessToken();
  267. $params = [
  268. "previousPromotionInfo"=>$previousPromotionInfo,
  269. "promotionInfo"=>$promotionInfo
  270. ];
  271. $res = HttpCurl::postCurl($url,$params);
  272. if($res["errcode"]!=0){
  273. $instance = new LoggerFactoryUtil(MiniprogrameLianMengUtil::class);
  274. $instance->info("微信信息:".json_encode($res));
  275. throw new CommonException(ErrorEnum::ERROR_SYSTEM);
  276. }
  277. }
  278. /**
  279. * 获取推广位列表
  280. * @param $start int 偏移
  281. * @param $limit int 每页数据
  282. */
  283. public static function getPromotionList($start, $limit)
  284. {
  285. $url = "https://api.weixin.qq.com/union/promoter/promotion/list";
  286. $params = [
  287. "access_token"=>self::getMiniLianMengAccessToken(),
  288. "start"=>$start,
  289. "limit"=>$limit
  290. ];
  291. $res = HttpCurl::getCurl($url,$params);
  292. if($res["errcode"]!=0){
  293. $instance = new LoggerFactoryUtil(MiniprogrameLianMengUtil::class);
  294. $instance->info("微信信息:".json_encode($res));
  295. throw new CommonException(ErrorEnum::ERROR_SYSTEM);
  296. }
  297. return [
  298. "total"=>$res["total"],
  299. "promotion_max_cnt"=>$res["promotionMaxCnt"],
  300. "promotion_source_list"=>$res["promotionSourceList"]
  301. ];
  302. }
  303. /**
  304. * 根据订单ID查询订单详情
  305. * @param $orderList array 订单id列表
  306. */
  307. public static function getOrderDetail($orderIdList)
  308. {
  309. $url = "https://api.weixin.qq.com/union/promoter/order/info?access_token=".self::getMiniLianMengAccessToken();
  310. $params = [
  311. "orderIdList"=>$orderIdList
  312. ];
  313. $res = HttpCurl::postCurl($url,$params);
  314. if($res["errcode"]!=0){
  315. $instance = new LoggerFactoryUtil(MiniprogrameLianMengUtil::class);
  316. $instance->info("微信信息:".json_encode($res));
  317. throw new CommonException(ErrorEnum::ERROR_SYSTEM);
  318. }
  319. return $res["orderList"];
  320. }
  321. /**
  322. * 根据订单支付时间、订单分佣状态拉取订单详情
  323. * @param GetOrderSearchParamBean $getOrderSearchParamBean
  324. */
  325. public static function getOrderSearch(GetOrderSearchParamBean $getOrderSearchParamBean)
  326. {
  327. $url = "https://api.weixin.qq.com/union/promoter/order/search";
  328. $params = [
  329. "access_token"=>self::getMiniLianMengAccessToken()
  330. ];
  331. //页码
  332. if($getOrderSearchParamBean->getPage()){
  333. $params["page"] = $getOrderSearchParamBean->getPage();
  334. }
  335. //起始时间戳
  336. if($getOrderSearchParamBean->getStartTimestamp()){
  337. $params["startTimestamp"] = $getOrderSearchParamBean->getStartTimestamp();
  338. }
  339. //结束时间戳
  340. if($getOrderSearchParamBean->getEndTimestamp()){
  341. $params["endTimestamp"] = $getOrderSearchParamBean->getEndTimestamp();
  342. }
  343. $res = HttpCurl::getCurl($url,$params);
  344. if($res["errcode"]!=0){
  345. $instance = new LoggerFactoryUtil(MiniprogrameLianMengUtil::class);
  346. $instance->info("微信信息:".json_encode($res));
  347. throw new CommonException(ErrorEnum::ERROR_SYSTEM);
  348. }
  349. return $res["orderList"];
  350. }
  351. }