select(['id','openid','invite_code'])->find(); if(!$user){//不存在就创建信息 $invite_code = $this->randString(); $member_info['invite_code'] = $invite_code; $member_info['openid'] = $wx_info['openid']; $member_info = WxUser::create($member_info); if (empty($member_info)) { return false; } $info = ['m_id' => $member_info->{'id'}, 't_code' => $invite_code]; }else{ $info = ['m_id' => $user->{'id'}, 't_code' => $user->{'invite_code'}]; } return $info; } /** * 随机8位字符串 * @return string */ function randString() { $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $rand = $code[rand(0,25)] .strtoupper(dechex(date('m'))) .date('d').substr(time(),-5) .substr(microtime(),2,5) .sprintf('%02d',rand(0,99)); for( $a = md5( $rand, true ), $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV', $d = '', $f = 0; $f < 8; $g = ord( $a[ $f ] ), $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ], $f++ ); return $d; } }