Wechat.php 9.1 KB

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