MiniProgram.php 5.3 KB

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