作者 何书鹏
1 个管道 的构建 通过 耗费 0 秒

1

... ... @@ -12,6 +12,8 @@ use EasyWeChat\Foundation\Application;
use addons\wechat\library\Wechat as WechatService;
use addons\wechat\library\Config as ConfigService;
use think\Log;
use app\api\model\UserHouse;
use app\api\model\House;
/**
* 微信接口
... ... @@ -44,26 +46,25 @@ class Wechat extends \think\addons\Controller
switch ($message->MsgType) {
case 'event': //事件消息
//验证码消息
if (in_array($event, ['subscribe', 'SCAN'])) {
return $eventkey;
}
// switch ($event) {
// case 'subscribe'://添加关注
// $subscribeMessage = WechatConfig::value('default.subscribe.message');
// $subscribeMessage = $subscribeMessage ? $subscribeMessage : "欢迎关注我们!";
// return $subscribeMessage;
// case 'unsubscribe'://取消关注
// return '';
// case 'LOCATION'://获取地理位置
// return '';
// case 'VIEW': //跳转链接,eventkey为链接
// return '';
// case 'SCAN': //扫码
// return '';
// default:
// break;
// //验证码消息
// if (in_array($event, ['subscribe', 'SCAN'])) {
// return $eventkey;
// }
switch ($event) {
case 'subscribe'://添加关注
$key = explode('_', $eventkey);
$this->joinHouse($key[1],$openid);
case 'unsubscribe'://取消关注
return '';
case 'LOCATION'://获取地理位置
return '';
case 'VIEW': //跳转链接,eventkey为链接
return '';
case 'SCAN': //扫码
$this->joinHouse($key[1],$openid);
default:
break;
}
return "";
case 'text': //文字消息
case 'image': //图片消息
... ... @@ -81,4 +82,43 @@ class Wechat extends \think\addons\Controller
$response->send();
}
/**
* 加入小区
*/
private function joinHouse($house_id,$openid){
$user = WechatService::getUserByOpenid($openid);
if($user){
$info = UserHouse::get(['user_id'=>$user['id'],'house_id'=>$house_id]);
$house = House::get($house_id);
if(!empty($info) && $info['status'] == 2){
return "您已加入【$house['name']】社区";
}
if(empty($info)){
// 直接绑定成功
$data = UserHouse::create([
'user_id' => $user['id'],
'house_id' => $house_id,
'name' => $user['nickname'],
'status' => 2,
'phone' => $user['mobile'],
'createtime' => time(),
'updatetime' => time()
]);
}else{
// 申请中改为已绑定
$info->status = 2;
$info->save();
}
// 添加当前社区
if(empty($user['house_id'])){
$user->house_id = $house_id;
$user->save();
}
// 社区人数加1
House::where('id',$house_id)->setInc('bindnum');
return "您已成功加入【$house['name']】社区";
}
return "请先登录社区公众号";
}
}
\ No newline at end of file
... ...