Wechat.php 8.9 KB

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