123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647 |
- <?php
- namespace crmeb\services\wechat;
- use crmeb\services\wechat\config\MiniProgramConfig;
- use crmeb\services\wechat\live\LiveClient;
- use crmeb\services\wechat\orderShipping\OrderClient;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\Exceptions\DecryptException;
- use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
- use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
- use EasyWeChat\Kernel\Support\Collection;
- use EasyWeChat\MiniProgram\Application;
- use GuzzleHttp\Exception\GuzzleException;
- use Psr\Http\Message\ResponseInterface;
- use Symfony\Component\HttpFoundation\Request;
- use think\facade\Cache;
- use think\Response;
- use Yurun\Util\Swoole\Guzzle\SwooleHandler;
- use crmeb\services\wechat\live\ServiceProvider as LiveServiceProvider;
- use Symfony\Component\Cache\Adapter\RedisAdapter;
- use crmeb\services\wechat\orderShipping\ServiceProvider as OrderShippingServiceProvider;
- class MiniProgram extends BaseApplication
- {
-
- protected $config;
-
- protected $application;
-
- protected static $property = [
- 'mediaService' => 'media',
- 'staffService' => 'customer_service',
- 'encryptor' => 'encryptor',
- 'qrcodeService' => 'app_code',
- 'subscribenoticeService' => 'subscribe_message',
- 'liveService' => 'live',
- 'orderShippingService' => 'orderShipping',
- ];
-
- public function __construct()
- {
- $this->config = app(MiniProgramConfig::class);
- $this->debug = DefaultConfig::value('logger');
- }
-
- public function application()
- {
- if (!$this->application) {
- $this->application = Factory::miniProgram($this->config->all());
- $request = request();
- $this->application['guzzle_handler'] = SwooleHandler::class;
- $this->application->rebind('request', new Request($request->get(), $request->post(), [], [], [], $request->server(), $request->getContent()));
- $this->application->register(new LiveServiceProvider());
- $this->application->register(new OrderShippingServiceProvider());
- $this->application->rebind('cache', new RedisAdapter(Cache::store('redis')->handler()));
- }
- return $this->application;
- }
- public static function serve(): Response
- {
- $make = self::instance();
- $make->application()->server->push($make->pushMessageHandler);
- $response = $make->application()->server->serve();
- return response($response->getContent());
- }
-
- public static function instance()
- {
- return app()->make(self::class);
- }
-
- public static function getUserInfo(string $code)
- {
- try {
- $response = self::instance()->application()->auth->session($code);
- self::logger('获得用户信息 根据code 获取session_key', compact('code'), $response);
- return $response;
- } catch (\Throwable $e) {
- throw new WechatException($e->getMessage());
- }
- }
-
- public static function decryptData(string $sessionKey, string $iv, string $encryptData)
- {
- $response = self::encryptor()->decryptData($sessionKey, $iv, $encryptData);
- self::logger('解密数据', compact('sessionKey', 'iv', 'encryptData'), $response);
- return $response;
- }
-
- public static function appCodeUnlimit(string $scene, string $path = '', int $width = 0)
- {
- $optional = [
- 'page' => $path,
- 'width' => $width
- ];
- if (!$optional['page']) {
- unset($optional['page']);
- }
- if (!$optional['width']) {
- unset($optional['width']);
- }
- $response = self::qrcodeService()->getUnlimit($scene, $optional);
- self::logger('获取小程序码', compact('scene', 'optional'), $response);
- return $response;
- }
-
- public static function sendSubscribeTemlate(string $touser, string $templateId, array $data, string $link = '')
- {
- $response = self::subscribenoticeService()->send([
- 'template_id' => $templateId,
- 'touser' => $touser,
- 'page' => $link,
- 'data' => $data
- ]);
- self::logger('发送订阅消息', compact('templateId', 'touser', 'link', 'data'), $response);
- return $response;
- }
-
- public static function addSubscribeTemplate(string $tid, array $kidList, string $sceneDesc = '')
- {
- try {
- $res = self::subscribenoticeService()->addTemplate($tid, $kidList, $sceneDesc);
- self::logger('添加订阅消息模版', compact('tid', 'kidList', 'sceneDesc'), $res);
- if (isset($res['errcode']) && $res['errcode'] == 0 && isset($res['priTmplId'])) {
- return $res['priTmplId'];
- } else {
- throw new WechatException($res['errmsg']);
- }
- } catch (\Throwable $e) {
- throw new WechatException($e);
- }
- }
-
- public static function delSubscribeTemplate(string $templateId)
- {
- try {
- $response = self::subscribenoticeService()->deleteTemplate($templateId);
- self::logger('删除订阅消息', compact('templateId'), $response);
- return $response;
- } catch (\Throwable $e) {
- throw new WechatException($e->getMessage());
- }
- }
-
- public static function getSubscribeTemplateKeyWords(string $tid)
- {
- try {
- $res = self::subscribenoticeService()->getTemplateKeywords($tid);
- self::logger('获取模版标题的关键词列表', compact('tid'), $res);
- if (isset($res['errcode']) && $res['errcode'] == 0 && isset($res['data'])) {
- return $res['data'];
- } else {
- throw new WechatException($res['errmsg']);
- }
- } catch (\Throwable $e) {
- throw new WechatException($e);
- }
- }
-
- public static function getLiveInfo(int $page = 1, int $limit = 10)
- {
- try {
- $res = self::liveService()->getRooms($page, $limit);
- self::logger('获取直播列表', compact('page', 'limit'), $res);
- if (isset($res['errcode']) && $res['errcode'] == 0 && isset($res['room_info']) && $res['room_info']) {
- return $res['room_info'];
- } else {
- return [];
- }
- } catch (\Throwable $e) {
- return [];
- }
- }
-
- public static function getLivePlayback(int $room_id, int $page = 1, int $limit = 10)
- {
- try {
- $res = self::liveService()->getPlaybacks($room_id, $page, $limit);
- self::logger('获取直播回放', compact('room_id', 'page', 'limit'), $res);
- if (isset($res['errcode']) && $res['errcode'] == 0 && isset($res['live_replay'])) {
- return $res['live_replay'];
- } else {
- throw new WechatException($res['errmsg']);
- }
- } catch (\Throwable $e) {
- throw new WechatException(ErrorMessage::getValidMessgae($e));
- }
- }
-
- public static function createLiveRoom(array $data)
- {
- try {
- $res = self::liveService()->createRoom($data);
- self::logger('创建直播间', compact('data'), $res);
- if (isset($res['errcode']) && $res['errcode'] == 0 && isset($res['roomId'])) {
- unset($res['errcode']);
- return $res;
- } else {
- throw new WechatException($res['errmsg']);
- }
- } catch (\Throwable $e) {
- throw new WechatException(ErrorMessage::getValidMessgae($e));
- }
- }
-
- public static function roomAddGoods(int $roomId, $ids)
- {
- try {
- $res = self::liveService()->roomAddGoods($roomId, $ids);
- self::logger('直播间添加商品', compact('roomId', 'ids'), $res);
- if (isset($res['errcode']) && $res['errcode'] == 0) {
- return true;
- } else {
- throw new WechatException($res['errmsg']);
- }
- } catch (\Throwable $e) {
- throw new WechatException(ErrorMessage::getValidMessgae($e));
- }
- }
-
- public static function getGoodsList(int $status = 2, int $page = 1, int $limit = 10)
- {
- try {
- $res = self::liveService()->getGoodsList($status, $page, $limit);
- self::logger('获取商品列表', compact('status', 'page', 'limit'), $res);
- if (isset($res['errcode']) && $res['errcode'] == 0 && isset($res['goods'])) {
- return $res['goods'];
- } else {
- throw new WechatException($res['errmsg']);
- }
- } catch (\Throwable $e) {
- throw new WechatException(ErrorMessage::getValidMessgae($e));
- }
- }
-
- public static function getGooodsInfo($goods_ids)
- {
- try {
- $res = self::liveService()->getGooodsInfo($goods_ids);
- self::logger('获取商品详情', compact('goods_ids'), $res);
- if (isset($res['errcode']) && $res['errcode'] == 0 && isset($res['goods'])) {
- return $res['goods'];
- } else {
- throw new WechatException($res['errmsg']);
- }
- } catch (\Throwable $e) {
- throw new WechatException(ErrorMessage::getValidMessgae($e));
- }
- }
-
- public static function addGoods(string $coverImgUrl, string $name, int $priceType, string $url, $price, $price2 = '')
- {
- try {
- $res = self::liveService()->addGoods($coverImgUrl, $name, $priceType, $url, $price, $price2);
- self::logger('添加商品', compact('coverImgUrl', 'name', 'priceType', 'url', 'price', 'price2'), $res);
- if (isset($res['errcode']) && $res['errcode'] == 0 && isset($res['goodsId'])) {
- unset($res['errcode']);
- return $res;
- } else {
- throw new WechatException($res['errmsg']);
- }
- } catch (\Throwable $e) {
- throw new WechatException(ErrorMessage::getValidMessgae($e));
- }
- }
-
- public static function resetauditGoods(int $goodsId, $auditId)
- {
- try {
- $res = self::liveService()->resetauditGoods($goodsId, $auditId);
- self::logger('商品撤回审核', compact('goodsId', 'auditId'), $res);
- if (isset($res['errcode']) && $res['errcode'] == 0) {
- return true;
- } else {
- throw new WechatException($res['errmsg']);
- }
- } catch (\Throwable $e) {
- throw new WechatException(ErrorMessage::getValidMessgae($e));
- }
- }
-
- public static function auditGoods(int $goodsId)
- {
- try {
- $res = self::liveService()->auditGoods($goodsId);
- self::logger('商品重新提交审核', compact('goodsId'), $res);
- if (isset($res['errcode']) && $res['errcode'] == 0 && isset($res['auditId'])) {
- return $res['auditId'];
- } else {
- throw new WechatException($res['errmsg']);
- }
- } catch (\Throwable $e) {
- throw new WechatException(ErrorMessage::getValidMessgae($e));
- }
- }
-
- public static function deleteGoods(int $goodsId)
- {
- try {
- $res = self::liveService()->deleteGoods($goodsId);
- self::logger('删除商品', compact('goodsId'), $res);
- if (isset($res['errcode']) && $res['errcode'] == 0) {
- return true;
- } else {
- throw new WechatException($res['errmsg']);
- }
- } catch (\Throwable $e) {
- throw new WechatException(ErrorMessage::getValidMessgae($e));
- }
- }
-
- public static function updateGoods(int $goodsId, string $coverImgUrl, string $name, int $priceType, string $url, $price, $price2 = '')
- {
- try {
- $res = self::liveService()->updateGoods($goodsId, $coverImgUrl, $name, $priceType, $url, $price, $price2);
- self::logger('更新商品', compact('goodsId', 'coverImgUrl', 'name', 'priceType', 'url', 'price', 'price2'), $res);
- if (isset($res['errcode']) && $res['errcode'] == 0) {
- return true;
- } else {
- throw new WechatException($res['errmsg']);
- }
- } catch (\Throwable $e) {
- throw new WechatException(ErrorMessage::getValidMessgae($e));
- }
- }
-
- public static function getRoleList($role = 2, int $page = 0, int $limit = 30, $keyword = '')
- {
- try {
- $res = self::liveService()->getRoleList($role, $page, $limit, $keyword);
- self::logger('获取成员列表', compact('role', 'page', 'limit', 'keyword'), $res);
- if (isset($res['errcode']) && $res['errcode'] == 0 && isset($res['list'])) {
- return $res['list'];
- } else {
- throw new WechatException($res['errmsg']);
- }
- } catch (\Throwable $e) {
- throw new WechatException(ErrorMessage::getValidMessgae($e));
- }
- }
-
- public static function temporaryUpload(string $path, string $type = 'image')
- {
- $response = self::mediaService()->upload($type, $path);
- self::logger('小程序-临时素材上传', compact('path', 'type'), $response);
- return new WechatResponse($response);
- }
-
- public static function shippingByTradeNo(string $out_trade_no, int $logistics_type, array $shipping_list, string $payer_openid, string $path, int $delivery_mode = 1, bool $is_all_delivered = true)
- {
- return self::orderShippingService()->shippingByTradeNo($out_trade_no, $logistics_type, $shipping_list, $payer_openid, $path, $delivery_mode, $is_all_delivered);
- }
-
- public static function combinedShippingByTradeNo(string $out_trade_no, int $logistics_type, array $sub_orders, string $payer_openid, int $delivery_mode = 2, bool $is_all_delivered = false)
- {
- return self::orderShippingService()->combinedShippingByTradeNo($out_trade_no, $logistics_type, $sub_orders, $payer_openid, $delivery_mode, $is_all_delivered);
- }
-
- public static function notifyConfirmByTradeNo(string $merchant_trade_no, string $received_time)
- {
- return self::orderShippingService()->notifyConfirmByTradeNo($merchant_trade_no, $received_time);
- }
-
- public static function isManaged()
- {
- return self::orderShippingService()->checkManaged();
- }
-
- public static function setMesJumpPathAndCheck($path)
- {
- return self::orderShippingService()->setMesJumpPathAndCheck($path);
- }
- }
|