|
|
|
<?php
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
// | 天诚科技 [ 刘海东 17600099397赋能开发者,助力企业发展 ]
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
// | Copyright (c) 2016~2020 https://www.tczxkj.com All rights reserved.
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
// | Licensed 该系统并不是自由软件,未经许可不能去掉相关版权
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
// | Author:甘肃天诚志信电子商务有限公司 刘海东 联系电话维系17600099397
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
namespace app\wap\model\user;
|
|
|
|
|
|
|
|
use basic\ModelBasic;
|
|
|
|
use service\WechatService;
|
|
|
|
use service\WechatSubscribe;
|
|
|
|
use service\CacheService as Cache;
|
|
|
|
use think\Session;
|
|
|
|
use traits\ModelTrait;
|
|
|
|
use think\exception\ValidateException;
|
|
|
|
|
|
|
|
/**微信信息表
|
|
|
|
* Class WechatUser
|
|
|
|
* @package app\wap\model\user
|
|
|
|
*/
|
|
|
|
class WechatUser extends ModelBasic
|
|
|
|
{
|
|
|
|
use ModelTrait;
|
|
|
|
|
|
|
|
protected $insert = ['add_time'];
|
|
|
|
|
|
|
|
public static function setAddTimeAttr($value)
|
|
|
|
{
|
|
|
|
return time();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 添加一个新用户
|
|
|
|
* @param array $wechatInfo
|
|
|
|
* @return boolen
|
|
|
|
* */
|
|
|
|
public static function setNewUserInfo($wechatInfo)
|
|
|
|
{
|
|
|
|
if (self::be(['openid' => $wechatInfo['openid']])) {
|
|
|
|
self::where(['openid' => $wechatInfo['openid']])->update(['uid' => $wechatInfo]);
|
|
|
|
} else {
|
|
|
|
self::set($wechatInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* .添加新用户
|
|
|
|
* @param $openid
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public static function setNewUser($openid)
|
|
|
|
{
|
|
|
|
$userInfo = WechatService::getUserInfo($openid);
|
|
|
|
$userInfo['nickname'] = '';
|
|
|
|
$userInfo['headimgurl'] = '/system/images/user_log.jpg';
|
|
|
|
if (!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
|
|
|
|
exception('请关注公众号!');
|
|
|
|
$userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
|
|
|
|
self::beginTrans();
|
|
|
|
$wechatUser = User::setWechatUser($userInfo);
|
|
|
|
if (!$wechatUser) {
|
|
|
|
self::rollbackTrans();
|
|
|
|
exception('用户信息储存失败!');
|
|
|
|
}
|
|
|
|
$wechatUser = self::set($wechatUser);
|
|
|
|
if (!$wechatUser) {
|
|
|
|
self::rollbackTrans();
|
|
|
|
exception('用户储存失败!');
|
|
|
|
}
|
|
|
|
|
|
|
|
self::commitTrans();
|
|
|
|
return $wechatUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 更新用户信息
|
|
|
|
* @param $openid
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function updateUser($openid)
|
|
|
|
{
|
|
|
|
$userInfo = WechatService::getUserInfo($openid);
|
|
|
|
$userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
|
|
|
|
return self::edit($userInfo, $openid, 'openid');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用户存在就更新 不存在就添加
|
|
|
|
* @param $openid
|
|
|
|
*/
|
|
|
|
public static function saveUser($openid)
|
|
|
|
{
|
|
|
|
self::be($openid, 'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用户取消关注
|
|
|
|
* @param $openid
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function unSubscribe($openid)
|
|
|
|
{
|
|
|
|
return self::edit(['subscribe' => 0], $openid, 'openid');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用uid获得openid
|
|
|
|
* @param $uid
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function uidToOpenid($uid, $update = false)
|
|
|
|
{
|
|
|
|
$cacheName = 'openid_' . $uid;
|
|
|
|
$openid = Cache::get($cacheName);
|
|
|
|
if ($openid && !$update) return $openid;
|
|
|
|
$openid = self::where('uid', $uid)->value('openid');
|
|
|
|
Cache::set($cacheName, $openid, 0);
|
|
|
|
return $openid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用uid获得Unionid
|
|
|
|
* @param $uid
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function uidToUnionid($uid, $update = false)
|
|
|
|
{
|
|
|
|
$cacheName = 'unionid_' . $uid;
|
|
|
|
$unionid = Cache::get($cacheName);
|
|
|
|
if ($unionid && !$update) return $unionid;
|
|
|
|
$unionid = self::where('uid', $uid)->value('unionid');
|
|
|
|
if (!$unionid) exception('对应的unionid不存在!');
|
|
|
|
Cache::set($cacheName, $unionid, 0);
|
|
|
|
return $unionid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用openid获得uid
|
|
|
|
* @param $uid
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function openidToUid($openid, $update = false)
|
|
|
|
{
|
|
|
|
$cacheName = 'uid_' . $openid;
|
|
|
|
$uid = Cache::get($cacheName);
|
|
|
|
if ($uid && !$update) return $uid;
|
|
|
|
$uid = self::where('openid', $openid)->value('uid');
|
|
|
|
if (!$uid) exception('对应的uid不存在!');
|
|
|
|
Cache::set($cacheName, $uid, 0);
|
|
|
|
return $uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取用户信息
|
|
|
|
* @param $openid
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getWechatInfo($openid)
|
|
|
|
{
|
|
|
|
if (is_numeric($openid)) $openid = self::uidToOpenid($openid);
|
|
|
|
$wechatInfo = self::where('openid', $openid)->find();
|
|
|
|
if (!$wechatInfo) {
|
|
|
|
self::setNewUser($openid);
|
|
|
|
$wechatInfo = self::where('openid', $openid)->find();
|
|
|
|
}
|
|
|
|
if (!$wechatInfo) exception('获取用户信息失败!');
|
|
|
|
return $wechatInfo->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 授权后获取用户信息
|
|
|
|
* @param $openid
|
|
|
|
* @param $user_type
|
|
|
|
*/
|
|
|
|
public function getAuthUserInfo($openid, $user_type)
|
|
|
|
{
|
|
|
|
$user = self::where(['unionid|openid' => $openid, 'user_type' => $user_type])->find();
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 更新微信用户信息
|
|
|
|
* @param $event
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function wechatUpdata($data)
|
|
|
|
{
|
|
|
|
[$uid, $userData] = $data;
|
|
|
|
/** @var UserServices $userServices */
|
|
|
|
$userServices = new User;
|
|
|
|
if (!$userInfo = $userServices->where('uid', $uid)->find()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/** @var LoginServices $loginService */
|
|
|
|
/* $loginService = app()->make(LoginServices::class);
|
|
|
|
$loginService->updateUserInfo($userData, $userInfo); */
|
|
|
|
//更新用户信息
|
|
|
|
/** @var WechatUserServices $wechatUser */
|
|
|
|
$wechatUser = new self();
|
|
|
|
|
|
|
|
$wechatUserInfo = [];
|
|
|
|
if (isset($userData['nickname']) && $userData['nickname']) $wechatUserInfo['nickname'] = filter_emoji($userData['nickname'] ?? '');//姓名
|
|
|
|
if (isset($userData['headimgurl']) && $userData['headimgurl']) $wechatUserInfo['headimgurl'] = $userData['avatarUrl'] ?? '';//头像
|
|
|
|
if (isset($userData['sex']) && $userData['sex']) $wechatUserInfo['sex'] = $userData['gender'] ?? '';//性别
|
|
|
|
if (isset($userData['language']) && $userData['language']) $wechatUserInfo['language'] = $userData['language'] ?? '';//语言
|
|
|
|
if (isset($userData['city']) && $userData['city']) $wechatUserInfo['city'] = $userData['city'] ?? '';//城市
|
|
|
|
if (isset($userData['province']) && $userData['province']) $wechatUserInfo['province'] = $userData['province'] ?? '';//省份
|
|
|
|
if (isset($userData['country']) && $userData['country']) $wechatUserInfo['country'] = $userData['country'] ?? '';//国家
|
|
|
|
if ($wechatUserInfo) {
|
|
|
|
if (isset($userData['openid']) && $userData['openid'] && false === $wechatUser->update(['uid' => $userInfo['uid'], 'openid' => $userData['openid']], $wechatUserInfo)) {
|
|
|
|
throw new ValidateException('更新失败');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 微信授权成功后
|
|
|
|
* @param $event
|
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
|
* @throws \think\exception\DbException
|
|
|
|
*/
|
|
|
|
public function wechatOauthAfter(array $data)
|
|
|
|
{
|
|
|
|
[$openid, $wechatInfo, $spreadId, $login_type, $userType] = $data;
|
|
|
|
$userServices = new User;
|
|
|
|
$spreadInfo = $userServices->where('uid', (int)$spreadId)->find();
|
|
|
|
if (!$spreadInfo) {
|
|
|
|
$spreadId = 0;
|
|
|
|
$wechatInfo['staff_id'] = 0;
|
|
|
|
$wechatInfo['agent_id'] = 0;
|
|
|
|
$wechatInfo['division_id'] = 0;
|
|
|
|
} else {
|
|
|
|
$wechatInfo['staff_id'] = $spreadInfo['staff_id'];
|
|
|
|
$wechatInfo['agent_id'] = $spreadInfo['agent_id'];
|
|
|
|
$wechatInfo['division_id'] = $spreadInfo['division_id'];
|
|
|
|
}
|
|
|
|
if (isset($wechatInfo['subscribe_scene'])) {
|
|
|
|
unset($wechatInfo['subscribe_scene']);
|
|
|
|
}
|
|
|
|
if (isset($wechatInfo['qr_scene'])) {
|
|
|
|
unset($wechatInfo['qr_scene']);
|
|
|
|
}
|
|
|
|
if (isset($wechatInfo['qr_scene_str'])) {
|
|
|
|
unset($wechatInfo['qr_scene_str']);
|
|
|
|
}
|
|
|
|
if ($login_type) {
|
|
|
|
$wechatInfo['login_type'] = $login_type;
|
|
|
|
}
|
|
|
|
if (!isset($wechatInfo['nickname'])) {
|
|
|
|
if (isset($wechatInfo['phone']) && $wechatInfo['phone']) {
|
|
|
|
$wechatInfo['nickname'] = substr_replace($wechatInfo['phone'], '****', 3, 4);
|
|
|
|
} else {
|
|
|
|
$wechatInfo['nickname'] = 'wx' . rand(100000, 999999);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$wechatInfo['is_complete'] = 1;
|
|
|
|
$wechatInfo['nickname'] = filter_emoji($wechatInfo['nickname']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$userInfo = [];
|
|
|
|
$uid = 0;
|
|
|
|
if (isset($wechatInfo['phone']) && $wechatInfo['phone']) {
|
|
|
|
$userInfo = $userServices->where(['phone' => $wechatInfo['phone']])->find();
|
|
|
|
}
|
|
|
|
if (!$userInfo) {
|
|
|
|
if (isset($wechatInfo['unionid']) && $wechatInfo['unionid']) {
|
|
|
|
$uid = ($data = self::where(['unionid' => $wechatInfo['unionid']])->find()) ? $data['uid'] : 0;
|
|
|
|
if ($uid) {
|
|
|
|
$userInfo = $userServices->where(['uid' => $uid])->find();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$userInfo = $this->getAuthUserInfo($openid, $userType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($userInfo) {
|
|
|
|
$uid = (int)$userInfo['uid'];
|
|
|
|
}
|
|
|
|
$wechatInfo['user_type'] = $userType;
|
|
|
|
//user表存在和wechat_user表同时存在
|
|
|
|
if ($userInfo) {
|
|
|
|
//更新用户表和wechat_user表
|
|
|
|
//判断该类性用户在wechatUser中是否存在
|
|
|
|
$wechatUser = self::where(['uid' => $uid, 'user_type' => $userType])->find();
|
|
|
|
$this->transaction(function () use ($wechatInfo, $userInfo, $uid, $userType, $spreadId, $wechatUser) {
|
|
|
|
$wechatInfo['code'] = $spreadId;
|
|
|
|
if ($wechatUser) {
|
|
|
|
if (!self::update($wechatUser['id'], $wechatInfo, 'id')) {
|
|
|
|
throw new ValidateException('更新数据失败');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$wechatInfo['uid'] = $uid;
|
|
|
|
if (!self::save($wechatInfo)) {
|
|
|
|
throw new ValidateException('写入信息失败');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
//user表没有用户,wechat_user表没有用户创建新用户
|
|
|
|
//不存在则创建用户
|
|
|
|
$userInfo = $this->transaction(function () use ($userServices, $wechatInfo, $spreadId, $userType, $login_type) {
|
|
|
|
$userInfo = $userServices->setWechatUser($wechatInfo, (int)$spreadId, $login_type);
|
|
|
|
if (!$userInfo) {
|
|
|
|
throw new ValidateException('生成User用户失败!');
|
|
|
|
}
|
|
|
|
$wechatInfo['uid'] = $userInfo['uid'];
|
|
|
|
$wechatInfo['add_time'] = time();
|
|
|
|
if (!self::save($wechatInfo)) {
|
|
|
|
throw new ValidateException('生成微信用户失败!');
|
|
|
|
}
|
|
|
|
return $userInfo;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return $userInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|