Wechat.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * 公众号行为处理
  4. */
  5. namespace app\api\controller;
  6. use think\facade\Db;
  7. use app\api\controller\WechatApi;
  8. /**
  9. *
  10. *
  11. * ----------------------------------------------------------------------------
  12. *
  13. * 微信控制器
  14. */
  15. class Wechat extends MobileMall
  16. {
  17. public $type;
  18. public $wxid;
  19. public $data;
  20. public $weixin;
  21. public function index()
  22. {
  23. //获取配置信息
  24. $wxConfig = model('wechat')->getOneWxconfig();
  25. $this->weixin = new WechatApi($wxConfig);
  26. $this->weixin->valid();
  27. $this->type = $this->weixin->getRev()->getRevType(); //获取消息类型MsgType
  28. $this->wxid = $this->weixin->getRev()->getRevFrom(); //获取消息类型MsgId
  29. $this->data = $this->weixin->getRevData(); //把获取的消息进行转码
  30. $reMsg = '';
  31. switch ($this->type) {
  32. //接收普通消息-文本消息
  33. case 'text':
  34. $content = $this->weixin->getRev()->getRevContent();
  35. break;
  36. //接收事件推送 事件类型,subscribe(订阅)、unsubscribe(取消订阅)
  37. case 'event':
  38. $event = $this->weixin->getRev()->getRevEvent();
  39. $content = json_encode($event);
  40. break;
  41. //接收普通消息-图片消息
  42. case 'image':
  43. $content = json_encode($this->weixin->getRev()->getRevPic());
  44. $reMsg = "图片很美!";
  45. break;
  46. default:
  47. $reMsg = '未识别信息';
  48. }
  49. /**
  50. *处理事件
  51. */
  52. if (!empty($reMsg)) {
  53. echo $this->weixin->text($reMsg)->reply();
  54. exit;
  55. }
  56. //一.接收事件推送
  57. if ($this->type == 'event') {
  58. //1.订阅(关注)事件
  59. if (isset($event['event']) && $event['event'] == 'subscribe') {
  60. $welcome = '欢迎关注';
  61. //当待了事件KEY值,则自动注册 KEY一般为推荐人ID
  62. if ($event['key']) {
  63. $qrscene = explode("qrscene_", $event['key']);
  64. $inviter_id = intval($qrscene[1]);
  65. $config = model('wechat')->getOneWxconfig();
  66. $wechat = new WechatApi($config);
  67. $expire_time = $config['expires_in'];
  68. if ($expire_time > TIMESTAMP) {
  69. //有效期内
  70. $wechat->access_token_ = $config['access_token'];
  71. } else {
  72. $access_token = $wechat->checkAuth();
  73. $web_expires = TIMESTAMP + 7000; // 提前200秒过期
  74. Db::name('wxconfig')->where(array('id' => $config['id']))->update(array('access_token' => $access_token, 'expires_in' => $web_expires));
  75. }
  76. $userinfo = $wechat->getwxUserInfo($this->wxid);
  77. $reg_info = array(
  78. 'member_wxopenid' => $this->wxid,
  79. 'member_wxunionid' => $userinfo['unionid'],
  80. 'nickname' => isset($userinfo['nickname']) ? $userinfo['nickname'] : '',
  81. 'headimgurl' => isset($userinfo['headimgurl']) ? $userinfo['headimgurl'] : '',
  82. 'inviter_id' => $inviter_id
  83. );
  84. $logic_connect_api = model('connectapi', 'logic');
  85. $wx_member = $logic_connect_api->wx_register($reg_info, 'wx');
  86. if (!empty($wx_member)) {
  87. $member_model = model('member');
  88. $member_model->getBuyerToken($wx_member['member_id'], $wx_member['member_name'], 'wap', $wx_member['member_wxopenid']);
  89. }
  90. }
  91. $mbusertoken = Db::name('mbusertoken')->where('member_openid', $this->wxid)->find();
  92. if (!empty($mbusertoken)) {
  93. $ret_url = '。系统已为您自动注册了一个账号,请<a href="' . config('ds_config.h5_site_url') . '/pages/member/index/Index?key=' . $mbusertoken['member_token'] . '&username=' . $mbusertoken['member_name'] . '">点击修改信息</a>';
  94. } else {
  95. $ret_url = '';
  96. }
  97. echo $this->weixin->text($welcome . $ret_url)->reply();
  98. exit;
  99. }
  100. //2.扫码已关注
  101. if (isset($event['event']) && $event['event'] == 'SCAN') {
  102. $welcome = '已关注';
  103. echo $this->weixin->text($welcome)->reply();
  104. exit;
  105. }
  106. //4.点击菜单拉取消息时的事件推送
  107. if ($event['event'] == 'CLICK') {
  108. $click = $event['key'];
  109. switch ($click) {
  110. case "commend": //店铺推荐商品
  111. case "hot": //点击率商品
  112. case "sale": //销售量
  113. case "collect": //收藏量
  114. $reMsg = $this->getGoods($click);
  115. if (!empty($reMsg)) {
  116. $this->MsgTypeNews($reMsg);
  117. } else {
  118. echo $this->weixin->text("success")->reply();
  119. exit;
  120. }
  121. break;
  122. //{后续可待添加}
  123. default:
  124. //从关键词回复中获取
  125. $this->MsgTypeText($click);
  126. echo $this->weixin->text("未定义此菜单事件{$click}")->reply();
  127. exit;
  128. }
  129. }
  130. }
  131. //二.文本消息(关键字回复/商品显示)
  132. if ($this->type == 'text') {
  133. //处理关键字
  134. $this->MsgTypeText($content);
  135. //处理商品的情况
  136. $reMsg = $this->getGoodsByKey($content);
  137. if (!empty($reMsg)) {
  138. $this->MsgTypeNews($reMsg);
  139. }
  140. /*处理其他输入文字*/
  141. echo $this->weixin->text("抱歉,暂时无法对您的输入作出处理。")->reply();
  142. exit;
  143. }
  144. }
  145. /**
  146. *文本格式消息回复
  147. */
  148. private function MsgTypeText($content)
  149. {
  150. //先处理是关键字的情况
  151. $value = $this->keywordsReply($content);
  152. if (!empty($value)) {
  153. echo $this->weixin->text($value['text'])->reply();
  154. exit;
  155. }
  156. }
  157. /**商品图文回复*/
  158. private function MsgTypeNews($reMsg)
  159. {
  160. $k = 0;
  161. foreach ($reMsg as $v) {
  162. $newsData[$k]['Title'] = $v['goods_name'];
  163. $newsData[$k]['Description'] = strip_tags($v['goods_name']);
  164. $newsData[$k]['PicUrl'] = goods_cthumb($v['goods_image']);
  165. $newsData[$k]['Url'] = config('ds_config.h5_site_url') . '/pages/home/goodsdetail/Goodsdetail?goods_id=' . $v['goods_id'];
  166. $k++;
  167. }
  168. echo $this->weixin->news($newsData)->reply();
  169. exit;
  170. }
  171. /**
  172. *关键字回复信息
  173. */
  174. public function keywordsReply($content)
  175. {
  176. //关键字查询
  177. $condition = array();
  178. $condition[] = array('k.keyword', '=', $content);
  179. $value = model('wechat')->getOneJoinWxkeyword($condition, $field = 't.text');
  180. return $value;
  181. }
  182. /**关键字商品信息*/
  183. public function getGoodsByKey($key)
  184. {
  185. $condi = "(goods_name like '%{$key}%' or goods_advword like '%{$key}%' or store_name like '%{$key}%')";
  186. $condi .= " and goods_state = 1 and goods_verify = 1";
  187. $res = Db::name('goods')->where($condi)->limit(4)->field('goods_id,goods_name,goods_image')->select()->toArray();
  188. $res = ds_change_arraykey($res, 'goods_id');
  189. return $res;
  190. }
  191. /**菜单事件商品信息*/
  192. public function getGoods($type)
  193. {
  194. //条件
  195. //后续可待添加
  196. $types = array('hot' => 'goods_click', 'sale' => 'goods_salenum', 'collect' => 'goods_collect', 'commend' => 'goods_commend');
  197. $condition = $types[$type] . ' DESC';
  198. $where = "goods_state = 1 and goods_verify = 1";
  199. $res = Db::name('goods')->field('goods_id,goods_name,goods_image')->where($where)->limit(4)->order($condition)->select()->toArray();
  200. $res = ds_change_arraykey($res, 'goods_id');
  201. return $res;
  202. }
  203. }