12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace App\Models;
- use Illuminate\Support\Facades\Redis;
- use App\Models\User\UserModel;
- /**
- * App\Models\TagModel
- *
- * @property int $id
- * @property string $name 名称
- * @property int|null $group 分组[1:我的标签;2:]
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TagModel newModelQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TagModel newQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TagModel query()
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TagModel whereGroup($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TagModel whereId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TagModel whereName($value)
- * @mixin \Eloquent
- */
- class TagModel extends Model
- {
- protected $table = 'kdgx_partner_charge_user_tag';
- /**
- * 获取用户的tag
- * @param UserModel $user
- * @return array|mixed
- */
- public function getAllTagByUser($user)
- {
- if ($json_tags = Redis::get("charge_user_tag_{$user->uid}")) {
- $tags = json_decode($json_tags, true);
- return $tags;
- } else {
- $datas = $this->get()->toArray();
- $result = array();
- $taged_1 = explode(',', $user->tag_1);
- $taged_2 = explode(',', $user->tag_2);
- $taged_3 = explode(',', $user->tag_3);
- $taged_4 = explode(',', $user->tag_4);
- foreach ($datas as $data) {
- if (!empty($data) && in_array($data['group'], [1, 2, 3, 4])) {
- $data['belong'] = 0;
- if (in_array($data['id'], ${"taged_{$data['group']}"})) {
- $data['belong'] = 1;
- }
- $result[$data['group']][$data['id']] = $data;
- }
- }
- Redis::set("charge_user_tag_{$user->uid}", json_encode($result));
- return $result;
- }
- }
- // 刷新用户标签缓存
- public function flushTagByUser(int $uid, array $data, $is_add = true)
- {
- if ($json_tags = Redis::get("charge_user_tag_{$uid}")) {
- $tags = json_decode($json_tags, true);
- if ($is_add) {
- $tags[$data['group']][$data['tag_id']]['belong'] = 1;
- } else {
- $tags[$data['group']][$data['tag_id']]['belong'] = 0;
- }
- Redis::set("charge_user_tag_{$uid}", json_encode($tags));
- return $tags;
- } else {
- $user = UserModel::findOrFail($uid);
- $tags = $this->getAllTagByUser($user);
- return $tags;
- }
- }
- }
|