multi-store/app/api/logic/user/UserLogic.php

121 lines
3.5 KiB
PHP
Raw Normal View History

<?php
namespace app\api\logic\user;
2024-06-04 23:15:30 +08:00
use app\common\{logic\BaseLogic,
model\system_store\SystemStore,
model\user\User,
model\user\UserRecharge,
2024-06-05 18:39:11 +08:00
model\user\UserShip,
2024-06-04 23:15:30 +08:00
service\wechat\WeChatMnpService};
/**
* 会员逻辑层
* Class UserLogic
* @package app\shopapi\logic
*/
class UserLogic extends BaseLogic
{
/**
* @notes 获取小程序手机号
* @param array $params
* @return bool
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
* @author 段誉
* @date 2023/2/27 11:49
*/
public static function getMobileByMnp(array $params)
{
try {
$response = (new WeChatMnpService())->getUserPhoneNumber($params['code']);
$phoneNumber = $response['phone_info']['purePhoneNumber'] ?? '';
if (empty($phoneNumber)) {
throw new \Exception('获取手机号码失败');
}
$user = User::where([
['mobile', '=', $phoneNumber],
['id', '<>', $params['user_id']]
])->findOrEmpty();
if (!$user->isEmpty()) {
throw new \Exception('手机号已被其他账号绑定');
}
// 绑定手机号
User::update([
'id' => $params['user_id'],
'mobile' => $phoneNumber
]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
2024-06-04 11:12:28 +08:00
public static function info($uid)
{
$data = User::where('id',$uid)
2024-06-05 18:39:11 +08:00
->field('avatar,real_name,nickname,account,mobile,sex,login_ip,user_money,total_recharge_amount,user_ship')
2024-06-04 11:12:28 +08:00
->find();
//判断是不是员工
if($data){
$data = $data->toArray();
2024-06-05 18:39:11 +08:00
$all =UserShip::field('id,title,limit')->select()->toArray();
$new = self::getNextArrayByID($all,$data['user_ship']);
$data['next_level'] = '';
$data['next_limit'] = 0;
if($new){
$data['next_level'] = $new['title'];
$data['next_limit'] = $new['limit'] - $data['total_recharge_amount'];
}
2024-06-04 11:12:28 +08:00
$data['is_staff'] = 0;
$data['store_id'] = 0;
if(isset($data['mobile']) && $data['mobile']){
$check = SystemStore::where('phone',$data['mobile'])->find()??[];
if ($check){
$data['is_staff'] = 1;
$data['store_id'] = $check['store_id'];
}
}
}else{
$data = [];
}
return $data;
}
2024-06-04 23:15:30 +08:00
public static function recharge($param)
{
$param['order_id'] = getNewOrderId('rc');
$param['recharge_type'] = 'wechat';
return UserRecharge::create($param);
}
2024-06-05 18:39:11 +08:00
public static function getNextArrayByID($arr, $id) {
// 遍历数组
foreach ($arr as $key => $value) {
// 检查当前数组的id是否与传入的id匹配
if ($value['id'] == $id) {
// 如果当前数组不是最后一个,则返回下一个数组
if ($key + 1 < count($arr)) {
return $arr[$key + 1];
}
// 如果当前数组是最后一个则返回null或空数组
return null;
}
}
// 如果没有找到匹配的id则返回null或空数组
return null;
}
}