WeChatController.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exceptions\WeChatException;
  4. use App\Http\Controllers\Core\User;
  5. use App\Models\Daily\DailyPushModel;
  6. use App\Models\Daily\SubscribeModel;
  7. use App\Models\User\AuthKey;
  8. use App\Models\User\Openid;
  9. use App\Services\User\ProfileService;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Redis;
  12. use Ixudra\Curl\Facades\Curl;
  13. class WeChatController extends Controller
  14. {
  15. //
  16. private $resource;
  17. public function server(Request $request)
  18. {
  19. if (isset($_GET['echostr'])) {
  20. return $_GET['echostr'];
  21. } else {
  22. $xml = file_get_contents('php://input');
  23. $this->resource = xml2array($xml);
  24. try {
  25. switch ($this->resource['MsgType']) {
  26. case 'text':
  27. return $this->text();
  28. break;
  29. case 'image':
  30. return $this->image();
  31. break;
  32. case 'voice':
  33. return $this->voice();
  34. break;
  35. case 'video':
  36. return $this->video();
  37. break;
  38. case 'shortvideo':
  39. return $this->shortVideo();
  40. break;
  41. case 'event':
  42. switch ($this->resource['Event']) {
  43. case 'subscribe':
  44. return $this->subscribe();
  45. break;
  46. case 'unsubscribe':
  47. return $this->unSubscribe();
  48. break;
  49. case 'SCAN':
  50. return $this->scan();
  51. break;
  52. case "CLICK":
  53. return $this->click();
  54. }
  55. break;
  56. }
  57. } catch (\Exception $e) {
  58. throw new WeChatException();
  59. }
  60. }
  61. }
  62. private function text()
  63. {
  64. $resource = $this->resource;
  65. switch ($this->resource['Content']) {
  66. case "开启每日推送":
  67. SubscribeModel::updateOrCreate(
  68. ['openid' => $resource['FromUserName'], 'public_id' => $resource['ToUserName']],
  69. ['subscribe' => 1]
  70. );
  71. $resource['Content'] = "已成功开启「时遇记」每日推送\n\n每晚 10:00 不见不散\n时光微妙,遇见你真好~\n\n如需关闭请回复“关闭每日推送”";
  72. return $this->returnText($resource);
  73. break;
  74. case "关闭每日推送":
  75. $resource = $this->resource;
  76. $resource['Content'] = "正在关闭「每日推送」消息提醒\n\n关闭后,你将无法收到小遇文章推送,每晚10:00小遇就不能陪你啦~\n\n确认关闭,请<a href='weixin://bizmsgmenu?msgmenucontent=确认关闭每日推送&msgmenuid=234234'>【点此】</a>~";
  77. return $this->returnText($resource);
  78. break;
  79. case '确认关闭每日推送':
  80. SubscribeModel::updateOrCreate(
  81. ['openid' => $resource['FromUserName'], 'public_id' => $resource['ToUserName']],
  82. ['subscribe' => 0]
  83. );
  84. $resource['Content'] = "已成功关闭「每日推送」\n\n每晚 10:00 ,小遇就不能准时给你报时啦~\n不过无论什么时候你想小遇了,或者说遇到什么心事了,随时记得来找小遇哦,小遇一直都在~\n\n<a href='weixin://bizmsgmenu?msgmenucontent=开启每日推送&msgmenuid=234234'>重新开启请【点此】</a>";
  85. return $this->returnText($resource);
  86. break;
  87. case '今日文章':
  88. $article = DailyPushModel::where('date', '<=', date('Y-m-d'))->orderBy('date', 'desc')->first();
  89. $content = $article->reply_content . "\n\n<a href='{$article->article_url}'>点此查看文章~</a>";
  90. // $page = "pages/starter/starter?launch_type=free&url=".urlencode("/pages/article-redirect/article-redirect?url=".urlencode($article->article_url));
  91. // $content = $article->reply_content."\n\n<a href='{$article->article_url}' data-miniprogram-path='{$page}' data-miniprogram-appid='wx4c1722a4055bd229'>点此查看文章~</a>";
  92. $resource['Content'] = $content;
  93. return $this->returnText($resource);
  94. break;
  95. }
  96. }
  97. private function image()
  98. {
  99. }
  100. private function voice()
  101. {
  102. }
  103. private function video()
  104. {
  105. }
  106. private function shortVideo()
  107. {
  108. }
  109. private function subscribe()
  110. {
  111. $resource = $this->resource;
  112. try {
  113. $openid = $resource['FromUserName'];
  114. $public_id = $resource['ToUserName'];
  115. $userinfo = \WeChat::Fans($public_id)->info($openid);
  116. $unionid = $userinfo['unionid'];
  117. $auth = AuthKey::where('auth_key', $unionid)->first();
  118. if (!collect($auth)->isEmpty()) {
  119. $uid = $auth->uid;
  120. $response = Curl::to("http://api.deep.fenpeiduixiang.com/api/analytic/check/new/scan?uid={$uid}")->asJsonResponse(false)->get();
  121. $isexsts = Redis::Sismember("log:bevip:page:tag", $uid);
  122. if ((isset($response->isScan) && 1 == $response->isScan) || $isexsts) {
  123. $ps = new ProfileService();
  124. $ps->beVip($uid, true);
  125. }
  126. AuthKey::updateOrCreate(
  127. ['auth_key' => $openid, 'auth_type' => $public_id],
  128. ['uid' => $uid, 'login_at' => time()]
  129. );
  130. }
  131. } catch (\Exception $exception) {
  132. }
  133. $articles = [
  134. [
  135. 'Title' => '分配对象啦!让我们谈一场72小时的恋爱好吗?',
  136. 'Description' => '点击查看活动详情,内含快捷报名通道',
  137. 'PicUrl' => 'http://mmbiz.qpic.cn/mmbiz_png/8a2FAXRUa7wjyhEBiberqMn6cicHlc9OY1kOPC5XibibAKXEPeWdhpqp8RZRiawyXZ6Z2cXz2UFGlhGlyjakbibDYjIQ/0?wx_fmt=png',
  138. 'Url' => 'https://mp.weixin.qq.com/s/8QRAKfxZgJ6LS1gKMRHqyw',
  139. ]
  140. ];
  141. return $this->returnNews($resource, $articles);
  142. }
  143. private function unSubscribe()
  144. {
  145. $resource = $this->resource;
  146. try {
  147. $openid = $resource['FromUserName'];
  148. Openid::where('openid', $openid)->update(['subscribe' => 0]);
  149. $openData = User::getUidByOpenid($openid);
  150. $profs = new ProfileService();
  151. $profs->beVip($openData['uid'], false);
  152. } catch (\Exception $exception) {
  153. throw new WeChatException();
  154. }
  155. }
  156. private function scan()
  157. {
  158. }
  159. private function click()
  160. {
  161. $resource = $this->resource;
  162. switch ($this->resource['EventKey']) {
  163. case 'menu_36850':
  164. SubscribeModel::updateOrCreate(
  165. ['openid' => $resource['FromUserName'], 'public_id' => $resource['ToUserName']],
  166. ['subscribe' => 1]
  167. );
  168. $resource['Content'] = "已成功开启「时遇记」每日推送\n\n每晚 10:00 不见不散\n时光微妙,遇见你真好~\n\n如需关闭请回复“关闭每日推送”";
  169. return $this->returnText($resource);
  170. break;
  171. }
  172. }
  173. /**
  174. * 发送文本消息
  175. * @param object $object
  176. * @return string
  177. */
  178. public function returnText($resuorce)
  179. {
  180. $template = '<xml>
  181. <ToUserName><![CDATA[%s]]></ToUserName>
  182. <FromUserName><![CDATA[%s]]></FromUserName>
  183. <CreateTime>%d</CreateTime>
  184. <MsgType><![CDATA[text]]></MsgType>
  185. <Content><![CDATA[%s]]></Content>
  186. <FuncFlag>0</FuncFlag>
  187. </xml>';
  188. return sprintf(
  189. $template,
  190. $resuorce['FromUserName'],
  191. $resuorce['ToUserName'],
  192. $resuorce['CreateTime'],
  193. $resuorce['Content']
  194. );
  195. }
  196. public function returnImage($resource)
  197. {
  198. $template = '<xml>
  199. <ToUserName><![CDATA[%s]]></ToUserName>
  200. <FromUserName><![CDATA[%s]]></FromUserName>
  201. <CreateTime>%d</CreateTime>
  202. <MsgType><![CDATA[image]]></MsgType>
  203. <Image><MediaId><![CDATA[%s]]></MediaId></Image>
  204. </xml>';
  205. return sprintf(
  206. $template,
  207. $resource['FromUserName'],
  208. $resource['ToUserName'],
  209. $resource['CreateTime'],
  210. $resource['MediaId']
  211. );
  212. }
  213. public function returnNews($resource, array $array)
  214. {
  215. $template = '<item>
  216. <Title><![CDATA[%s]]></Title>
  217. <Description><![CDATA[%s]]></Description>
  218. <PicUrl><![CDATA[%s]]></PicUrl>
  219. <Url><![CDATA[%s]]></Url>
  220. </item>';
  221. $item = null;
  222. foreach ($array as $value) {
  223. $item .= sprintf($template, @$value['Title'], @$value['Description'], @$value['PicUrl'], @$value['Url']);
  224. }
  225. $template = '<xml>
  226. <ToUserName><![CDATA[%s]]></ToUserName>
  227. <FromUserName><![CDATA[%s]]></FromUserName>
  228. <CreateTime>%d</CreateTime>
  229. <MsgType><![CDATA[news]]></MsgType>
  230. <ArticleCount>%d</ArticleCount>
  231. <Articles>%s</Articles>
  232. </xml>';
  233. return sprintf(
  234. $template,
  235. $resource['FromUserName'],
  236. $resource['ToUserName'],
  237. $resource['CreateTime'],
  238. count($array),
  239. $item
  240. );
  241. }
  242. }