UpdateInviteCode.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\Console\Commands;
  3. use app\backend\modules\member\models\Member;
  4. use app\common\models\AccountWechats;
  5. use app\frontend\modules\member\models\MemberUniqueModel;
  6. use ClassesWithParents\D;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\DB;
  9. class UpdateInviteCode extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'migrate:invite_code ';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. //重构数据库
  23. protected $description = '修改邀请码';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. protected $uniacid = 2;
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return mixed
  38. */
  39. public function handle()
  40. {
  41. \YunShop::app()->uniacid = 2;
  42. $member = \app\common\models\MemberShopInfo::uniacid()
  43. ->select('member_id','invite_code')
  44. ->whereRaw('LENGTH(invite_code)<8')
  45. ->get()->toArray();
  46. if(empty($member)){
  47. \Log::info('是否进来');
  48. return '';
  49. }else{
  50. foreach ($member as $key => $value) {
  51. $invite_code = self::getInviteCode();
  52. if($invite_code){
  53. \Log::info('会员',$value['member_id']);
  54. \Log::info('邀请码',$invite_code);
  55. \app\common\models\MemberShopInfo::updateInviteCode($value['member_id'], $invite_code);
  56. }
  57. }
  58. }
  59. }
  60. public static function getInviteCode()
  61. {
  62. $invite_code = self::generateInviteCode();
  63. if (self::chkInviteCode($invite_code)) {
  64. return $invite_code;
  65. } else {
  66. while (true) {
  67. self::getInviteCode();
  68. }
  69. }
  70. }
  71. /**
  72. * 生成邀请码
  73. *
  74. * @return string
  75. */
  76. public static function generateInviteCode()
  77. {
  78. $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  79. $rand = $str[rand(0, 25)]
  80. . strtoupper(dechex(date('m')))
  81. . date('d') . substr(time(), -5)
  82. . substr(microtime(), 2, 5)
  83. . sprintf('%02d', rand(0, 99));
  84. $code = '';
  85. for ($f = 0; $f < 8; $f++) {
  86. $a = md5($rand, true);
  87. $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV';
  88. $g = ord($a[$f]);
  89. $code .= $s[($g ^ ord($a[$f + 8])) - $g & 0x1F];
  90. };
  91. return $code;
  92. }
  93. /**
  94. * 验证邀请码
  95. *
  96. * @param $code
  97. */
  98. public static function chkInviteCode($code)
  99. {
  100. if (!\app\common\models\MemberShopInfo::chkInviteCode($code)) {
  101. return true;
  102. }
  103. return false;
  104. }
  105. }