MiniProgram.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace app\samos\wechat;
  3. use EasyWeChat\Factory;
  4. use app\model\Config;
  5. use app\model\Test;
  6. class MiniProgram
  7. {
  8. static function makemini()
  9. {
  10. $Configdata = Config::getconfig('miniprogram');
  11. $appdata['lan'] = Author()::getlan();
  12. $t = input('get.t', '', 'serach_in');
  13. if (empty($t)) {
  14. if (!empty($Configdata['app_id']) && !empty($Configdata['secret'])) {
  15. return Factory::miniProgram([
  16. 'app_id' => trim($Configdata['app_id']),
  17. 'secret' => trim($Configdata['secret'])
  18. ]);
  19. }
  20. } else {
  21. if (!empty($Configdata['techapp_id']) && !empty($Configdata['techsecret'])) {
  22. return Factory::miniProgram([
  23. 'app_id' => trim($Configdata['techapp_id']),
  24. 'secret' => trim($Configdata['techsecret'])
  25. ]);
  26. }
  27. }
  28. }
  29. static function getQrcode($scene, $page, $width = 230)
  30. {
  31. $app = self::makemini();
  32. if (!empty($app)) {
  33. $response = $app->app_code->getUnlimit($scene, [
  34. 'page' => $page,
  35. 'width' => $width,
  36. ]);
  37. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  38. $filename = $response->save(config('filesystem.disks.public.root'), 'qrcode' . md5(base64_encode(time())) . '.jpg');
  39. }
  40. $ifw7ppath = dirname(dirname(dirname(dirname(__DIR__))));
  41. $ifw7ppatharray = explode('/', $ifw7ppath);
  42. if (end($ifw7ppatharray) == 'addons') {
  43. $w7ppath = 'addons/' . config('database.app_name') . '/';
  44. } else {
  45. $w7ppath = '';
  46. }
  47. $appdata['lan'] = Author()::getlan();
  48. return toimg($w7ppath . 'public/uploads/' . $filename);
  49. }
  50. }
  51. static function getQrcode2($page, $width = 230)
  52. {
  53. $app = self::makemini();
  54. if (!empty($app)) {
  55. $response = $app->app_code->getQrCode($page, $width);
  56. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  57. $filename = $response->save(config('filesystem.disks.public.root'), 'qrcode' . md5(base64_encode(time())) . '.jpg');
  58. }
  59. $ifw7ppath = dirname(dirname(dirname(dirname(__DIR__))));
  60. $ifw7ppatharray = explode('/', $ifw7ppath);
  61. if (end($ifw7ppatharray) == 'addons') {
  62. $w7ppath = 'addons/' . config('database.app_name') . '/';
  63. } else {
  64. $w7ppath = '';
  65. }
  66. $appdata['lan'] = Author()::getlan();
  67. return toimg($w7ppath . 'public/uploads/' . $filename);
  68. }
  69. }
  70. //判断是否存在页面
  71. static function ispagethereare($page)
  72. {
  73. //判断如果第一个字符是“/”,则去掉
  74. $firststr = mb_substr($page, 0, 1);
  75. if ($firststr == '/') {
  76. $page = mb_substr($page, 1);
  77. }
  78. $app = self::makemini();
  79. if (!empty($app)) {
  80. $response = $app->app_code->getUnlimit('i', [
  81. 'page' => $page,
  82. 'width' => 200,
  83. ]);
  84. }
  85. //Test::create(['title' => $page . '测试判断是否存在页面', 'info' => serialize($response)]);
  86. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  87. return true;
  88. } else {
  89. return false;
  90. }
  91. }
  92. static function urllink($param)
  93. {
  94. $app = self::makemini();
  95. if (!empty($app)) {
  96. $url = 'https://api.weixin.qq.com/wxa/generate_urllink?access_token=' . $app->access_token->getToken(true)['access_token'];
  97. $retlink = urlpost($url, $param);
  98. $appdata['lan'] = Author()::getlan();
  99. return $retlink;
  100. }
  101. }
  102. static function createLiveRoom($data)
  103. {
  104. $create_data = [
  105. 'name' => '', // 房间名字
  106. 'coverImg' => '', // 通过 uploadfile 上传,填写 mediaID
  107. 'startTime' => 0, // 开始时间
  108. 'endTime' => 0, // 结束时间
  109. 'anchorName' => '', // 主播昵称
  110. 'anchorWechat' => '', // 主播微信号
  111. 'shareImg' => '', //通过 uploadfile 上传,填写 mediaID
  112. 'feedsImg' => '', //通过 uploadfile 上传,填写 mediaID
  113. 'isFeedsPublic' => 1, // 是否开启官方收录,1 开启,0 关闭
  114. 'type' => 1, // 直播类型,1 推流 0 手机直播
  115. 'screenType' => 0, // 1:横屏 0:竖屏
  116. 'closeLike' => 0, // 是否 关闭点赞 1 关闭
  117. 'closeGoods' => 0, // 是否 关闭商品货架,1:关闭
  118. 'closeComment' => 0, // 是否开启评论,1:关闭
  119. 'closeReplay' => 1, // 是否关闭回放 1 关闭
  120. 'closeShare' => 0, // 是否关闭分享 1 关闭
  121. 'closeKf' => 0 // 是否关闭客服,1 关闭
  122. ];
  123. $params = array_merge($create_data, $data);
  124. $app = self::makemini();
  125. if (!empty($app)) {
  126. $params['coverImg'] = $app->media->uploadImage(localpic($params['coverImg']))['media_id'];
  127. $params['shareImg'] = $app->media->uploadImage(localpic($params['shareImg']))['media_id'];
  128. $params['feedsImg'] = $params['coverImg'];
  129. $url = 'https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token=' . $app->access_token->getToken(true)['access_token'];
  130. $res = urlpost($url, $params);
  131. $appdata['lan'] = Author()::getlan();
  132. return $res;
  133. }
  134. }
  135. static function getphonenumber($code)
  136. {
  137. $param['code'] = $code;
  138. $app = self::makemini();
  139. if (!empty($app)) {
  140. $access_token = $app->access_token->getToken(true)['access_token'];
  141. $url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=' . $access_token;
  142. $phonenumberdate = urlpost($url, $param);
  143. if ($phonenumberdate['errmsg'] == 'ok') {
  144. $date = $phonenumberdate['phone_info'];
  145. }
  146. $appdata['lan'] = Author()::getlan();
  147. }
  148. return $date;
  149. }
  150. static function subscribemessage($tpl)
  151. {
  152. //订单支付
  153. $data['pay_tpl']['tid'] = 1221; // 模板标题 id,可通过接口获取,也可登录小程序后台查看获取
  154. $data['pay_tpl']['kidList'] = [1, 2, 3, 4, 6];
  155. $data['pay_tpl']['sceneDesc'] = '通知用户订单已支付成功'; // 服务场景描述,非必填
  156. $appdata['lan'] = Author()::getlan();
  157. return $data[$tpl];
  158. }
  159. }