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.
141 lines
3.7 KiB
141 lines
3.7 KiB
<?php
|
|
|
|
namespace app\service\user;
|
|
|
|
use app\model\InvoiceHead;
|
|
use app\model\WechatPucode;
|
|
use app\model\WechatUser;
|
|
use app\service\BaseService;
|
|
|
|
class UserService extends BaseService
|
|
{
|
|
|
|
/**
|
|
* 随机字符串,默认长度10
|
|
* @param int $num
|
|
* @return string
|
|
*/
|
|
public function setSalt(int $num = 10): string
|
|
{
|
|
$str = 'qwertyuiopasdfghjklzxcvbnm1234567890';
|
|
return substr(str_shuffle($str), 10, $num);
|
|
}
|
|
|
|
/**
|
|
* 密码加密
|
|
* @param string $pwd
|
|
* @param string $salt
|
|
* @return string
|
|
*/
|
|
public function setPassword(string $pwd, string $salt): string
|
|
{
|
|
return md5(md5($pwd . $salt) . $salt);
|
|
}
|
|
|
|
/**
|
|
* 添加、修改密码
|
|
* @param $id
|
|
* @param $password
|
|
* @return bool
|
|
*/
|
|
public function savePassword($id, $password)
|
|
{
|
|
$salt = $this->setSalt();
|
|
|
|
$savePassword = $this->setPassword($password, $salt);
|
|
|
|
$data = [
|
|
'password' => $savePassword,
|
|
'salt' => $salt
|
|
];
|
|
|
|
$User = new WechatUser();
|
|
return $User->where('id', $id)->save($data);
|
|
}
|
|
|
|
public static function addUser(
|
|
int $phone,
|
|
string $email,
|
|
string $pucode,
|
|
string $tax_number,
|
|
int $type
|
|
)
|
|
{
|
|
$where = ['phone' => $phone, 'delete_time' => 0];
|
|
$query = WechatUser::where($where)->find();
|
|
if (!$query) {
|
|
|
|
$save = [
|
|
'nickname' => '导入用户',
|
|
'phone' => $phone,
|
|
'email' => $email,
|
|
'create_time' => time()
|
|
];
|
|
$id = (new WechatUser())->insertGetId($save);
|
|
} else {
|
|
$id = $query['id'];
|
|
$query->email = $email;
|
|
$query->save();
|
|
}
|
|
|
|
if ($id) {
|
|
|
|
$puCodeQuery = (new WechatPucode())->where('pucode', $pucode)->where('wechat_user_id', $id)->find();
|
|
if ($puCodeQuery) {
|
|
$puCodeQuery->wechat_user_id = $id;
|
|
$puCodeQuery->create_time = time();
|
|
$puCodeQuery->save();
|
|
} else {
|
|
$codeSave = [
|
|
'wechat_user_id' => $id,
|
|
'pucode' => $pucode,
|
|
'create_time' => time()
|
|
];
|
|
(new WechatPucode())->save($codeSave);
|
|
}
|
|
|
|
$headSave = [
|
|
'wechat_user_id' => $id
|
|
];
|
|
|
|
if ($tax_number) {
|
|
$headQuery = (new InvoiceHead())->where($headSave)->find();
|
|
if ($headQuery && ($headQuery['pucode'] == $pucode || empty($headQuery['pucode']))) {
|
|
$headQuery->type = $type;
|
|
$headQuery->tax_number = $tax_number;
|
|
$headQuery->phone = $phone;
|
|
$headQuery->pucode = $pucode;
|
|
$headQuery->create_time = time();
|
|
$headQuery->save();
|
|
} else {
|
|
|
|
$headSave['type'] = $type;
|
|
$headSave['tax_number'] = $tax_number;
|
|
$headSave['phone'] = $phone;
|
|
$headSave['pucode'] = $pucode;
|
|
$headSave['create_time'] = time();
|
|
(new InvoiceHead())->save($headSave);
|
|
}
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public static function getTaxUser($tax_id, $wechat_user_id)
|
|
{
|
|
$where = [
|
|
['tax_number', '=', $tax_id],
|
|
['wechat_user_id', '<>', $wechat_user_id],
|
|
];
|
|
$user_ids = InvoiceHead::where($where)->column('wechat_user_id');
|
|
|
|
if ($user_ids) {
|
|
|
|
return WechatPucode::whereIn('wechat_user_id', $user_ids)->column('pucode');
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|