MiniProgramService.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace app\main\service;
  3. use app\common\library\Redis;
  4. use EasyWeChat\Factory;
  5. use \EasyWeChat\Kernel\Http\StreamResponse;
  6. use fast\Random;
  7. use think\Config;
  8. class MiniProgramService
  9. {
  10. protected static $self = null;
  11. private static $appId;
  12. private static $secret;
  13. public static $theme;
  14. public static function instance()
  15. {
  16. if (self::$self == null) {
  17. self::$appId = config('site.mini_appid');
  18. self::$secret = config('site.mini_secret');
  19. self::$theme = config('site.theme');
  20. if (!self::$appId){
  21. self::getMiniFromRedis();
  22. }
  23. self::$self = new self();
  24. }
  25. return self::$self;
  26. }
  27. public static function initMiniProgram()
  28. {
  29. $wConfig = config::get('wechat');
  30. $config['app_id'] = self::$appId;
  31. $config['secret'] = self::$secret;
  32. $config['log'] = $wConfig['log'];
  33. $app = Factory::miniProgram($config);
  34. return $app;
  35. }
  36. public static function getMiniFromRedis(){
  37. $site = Redis::instance()->get('site');
  38. $site = json_decode($site,true);
  39. self::$theme = $site['theme']??'qt';
  40. self::$appId = $site['mini_appid'] ?? 'wx5413e3ee18d63891';
  41. self::$secret = $site['mini_secret'] ?? '3f90edade24ecbff6353e9ba9d175049';
  42. }
  43. /**
  44. * 生成发送的小程序内容
  45. * @param $item
  46. * @param $channelId
  47. * @return string
  48. */
  49. public function getMiniProgram($mini,$channelId)
  50. {
  51. $channelId = self::$theme.'_'.$channelId;
  52. if ( !isset($mini['type']) || !$mini['type']){
  53. return '';
  54. }
  55. $content = $mini;
  56. $content['page'] = trim($content['page']);
  57. $t = '';
  58. switch ($content['type']){
  59. case 1:
  60. $content['page'] .= '?channelCode='.$channelId;
  61. $t = self::getMiniText($content['page'],$content['content']);
  62. break;
  63. case 2:
  64. $content['page'] .= '?channelCode='.$channelId;
  65. $t = self::getMiniImg($content['page'],$content['pic']);
  66. // print_r($t);exit;
  67. break;
  68. case 3:
  69. $content['page'] .= '?channelCode='.$channelId;
  70. $t = self::getMiniCard($content['page'],$content['title'],$content['pic']);
  71. break;
  72. case 4:
  73. $scene = '@channelCode='.$channelId;
  74. $t = self::getAppCode($scene,$content['page'],$channelId);
  75. break;
  76. default:
  77. break;
  78. }
  79. return $t;
  80. }
  81. public static function getAppCode($scene,$page,$channelId, $cache = false)
  82. {
  83. $redisKey = 'MINI:'.$channelId.$page;
  84. if($cache){
  85. $url = Redis::instance()->get($redisKey);
  86. if (!empty($url)){
  87. return $url;
  88. }
  89. }
  90. $app = self::initMiniProgram();
  91. $path = '/uploads/wcode/'.date('Ymd').'/';
  92. $picPath = ROOT_PATH.'public/'.$path;
  93. $page = trim($page,'/');
  94. $picFileName = time().$channelId.rand(1000,9999).'.png';
  95. try{
  96. $response = $app->app_code->getUnlimit($scene,['page'=>$page]);
  97. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  98. $response->saveAs($picPath, $picFileName);
  99. } else {
  100. $msg = var_export($response, true);
  101. LogService::info('高级群发生成小程序码异常01'.$msg);
  102. }
  103. }catch (\Throwable $th){
  104. LogService::error('高级群发生成小程序码异常02'.$th->getMessage());
  105. return '';
  106. }
  107. if ($cache){
  108. Redis::instance()->setex($redisKey,86400,$path.$picFileName);
  109. }
  110. return $path.$picFileName;
  111. }
  112. /**
  113. * 小程序卡片
  114. * @param $appId
  115. * @param $page
  116. * @param $title
  117. * @param $pic
  118. * @return string
  119. */
  120. public static function getMiniCard($page,$title,$pic){
  121. $str = '<mp-miniprogram data-miniprogram-appid="'.self::$appId.'" data-miniprogram-path="'.$page.'" data-miniprogram-title="'.$title.'" data-miniprogram-imageurl="'.$pic.'"></mp-miniprogram>';
  122. return $str;
  123. }
  124. /**
  125. * 文字跳转小程序
  126. * @param $appId
  127. * @param $page
  128. * @param $title
  129. * @return string
  130. */
  131. public static function getMiniText($page,$title,$channelId=0)
  132. {
  133. if ($channelId){
  134. $page = trim($page).'?channelCode='.self::$theme.'_'.$channelId;
  135. }
  136. return '<a data-miniprogram-appid="'.self::$appId.'" data-miniprogram-path="'.$page.'" href="">'.$title.'</a>';
  137. }
  138. /**
  139. * 文字跳转小程序
  140. * @param $appId
  141. * @param $page
  142. * @param $title
  143. * @return string
  144. */
  145. public static function getMiniText2($page,$title,$channelId=0)
  146. {
  147. if ($channelId){
  148. $page = trim($page).'?channelCode='.self::$theme.'_'.$channelId;
  149. }
  150. return "<a data-miniprogram-appid='".self::$appId."' data-miniprogram-path='".$page."' href=''>".$title."</a>";
  151. }
  152. /**
  153. * 图片跳转小程序
  154. * @param $appId
  155. * @param $page
  156. * @param $pic
  157. * @return string
  158. */
  159. public static function getMiniImg($page,$pic)
  160. {
  161. $str = "<p><a data-miniprogram-appid='".self::$appId;
  162. $str .= "' data-miniprogram-path='".$page;
  163. $str .= "' href=''><img src='".$pic;
  164. $str .= "' alt= data-width='200px' data-ratio=''></a></p>";
  165. return $str;
  166. }
  167. /**
  168. * 小程序渠道权限
  169. * @param int $channelId
  170. * @return int
  171. */
  172. public function miniWrite($channelId=0,$type)
  173. {
  174. if (in_array($type,[1,2,6])){//超管 管理员 运营
  175. return 1;
  176. }
  177. $re = 0;
  178. if ($channelId){
  179. $re = (int)model('ChannelSpecialManage')->isWhite('sendMini', $channelId);
  180. }
  181. if (!$re){
  182. $ids = config('site.mini_ids');
  183. if ($ids == -1 ){
  184. $re = 1;
  185. }
  186. if (strstr($ids.',',(string)$channelId.',') !== false){
  187. $re = 1;
  188. }
  189. }
  190. return $re;
  191. }
  192. /**
  193. * 配置小程序入口页面
  194. * @return array
  195. */
  196. public function getMiniPage()
  197. {
  198. $pageStr = config('site.mini_page');
  199. if (!$pageStr){
  200. return [];
  201. }
  202. $pageArr = explode(PHP_EOL,$pageStr);
  203. foreach ($pageArr as $k=>$v){
  204. $temp = explode('@',$v);
  205. $miniPage[trim($temp[1])] = $temp[0];
  206. }
  207. return $miniPage;
  208. }
  209. /**
  210. * 客服消息,处理小程序
  211. * @param $channelId
  212. * @param $args
  213. * @return string
  214. */
  215. public function getMiniContent($channelId, $args)
  216. {
  217. $miniContent = [];
  218. $args['page'] = trim($args['page']);
  219. if (isset($args['mini_type'])){
  220. $miniContent = [
  221. 'mini_type'=>$args['mini_type'],
  222. 'appid'=>self::$appId,
  223. ];
  224. switch ($args['mini_type']){//1是文本类型 2 图片 3小程序卡片 4小程序码
  225. case '1':
  226. $miniContent['title'] = $args['title'];
  227. $miniContent['page'] = $args['page'].'?@channelCode='.self::$theme.'_'.$channelId;
  228. break;
  229. case '2':
  230. break;
  231. case '3':
  232. $miniContent['title'] = $args['title'];
  233. $miniContent['page'] = $args['page'].'?@channelCode='.self::$theme.'_'.$channelId;
  234. $res = HigeMessageService::instance()->image2media($channelId, $args['image']);
  235. $miniContent['media_id'] = $res['media_id']??'';
  236. break;
  237. case '4':
  238. $scene = '@channelCode='.self::$theme.'_'.$channelId;
  239. $miniCode = self::getAppCode($scene,$args['page'],$channelId,true);
  240. $res = HigeMessageService::instance()->image2media($channelId,$miniCode);
  241. $miniContent['media_id'] = $res['media_id']??'';
  242. break;
  243. }
  244. }
  245. return (string)json_encode($miniContent);
  246. }
  247. }