QuanziUp.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2025 赤店商城 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. use yii\behaviors\TimestampBehavior;
  10. use yii\db\ActiveRecord;
  11. class QuanziUp extends \yii\db\ActiveRecord
  12. {
  13. /**
  14. * @inheritdoc
  15. */
  16. public static function tableName()
  17. {
  18. return '{{%quanzi_up}}';
  19. }
  20. public function behaviors()
  21. {
  22. return [
  23. [
  24. // 自动更新创建和更新时间
  25. 'class' => TimestampBehavior::class,
  26. ]
  27. ];
  28. }
  29. public static function upInit($user_id) {
  30. if(empty($user_id)){
  31. return new self();
  32. }
  33. $key = ['quanzi', 'upInit', $user_id];
  34. // cache_lock_del($key);
  35. if(!cache_lock($key, 86400*999)){
  36. try{
  37. $up = QuanziUp::findOne(['user_id' => $user_id]);
  38. if(!$up){
  39. $user = User::findOne($user_id);
  40. $up = new QuanziUp();
  41. $up->store_id = $user->store_id;
  42. $up->user_id = $user->id;
  43. $up->status = 1;
  44. $up->save();
  45. }
  46. } catch (\Exception $ex) {
  47. \Yii::error($ex);
  48. debug_log([__FUNCTION__, 'err', $user_id, $ex->getMessage()], __CLASS__ . '.log');
  49. cache_lock_del($key);
  50. }
  51. }
  52. return new self();
  53. }
  54. public static function blackUpUserIdAdd($user_id, $up_user_id, $del = 0) {
  55. $info = QuanziUp::upInit($user_id)::findOne(['user_id' => $user_id]);
  56. $ids = $info['black_up_user_ids'] ? explode(',', $info['black_up_user_ids']) : [];
  57. if($del){
  58. if(in_array($up_user_id, $ids)){
  59. $info->black_up_user_ids = implode(',', array_values(array_diff($ids, [$up_user_id])));
  60. $info->save();
  61. }
  62. }else{
  63. if(!in_array($up_user_id, $ids)){
  64. $info->black_up_user_ids = implode(',', array_values(array_merge($ids, [$up_user_id])));
  65. $info->save();
  66. }
  67. }
  68. }
  69. public static function labelUserSave($user_id, $label = '') {
  70. $info = QuanziUp::upInit($user_id)::findOne(['user_id' => $user_id]);
  71. $info->label_user = trim($label);
  72. $info->save();
  73. }
  74. }