Core.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace App\Http\Controllers\Miniprogram;
  3. use App\Exceptions\AlertException;
  4. use App\Jobs\WelcomeJob;
  5. use App\Models\Common\FormidModel;
  6. use App\Models\Log\FpdxMiniprogramIntoQueueModel;
  7. use App\Models\User\Openid;
  8. use Carbon\Carbon;
  9. use Illuminate\Http\JsonResponse;
  10. use Illuminate\Http\Request;
  11. use App\Http\Controllers\Controller;
  12. use Illuminate\Support\Facades\Redis;
  13. use App\Models\Common\TokenModel;
  14. use App\Models\User\AuthKey;
  15. use Ixudra\Curl\Facades\Curl;
  16. /**
  17. * Class Core
  18. * @package App\Http\Controllers\Miniprogram
  19. */
  20. class Core extends Controller
  21. {
  22. /**
  23. * 发送客服消息
  24. * @param int $uid to用户uid
  25. * @param array $data
  26. * @return boolean
  27. * @throws \Exception
  28. */
  29. public function custom(int $uid = null, array $data)
  30. {
  31. $public_id = config('miniprogram.public_id');
  32. $authKey = new AuthKey();
  33. if (is_null($data['touser'])) {
  34. $openid = $authKey->getKeyOfType($uid, $public_id);
  35. $data['touser'] = $openid;
  36. }
  37. $access_token = TokenModel::getToken($public_id);
  38. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";
  39. $response = Curl::to($url)->withData(json_encode($data, JSON_UNESCAPED_UNICODE))->asJsonResponse()->post();
  40. if (isset($response->errcode) && 0 == $response->errcode) {
  41. return true;
  42. } else {
  43. return false;
  44. }
  45. }
  46. /**
  47. * 发送模板消息
  48. * @param int $uid to用户uid
  49. * @param string $template_id
  50. * @param string $public_id
  51. * @param string $page
  52. * @param array $datas
  53. * @param string $page_type
  54. * @return bool
  55. */
  56. public function template(
  57. $uid,
  58. string $template_id,
  59. string $public_id,
  60. $page = null,
  61. array $datas = array(),
  62. string $page_type = "miniprogram"
  63. ) {
  64. try {
  65. $openid = Openid::ofPublic($uid, $public_id)->value('openid');
  66. $data = [
  67. 'touser' => $openid,
  68. 'template_id' => $template_id,
  69. 'data' => $datas
  70. ];
  71. if ($page_type == "miniprogram") {
  72. $data['miniprogram'] = [
  73. "appid" => "wx4c1722a4055bd229",
  74. "pagepath" => $page,
  75. ];
  76. }
  77. if ($page_type == "h5") {
  78. $data['url'] = $page;
  79. }
  80. foreach ($datas as $key => $value) {
  81. $data['data'][$key] = array(
  82. 'value' => $value['value'],
  83. 'color' => $value['color'] ?? "#000000"
  84. );
  85. }
  86. $access_token = TokenModel::getToken($public_id);
  87. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
  88. $response = \Curl::to($url)->withData($data)->asJson()->post();
  89. if (isset($response->errcode) && !$response->errcode) {
  90. return true;
  91. }
  92. } catch (\Exception $exception) {
  93. }
  94. return false;
  95. }
  96. public function miniTemplate(int $to_uid, string $template_id, string $public_id, $page, array $data)
  97. {
  98. $openid = AuthKey::where('uid', $to_uid)
  99. ->where('auth_type', $public_id)
  100. ->value('auth_key');
  101. if (empty($openid)) {
  102. return false;
  103. }
  104. $form = FormidModel::where([
  105. ['openid', $openid],
  106. ['public_id', $public_id],
  107. ['state', 0],
  108. ['created_at', '>', time() - 86400 * 6.5]
  109. ])->orderBy('created_at', 'asc')->first();
  110. if (collect($form)->isEmpty()) {
  111. return false;
  112. }
  113. $access_token = TokenModel::getToken($public_id);
  114. $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$access_token}";
  115. $data = [
  116. 'touser' => $form->openid,
  117. 'template_id' => $template_id,
  118. 'page' => $page,
  119. 'form_id' => $form->form_id,
  120. 'data' => $data
  121. ];
  122. $result = \Curl::to($url)->withData($data)->asJson()->post();
  123. $form->state = 1;
  124. $form->save();
  125. if (isset($result->errcode) && $result->errcode) {
  126. return false;
  127. }
  128. return true;
  129. }
  130. /**
  131. * 登录凭证校验
  132. * @param string jscode
  133. * @return JsonResponse
  134. * {
  135. * "openid": openid,
  136. * "unionid": 有|没有,
  137. * "session_key": 会话密钥
  138. * }
  139. * @throws \Exception
  140. */
  141. public function jscode2session(string $jscode)
  142. {
  143. $app_id = config('miniprogram.app_id');
  144. $app_secret = config('miniprogram.app_secret');
  145. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$app_id}&secret={$app_secret}&js_code={$jscode}&grant_type=authorization_code";
  146. $response = \Curl::to($url)->asJsonResponse()->get();
  147. if (isset($response->errcode)) {
  148. throw new AlertException($response->errmsg, $response->errcode);
  149. } else {
  150. try {
  151. if (!FpdxMiniprogramIntoQueueModel::where('openid', $response->openid)->exists()) {
  152. WelcomeJob::dispatch($response->openid)->delay(Carbon::now()->addHours(1));
  153. FpdxMiniprogramIntoQueueModel::create(['openid' => $response->openid, 'created_at' => time()]);
  154. }
  155. } catch (\Exception $exception) {
  156. }
  157. return $response;
  158. }
  159. }
  160. /**
  161. * 判断应用是否开启
  162. * @return array
  163. */
  164. public function isOpen()
  165. {
  166. $public_id = config('miniprogram.public_id');
  167. $is_open = Redis::hget("miniprogram:open", $public_id) ?? 1;
  168. return array(
  169. 'code' => 200,
  170. 'message' => 'success',
  171. 'data' => $is_open
  172. );
  173. }
  174. /**
  175. * 获取B类二维码
  176. * @param Request $request
  177. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  178. * @throws \Exception
  179. */
  180. public function getQrcodeB(Request $request)
  181. {
  182. $this->validate($request, [
  183. 'scene' => 'required'
  184. ]);
  185. $public_id = config('miniprogram.public_id');
  186. $access_token = TokenModel::getToken($public_id);
  187. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$access_token}";
  188. $data = [
  189. 'scene' => urldecode($request->input('scene')),
  190. 'width' => 430,
  191. 'auto_color' => false,
  192. 'line_color' => [
  193. 'r' => 0,
  194. 'g' => 0,
  195. 'b' => 0,
  196. ],
  197. 'is_hyaline' => false,
  198. ];
  199. $response = \Curl::to($url)->withData($data)->asJsonRequest()->post();
  200. if (isset($response->errcode) || isset($response['errcode'])) {
  201. app('sentry')->captureMessage("微信API调用异常", array(), array(
  202. 'level' => 'info',
  203. 'extra' => array(
  204. 'errcode' => $response->errcode,
  205. 'errmsg' => $response->errmsg
  206. )
  207. ));
  208. }
  209. return response($response, 200, ['Content-Type' => 'image/png']);
  210. }
  211. /**
  212. * 判断应用是否开启
  213. * @param string kolid
  214. * @return string url
  215. * @throws \Exception
  216. *
  217. */
  218. public function getKolQrcode(string $kolid)
  219. {
  220. $public_id = config('miniprogram.public_id');
  221. $access_token = TokenModel::getToken($public_id);
  222. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$access_token}";
  223. $data = [
  224. 'scene' => "media_id={$kolid}",
  225. 'width' => '430',
  226. 'auto_color' => false,
  227. 'line_color' => [
  228. 'r' => 0,
  229. 'g' => 0,
  230. 'b' => 0,
  231. ],
  232. 'is_hyaline' => false,
  233. ];
  234. $response = \Curl::to($url)->withData($data)->asJsonRequest()->post();
  235. return response($response, 200, ['Content-Type' => 'image/png']);
  236. }
  237. }