宝体数据调用接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

192 lines
5.3 KiB

<?php
namespace app\api\model;
use think\Cache;
use app\common\library\wechat\WxUser;
use app\common\exception\BaseException;
use app\common\model\User as UserModel;
use app\api\model\dealer\Referee as RefereeModel;
use app\api\model\dealer\Setting as DealerSettingModel;
/**
* 用户模型类
* Class User
* @package app\api\model
*/
class User extends UserModel
{
private $token;
/**
* 隐藏字段
* @var array
*/
protected $hidden = [
'open_id',
'is_delete',
'wxapp_id',
'create_time',
'update_time'
];
/**
* 获取用户信息
* @param $token
* @return null|static
* @throws \think\exception\DbException
*/
public static function getUser($token)
{
return self::detail(['open_id' => Cache::get($token)['openid']]);
}
/**
* 用户登录
* @param array $post
* @return string
* @throws BaseException
* @throws \think\Exception
* @throws \think\exception\DbException
*/
public function login($post)
{
// 微信登录 获取session_key
$session = $this->wxlogin($post['code']);
// 自动注册用户
$referee_id = isset($post['referee_id']) ? $post['referee_id'] : null;
$userInfo = json_decode(htmlspecialchars_decode($post['user_info']), true);
$user_id = $this->register($session['openid'], $userInfo, $referee_id);
// 生成token (session3rd)
$this->token = $this->token($session['openid']);
// 记录缓存, 7天
Cache::set($this->token, $session, 86400 * 7);
return $user_id;
}
/**
* 获取token
* @return mixed
*/
public function getToken()
{
return $this->token;
}
/**
* 微信登录
* @param $code
* @return array|mixed
* @throws BaseException
* @throws \think\exception\DbException
*/
private function wxlogin($code)
{
// 获取当前小程序信息
$wxConfig = Wxapp::getWxappCache();
// 微信登录 (获取session_key)
$WxUser = new WxUser($wxConfig['app_id'], $wxConfig['app_secret']);
if (!$session = $WxUser->sessionKey($code))
throw new BaseException(['msg' => 'session_key 获取失败']);
return $session;
}
/**
* 生成用户认证的token
* @param $openid
* @return string
*/
private function token($openid)
{
$wxapp_id = self::$wxapp_id;
// 生成一个不会重复的随机字符串
$guid = \getGuidV4();
// 当前时间戳 (精确到毫秒)
$timeStamp = microtime(true);
// 自定义一个盐
$salt = 'token_salt';
return md5("{$wxapp_id}_{$timeStamp}_{$openid}_{$guid}_{$salt}");
}
/**
* 自动注册用户
* @param $open_id
* @param $data
* @param int $referee_id
* @return mixed
* @throws \Exception
* @throws \think\exception\DbException
*/
private function register($open_id, $data, $referee_id = null)
{
// 查询用户是否已存在
$user = self::detail(['open_id' => $open_id]);
$model = $user ?: $this;
$data['open_id'] = $open_id;
$data['wxapp_id'] = self::$wxapp_id;
// 用户昵称
$data['nickName'] = preg_replace('/[\xf0-\xf7].{3}/', '', $data['nickName']);
try {
$this->startTrans();
// 保存/更新用户记录
if (!$model->allowField(true)->save($data))
throw new BaseException(['msg' => '用户注册失败']);
// 记录推荐人关系
if (!$user && $referee_id > 0)
RefereeModel::createRelation($model['user_id'], $referee_id);
$this->commit();
} catch (\Exception $e) {
$this->rollback();
throw new BaseException(['msg' => $e->getMessage()]);
}
return $model['user_id'];
}
/**
* 个人中心菜单列表
* @return array
*/
public function getMenus()
{
$menus = [
'address' => [
'name' => '收货地址',
'url' => 'pages/address/index',
'icon' => 'map'
],
'coupon' => [
'name' => '领券中心',
'url' => 'pages/coupon/coupon',
'icon' => 'lingquan'
],
'sharing_order' => [
'name' => '拼团订单',
'url' => 'pages/sharing/order/index',
'icon' => 'pintuan'
],
'my_coupon' => [
'name' => '我的优惠券',
'url' => 'pages/user/coupon/coupon',
'icon' => 'youhuiquan'
],
'dealer' => [
'name' => '分销中心',
'url' => 'pages/dealer/index/index',
'icon' => 'fenxiaozhongxin'
],
'help' => [
'name' => '我的帮助',
'url' => 'pages/user/help/index',
'icon' => 'help'
],
];
// 判断分销功能是否开启
if (DealerSettingModel::isOpen()) {
$menus['dealer']['name'] = DealerSettingModel::getDealerTitle();
} else {
unset($menus['dealer']);
}
return $menus;
}
}