1 line
6.9 KiB
PHP
1 line
6.9 KiB
PHP
<?php
|
|
|
|
namespace app\api\model\food;
|
|
|
|
use app\common\model\food\User as UserModel;
|
|
use app\api\model\Setting as SettingModel;
|
|
use hema\wechat\Driver as Wechat;
|
|
use hema\wechat\WxBizDataCrypt;
|
|
use hema\wechat\Pay as WxPay;
|
|
use think\facade\Cache;
|
|
use hema\alipay\Driver as Alipay;
|
|
use hema\alipay\engine\AopEncrypt;
|
|
|
|
/**
|
|
|
|
* 用户模型类
|
|
|
|
*/
|
|
|
|
class User extends UserModel
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 隐藏字段
|
|
|
|
*/
|
|
|
|
protected $hidden = [];
|
|
|
|
/**
|
|
* 手机号
|
|
*/
|
|
public function getPhoneAttr($value)
|
|
{
|
|
if(!empty($value)){
|
|
return hide_phone($value);
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
/**
|
|
* 获取用户信息
|
|
*/
|
|
public static function getDetail($user_id)
|
|
{
|
|
if($user = self::get($user_id,['address_default'])){
|
|
return $user;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 微信小程序用户自动注册/登录
|
|
*/
|
|
public function autoLogin(array $data)
|
|
{
|
|
//支付宝
|
|
if(isset($data['mp']) and $data['mp'] == 'alipay'){
|
|
//微信登录 (获取session_key)
|
|
$alipay = new Alipay($data['applet_id']);
|
|
if(!$result = $alipay->systemOauthToken($data['code'])){
|
|
$this->error = $alipay->getError();
|
|
return false;
|
|
}
|
|
$userInfo = [
|
|
'union_id' => $result['alipay_user_id'],
|
|
'open_id' => $result['user_id'],
|
|
'platform' => 30
|
|
];
|
|
}else{
|
|
//微信
|
|
//(获取session_key)
|
|
$wx = new Wechat;
|
|
if(!$result = $wx->getSessionKey($data['code'],$data['applet_id'])){
|
|
$this->error = $wx->getError();
|
|
return false;
|
|
}
|
|
if(isset($result['unionid'])){
|
|
$userInfo['union_id'] = $result['unionid'];
|
|
}
|
|
$userInfo['open_id'] = $result['openid'];
|
|
}
|
|
if (!$user = $this->where(['open_id' => $userInfo['open_id']])->find()) {
|
|
//用户不存在才执行
|
|
$user =$this;
|
|
$userInfo['applet_id'] = $data['applet_id'];
|
|
}else{
|
|
$userInfo['login_count'] = $user['login_count'] + 1;
|
|
}
|
|
$user->save($userInfo);
|
|
$user = $this->where(['open_id' => $userInfo['open_id']])->find();
|
|
if(is_null(Cache::get('food_token_' . $user['user_id']))){
|
|
$user['is_login'] = false;
|
|
}else{
|
|
$user['is_login'] = true;
|
|
}
|
|
return $user;
|
|
}
|
|
|
|
/**
|
|
* 获取用户手机号
|
|
*/
|
|
public function getPhoneNumber($post, $userInfo)
|
|
{
|
|
//支付宝
|
|
if(isset($post['mp']) and $post['mp'] == 'alipay'){
|
|
$de = new AopEncrypt($post['applet_id']);
|
|
$res = json_decode($de->decrypt($post['encryptedData']),true);
|
|
if(isset($res['code']) and $res['code'] != '10000'){
|
|
$this->error = '错误代码:' . $res['subCode'] . ',错误信息:' . $res['subMsg'];
|
|
return false;
|
|
}
|
|
$phone = $res['mobile'];
|
|
}else{
|
|
// 微信登录 获取session_key
|
|
$wx = new Wechat;
|
|
if(!$result = $wx->getSessionKey($post['code'],$post['applet_id'])){
|
|
$this->error = $wx->getError();
|
|
return false;
|
|
}
|
|
$sessionKey = $result['session_key'];
|
|
$encryptedData= $post['encryptedData'];
|
|
$iv = $post['iv'];
|
|
$pc = new WxBizDataCrypt($sessionKey,$post['applet_id']);
|
|
$errCode = $pc->decryptData($encryptedData,$iv,$data);
|
|
$data = json_decode($data,true);
|
|
if(!isset($data['phoneNumber'])){
|
|
$this->error = '解密错误';
|
|
return false;
|
|
}
|
|
$phone = $data['phoneNumber'];
|
|
}
|
|
$userInfo->phone = $phone;
|
|
$userInfo->save();
|
|
$user = $this->where(['open_id' => $userInfo->open_id])->find();
|
|
Cache::set('food_token_' . $user['user_id'],$user,3600*24*7);//登录状态存储7天
|
|
$user['is_login'] = true;
|
|
return $user;
|
|
}
|
|
|
|
/**
|
|
* 验证码登录
|
|
*/
|
|
public function h5CodeLogin($post)
|
|
{
|
|
//手机验证码是否正确
|
|
if(is_null(Cache::pull($post['phone'] . '_' . $post['captcha']))){
|
|
$this->error = '验证码错误';
|
|
return false;
|
|
}
|
|
$filter = [
|
|
'platform' => 20,//H5平台
|
|
'phone' => $post['phone']
|
|
];
|
|
if(!$user = $this->where($filter)->find()){
|
|
//用户不存在才执行
|
|
$user =$this;
|
|
$data['platform'] = 20;
|
|
$data['phone'] = $post['phone'];
|
|
$data['applet_id'] = $post['applet_id'];
|
|
}else{
|
|
$data['login_count'] = $user['login_count'] + 1;
|
|
}
|
|
$user->save($data);
|
|
$user = $this->where($filter)->find();
|
|
Cache::set('food_token_' . $user['user_id'],$user,3600*24*7);//登录状态存储7天
|
|
$user['is_login'] = true;
|
|
return $user;
|
|
}
|
|
|
|
/**
|
|
* 开通会员
|
|
*/
|
|
public function openVip()
|
|
{
|
|
$this->save(['v' => 1]);
|
|
//赠送优惠券
|
|
$grade = Setting::getItem('vip');
|
|
if(sizeof($grade['vip'][0]['gift']) > 0){
|
|
$coupon = new CouponUser;
|
|
$coupon->add($grade['vip'][0]['gift'],$this->user_id);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 获取开通会员支付详情 - 微信回调
|
|
*/
|
|
public function payDetail(string $order_no)
|
|
{
|
|
return Cache::get($order_no);
|
|
}
|
|
|
|
/**
|
|
* 更新用户会员状态 - 微信回调
|
|
*/
|
|
public function updatePayStatus(string $transaction_id, array $data)
|
|
{
|
|
//更新用户会员状态
|
|
$user = self::get($data['user_id']);
|
|
$user->v = 1;//开通会员
|
|
$user->score = ['inc', (int)$data['money']];//增加积分
|
|
$user->pay = ['inc', $data['money']];//增加消费金额
|
|
$user->save();
|
|
//增加消费记录
|
|
$model = new Record;
|
|
$arr = [];
|
|
$arr[0] = $data;
|
|
$arr[1] = $data;
|
|
$arr[1]['mode'] = 40;//赠送
|
|
$arr[1]['type'] = 90;//积分
|
|
$model->saveAll($arr);
|
|
//赠送优惠券
|
|
$grade = Setting::getItem('vip');
|
|
if(sizeof($grade['vip'][0]['gift']) > 0){
|
|
$coupon = new CouponUser;
|
|
$coupon->add($grade['vip'][0]['gift'],$data['user_id']);
|
|
}
|
|
//账单分账
|
|
if($data['type'] == 30){
|
|
$wxpay = new WxPay(SettingModel::getItem('wxpay',$data['applet_id']));
|
|
$wxpay->divide([
|
|
'out_order_no' => $data['order_no'],
|
|
'transaction_id' => $transaction_id,
|
|
'total' => $data['money'],
|
|
],$data['applet_id']);//进行分账
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 安全退出
|
|
*/
|
|
public function logout($user_id)
|
|
{
|
|
Cache::delete('food_token_' . $user_id);
|
|
return true;
|
|
}
|
|
} |