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