Beispiel #1
0
 public function createShareCode($recordId, $userId)
 {
     $shareCode = ShareCode::findAll(['f_record_id' => $recordId, 'user_id' => $userId]);
     if ($shareCode != null) {
         return '1';
         //分享码已存在
     }
     $conn = \Yii::$app->db;
     $sql = 'select count(*) as num from share_code where user_id="' . $userId . '"';
     $command = $conn->createCommand($sql);
     $result = $command->queryOne();
     $conn->close();
     $num = $result['num'];
     if ($num >= 20) {
         return '2';
         //分享码超出上限
     }
     $code = $this->generateCode(8);
     $exist = ShareCode::findOne(['code' => $code]);
     while ($exist != null) {
         $code = $this->generateCode(8);
         $exist = ShareCode::findOne(['code' => $code]);
     }
     $shareCode = new ShareCode();
     $shareCode->code_id = md5($recordId . date('Y-m-d H:i:s'));
     $shareCode->code = $code;
     $shareCode->user_id = $userId;
     $shareCode->f_record_id = $recordId;
     $shareCode->create_date = date('Y-m-d H:i:s');
     if ($shareCode->save()) {
         return $code;
     } else {
         return '3';
         //创建失败
     }
 }