User.php
3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace domain\User;
use Yii;
use domain\User\models\User as UserModel;
use Exception;
/**
* Class User
* @package domain\User
*/
class User
{
/**
* 创建厂商
* @param $item
*/
static function create($item)
{
$transaction = Yii::$app->db->beginTransaction();
try {
$UserModel = Yii::createObject(UserModel::className());
$UserModel->name = trim($item["openid"]); // openid
$UserModel->unionid = trim($item['unionid']); // unionid
$UserModel->nickname = trim($item['nickname']); //昵称
$UserModel->headimgurl = trim($item['headimgurl']); //头像
$UserModel->gender = trim($item['gender']); //性别
$UserModel->country = trim($item['country']); //区
$UserModel->province = trim($item['province']); //省份
$UserModel->city = trim($item['city']); //城市
$UserModel->subscribe = trim($item['subscribe']); // 是否关注
$result = $UserModel->save();
if($result) {
$result = $UserModel->id;
$transaction->commit();
} else {
$transaction->rollBack();
}
return $result;
} catch (Exception $e) {
$transaction->rollBack();
return false;
}
}
/**
* @param $id
* @param $item
* @return null|static
*/
static function update($id, $item)
{
$UserModel = UserModel::findOne($id);
if (empty($UserModel)) {
return false;
}
if (isset($item['openid']) && !empty($item['openid'])) {
$UserModel->openid = trim($item["openid"]); // openid
}
if (isset($item['unionid']) && !empty($item['unionid'])) {
$UserModel->unionid = trim($item['unionid']); // unionid
}
if (isset($item['nickname']) && !empty($item['nickname'])) {
$UserModel->nickname = trim($item['nickname']); // 昵称
}
if (isset($item['headimgurl']) && !empty($item['headimgurl'])) {
$UserModel->headimgurl = trim($item['headimgurl']); //头像
}
if (isset($item['country']) && !empty($item['country'])) {
$UserModel->country = trim($item['country']); //区
}
if (isset($item['province']) && !empty($item['province'])) {
$UserModel->province = trim($item['province']); //省份
}
if (isset($item['city']) && !empty($item['city'])) {
$UserModel->city = trim($item['city']); //城市
}
if (isset($item['subscribe']) && !empty($item['subscribe'])) {
$UserModel->subscribe = trim($item['subscribe']); // 是否关注
}
$resultSave = $UserModel->save();
return $resultSave;
}
/**
* @param $id
* @param $item
* @return null|static
*/
public static function delete($id)
{
$UserModel = UserModel::findOne($id);
if (empty($UserModel)) {
return false;
}
return UserModel::updateAll(["is_delete" => 1], ["id" => $id]);
}
}